Skip to content

Commit 0e1128c

Browse files
committed
Merge branch '2.7' into 2.8
Conflicts: cookbook/security/api_key_authentication.rst
2 parents 2e2e2dd + 72ce4da commit 0e1128c

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

cookbook/security/api_key_authentication.rst

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ value and then a User object is created::
5353
// $apiKey = $request->headers->get('apikey');
5454

5555
if (!$apiKey) {
56-
throw new BadCredentialsException('No API key found');
56+
throw new BadCredentialsException();
5757

5858
// or to just skip api key authentication
5959
// return null;
@@ -66,6 +66,11 @@ value and then a User object is created::
6666
);
6767
}
6868

69+
public function supportsToken(TokenInterface $token, $providerKey)
70+
{
71+
return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
72+
}
73+
6974
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
7075
{
7176
if (!$userProvider instanceof ApiKeyUserProvider) {
@@ -97,11 +102,6 @@ value and then a User object is created::
97102
$user->getRoles()
98103
);
99104
}
100-
101-
public function supportsToken(TokenInterface $token, $providerKey)
102-
{
103-
return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
104-
}
105105
}
106106

107107
.. versionadded:: 2.8
@@ -111,7 +111,7 @@ value and then a User object is created::
111111

112112
Once you've :ref:`configured <cookbook-security-api-key-config>` everything,
113113
you'll be able to authenticate by adding an apikey parameter to the query
114-
string, like ``http://example.com/admin/foo?apikey=37b51d194a7513e45b56f6524f2d51f2``.
114+
string, like ``http://example.com/api/foo?apikey=37b51d194a7513e45b56f6524f2d51f2``.
115115

116116
The authentication process has several steps, and your implementation will
117117
probably differ:
@@ -196,7 +196,7 @@ The ``$userProvider`` might look something like this::
196196
null,
197197
// the roles for the user - you may choose to determine
198198
// these dynamically somehow based on the user
199-
array('ROLE_USER')
199+
array('ROLE_API')
200200
);
201201
}
202202

@@ -268,6 +268,7 @@ would allow you to have custom data on the ``User`` object.
268268

269269
Finally, just make sure that ``supportsClass()`` returns ``true`` for User
270270
objects with the same class as whatever user you return in ``loadUserByUsername()``.
271+
271272
If your authentication is stateless like in this example (i.e. you expect
272273
the user to send the API key with every request and so you don't save the
273274
login to the session), then you can simply throw the ``UnsupportedUserException``
@@ -281,7 +282,7 @@ exception in ``refreshUser()``.
281282
Handling Authentication Failure
282283
-------------------------------
283284

284-
In order for your ``ApiKeyAuthenticator`` to correctly display a 403
285+
In order for your ``ApiKeyAuthenticator`` to correctly display a 401
285286
http status when either bad credentials or authentication fails you will
286287
need to implement the :class:`Symfony\\Component\\Security\\Http\\Authentication\\AuthenticationFailureHandlerInterface` on your
287288
Authenticator. This will provide a method ``onAuthenticationFailure`` which
@@ -308,7 +309,7 @@ you can use to create an error ``Response``.
308309
// this contains information about *why* authentication failed
309310
// use it, or return your own message
310311
strtr($exception->getMessageKey(), $exception->getMessageData()),
311-
403
312+
401
312313
);
313314
}
314315
}
@@ -377,7 +378,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
377378
378379
firewalls:
379380
secured_area:
380-
pattern: ^/admin
381+
pattern: ^/api
381382
stateless: true
382383
simple_preauth:
383384
authenticator: apikey_authenticator
@@ -400,7 +401,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
400401
<!-- ... -->
401402
402403
<firewall name="secured_area"
403-
pattern="^/admin"
404+
pattern="^/api"
404405
stateless="true"
405406
provider="api_key_user_provider"
406407
>
@@ -420,7 +421,7 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
420421
$container->loadFromExtension('security', array(
421422
'firewalls' => array(
422423
'secured_area' => array(
423-
'pattern' => '^/admin',
424+
'pattern' => '^/api',
424425
'stateless' => true,
425426
'simple_preauth' => array(
426427
'authenticator' => 'apikey_authenticator',
@@ -435,6 +436,44 @@ using the ``simple_preauth`` and ``provider`` keys respectively:
435436
),
436437
));
437438
439+
If you have defined ``access_control``, make sure to add a new entry:
440+
441+
.. configuration-block::
442+
443+
.. code-block:: yaml
444+
445+
# app/config/security.yml
446+
security:
447+
# ...
448+
449+
access_control:
450+
- { path: ^/api, roles: ROLE_API }
451+
452+
.. code-block:: xml
453+
454+
<!-- app/config/security.xml -->
455+
<?xml version="1.0" encoding="UTF-8"?>
456+
<srv:container xmlns="http://symfony.com/schema/dic/security"
457+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
458+
xmlns:srv="http://symfony.com/schema/dic/services"
459+
xsi:schemaLocation="http://symfony.com/schema/dic/services
460+
http://symfony.com/schema/dic/services/services-1.0.xsd">
461+
462+
<rule path="^/api" role="ROLE_API" />
463+
</srv:container>
464+
465+
.. code-block:: php
466+
467+
// app/config/security.php
468+
$container->loadFromExtension('security', array(
469+
'access_control' => array(
470+
array(
471+
'path' => '^/api',
472+
'role' => 'ROLE_API',
473+
),
474+
),
475+
));
476+
438477
That's it! Now, your ``ApiKeyAuthenticator`` should be called at the beginning
439478
of each request and your authentication process will take place.
440479

@@ -467,7 +506,7 @@ configuration or set it to ``false``:
467506
468507
firewalls:
469508
secured_area:
470-
pattern: ^/admin
509+
pattern: ^/api
471510
stateless: false
472511
simple_preauth:
473512
authenticator: apikey_authenticator
@@ -490,7 +529,7 @@ configuration or set it to ``false``:
490529
<!-- ... -->
491530
492531
<firewall name="secured_area"
493-
pattern="^/admin"
532+
pattern="^/api"
494533
stateless="false"
495534
provider="api_key_user_provider"
496535
>
@@ -509,7 +548,7 @@ configuration or set it to ``false``:
509548
$container->loadFromExtension('security', array(
510549
'firewalls' => array(
511550
'secured_area' => array(
512-
'pattern' => '^/admin',
551+
'pattern' => '^/api',
513552
'stateless' => false,
514553
'simple_preauth' => array(
515554
'authenticator' => 'apikey_authenticator',
-564 Bytes
Loading

0 commit comments

Comments
 (0)