|
1 | 1 | How to Write a Custom Authenticator |
2 | 2 | =================================== |
3 | 3 |
|
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. |
9 | 8 |
|
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:: |
16 | 26 |
|
17 | 27 | // src/Security/ApiKeyAuthenticator.php |
18 | 28 | namespace App\Security; |
@@ -77,13 +87,23 @@ method that fits most use-cases:: |
77 | 87 | } |
78 | 88 | } |
79 | 89 |
|
| 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 | + |
80 | 97 | .. tip:: |
81 | 98 |
|
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 |
83 | 100 | :class:`Symfony\\Component\\Security\\Http\\Authenticator\\AbstractLoginFormAuthenticator` |
84 | | - class instead to make your job easier. |
| 101 | + to simplify your implementation. |
85 | 102 |
|
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: |
87 | 107 |
|
88 | 108 | .. configuration-block:: |
89 | 109 |
|
@@ -479,4 +499,5 @@ authenticator methods (e.g. ``createToken()``):: |
479 | 499 | } |
480 | 500 | } |
481 | 501 |
|
| 502 | +.. _`Symfony Maker`: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html |
482 | 503 | .. _`session storage flooding`: https://symfony.com/blog/cve-2016-4423-large-username-storage-in-session |
0 commit comments