@@ -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
@@ -64,15 +57,15 @@ value and then a User object is created::
64
57
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
65
58
{
66
59
$apiKey = $token->getCredentials();
67
- $username = $this-> userProvider->getUsernameForApiKey($apiKey);
60
+ $username = $userProvider->getUsernameForApiKey($apiKey);
68
61
69
62
if (!$username) {
70
63
throw new AuthenticationException(
71
64
sprintf('API Key "%s" does not exist.', $apiKey)
72
65
);
73
66
}
74
67
75
- $user = $this-> userProvider->loadUserByUsername($username);
68
+ $user = $userProvider->loadUserByUsername($username);
76
69
77
70
return new PreAuthenticatedToken(
78
71
$user,
@@ -189,7 +182,7 @@ The ``$userProvider`` might look something like this::
189
182
}
190
183
}
191
184
192
- Now register your user provider as service:
185
+ Now register your user provider as a service:
193
186
194
187
.. configuration-block ::
195
188
@@ -255,7 +248,7 @@ exception in ``refreshUser()``.
255
248
Handling Authentication Failure
256
249
-------------------------------
257
250
258
- In order for your ``ApiKeyAuthentication `` to correctly display a 403
251
+ In order for your ``ApiKeyAuthenticator `` to correctly display a 403
259
252
http status when either bad credentials or authentication fails you will
260
253
need to implement the :class: `Symfony\\ Component\\ Security\\ Http\\ Authentication\\ AuthenticationFailureHandlerInterface ` on your
261
254
Authenticator. This will provide a method ``onAuthenticationFailure `` which
@@ -287,11 +280,9 @@ you can use to create an error ``Response``.
287
280
Configuration
288
281
-------------
289
282
290
- Once you have your ``ApiKeyAuthentication `` all setup, you need to register
283
+ Once you have your ``ApiKeyAuthenticator `` all setup, you need to register
291
284
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 `).
285
+ First, register it as a service.
295
286
296
287
.. configuration-block ::
297
288
@@ -302,8 +293,7 @@ your custom user provider as a service called ``your_api_key_user_provider``
302
293
# ...
303
294
304
295
apikey_authenticator :
305
- class : AppBundle\Security\ApiKeyAuthenticator
306
- arguments : ["@api_key_user_provider"]
296
+ class : AppBundle\Security\ApiKeyAuthenticator
307
297
308
298
.. code-block :: xml
309
299
@@ -316,11 +306,7 @@ your custom user provider as a service called ``your_api_key_user_provider``
316
306
<services >
317
307
<!-- ... -->
318
308
319
- <service id =" apikey_authenticator"
320
- class =" AppBundle\Security\ApiKeyAuthenticator"
321
- >
322
- <argument type =" service" id =" api_key_user_provider" />
323
- </service >
309
+ <service id =" apikey_authenticator" class =" AppBundle\Security\ApiKeyAuthenticator" />
324
310
</services >
325
311
</container >
326
312
@@ -333,12 +319,12 @@ your custom user provider as a service called ``your_api_key_user_provider``
333
319
// ...
334
320
335
321
$container->setDefinition('apikey_authenticator', new Definition(
336
- 'AppBundle\Security\ApiKeyAuthenticator',
337
- array(new Reference('api_key_user_provider'))
322
+ 'AppBundle\Security\ApiKeyAuthenticator'
338
323
));
339
324
340
- Now, activate it in the ``firewalls `` section of your security configuration
341
- using the ``simple_preauth `` key:
325
+ Now, activate it and your custom user provider (see :doc: `/cookbook/security/custom_provider `)
326
+ in the ``firewalls `` section of your security configuration
327
+ using the ``simple_preauth `` and ``provider `` keys respectively:
342
328
343
329
.. configuration-block ::
344
330
@@ -354,6 +340,7 @@ using the ``simple_preauth`` key:
354
340
stateless : true
355
341
simple_preauth :
356
342
authenticator : apikey_authenticator
343
+ provider : api_key_user_provider
357
344
358
345
providers :
359
346
api_key_user_provider :
@@ -374,6 +361,7 @@ using the ``simple_preauth`` key:
374
361
<firewall name =" secured_area"
375
362
pattern =" ^/admin"
376
363
stateless =" true"
364
+ provider =" api_key_user_provider"
377
365
>
378
366
<simple-preauth authenticator =" apikey_authenticator" />
379
367
</firewall >
@@ -396,6 +384,7 @@ using the ``simple_preauth`` key:
396
384
'simple_preauth' => array(
397
385
'authenticator' => 'apikey_authenticator',
398
386
),
387
+ 'provider' => 'api_key_user_provider',
399
388
),
400
389
),
401
390
'providers' => array(
@@ -405,7 +394,7 @@ using the ``simple_preauth`` key:
405
394
),
406
395
));
407
396
408
- That's it! Now, your ``ApiKeyAuthentication `` should be called at the beginning
397
+ That's it! Now, your ``ApiKeyAuthenticator `` should be called at the beginning
409
398
of each request and your authentication process will take place.
410
399
411
400
The ``stateless `` configuration parameter prevents Symfony from trying to
@@ -441,6 +430,7 @@ configuration or set it to ``false``:
441
430
stateless : false
442
431
simple_preauth :
443
432
authenticator : apikey_authenticator
433
+ provider : api_key_user_provider
444
434
445
435
providers :
446
436
api_key_user_provider :
@@ -461,6 +451,7 @@ configuration or set it to ``false``:
461
451
<firewall name =" secured_area"
462
452
pattern =" ^/admin"
463
453
stateless =" false"
454
+ provider =" api_key_user_provider"
464
455
>
465
456
<simple-preauth authenticator =" apikey_authenticator" />
466
457
</firewall >
@@ -482,6 +473,7 @@ configuration or set it to ``false``:
482
473
'simple_preauth' => array(
483
474
'authenticator' => 'apikey_authenticator',
484
475
),
476
+ 'provider' => 'api_key_user_provider',
485
477
),
486
478
),
487
479
'providers' => array(
@@ -505,7 +497,7 @@ to see if the stored token has a valid User object that can be used::
505
497
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
506
498
{
507
499
$apiKey = $token->getCredentials();
508
- $username = $this-> userProvider->getUsernameForApiKey($apiKey);
500
+ $username = $userProvider->getUsernameForApiKey($apiKey);
509
501
510
502
// User is the Entity which represents your user
511
503
$user = $token->getUser();
@@ -524,7 +516,7 @@ to see if the stored token has a valid User object that can be used::
524
516
);
525
517
}
526
518
527
- $user = $this-> userProvider->loadUserByUsername($username);
519
+ $user = $userProvider->loadUserByUsername($username);
528
520
529
521
return new PreAuthenticatedToken(
530
522
$user,
@@ -598,13 +590,10 @@ current URL is before creating the token in ``createToken()``::
598
590
599
591
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
600
592
{
601
- protected $userProvider;
602
-
603
593
protected $httpUtils;
604
594
605
- public function __construct(UserProviderInterface $userProvider, HttpUtils $httpUtils)
595
+ public function __construct(HttpUtils $httpUtils)
606
596
{
607
- $this->userProvider = $userProvider;
608
597
$this->httpUtils = $httpUtils;
609
598
}
610
599
@@ -639,7 +628,7 @@ service:
639
628
640
629
apikey_authenticator :
641
630
class : AppBundle\Security\ApiKeyAuthenticator
642
- arguments : ["@api_key_user_provider", "@ security.http_utils"]
631
+ arguments : ["@security.http_utils"]
643
632
644
633
.. code-block :: xml
645
634
@@ -655,7 +644,6 @@ service:
655
644
<service id =" apikey_authenticator"
656
645
class =" AppBundle\Security\ApiKeyAuthenticator"
657
646
>
658
- <argument type =" service" id =" api_key_user_provider" />
659
647
<argument type =" service" id =" security.http_utils" />
660
648
</service >
661
649
</services >
@@ -672,7 +660,6 @@ service:
672
660
$container->setDefinition('apikey_authenticator', new Definition(
673
661
'AppBundle\Security\ApiKeyAuthenticator',
674
662
array(
675
- new Reference('api_key_user_provider'),
676
663
new Reference('security.http_utils')
677
664
)
678
665
));
0 commit comments