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

Adding functionality to allow accessing authenticated user outside of… #52

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

conrad10781
Copy link

Adding functionality to allow accessing authenticated user outside of superglobal(s).

Currently, the middleware authenticates, but retrieving the authenticated user
is still done via $_SERVER["PHP_AUTH_USER"] or $request->getServerParams()["PHP_AUTH_USER"].

There is something unnatural about both, as with any framework you're normally
not directly interacting with any of the superglobals, or their variables.
IE you access $request->getUri()->getPath() in the application, rather than
$_SERVER["REQUEST_URI"] , or $request->getServerParams()["REQUEST_URI"].

With this PR, you will be able to access the authenticated user with
$request->getAttribute("authorized_user") in your route, or other middleware ( IE ACL ),
and the attribute name is customizable through the existing options, which the
README has been updated to provide clarity on.

… superglobal(s).

Currently, the middleware authenticates, but retrieving the authenticated user
is still done via $_SERVER["PHP_AUTH_USER"] or $request->getServerParams()["PHP_AUTH_USER"].

There is something unnatural about both, as with any framework you're normally
 not directly interacting with any of the superglobals, or their variables.
IE you access $request->getUri()->getPath() in the application, rather than
$_SERVER["REQUEST_URI"] , or $request->getServerParams()["REQUEST_URI"].

With this PR, you will be able to access the authenticated user with
$request->getAttribute("authorized_user") in your route, or other middleware ( IE ACL ),
and the attribute name is customizable through the existing options, which the
README has been updated to provide clarity on.
@conrad10781 conrad10781 mentioned this pull request Dec 13, 2017
@tuupola tuupola self-assigned this Mar 1, 2018
@tuupola
Copy link
Owner

tuupola commented Dec 22, 2018

Going through unanswered issues and PRs.

This is a good addition and quite common use case. Currently you can do same thing with the before setting.

$app->add(new Tuupola\Middleware\HttpBasicAuthentication([
    "users" => [
        "root" => "t00r",
        "somebody" => "passw0rd"
    ],
    "before" => function ($request, $arguments) {
        return $request->withAttribute("user", $arguments["user"]);
    }
]));

I need to think a bit if the use case is common enough to warrant it's own specific config.

@conrad10781
Copy link
Author

It's been a while since I originally wrote and submitted this PR. I believe the reason "before" didn't work was that the user wasn't authenticated yet, and in "after" the only thing that worked was returning the $response, so modifying the $request wasn't possible.

The custom authenticator doesn't allow to modify either, so there was an issue with trying to utilize before to set the value, but then trying to revoke it in another part of the middleware if the authentication failed.

Ultimately, my implementation was bit more complicated, but I knew this use case was very common, it was just that the "before" didn't work when using the custom authenticator.

Happy Holidays!

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

2 participants