@@ -32,13 +32,6 @@ value and then a User object is created::
32
32
33
33
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
34
34
{
35
- protected $userProvider;
36
-
37
- public function __construct(ApiKeyUserProvider $userProvider)
38
- {
39
- $this->userProvider = $userProvider;
40
- }
41
-
42
35
public function createToken(Request $request, $providerKey)
43
36
{
44
37
// look for an apikey query parameter
@@ -63,16 +56,25 @@ value and then a User object is created::
63
56
64
57
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
65
58
{
59
+ if (!$userProvider instanceof ApiKeyUserProvider) {
60
+ throw new \InvalidArgumentException(
61
+ sprintf(
62
+ 'The user provider must be an instance of ApiKeyUserProvider (%s was given).',
63
+ get_class($userProvider)
64
+ )
65
+ );
66
+ }
67
+
66
68
$apiKey = $token->getCredentials();
67
- $username = $this-> userProvider->getUsernameForApiKey($apiKey);
69
+ $username = $userProvider->getUsernameForApiKey($apiKey);
68
70
69
71
if (!$username) {
70
72
throw new AuthenticationException(
71
73
sprintf('API Key "%s" does not exist.', $apiKey)
72
74
);
73
75
}
74
76
75
- $user = $this-> userProvider->loadUserByUsername($username);
77
+ $user = $userProvider->loadUserByUsername($username);
76
78
77
79
return new PreAuthenticatedToken(
78
80
$user,
@@ -189,7 +191,7 @@ The ``$userProvider`` might look something like this::
189
191
}
190
192
}
191
193
192
- Now register your user provider as service:
194
+ Now register your user provider as a service:
193
195
194
196
.. configuration-block ::
195
197
@@ -255,7 +257,7 @@ exception in ``refreshUser()``.
255
257
Handling Authentication Failure
256
258
-------------------------------
257
259
258
- In order for your ``ApiKeyAuthentication `` to correctly display a 403
260
+ In order for your ``ApiKeyAuthenticator `` to correctly display a 403
259
261
http status when either bad credentials or authentication fails you will
260
262
need to implement the :class: `Symfony\\ Component\\ Security\\ Http\\ Authentication\\ AuthenticationFailureHandlerInterface ` on your
261
263
Authenticator. This will provide a method ``onAuthenticationFailure `` which
@@ -287,11 +289,9 @@ you can use to create an error ``Response``.
287
289
Configuration
288
290
-------------
289
291
290
- Once you have your ``ApiKeyAuthentication `` all setup, you need to register
292
+ Once you have your ``ApiKeyAuthenticator `` all setup, you need to register
291
293
it as a service and use it in your security configuration (e.g. ``security.yml ``).
292
- First, register it as a service. This assumes that you have already setup
293
- your custom user provider as a service called ``your_api_key_user_provider ``
294
- (see :doc: `/cookbook/security/custom_provider `).
294
+ First, register it as a service.
295
295
296
296
.. configuration-block ::
297
297
@@ -302,8 +302,8 @@ your custom user provider as a service called ``your_api_key_user_provider``
302
302
# ...
303
303
304
304
apikey_authenticator :
305
- class : AppBundle\Security\ApiKeyAuthenticator
306
- arguments : ["@api_key_user_provider"]
305
+ class : AppBundle\Security\ApiKeyAuthenticator
306
+ public : false
307
307
308
308
.. code-block :: xml
309
309
@@ -318,9 +318,7 @@ your custom user provider as a service called ``your_api_key_user_provider``
318
318
319
319
<service id =" apikey_authenticator"
320
320
class =" AppBundle\Security\ApiKeyAuthenticator"
321
- >
322
- <argument type =" service" id =" api_key_user_provider" />
323
- </service >
321
+ public =" false" />
324
322
</services >
325
323
</container >
326
324
@@ -332,13 +330,13 @@ your custom user provider as a service called ``your_api_key_user_provider``
332
330
333
331
// ...
334
332
335
- $container->setDefinition('apikey_authenticator', new Definition(
336
- 'AppBundle\Security\ApiKeyAuthenticator',
337
- array(new Reference('api_key_user_provider'))
338
- ));
333
+ $definition = new Definition('AppBundle\Security\ApiKeyAuthenticator');
334
+ $definition->setPublic(false);
335
+ $container->setDefinition('apikey_authenticator', $definition);
339
336
340
- Now, activate it in the ``firewalls `` section of your security configuration
341
- using the ``simple_preauth `` key:
337
+ Now, activate it and your custom user provider (see :doc: `/cookbook/security/custom_provider `)
338
+ in the ``firewalls `` section of your security configuration
339
+ using the ``simple_preauth `` and ``provider `` keys respectively:
342
340
343
341
.. configuration-block ::
344
342
@@ -354,6 +352,7 @@ using the ``simple_preauth`` key:
354
352
stateless : true
355
353
simple_preauth :
356
354
authenticator : apikey_authenticator
355
+ provider : api_key_user_provider
357
356
358
357
providers :
359
358
api_key_user_provider :
@@ -374,6 +373,7 @@ using the ``simple_preauth`` key:
374
373
<firewall name =" secured_area"
375
374
pattern =" ^/admin"
376
375
stateless =" true"
376
+ provider =" api_key_user_provider"
377
377
>
378
378
<simple-preauth authenticator =" apikey_authenticator" />
379
379
</firewall >
@@ -396,6 +396,7 @@ using the ``simple_preauth`` key:
396
396
'simple_preauth' => array(
397
397
'authenticator' => 'apikey_authenticator',
398
398
),
399
+ 'provider' => 'api_key_user_provider',
399
400
),
400
401
),
401
402
'providers' => array(
@@ -405,7 +406,7 @@ using the ``simple_preauth`` key:
405
406
),
406
407
));
407
408
408
- That's it! Now, your ``ApiKeyAuthentication `` should be called at the beginning
409
+ That's it! Now, your ``ApiKeyAuthenticator `` should be called at the beginning
409
410
of each request and your authentication process will take place.
410
411
411
412
The ``stateless `` configuration parameter prevents Symfony from trying to
@@ -441,6 +442,7 @@ configuration or set it to ``false``:
441
442
stateless : false
442
443
simple_preauth :
443
444
authenticator : apikey_authenticator
445
+ provider : api_key_user_provider
444
446
445
447
providers :
446
448
api_key_user_provider :
@@ -461,6 +463,7 @@ configuration or set it to ``false``:
461
463
<firewall name =" secured_area"
462
464
pattern =" ^/admin"
463
465
stateless =" false"
466
+ provider =" api_key_user_provider"
464
467
>
465
468
<simple-preauth authenticator =" apikey_authenticator" />
466
469
</firewall >
@@ -482,6 +485,7 @@ configuration or set it to ``false``:
482
485
'simple_preauth' => array(
483
486
'authenticator' => 'apikey_authenticator',
484
487
),
488
+ 'provider' => 'api_key_user_provider',
485
489
),
486
490
),
487
491
'providers' => array(
@@ -504,8 +508,17 @@ to see if the stored token has a valid User object that can be used::
504
508
// ...
505
509
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
506
510
{
511
+ if (!$userProvider instanceof ApiKeyUserProvider) {
512
+ throw new \InvalidArgumentException(
513
+ sprintf(
514
+ 'The user provider must be an instance of ApiKeyUserProvider (%s was given).',
515
+ get_class($userProvider)
516
+ )
517
+ );
518
+ }
519
+
507
520
$apiKey = $token->getCredentials();
508
- $username = $this-> userProvider->getUsernameForApiKey($apiKey);
521
+ $username = $userProvider->getUsernameForApiKey($apiKey);
509
522
510
523
// User is the Entity which represents your user
511
524
$user = $token->getUser();
@@ -524,7 +537,7 @@ to see if the stored token has a valid User object that can be used::
524
537
);
525
538
}
526
539
527
- $user = $this-> userProvider->loadUserByUsername($username);
540
+ $user = $userProvider->loadUserByUsername($username);
528
541
529
542
return new PreAuthenticatedToken(
530
543
$user,
@@ -598,13 +611,10 @@ current URL is before creating the token in ``createToken()``::
598
611
599
612
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
600
613
{
601
- protected $userProvider;
602
-
603
614
protected $httpUtils;
604
615
605
- public function __construct(UserProviderInterface $userProvider, HttpUtils $httpUtils)
616
+ public function __construct(HttpUtils $httpUtils)
606
617
{
607
- $this->userProvider = $userProvider;
608
618
$this->httpUtils = $httpUtils;
609
619
}
610
620
@@ -639,7 +649,8 @@ service:
639
649
640
650
apikey_authenticator :
641
651
class : AppBundle\Security\ApiKeyAuthenticator
642
- arguments : ["@api_key_user_provider", "@security.http_utils"]
652
+ arguments : ["@security.http_utils"]
653
+ public : false
643
654
644
655
.. code-block :: xml
645
656
@@ -654,8 +665,8 @@ service:
654
665
655
666
<service id =" apikey_authenticator"
656
667
class =" AppBundle\Security\ApiKeyAuthenticator"
668
+ public =" false"
657
669
>
658
- <argument type =" service" id =" api_key_user_provider" />
659
670
<argument type =" service" id =" security.http_utils" />
660
671
</service >
661
672
</services >
@@ -669,12 +680,13 @@ service:
669
680
670
681
// ...
671
682
672
- $container->setDefinition('apikey_authenticator', new Definition(
683
+ $definition = new Definition(
673
684
'AppBundle\Security\ApiKeyAuthenticator',
674
685
array(
675
- new Reference('api_key_user_provider'),
676
686
new Reference('security.http_utils')
677
687
)
678
- ));
688
+ );
689
+ $definition->setPublic(false);
690
+ $container->setDefinition('apikey_authenticator', $definition);
679
691
680
692
That's it! Have fun!
0 commit comments