@@ -54,6 +54,11 @@ value and then a User object is created::
54
54
);
55
55
}
56
56
57
+ public function supportsToken(TokenInterface $token, $providerKey)
58
+ {
59
+ return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
60
+ }
61
+
57
62
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
58
63
{
59
64
if (!$userProvider instanceof ApiKeyUserProvider) {
@@ -83,11 +88,6 @@ value and then a User object is created::
83
88
$user->getRoles()
84
89
);
85
90
}
86
-
87
- public function supportsToken(TokenInterface $token, $providerKey)
88
- {
89
- return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
90
- }
91
91
}
92
92
93
93
Once you've :ref: `configured <cookbook-security-api-key-config >` everything,
@@ -177,7 +177,7 @@ The ``$userProvider`` might look something like this::
177
177
null,
178
178
// the roles for the user - you may choose to determine
179
179
// these dynamically somehow based on the user
180
- array('ROLE_USER ')
180
+ array('ROLE_API ')
181
181
);
182
182
}
183
183
@@ -249,6 +249,7 @@ would allow you to have custom data on the ``User`` object.
249
249
250
250
Finally, just make sure that ``supportsClass() `` returns ``true `` for User
251
251
objects with the same class as whatever user you return in ``loadUserByUsername() ``.
252
+
252
253
If your authentication is stateless like in this example (i.e. you expect
253
254
the user to send the API key with every request and so you don't save the
254
255
login to the session), then you can simply throw the ``UnsupportedUserException ``
@@ -262,7 +263,7 @@ exception in ``refreshUser()``.
262
263
Handling Authentication Failure
263
264
-------------------------------
264
265
265
- In order for your ``ApiKeyAuthenticator `` to correctly display a 403
266
+ In order for your ``ApiKeyAuthenticator `` to correctly display a 401
266
267
http status when either bad credentials or authentication fails you will
267
268
need to implement the :class: `Symfony\\ Component\\ Security\\ Http\\ Authentication\\ AuthenticationFailureHandlerInterface ` on your
268
269
Authenticator. This will provide a method ``onAuthenticationFailure `` which
@@ -285,7 +286,7 @@ you can use to create an error ``Response``.
285
286
286
287
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
287
288
{
288
- return new Response("Authentication Failed.", 403 );
289
+ return new Response("Authentication Failed.", 401 );
289
290
}
290
291
}
291
292
@@ -411,6 +412,46 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
411
412
),
412
413
));
413
414
415
+ If you have defined `access_control `, make sure to add new entry:
416
+
417
+ .. configuration-block ::
418
+
419
+ .. code-block :: yaml
420
+
421
+ # app/config/security.yml
422
+ security :
423
+ # ...
424
+
425
+ access_control :
426
+ - { path: ^/admin, roles: ROLE_API }
427
+
428
+ .. code-block :: xml
429
+
430
+ <!-- app/config/security.xml -->
431
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
432
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
433
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
434
+ xmlns : srv =" http://symfony.com/schema/dic/services"
435
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
436
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
437
+
438
+ <rule path =" ^/admin"
439
+ role =" ROLE_API"
440
+ />
441
+ </srv : container >
442
+
443
+ .. code-block :: php
444
+
445
+ // app/config/security.php
446
+ $container->loadFromExtension('security', array(
447
+ 'access_control' => array(
448
+ array(
449
+ 'path' => '^/admin',
450
+ 'role' => 'ROLE_API',
451
+ ),
452
+ ),
453
+ ));
454
+
414
455
That's it! Now, your ``ApiKeyAuthenticator `` should be called at the beginning
415
456
of each request and your authentication process will take place.
416
457
0 commit comments