tuupola / slim-basic-auth

PSR-7 and PSR-15 HTTP Basic Authentication Middleware
MIT License
440 stars 66 forks source link

Access user/passwd from Authenticator within Slim route handler #102

Closed witchi closed 3 years ago

witchi commented 3 years ago

I have an own Authenticator (implements AuthenticatorInterface), where I can check the user/password. But I also need both parameters later within the Slim route handler because I have to send the same credentials to a further webservice:

$this->get('/api/things', function(Request $request, Response $response, array $args) {

    // FIXME: how I can access user and password from the Authenticator?
    $api = new Api($this->externalUri, $user, $passwd);
    $things =  $api->getThings();

    // ....

    return $response;
});
witchi commented 3 years ago

Ahh, I can use "before" and inject the user/password into the request attributes, which I can extract with $req->getAttribute() later. And the request will be cloned, so I have to use something like this:

"before" => function ($request, $arguments) {
        return $request->withAttribute("user", $arguments["user"])->withAttribute("passwd", $arguments["password"]);
}