Skip to content

Commit c3b3b7f

Browse files
committed
minor #11392 Add info about storing remember me tokens in a database (pierredup)
This PR was merged into the 3.4 branch. Discussion ---------- Add info about storing remember me tokens in a database I only recently saw that the remember me functionality has a `token_provider` option, and there is a default class to store tokens in the database as part of the DoctrineBridge, however there is no clear information on how this provider should be used (the class itself contains some hints, but the `token_provider` setting wasn't clear which values it accepts). So I want to add a section to the remember me docs to explain how to use the `DoctrineTokenProvider`, which might also give some hints on how to create a custom provider Commits ------- 311543f Add info about storing remember me tokens in a database
2 parents 9c40497 + 311543f commit c3b3b7f

File tree

1 file changed

+115
-6
lines changed

1 file changed

+115
-6
lines changed

security/remember_me.rst

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,8 @@ The ``remember_me`` firewall defines the following configuration options:
125125
end user.
126126

127127
``token_provider`` (default value: ``null``)
128-
Defines the service id of a token provider to use. By default, tokens are
129-
stored in a cookie. For example, you might want to store the token in a
130-
database, to not have a (hashed) version of the password in a cookie. The
131-
DoctrineBridge comes with a
132-
``Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider`` that
133-
you can use.
128+
Defines the service id of a token provider to use. If you want to store tokens
129+
in the database, see :ref:`token_in_database`.
134130

135131
Forcing the User to Opt-Out of the Remember Me Feature
136132
------------------------------------------------------
@@ -261,3 +257,116 @@ your controller using annotations::
261257

262258
For more information on securing services or methods in this way,
263259
see :doc:`/security/securing_services`.
260+
261+
.. _token_in_database:
262+
263+
Storing Remember Me Tokens in the Database
264+
------------------------------------------
265+
266+
By default, tokens are stored in a cookie. You can choose to store the token in a database,
267+
to not have a (hashed) version of the password in a cookie.
268+
The DoctrineBridge comes with a
269+
:class:`Symfony\\Bridge\\Doctrine\\Security\\RememberMe\\DoctrineTokenProvider` class
270+
that you can use. In order to use the ``DoctrineTokenProvider``, you first
271+
need to register it as a service:
272+
273+
.. configuration-block::
274+
275+
.. code-block:: yaml
276+
277+
# app/config/services.yml
278+
services:
279+
# ...
280+
281+
Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider: ~
282+
283+
.. code-block:: xml
284+
285+
<!-- app/config/services.xml -->
286+
<?xml version="1.0" encoding="UTF-8" ?>
287+
<container xmlns="http://symfony.com/schema/dic/services"
288+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
289+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
290+
291+
<services>
292+
<service id="Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider" />
293+
</services>
294+
</container>
295+
296+
.. code-block:: php
297+
298+
// app/config/services.php
299+
use Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider;
300+
301+
$container->register(DoctrineTokenProvider::class);
302+
303+
The ``DoctrineTokenProvider`` makes use of a database table to store the tokens.
304+
You need to ensure the following table exists in your database:
305+
306+
.. code-block:: sql
307+
308+
CREATE TABLE `rememberme_token` (
309+
`series` char(88) UNIQUE PRIMARY KEY NOT NULL,
310+
`value` char(88) NOT NULL,
311+
`lastUsed` datetime NOT NULL,
312+
`class` varchar(100) NOT NULL,
313+
`username` varchar(200) NOT NULL
314+
);
315+
316+
Then you need to set the ``token_provider`` option of the ``remember_me`` config
317+
to the service you just created:
318+
319+
.. configuration-block::
320+
321+
.. code-block:: yaml
322+
323+
# app/config/security.yml
324+
security:
325+
# ...
326+
327+
firewalls:
328+
main:
329+
# ...
330+
remember_me:
331+
# ...
332+
token_provider: '@Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider'
333+
334+
.. code-block:: xml
335+
336+
<!-- app/config/security.xml -->
337+
<?xml version="1.0" encoding="UTF-8" ?>
338+
<srv:container xmlns="http://symfony.com/schema/dic/security"
339+
xmlns:srv="http://symfony.com/schema/dic/services"
340+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
341+
xsi:schemaLocation="http://symfony.com/schema/dic/services
342+
https://symfony.com/schema/dic/services/services-1.0.xsd">
343+
344+
<config>
345+
<!-- ... -->
346+
347+
<firewall name="main">
348+
<!-- ... -->
349+
350+
<remember-me
351+
token_profider="@Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider"
352+
/>
353+
</firewall>
354+
</config>
355+
</srv:container>
356+
357+
.. code-block:: php
358+
359+
// app/config/security.php
360+
$container->loadFromExtension('security', [
361+
// ...
362+
363+
'firewalls' => [
364+
'main' => [
365+
// ...
366+
'remember_me' => [
367+
// ...
368+
'token_provider' => '@Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider',
369+
],
370+
],
371+
],
372+
]);

0 commit comments

Comments
 (0)