Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,38 @@ $app->add(new \Slim\Middleware\HttpBasicAuthentication([
]));
```

## Accessing Authenticated User

By default, a successfully authenticated user will be available in your route via:

```php
$user = $request->getAttribute("authorized_user");
```

If you need to use a different attribute name, this is configurable via "authorized_user_attribute". For example:

``` php
$app = new \Slim\App;

$app->add(new \Slim\Middleware\HttpBasicAuthentication([
"path" => "/admin",
"secure" => true,
"authorized_user_attribute" => "something_else_you_want_to_use"
"relaxed" => ["localhost", "dev.example.com"],
"users" => [
"root" => "t00r",
"somebody" => "passw0rd"
]
]));
```

Now you would access the user with:

```php
$user = $request->getAttribute("something_else_you_want_to_use");
```


## Custom authentication methods

Sometimes passing users in an array is not enough. To authenticate against custom datasource you can pass a callable as `authenticator` parameter. This can be either a class which implements AuthenticatorInterface or anonymous function. Callable receives an array containing `user` and `password` as argument. In both cases authenticator must return either `true` or `false`.
Expand Down
8 changes: 4 additions & 4 deletions src/HttpBasicAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class HttpBasicAuthentication
"passthrough" => null,
"realm" => "Protected",
"environment" => "HTTP_AUTHORIZATION",
"authorized_user_attribute" => "authorized_user",
"authenticator" => null,
"callback" => null,
"error" => null
Expand Down Expand Up @@ -145,10 +146,9 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
]);
}
}


/* Everything ok, call next middleware. */
return $next($request, $response);

/* Everything ok, set argument and call next middleware. */
return $next($request->withAttribute($this->options["authorized_user_attribute"], $user), $response);
}

private function hydrate($data = [])
Expand Down