@@ -22,8 +22,8 @@ Your exact situation may differ, but in this example, a token is read
22
22
from an ``apikey `` query parameter, the proper username is loaded from that
23
23
value and then a User object is created::
24
24
25
- // src/Acme/HelloBundle /Security/ApiKeyAuthenticator.php
26
- namespace Acme\HelloBundle \Security;
25
+ // src/AppBundle /Security/ApiKeyAuthenticator.php
26
+ namespace AppBundle \Security;
27
27
28
28
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
29
29
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -147,8 +147,8 @@ used by Symfony's core user provider system).
147
147
148
148
The ``$userProvider `` might look something like this::
149
149
150
- // src/Acme/HelloBundle /Security/ApiKeyUserProvider.php
151
- namespace Acme\HelloBundle \Security;
150
+ // src/AppBundle /Security/ApiKeyUserProvider.php
151
+ namespace AppBundle \Security;
152
152
153
153
use Symfony\Component\Security\Core\User\UserProviderInterface;
154
154
use Symfony\Component\Security\Core\User\User;
@@ -192,6 +192,41 @@ The ``$userProvider`` might look something like this::
192
192
}
193
193
}
194
194
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
+
195
230
.. note ::
196
231
197
232
Read the dedicated article to learn
@@ -231,8 +266,8 @@ you can use to create an error ``Response``.
231
266
232
267
.. code-block :: php
233
268
234
- // src/Acme/HelloBundle /Security/ApiKeyAuthenticator.php
235
- namespace Acme\HelloBundle \Security;
269
+ // src/AppBundle /Security/ApiKeyAuthenticator.php
270
+ namespace AppBundle \Security;
236
271
237
272
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
238
273
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -270,8 +305,8 @@ your custom user provider as a service called ``your_api_key_user_provider``
270
305
# ...
271
306
272
307
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 "]
275
310
276
311
.. code-block :: xml
277
312
@@ -285,9 +320,9 @@ your custom user provider as a service called ``your_api_key_user_provider``
285
320
<!-- ... -->
286
321
287
322
<service id =" apikey_authenticator"
288
- class =" Acme\HelloBundle \Security\ApiKeyAuthenticator"
323
+ class =" AppBundle \Security\ApiKeyAuthenticator"
289
324
>
290
- <argument type =" service" id =" your_api_key_user_provider " />
325
+ <argument type =" service" id =" api_key_user_provider " />
291
326
</service >
292
327
</services >
293
328
</container >
@@ -301,8 +336,8 @@ your custom user provider as a service called ``your_api_key_user_provider``
301
336
// ...
302
337
303
338
$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 '))
306
341
));
307
342
308
343
Now, activate it in the ``firewalls `` section of your security configuration
@@ -323,6 +358,10 @@ using the ``simple_preauth`` key:
323
358
simple_preauth :
324
359
authenticator : apikey_authenticator
325
360
361
+ providers :
362
+ api_key_user_provider :
363
+ id : api_key_user_provider
364
+
326
365
.. code-block :: xml
327
366
328
367
<!-- app/config/security.xml -->
@@ -341,6 +380,8 @@ using the ``simple_preauth`` key:
341
380
>
342
381
<simple-preauth authenticator =" apikey_authenticator" />
343
382
</firewall >
383
+
384
+ <provider name =" api_key_user_provider" id =" api_key_user_provider" />
344
385
</config >
345
386
</srv : container >
346
387
@@ -360,6 +401,11 @@ using the ``simple_preauth`` key:
360
401
),
361
402
),
362
403
),
404
+ 'providers' => array(
405
+ 'api_key_user_provider' => array(
406
+ 'id' => 'api_key_user_provider',
407
+ ),
408
+ ),
363
409
));
364
410
365
411
That's it! Now, your ``ApiKeyAuthentication `` should be called at the beginning
@@ -399,6 +445,10 @@ configuration or set it to ``false``:
399
445
simple_preauth :
400
446
authenticator : apikey_authenticator
401
447
448
+ providers :
449
+ api_key_user_provider :
450
+ id : api_key_user_provider
451
+
402
452
.. code-block :: xml
403
453
404
454
<!-- app/config/security.xml -->
@@ -417,6 +467,8 @@ configuration or set it to ``false``:
417
467
>
418
468
<simple-preauth authenticator =" apikey_authenticator" />
419
469
</firewall >
470
+
471
+ <provider name =" api_key_user_provider" id =" api_key_user_provider" />
420
472
</config >
421
473
</srv : container >
422
474
@@ -435,14 +487,19 @@ configuration or set it to ``false``:
435
487
),
436
488
),
437
489
),
490
+ 'providers' => array(
491
+ 'api_key_user_provider' => array(
492
+ 'id' => 'api_key_user_provider',
493
+ ),
494
+ ),
438
495
));
439
496
440
497
Even though the token is being stored in the session, the credentials - in this
441
498
case the API key (i.e. ``$token->getCredentials() ``) - are not stored in the session
442
499
for security reasons. To take advantage of the session, update ``ApiKeyAuthenticator ``
443
500
to see if the stored token has a valid User object that can be used::
444
501
445
- // src/Acme/HelloBundle /Security/ApiKeyAuthenticator.php
502
+ // src/AppBundle /Security/ApiKeyAuthenticator.php
446
503
// ...
447
504
448
505
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
@@ -496,7 +553,7 @@ stored in the database, then you may want to re-query for a fresh version
496
553
of the user to make sure it's not out-of-date. But regardless of your requirements,
497
554
``refreshUser() `` should now return the User object::
498
555
499
- // src/Acme/HelloBundle /Security/ApiKeyUserProvider.php
556
+ // src/AppBundle /Security/ApiKeyUserProvider.php
500
557
501
558
// ...
502
559
class ApiKeyUserProvider implements UserProviderInterface
@@ -536,7 +593,7 @@ a certain URL (e.g. the redirect URL in OAuth).
536
593
Fortunately, handling this situation is easy: just check to see what the
537
594
current URL is before creating the token in ``createToken() ``::
538
595
539
- // src/Acme/HelloBundle /Security/ApiKeyAuthenticator.php
596
+ // src/AppBundle /Security/ApiKeyAuthenticator.php
540
597
541
598
// ...
542
599
use Symfony\Component\Security\Http\HttpUtils;
@@ -548,7 +605,7 @@ current URL is before creating the token in ``createToken()``::
548
605
549
606
protected $httpUtils;
550
607
551
- public function __construct(ApiKeyUserProviderInterface $userProvider, HttpUtils $httpUtils)
608
+ public function __construct(UserProviderInterface $userProvider, HttpUtils $httpUtils)
552
609
{
553
610
$this->userProvider = $userProvider;
554
611
$this->httpUtils = $httpUtils;
@@ -584,8 +641,8 @@ service:
584
641
# ...
585
642
586
643
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"]
589
646
590
647
.. code-block :: xml
591
648
@@ -599,9 +656,9 @@ service:
599
656
<!-- ... -->
600
657
601
658
<service id =" apikey_authenticator"
602
- class =" Acme\HelloBundle \Security\ApiKeyAuthenticator"
659
+ class =" AppBundle \Security\ApiKeyAuthenticator"
603
660
>
604
- <argument type =" service" id =" your_api_key_user_provider " />
661
+ <argument type =" service" id =" api_key_user_provider " />
605
662
<argument type =" service" id =" security.http_utils" />
606
663
</service >
607
664
</services >
@@ -616,9 +673,9 @@ service:
616
673
// ...
617
674
618
675
$container->setDefinition('apikey_authenticator', new Definition(
619
- 'Acme\HelloBundle \Security\ApiKeyAuthenticator',
676
+ 'AppBundle \Security\ApiKeyAuthenticator',
620
677
array(
621
- new Reference('your_api_key_user_provider '),
678
+ new Reference('api_key_user_provider '),
622
679
new Reference('security.http_utils')
623
680
)
624
681
));
0 commit comments