Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Login] Add login suggestion and redirection for error 403 #9041

Merged
merged 3 commits into from
May 16, 2024

Conversation

maximemulder
Copy link
Contributor

@maximemulder maximemulder commented Feb 2, 2024

Brief summary of changes

Whenever an unauthenticated gets a 403 error, they can try logging in and be redirected to the page they got the 403 on.

  • Have you updated related documentation?

Testing instructions (if applicable)

  1. Go to a page that exists and require authentication while not being authenticated.
  2. Click "Try logging in"
  3. Log in (after one or several tries)
  4. You are redirected to the previous page

I implemented this using an URL query parameter, but I guess using window.history could also have worked. If anyone has an opinion on this please tell me.

@maximemulder maximemulder changed the title Add login suggestion and redirection for error 403 [Login] Add login suggestion and redirection for error 403 Feb 2, 2024
// Variables used to suggest the user to login and later redirect them if they
// are not authenticated in a 403.
$tpl_data['anonymous'] = $user instanceof \LORIS\AnonymousUser;
$tpl_data['url'] = urlencode($_SERVER['REQUEST_URI']);
Copy link
Contributor Author

@maximemulder maximemulder Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I used $_SERVER['REQUEST_URI'] instead of the $uri object here because the latter seems to return a lorispath=url instead of the URL I want directly. I haven't been able to find why there is this lorispath so if anyone has an explanation I am all ears.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like the mod_rewrite rule from Apache leaking through somewhere. That should not happen. How were you testing it / in what kind of environment that you had that problem?

Copy link
Contributor Author

@maximemulder maximemulder Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like the mod_rewrite rule from Apache leaking through somewhere. That should not happen. How were you testing it / in what kind of environment that you had that problem?

I am using a MCIN VM with Apache 2.4 and PHP 8.1. I did not notice anything obviously wrong with my installation so the fact that this is abnormal is certainly puzzling.

@maximemulder maximemulder force-pushed the 403-login branch 2 times, most recently from 266299b to b5a8cfc Compare February 2, 2024 22:12
// Variables used to suggest the user to login and later redirect them if they
// are not authenticated in a 403.
$tpl_data['anonymous'] = $user instanceof \LORIS\AnonymousUser;
$tpl_data['url'] = urlencode($_SERVER['REQUEST_URI']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be using the URI from the ServerRequestInterface passed as an argument, not the PHP superglobal.

Copy link
Contributor Author

@maximemulder maximemulder Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did some tests with @ridz1208, who uses Apache similarly to me, and his $uri->__toString() also contains the ?lorispath=, which we obviously don't want in the redirection URL. Therefore, I don't think we can get the URL in the backend without using the PHP superglobal, unless we prevent lorispath from leaking (which may be a good idea but would require dedicated work on it).

I can get the URL in the front-end so it's either that or the superglobal, both are kinda hackish but could be removed once we Reactify the errors imo.

Copy link
Collaborator

@driusan driusan Feb 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a bug unrelated to your PR, just use $uri->__toString and create an issue about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we should use $uri->__toString() before the issue is fixed (if we decide to fix it) as it seems to happen on most setups except for yours and Github Actions' ones. I personally think we should either fix the issue first or add a comment and merge. I'll try to see if there is a clean quick fix for the issue if I have some time today.

I created the issue at #9049.

@driusan
Copy link
Collaborator

driusan commented Feb 27, 2024

@ridz1208 can you review?

@maximemulder
Copy link
Contributor Author

I just noticed that I did this PR quite some time ago and wasn't aware of the testing plan at the time, gonna add it before merging.

@maximemulder maximemulder marked this pull request as draft March 14, 2024 14:10
@maximemulder maximemulder marked this pull request as ready for review March 14, 2024 16:48
Copy link
Contributor

@kongtiaowang kongtiaowang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kongtiaowang kongtiaowang added the Passed Manual Tests PR has undergone proper testing by at least one peer label May 14, 2024
@driusan driusan merged commit a61790d into aces:main May 16, 2024
28 checks passed
@ridz1208 ridz1208 added this to the 26.0.0 milestone Jun 6, 2024
@maximemulder maximemulder deleted the 403-login branch October 29, 2024 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Passed Manual Tests PR has undergone proper testing by at least one peer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants