8000 Use configured user provider instead of injection · symfony/symfony-docs@f7d7f81 · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f7d7f81

Browse files
mvarweaverryan
authored andcommitted
Use configured user provider instead of injection
1 parent 2a949b9 commit f7d7f81

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

cookbook/security/api_key_authentication.rst

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ value and then a User object is created::
3232

3333
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
3434
{
35-
protected $userProvider;
36-
37-
public function __construct(ApiKeyUserProvider $userProvider)
38-
{
39-
$this->userProvider = $userProvider;
40-
}
41-
4235
public function createToken(Request $request, $providerKey)
4336
{
4437
// look for an apikey query parameter
@@ -64,15 +57,15 @@ value and then a User object is created::
6457
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
6558
{
6659
$apiKey = $token->getCredentials();
67-
$username = $this->userProvider->getUsernameForApiKey($apiKey);
60+
$username = $userProvider->getUsernameForApiKey($apiKey);
6861

6962
if (!$username) {
7063
throw new AuthenticationException(
7164
sprintf('API Key "%s" does not exist.', $apiKey)
7265
);
7366
}
7467

75-
$user = $this->userProvider->loadUserByUsername($username);
68+
$user = $userProvider->loadUserByUsername($username);
7669

7770
return new PreAuthenticatedToken(
7871
$user,
@@ -189,7 +182,7 @@ The ``$userProvider`` might look something like this::
189182
}
190183
}
191184

192-
Now register your user provider as service:
185+
Now register your user provider as a service:
193186

194187
.. configuration-block::
195188

@@ -255,7 +248,7 @@ exception in ``refreshUser()``.
255248
Handling Authentication Failure
256249
-------------------------------
257250

258-
In order for your ``ApiKeyAuthentication`` to correctly display a 403
251+
In order for your ``ApiKeyAuthenticator`` to correctly display a 403
259252
http status when either bad credentials or authentication fails you will
260253
need to implement the :class:`Symfony\\Component\\Security\\Http\\Authentication\\AuthenticationFailureHandlerInterface` on your
261254
Authenticator. This will provide a method ``onAuthenticationFailure`` which
@@ -287,11 +280,9 @@ you can use to create an error ``Response``.
287280
Configuration
288281
-------------
289282

290-
Once you have your ``ApiKeyAuthentication`` all setup, you need to register
283+
Once you have your ``ApiKeyAuthenticator`` all setup, you need to register
291284
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.
295286

296287
.. configuration-block::
297288

@@ -302,8 +293,7 @@ your custom user provider as a service called ``your_api_key_user_provider``
302293
# ...
303294
304295
apikey_authenticator:
305-
class: AppBundle\Security\ApiKeyAuthenticator
306-
arguments: ["@api_key_user_provider"]
296+
class: AppBundle\Security\ApiKeyAuthenticator
307297
308298
.. code-block:: xml
309299
@@ -316,11 +306,7 @@ your custom user provider as a service called ``your_api_key_user_provider``
316306
<services>
317307
<!-- ... -->
318308
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" />
324310
</services>
325311
</container>
326312
@@ -333,12 +319,12 @@ your custom user provider as a service called ``your_api_key_user_provider``
333319
// ...
334320
335321
$container->setDefinition('apikey_authenticator', new Definition(
336-
'AppBundle\Security\ApiKeyAuthenticator',
337-
array(new Reference('api_key_user_provider'))
322+
'AppBundle\Security\ApiKeyAuthenticator'
338323
));
339324
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:
342328

343329
.. configuration-block::
344330

@@ -354,6 +340,7 @@ using the ``simple_preauth`` key:
354340
stateless: true
355341
simple_preauth:
356342
authenticator: apikey_authenticator
343+
provider: api_key_user_provider
357344
358345
providers:
359346
api_key_user_provider:
@@ -374,6 +361,7 @@ using the ``simple_preauth`` key:
374361
<firewall name="secured_area"
375362
pattern="^/admin"
376363
stateless="true"
364+
provider="api_key_user_provider"
377365
>
378366
<simple-preauth authenticator="apikey_authenticator" />
379367
</firewall>
@@ -396,6 +384,7 @@ using the ``simple_preauth`` key:
396384
'simple_preauth' => array(
397385
'authenticator' => 'apikey_authenticator',
398386
),
387+
'provider' => 'api_key_user_provider',
399388
),
400389
),
401390
'providers' => array(
@@ -405,7 +394,7 @@ using the ``simple_preauth`` key:
405394
),
406395
));
407396
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
409398
of each request and your authentication process will take place.
410399

411400
The ``stateless`` configuration parameter prevents Symfony from trying to
@@ -441,6 +430,7 @@ configuration or set it to ``false``:
441430
stateless: false
442431
simple_preauth:
443432
authenticator: apikey_authenticator
433+
provider: api_key_user_provider
444434
445435
providers:
446436
api_key_user_provider:
@@ -461,6 +451,7 @@ configuration or set it to ``false``:
461451
<firewall name="secured_area"
462452
pattern="^/admin"
463453
stateless="false"
454+
provider="api_key_user_provider"
464455
>
465456
<simple-preauth authenticator="apikey_authenticator" />
466457
</firewall>
@@ -482,6 +473,7 @@ configuration or set it to ``false``:
482473
'simple_preauth' => array(
483474
'authenticator' => 'apikey_authenticator',
484475
),
476+
'provider' => 'api_key_user_provider',
485477
),
486478
),
487479
'providers' => array(
@@ -505,7 +497,7 @@ to see if the stored token has a valid User object that can be used::
505497
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
506498
{
507499
$apiKey = $token->getCredentials();
508-
$username = $this->userProvider->getUsernameForApiKey($apiKey);
500+
$username = $userProvider->getUsernameForApiKey($apiKey);
509501

510502
// User is the Entity which represents your user
511503
$user = $token->getUser();
@@ -524,7 +516,7 @@ to see if the stored token has a valid User object that can be used::
524516
);
525517
}
526518

527-
$user = $this->userProvider->loadUserByUsername($username);
519+
$user = $userProvider->loadUserByUsername($username);
528520

529521
return new PreAuthenticatedToken(
530522
$user,
@@ -598,13 +590,10 @@ current URL is before creating the token in ``createToken()``::
598590

599591
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
600592
{
601-
protected $userProvider;
602-
603593
protected $httpUtils;
604594

605-
public function __construct(UserProviderInterface $userProvider, HttpUtils $httpUtils)
595+
public function __construct(HttpUtils $httpUtils)
< E0B5 /td>
606596
{
607-
$this->userProvider = $userProvider;
608597
$this->httpUtils = $httpUtils;
609598
}
610599

@@ -639,7 +628,7 @@ service:
639628
640629
apikey_authenticator:
641630
class: AppBundle\Security\ApiKeyAuthenticator
642-
arguments: ["@api_key_user_provider", "@security.http_utils"]
631+
arguments: ["@security.http_utils"]
643632
644633
.. code-block:: xml
645634
@@ -655,7 +644,6 @@ service:
655644
<service id="apikey_authenticator"
656645
class="AppBundle\Security\ApiKeyAuthenticator"
657646
>
658-
<argument type="service" id="api_key_user_provider" />
659647
<argument type="service" id="security.http_utils" />
660648
</service>
661649
</services>
@@ -672,7 +660,6 @@ service:
672660
$container->setDefinition('apikey_authenticator', new Definition(
673661
'AppBundle\Security\ApiKeyAuthenticator',
674662
array(
675-
new Reference('api_key_user_provider'),
676663
new Reference('security.http_utils')
677664
)
678665
));

0 commit comments

Comments
 (0)
0