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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

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

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

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants