Skip to content

Commit 5bab527

Browse files
committed
Merge branch '7.2' into 7.3
* 7.2: Tweaks [security] `make:security:custom`
2 parents 8f5409f + 3b50d58 commit 5bab527

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

security/custom_authenticator.rst

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
How to Write a Custom Authenticator
22
===================================
33

4-
Symfony comes with :ref:`many authenticators <security-authenticators>` and
5-
third party bundles also implement more complex cases like JWT and oAuth
6-
2.0. However, sometimes you need to implement a custom authentication
7-
mechanism that doesn't exist yet or you need to customize one. In such
8-
cases, you must create and use your own authenticator.
4+
Symfony comes with :ref:`many authenticators <security-authenticators>`, and
5+
third-party bundles also implement more complex cases like JWT and OAuth 2.0.
6+
However, sometimes you need to implement a custom authentication mechanism
7+
that doesn't exist yet, or you need to customize an existing one.
98

10-
Authenticators should implement the
11-
:class:`Symfony\\Component\\Security\\Http\\Authenticator\\AuthenticatorInterface`.
12-
You can also extend
13-
:class:`Symfony\\Component\\Security\\Http\\Authenticator\\AbstractAuthenticator`,
14-
which has a default implementation for the ``createToken()``
15-
method that fits most use-cases::
9+
To save time, you can install `Symfony Maker`_ and let Symfony generate a new
10+
authenticator by running the following command:
11+
12+
.. code-block:: terminal
13+
14+
$ php bin/console make:security:custom
15+
16+
What is the class name of the authenticator (e.g. CustomAuthenticator):
17+
> ApiKeyAuthenticator
18+
19+
updated: config/packages/security.yaml
20+
created: src/Security/ApiKeyAuthenticator.php
21+
22+
Success!
23+
24+
Open the ``src/Security/ApiKeyAuthenticator.php`` file created by this command,
25+
and you'll find something like the following::
1626

1727
// src/Security/ApiKeyAuthenticator.php
1828
namespace App\Security;
@@ -77,13 +87,23 @@ method that fits most use-cases::
7787
}
7888
}
7989

90+
Authenticators must implement the
91+
:class:`Symfony\\Component\\Security\\Http\\Authenticator\\AuthenticatorInterface`.
92+
You can also extend
93+
:class:`Symfony\\Component\\Security\\Http\\Authenticator\\AbstractAuthenticator`,
94+
which provides a default implementation of the ``createToken()`` method suitable
95+
for most use cases.
96+
8097
.. tip::
8198

82-
If your custom authenticator is a login form, you can extend from the
99+
If your custom authenticator is a login form, consider extending
83100
:class:`Symfony\\Component\\Security\\Http\\Authenticator\\AbstractLoginFormAuthenticator`
84-
class instead to make your job easier.
101+
to simplify your implementation.
85102

86-
The authenticator can be enabled using the ``custom_authenticators`` setting:
103+
Custom authenticators must be explicitly enabled in the security configuration
104+
using the ``custom_authenticators`` setting of your firewall(s). If you used the
105+
``make:security:custom`` command, this configuration is already updated, but you
106+
should review it:
87107

88108
.. configuration-block::
89109

@@ -479,4 +499,5 @@ authenticator methods (e.g. ``createToken()``)::
479499
}
480500
}
481501

502+
.. _`Symfony Maker`: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html
482503
.. _`session storage flooding`: https://symfony.com/blog/cve-2016-4423-large-username-storage-in-session

0 commit comments

Comments
 (0)