@@ -22,8 +22,8 @@ Your exact situation may differ, but in this example, a token is read
2222from an ``apikey `` query parameter, the proper username is loaded from that
2323value and then a User object is created::
2424
25- // src/Acme/HelloBundle /Security/ApiKeyAuthenticator.php
26- namespace Acme\HelloBundle \Security;
25+ // src/AppBundle /Security/ApiKeyAuthenticator.php
26+ namespace AppBundle \Security;
2727
2828 use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
2929 use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -147,8 +147,8 @@ used by Symfony's core user provider system).
147147
148148The ``$userProvider `` might look something like this::
149149
150- // src/Acme/HelloBundle /Security/ApiKeyUserProvider.php
151- namespace Acme\HelloBundle \Security;
150+ // src/AppBundle /Security/ApiKeyUserProvider.php
151+ namespace AppBundle \Security;
152152
153153 use Symfony\Component\Security\Core\User\UserProviderInterface;
154154 use Symfony\Component\Security\Core\User\User;
@@ -192,6 +192,41 @@ The ``$userProvider`` might look something like this::
192192 }
193193 }
194194
195+ Now register your user provider as service:
196+
197+ .. configuration-block ::
198+
199+ .. code-block :: yaml
200+
201+ # app/config/services.yml
202+ services :
203+ api_key_user_provider :
204+ class : AppBundle\Security\ApiKeyUserProvider
205+
206+ .. code-block :: xml
207+
208+ <!-- app/config/services.xml -->
209+ <?xml version =" 1.0" ?>
210+ <container xmlns =" http://symfony.com/schema/dic/services"
211+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
212+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
213+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
214+ <services >
215+ <!-- ... -->
216+
217+ <service id =" api_key_user_provider"
218+ class =" AppBundle\Security\ApiKeyUserProvider" />
219+ </services >
220+ </container >
221+
222+ .. code-block :: php
223+
224+ // app/config/services.php
225+
226+ // ...
227+ $container
228+ ->register('api_key_user_provider', 'AppBundle\Security\ApiKeyUserProvider');
229+
195230 .. note ::
196231
197232 Read the dedicated article to learn
@@ -231,8 +266,8 @@ you can use to create an error ``Response``.
231266
232267.. code-block :: php
233268
234- // src/Acme/HelloBundle /Security/ApiKeyAuthenticator.php
235- namespace Acme\HelloBundle \Security;
269+ // src/AppBundle /Security/ApiKeyAuthenticator.php
270+ namespace AppBundle \Security;
236271
237272 use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
238273 use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -270,8 +305,8 @@ your custom user provider as a service called ``your_api_key_user_provider``
270305 # ...
271306
272307 apikey_authenticator :
273- class : Acme\HelloBundle \Security\ApiKeyAuthenticator
274- arguments : ["@your_api_key_user_provider "]
308+ class : AppBundle \Security\ApiKeyAuthenticator
309+ arguments : ["@api_key_user_provider "]
275310
276311 .. code-block :: xml
277312
@@ -285,9 +320,9 @@ your custom user provider as a service called ``your_api_key_user_provider``
285320 <!-- ... -->
286321
287322 <service id =" apikey_authenticator"
288- class =" Acme\HelloBundle \Security\ApiKeyAuthenticator"
323+ class =" AppBundle \Security\ApiKeyAuthenticator"
289324 >
290- <argument type =" service" id =" your_api_key_user_provider " />
325+ <argument type =" service" id =" api_key_user_provider " />
291326 </service >
292327 </services >
293328 </container >
@@ -301,8 +336,8 @@ your custom user provider as a service called ``your_api_key_user_provider``
301336 // ...
302337
303338 $container->setDefinition('apikey_authenticator', new Definition(
304- 'Acme\HelloBundle \Security\ApiKeyAuthenticator',
305- array(new Reference('your_api_key_user_provider '))
339+ 'AppBundle \Security\ApiKeyAuthenticator',
340+ array(new Reference('api_key_user_provider '))
306341 ));
307342
308343 Now, activate it in the ``firewalls `` section of your security configuration
@@ -323,6 +358,10 @@ using the ``simple_preauth`` key:
323358 simple_preauth :
324359 authenticator : apikey_authenticator
325360
361+ providers :
362+ api_key_user_provider :
363+ id : api_key_user_provider
364+
326365 .. code-block :: xml
327366
328367 <!-- app/config/security.xml -->
@@ -341,6 +380,8 @@ using the ``simple_preauth`` key:
341380 >
342381 <simple-preauth authenticator =" apikey_authenticator" />
343382 </firewall >
383+
384+ <provider name =" api_key_user_provider" id =" api_key_user_provider" />
344385 </config >
345386 </srv : container >
346387
@@ -360,6 +401,11 @@ using the ``simple_preauth`` key:
360401 ),
361402 ),
362403 ),
404+ 'providers' => array(
405+ 'api_key_user_provider' => array(
406+ 'id' => 'api_key_user_provider',
407+ ),
408+ ),
363409 ));
364410
365411 That's it! Now, your ``ApiKeyAuthentication `` should be called at the beginning
@@ -399,6 +445,10 @@ configuration or set it to ``false``:
399445 simple_preauth :
400446 authenticator : apikey_authenticator
401447
448+ providers :
449+ api_key_user_provider :
450+ id : api_key_user_provider
451+
402452 .. code-block :: xml
403453
404454 <!-- app/config/security.xml -->
@@ -417,6 +467,8 @@ configuration or set it to ``false``:
417467 >
418468 <simple-preauth authenticator =" apikey_authenticator" />
419469 </firewall >
470+
471+ <provider name =" api_key_user_provider" id =" api_key_user_provider" />
420472 </config >
421473 </srv : container >
422474
@@ -435,14 +487,19 @@ configuration or set it to ``false``:
435487 ),
436488 ),
437489 ),
490+ 'providers' => array(
491+ 'api_key_user_provider' => array(
492+ 'id' => 'api_key_user_provider',
493+ ),
494+ ),
438495 ));
439496
440497 Even though the token is being stored in the session, the credentials - in this
441498case the API key (i.e. ``$token->getCredentials() ``) - are not stored in the session
442499for security reasons. To take advantage of the session, update ``ApiKeyAuthenticator ``
443500to see if the stored token has a valid User object that can be used::
444501
445- // src/Acme/HelloBundle /Security/ApiKeyAuthenticator.php
502+ // src/AppBundle /Security/ApiKeyAuthenticator.php
446503 // ...
447504
448505 class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
@@ -496,7 +553,7 @@ stored in the database, then you may want to re-query for a fresh version
496553of the user to make sure it's not out-of-date. But regardless of your requirements,
497554``refreshUser() `` should now return the User object::
498555
499- // src/Acme/HelloBundle /Security/ApiKeyUserProvider.php
556+ // src/AppBundle /Security/ApiKeyUserProvider.php
500557
501558 // ...
502559 class ApiKeyUserProvider implements UserProviderInterface
@@ -536,7 +593,7 @@ a certain URL (e.g. the redirect URL in OAuth).
536593Fortunately, handling this situation is easy: just check to see what the
537594current URL is before creating the token in ``createToken() ``::
538595
539- // src/Acme/HelloBundle /Security/ApiKeyAuthenticator.php
596+ // src/AppBundle /Security/ApiKeyAuthenticator.php
540597
541598 // ...
542599 use Symfony\Component\Security\Http\HttpUtils;
@@ -548,7 +605,7 @@ current URL is before creating the token in ``createToken()``::
548605
549606 protected $httpUtils;
550607
551- public function __construct(ApiKeyUserProviderInterface $userProvider, HttpUtils $httpUtils)
608+ public function __construct(UserProviderInterface $userProvider, HttpUtils $httpUtils)
552609 {
553610 $this->userProvider = $userProvider;
554611 $this->httpUtils = $httpUtils;
@@ -584,8 +641,8 @@ service:
584641 # ...
585642
586643 apikey_authenticator :
587- class : Acme\HelloBundle \Security\ApiKeyAuthenticator
588- arguments : ["@your_api_key_user_provider ", "@security.http_utils"]
644+ class : AppBundle \Security\ApiKeyAuthenticator
645+ arguments : ["@api_key_user_provider ", "@security.http_utils"]
589646
590647 .. code-block :: xml
591648
@@ -599,9 +656,9 @@ service:
599656 <!-- ... -->
600657
601658 <service id =" apikey_authenticator"
602- class =" Acme\HelloBundle \Security\ApiKeyAuthenticator"
659+ class =" AppBundle \Security\ApiKeyAuthenticator"
603660 >
604- <argument type =" service" id =" your_api_key_user_provider " />
661+ <argument type =" service" id =" api_key_user_provider " />
605662 <argument type =" service" id =" security.http_utils" />
606663 </service >
607664 </services >
@@ -616,9 +673,9 @@ service:
616673 // ...
617674
618675 $container->setDefinition('apikey_authenticator', new Definition(
619- 'Acme\HelloBundle \Security\ApiKeyAuthenticator',
676+ 'AppBundle \Security\ApiKeyAuthenticator',
620677 array(
621- new Reference('your_api_key_user_provider '),
678+ new Reference('api_key_user_provider '),
622679 new Reference('security.http_utils')
623680 )
624681 ));
0 commit comments