@@ -47,7 +47,7 @@ value and then a User object is created::
47
47
// $apiKey = $request->headers->get('apikey');
48
48
49
49
if (!$apiKey) {
50
- throw new BadCredentialsException('No API key found' );
50
+ throw new BadCredentialsException();
51
51
52
52
// or to just skip api key authentication
53
53
// return null;
@@ -60,6 +60,11 @@ value and then a User object is created::
60
60
);
61
61
}
62
62
63
+ public function supportsToken(TokenInterface $token, $providerKey)
64
+ {
65
+ return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
66
+ }
67
+
63
68
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
64
69
{
65
70
if (!$userProvider instanceof ApiKeyUserProvider) {
@@ -91,16 +96,11 @@ value and then a User object is created::
91
96
$user->getRoles()
92
97
);
93
98
}
94
-
95
- public function supportsToken(TokenInterface $token, $providerKey)
96
- {
97
- return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
98
- }
99
99
}
100
100
101
101
Once you've :ref: `configured <cookbook-security-api-key-config >` everything,
102
102
you'll be able to authenticate by adding an apikey parameter to the query
103
- string, like ``http://example.com/admin /foo?apikey=37b51d194a7513e45b56f6524f2d51f2 ``.
103
+ string, like ``http://example.com/api /foo?apikey=37b51d194a7513e45b56f6524f2d51f2 ``.
104
104
105
105
The authentication process has several steps, and your implementation will
106
106
probably differ:
@@ -185,7 +185,7 @@ The ``$userProvider`` might look something like this::
185
185
null,
186
186
// the roles for the user - you may choose to determine
187
187
// these dynamically somehow based on the user
188
- array('ROLE_USER ')
188
+ array('ROLE_API ')
189
189
);
190
190
}
191
191
@@ -257,6 +257,7 @@ would allow you to have custom data on the ``User`` object.
257
257
258
258
Finally, just make sure that ``supportsClass() `` returns ``true `` for User
259
259
objects with the same class as whatever user you return in ``loadUserByUsername() ``.
260
+
260
261
If your authentication is stateless like in this example (i.e. you expect
261
262
the user to send the API key with every request and so you don't save the
262
263
login to the session), then you can simply throw the ``UnsupportedUserException ``
@@ -270,7 +271,7 @@ exception in ``refreshUser()``.
270
271
Handling Authentication Failure
271
272
-------------------------------
272
273
273
- In order for your ``ApiKeyAuthenticator `` to correctly display a 403
274
+ In order for your ``ApiKeyAuthenticator `` to correctly display a 401
274
275
http status when either bad credentials or authentication fails you will
275
276
need to implement the :class: `Symfony\\ Component\\ Security\\ Http\\ Authentication\\ AuthenticationFailureHandlerInterface ` on your
276
277
Authenticator. This will provide a method ``onAuthenticationFailure `` which
@@ -297,7 +298,7 @@ you can use to create an error ``Response``.
297
298
// this contains information about *why* authentication failed
298
299
// use it, or return your own message
299
300
strtr($exception->getMessageKey(), $exception->getMessageData()),
300
- 403
301
+ 401
301
302
);
302
303
}
303
304
}
@@ -366,7 +367,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
366
367
367
368
firewalls :
368
369
secured_area :
369
- pattern : ^/admin
370
+ pattern : ^/api
370
371
stateless : true
371
372
simple_preauth :
372
373
authenticator : apikey_authenticator
@@ -389,7 +390,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
389
390
<!-- ... -->
390
391
391
392
<firewall name =" secured_area"
392
- pattern =" ^/admin "
393
+ pattern =" ^/api "
393
394
stateless =" true"
394
395
provider =" api_key_user_provider"
395
396
>
@@ -409,7 +410,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
409
410
$container->loadFromExtension('security', array(
410
411
'firewalls' => array(
411
412
'secured_area' => array(
412
- 'pattern' => '^/admin ',
413
+ 'pattern' => '^/api ',
413
414
'stateless' => true,
414
415
'simple_preauth' => array(
415
416
'authenticator' => 'apikey_authenticator',
@@ -424,6 +425,44 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
424
425
),
425
426
));
426
427
428
+ If you have defined ``access_control ``, make sure to add a new entry:
429
+
430
+ .. configuration-block ::
431
+
432
+ .. code-block :: yaml
433
+
434
+ # app/config/security.yml
435
+ security :
436
+ # ...
437
+
438
+ access_control :
439
+ - { path: ^/api, roles: ROLE_API }
440
+
441
+ .. code-block :: xml
442
+
443
+ <!-- app/config/security.xml -->
444
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
445
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
446
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
447
+ xmlns : srv =" http://symfony.com/schema/dic/services"
448
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
449
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
450
+
451
+ <rule path =" ^/api" role =" ROLE_API" />
452
+ </srv : container >
453
+
454
+ .. code-block :: php
455
+
456
+ // app/config/security.php
457
+ $container->loadFromExtension('security', array(
458
+ 'access_control' => array(
459
+ array(
460
+ 'path' => '^/api',
461
+ 'role' => 'ROLE_API',
462
+ ),
463
+ ),
464
+ ));
465
+
427
466
That's it! Now, your ``ApiKeyAuthenticator `` should be called at the beginning
428
467
of each request and your authentication process will take place.
429
468
@@ -456,7 +495,7 @@ configuration or set it to ``false``:
456
495
457
496
firewalls :
458
497
secured_area :
459
- pattern : ^/admin
498
+ pattern : ^/api
460
499
stateless : false
461
500
simple_preauth :
462
501
authenticator : apikey_authenticator
@@ -479,7 +518,7 @@ configuration or set it to ``false``:
479
518
<!-- ... -->
480
519
481
520
<firewall name =" secured_area"
482
- pattern =" ^/admin "
521
+ pattern =" ^/api "
483
522
stateless =" false"
484
523
provider =" api_key_user_provider"
485
524
>
@@ -498,7 +537,7 @@ configuration or set it to ``false``:
498
537
$container->loadFromExtension('security', array(
499
538
'firewalls' => array(
500
539
'secured_area' => array(
501
- 'pattern' => '^/admin ',
540
+ 'pattern' => '^/api ',
502
541
'stateless' => false,
503
542
'simple_preauth' => array(
504
543
'authenticator' => 'apikey_authenticator',
0 commit comments