@@ -53,7 +53,7 @@ value and then a User object is created::
53
53
// $apiKey = $request->headers->get('apikey');
54
54
55
55
if (!$apiKey) {
56
- throw new BadCredentialsException('No API key found' );
56
+ throw new BadCredentialsException();
57
57
58
58
// or to just skip api key authentication
59
59
// return null;
@@ -66,6 +66,11 @@ value and then a User object is created::
66
66
);
67
67
}
68
68
69
+ public function supportsToken(TokenInterface $token, $providerKey)
70
+ {
71
+ return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
72
+ }
73
+
69
74
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
70
75
{
71
76
if (!$userProvider instanceof ApiKeyUserProvider) {
@@ -97,11 +102,6 @@ value and then a User object is created::
97
102
$user->getRoles()
98
103
);
99
104
}
100
-
101
- public function supportsToken(TokenInterface $token, $providerKey)
102
- {
103
- return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
104
- }
105
105
}
106
106
107
107
.. versionadded :: 2.8
@@ -111,7 +111,7 @@ value and then a User object is created::
111
111
112
112
Once you've :ref: `configured <cookbook-security-api-key-config >` everything,
113
113
you'll be able to authenticate by adding an apikey parameter to the query
114
- string, like ``http://example.com/admin /foo?apikey=37b51d194a7513e45b56f6524f2d51f2 ``.
114
+ string, like ``http://example.com/api /foo?apikey=37b51d194a7513e45b56f6524f2d51f2 ``.
115
115
116
116
The authentication process has several steps, and your implementation will
117
117
probably differ:
@@ -196,7 +196,7 @@ The ``$userProvider`` might look something like this::
196
196
null,
197
197
// the roles for the user - you may choose to determine
198
198
// these dynamically somehow based on the user
199
- array('ROLE_USER ')
199
+ array('ROLE_API ')
200
200
);
201
201
}
202
202
@@ -268,6 +268,7 @@ would allow you to have custom data on the ``User`` object.
268
268
269
269
Finally, just make sure that ``supportsClass() `` returns ``true `` for User
270
270
objects with the same class as whatever user you return in ``loadUserByUsername() ``.
271
+
271
272
If your authentication is stateless like in this example (i.e. you expect
272
273
the user to send the API key with every request and so you don't save the
273
274
login to the session), then you can simply throw the ``UnsupportedUserException ``
@@ -281,7 +282,7 @@ exception in ``refreshUser()``.
281
282
Handling Authentication Failure
282
283
-------------------------------
283
284
284
- In order for your ``ApiKeyAuthenticator `` to correctly display a 403
285
+ In order for your ``ApiKeyAuthenticator `` to correctly display a 401
285
286
http status when either bad credentials or authentication fails you will
286
287
need to implement the :class: `Symfony\\ Component\\ Security\\ Http\\ Authentication\\ AuthenticationFailureHandlerInterface ` on your
287
288
Authenticator. This will provide a method ``onAuthenticationFailure `` which
@@ -308,7 +309,7 @@ you can use to create an error ``Response``.
308
309
// this contains information about *why* authentication failed
309
310
// use it, or return your own message
310
311
strtr($exception->getMessageKey(), $exception->getMessageData()),
311
- 403
312
+ 401
312
313
);
313
314
}
314
315
}
@@ -377,7 +378,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
377
378
378
379
firewalls :
379
380
secured_area :
380
- pattern : ^/admin
381
+ pattern : ^/api
381
382
stateless : true
382
383
simple_preauth :
383
384
authenticator : apikey_authenticator
@@ -400,7 +401,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
400
401
<!-- ... -->
401
402
402
403
<firewall name =" secured_area"
403
- pattern =" ^/admin "
404
+ pattern =" ^/api "
404
405
stateless =" true"
405
406
provider =" api_key_user_provider"
406
407
>
@@ -420,7 +421,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
420
421
$container->loadFromExtension('security', array(
421
422
'firewalls' => array(
422
423
'secured_area' => array(
423
- 'pattern' => '^/admin ',
424
+ 'pattern' => '^/api ',
424
425
'stateless' => true,
425
426
'simple_preauth' => array(
426
427
'authenticator' => 'apikey_authenticator',
@@ -435,6 +436,44 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
435
436
),
436
437
));
437
438
439
+ If you have defined ``access_control ``, make sure to add a new entry:
440
+
441
+ .. configuration-block ::
442
+
443
+ .. code-block :: yaml
444
+
445
+ # app/config/security.yml
446
+ security :
447
+ # ...
448
+
449
+ access_control :
450
+ - { path: ^/api, roles: ROLE_API }
451
+
452
+ .. code-block :: xml
453
+
454
+ <!-- app/config/security.xml -->
455
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
456
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
457
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
458
+ xmlns : srv =" http://symfony.com/schema/dic/services"
459
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
460
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
461
+
462
+ <rule path =" ^/api" role =" ROLE_API" />
463
+ </srv : container >
464
+
465
+ .. code-block :: php
466
+
467
+ // app/config/security.php
468
+ $container->loadFromExtension('security', array(
469
+ 'access_control' => array(
470
+ array(
471
+ 'path' => '^/api',
472
+ 'role' => 'ROLE_API',
473
+ ),
474
+ ),
475
+ ));
476
+
438
477
That's it! Now, your ``ApiKeyAuthenticator `` should be called at the beginning
439
478
of each request and your authentication process will take place.
440
479
@@ -467,7 +506,7 @@ configuration or set it to ``false``:
467
506
468
507
firewalls :
469
508
secured_area :
470
- pattern : ^/admin
509
+ pattern : ^/api
471
510
stateless : false
472
511
simple_preauth :
473
512
authenticator : apikey_authenticator
@@ -490,7 +529,7 @@ configuration or set it to ``false``:
490
529
<!-- ... -->
491
530
492
531
<firewall name =" secured_area"
493
- pattern =" ^/admin "
532
+ pattern =" ^/api "
494
533
stateless =" false"
495
534
provider =" api_key_user_provider"
496
535
>
@@ -509,7 +548,7 @@ configuration or set it to ``false``:
509
548
$container->loadFromExtension('security', array(
510
549
'firewalls' => array(
511
550
'secured_area' => array(
512
- 'pattern' => '^/admin ',
551
+ 'pattern' => '^/api ',
513
552
'stateless' => false,
514
553
'simple_preauth' => array(
515
554
'authenticator' => 'apikey_authenticator',
0 commit comments