Skip to content

Commit d03b7ac

Browse files
committed
Introduce the check concept.
1 parent cb0b589 commit d03b7ac

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ tasks:
2222

2323
* If the `stack.authn.token` is set, it wraps the application in
2424
`WwwAuthenticateStackChallenge` and delegates.
25-
* If the there is an `authorization` header, it returns the result of then
26-
**authenticate** callback.
25+
* Checks the request by calling the **check** callback. The return value is a
26+
boolean. If true, the **authenticate** callback is called and its return
27+
value is returned. If false, we should not. The default check is to see if
28+
there is an Authorization header.
2729
* If anonymous requests are received and anonymous requests are allowed, it
2830
wraps the application in `WwwAuthenticateStackChallenge` and delegates.
2931
* Otherwise, it returns the result of the **challenge** callback.
@@ -36,6 +38,18 @@ tasks:
3638
use Symfony\Component\HttpFoundation\Response;
3739
use Symfony\Component\HttpKernel\HttpKernelInterface;
3840

41+
$check = function (
42+
Request $request,
43+
$type = HttpKernelInterface::MASTER_REQUEST,
44+
$catch = true
45+
) {
46+
// This is the default 'check' callback if a check callback is not defined.
47+
// This is here merely for demonstration purposes; if authentication relies
48+
// on the existence of an 'authorization' header a 'check' callback does not
49+
// need to be defined.
50+
return $request->headers->has('authorization');
51+
};
52+
3953
$challenge = function (Response $response) {
4054
// Assumptions that can be made:
4155
// * 401 status code
@@ -69,6 +83,7 @@ $authenticate = function (HttpKernelInterface $app, $anonymous) {
6983

7084
return (new Authentication($app, [
7185
'challenge' => $challenge,
86+
'check' => $check,
7287
'authenticate' => $authenticate,
7388
'anonymous' => true, // default: false
7489
]))

src/Dflydev/Stack/Authentication.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Authentication implements HttpKernelInterface
1010
{
1111
private $app;
1212
private $challenge;
13+
private $check;
1314
private $authenticate;
1415
private $anonymous;
1516

@@ -19,15 +20,29 @@ public function __construct(HttpKernelInterface $app, array $options = [])
1920

2021
if (!isset($options['challenge'])) {
2122
$options['challenge'] = function (Response $response) {
23+
// Default challenge is to not challenge.
2224
return $response;
2325
};
2426
}
2527

28+
if (!isset($options['check'])) {
29+
$options['check'] = function (
30+
Request $request,
31+
$type = HttpKernelInterface::MASTER_REQUEST,
32+
$catch = true
33+
) {
34+
// Default check is to see if the request has an authorization
35+
// header.
36+
return $request->headers->has('authorization');
37+
};
38+
}
39+
2640
if (!isset($options['authenticate'])) {
2741
throw new \InvalidArgumentException("The 'authenticate' configuration is required");
2842
}
2943

3044
$this->challenge = $options['challenge'];
45+
$this->check = $options['check'];
3146
$this->authenticate = $options['authenticate'];
3247
$this->anonymous = $options['anonymous'];
3348
}
@@ -44,9 +59,9 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
4459
->handle($request, $type, $catch);
4560
}
4661

47-
if ($request->headers->has('authorization')) {
48-
// If we have an authorization header we should try and authenticate
49-
// the request.
62+
if (call_user_func($this->check, $request, $type, $catch)) {
63+
// Check the request to see if we should authenticate. If we should,
64+
// we should call our authenticate callback and return its response.
5065
return call_user_func($this->authenticate, $this->app, $this->anonymous);
5166
}
5267

0 commit comments

Comments
 (0)