Skip to content

Commit c4cbd84

Browse files
peterrehmweaverryan
authored andcommitted
Updated according to comment and changed to AppBundle
1 parent a6fb18c commit c4cbd84

File tree

1 file changed

+68
-30
lines changed

1 file changed

+68
-30
lines changed

cookbook/security/api_key_authentication.rst

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Your exact situation may differ, but in this example, a token is read
2222
from an ``apikey`` query parameter, the proper username is loaded from that
2323
value 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

148148
The ``$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,44 @@ The ``$userProvider`` might look something like this::
192192
}
193193
}
194194

195+
No 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+
use Symfony\Component\DependencyInjection\Definition;
226+
227+
// ...
228+
229+
$container->setDefinition('api_key_user_provider', new Definition(
230+
'AppBundle\Security\ApiKeyUserProvider',
231+
));
232+
195233
.. note::
196234

197235
Read the dedicated article to learn
@@ -231,8 +269,8 @@ you can use to create an error ``Response``.
231269

232270
.. code-block:: php
233271
234-
// src/Acme/HelloBundle/Security/ApiKeyAuthenticator.php
235-
namespace Acme\HelloBundle\Security;
272+
// src/AppBundle/Security/ApiKeyAuthenticator.php
273+
namespace AppBundle\Security;
236274
237275
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
238276
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -270,8 +308,8 @@ your custom user provider as a service called ``your_api_key_user_provider``
270308
# ...
271309
272310
apikey_authenticator:
273-
class: Acme\HelloBundle\Security\ApiKeyAuthenticator
274-
arguments: ["@your_api_key_user_provider"]
311+
class: AppBundle\Security\ApiKeyAuthenticator
312+
arguments: ["@api_key_user_provider"]
275313
276314
.. code-block:: xml
277315
@@ -285,9 +323,9 @@ your custom user provider as a service called ``your_api_key_user_provider``
285323
<!-- ... -->
286324
287325
<service id="apikey_authenticator"
288-
class="Acme\HelloBundle\Security\ApiKeyAuthenticator"
326+
class="AppBundle\Security\ApiKeyAuthenticator"
289327
>
290-
<argument type="service" id="your_api_key_user_provider" />
328+
<argument type="service" id="api_key_user_provider" />
291329
</service>
292330
</services>
293331
</container>
@@ -301,8 +339,8 @@ your custom user provider as a service called ``your_api_key_user_provider``
301339
// ...
302340
303341
$container->setDefinition('apikey_authenticator', new Definition(
304-
'Acme\HelloBundle\Security\ApiKeyAuthenticator',
305-
array(new Reference('your_api_key_user_provider'))
342+
'AppBundle\Security\ApiKeyAuthenticator',
343+
array(new Reference('api_key_user_provider'))
306344
));
307345
308346
Now, activate it in the ``firewalls`` section of your security configuration
@@ -324,8 +362,8 @@ using the ``simple_preauth`` key:
324362
authenticator: apikey_authenticator
325363
326364
providers:
327-
simple_preauth:
328-
id: your_api_key_user_provider
365+
api_key_user_provider:
366+
id: api_key_user_provider
329367
330368
.. code-block:: xml
331369
@@ -346,7 +384,7 @@ using the ``simple_preauth`` key:
346384
<simple-preauth authenticator="apikey_authenticator" />
347385
</firewall>
348386
349-
<provider name="simple_preauth" id="your_api_key_user_provider" />
387+
<provider name="api_key_user_provider" id="api_key_user_provider" />
350388
</config>
351389
</srv:container>
352390
@@ -368,7 +406,7 @@ using the ``simple_preauth`` key:
368406
),
369407
'providers' => array(
370408
'simple_preauth' => array(
371-
'id' => 'your_api_key_user_provider',
409+
'id' => 'api_key_user_provider',
372410
),
373411
),
374412
));
@@ -411,8 +449,8 @@ configuration or set it to ``false``:
411449
authenticator: apikey_authenticator
412450
413451
providers:
414-
simple_preauth:
415-
id: your_api_key_user_provider
452+
api_key_user_provider:
453+
id: api_key_user_provider
416454
417455
.. code-block:: xml
418456
@@ -433,7 +471,7 @@ configuration or set it to ``false``:
433471
<simple-preauth authenticator="apikey_authenticator" />
434472
</firewall>
435473
436-
<provider name="simple_preauth" id="your_api_key_user_provider" />
474+
<provider name="api_key_user_provider" id="api_key_user_provider" />
437475
</config>
438476
</srv:container>
439477
@@ -453,8 +491,8 @@ configuration or set it to ``false``:
453491
),
454492
),
455493
'providers' => array(
456-
'simple_preauth' => array(
457-
'id' => 'your_api_key_user_provider',
494+
'api_key_user_provider' => array(
495+
'id' => 'api_key_user_provider',
458496
),
459497
),
460498
));
@@ -464,7 +502,7 @@ case the API key (i.e. ``$token->getCredentials()``) - are not stored in the ses
464502
for security reasons. To take advantage of the session, update ``ApiKeyAuthenticator``
465503
to see if the stored token has a valid User object that can be used::
466504

467-
// src/Acme/HelloBundle/Security/ApiKeyAuthenticator.php
505+
// src/AppBundle/Security/ApiKeyAuthenticator.php
468506
// ...
469507

470508
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
@@ -518,7 +556,7 @@ stored in the database, then you may want to re-query for a fresh version
518556
of the user to make sure it's not out-of-date. But regardless of your requirements,
519557
``refreshUser()`` should now return the User object::
520558

521-
// src/Acme/HelloBundle/Security/ApiKeyUserProvider.php
559+
// src/AppBundle/Security/ApiKeyUserProvider.php
522560

523561
// ...
524562
class ApiKeyUserProvider implements UserProviderInterface
@@ -558,7 +596,7 @@ a certain URL (e.g. the redirect URL in OAuth).
558596
Fortunately, handling this situation is easy: just check to see what the
559597
current URL is before creating the token in ``createToken()``::
560598

561-
// src/Acme/HelloBundle/Security/ApiKeyAuthenticator.php
599+
// src/AppBundle/Security/ApiKeyAuthenticator.php
562600

563601
// ...
564602
use Symfony\Component\Security\Http\HttpUtils;
@@ -570,7 +608,7 @@ current URL is before creating the token in ``createToken()``::
570608

571609
protected $httpUtils;
572610

573-
public function __construct(ApiKeyUserProviderInterface $userProvider, HttpUtils $httpUtils)
611+
public function __construct(UserProviderInterface $userProvider, HttpUtils $httpUtils)
574612
{
575613
$this->userProvider = $userProvider;
576614
$this->httpUtils = $httpUtils;
@@ -606,8 +644,8 @@ service:
606644
# ...
607645
608646
apikey_authenticator:
609-
class: Acme\HelloBundle\Security\ApiKeyAuthenticator
610-
arguments: ["@your_api_key_user_provider", "@security.http_utils"]
647+
class: AppBundle\Security\ApiKeyAuthenticator
648+
arguments: ["@api_key_user_provider", "@security.http_utils"]
611649
612650
.. code-block:: xml
613651
@@ -621,9 +659,9 @@ service:
621659
<!-- ... -->
622660
623661
<service id="apikey_authenticator"
624-
class="Acme\HelloBundle\Security\ApiKeyAuthenticator"
662+
class="AppBundle\Security\ApiKeyAuthenticator"
625663
>
626-
<argument type="service" id="your_api_key_user_provider" />
664+
<argument type="service" id="api_key_user_provider" />
627665
<argument type="service" id="security.http_utils" />
628666
</service>
629667
</services>
@@ -640,7 +678,7 @@ service:
640678
$container->setDefinition('apikey_authenticator', new Definition(
641679
'Acme\HelloBundle\Security\ApiKeyAuthenticator',
642680
array(
643-
new Reference('your_api_key_user_provider'),
681+
new Reference('api_key_user_provider'),
644682
new Reference('security.http_utils')
645683
)
646684
));

0 commit comments

Comments
 (0)