PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/cookbook/security/remember_me.rst

https://github.com/pedrobc/symfony-docs
ReStructuredText | 207 lines | 157 code | 50 blank | 0 comment | 0 complexity | 0ce701c16c0a4c81b4301c9758b2a33a MD5 | raw file
  1. How to add "Remember Me" Login Functionality
  2. ============================================
  3. Once a user is authenticated, their credentials are typically stored in the
  4. session. This means that when the session ends they will be logged out and
  5. have to provide their login details again next time they wish to access the
  6. application. You can allow users to choose to stay logged in for longer than
  7. the session lasts using a cookie with the ``remember_me`` firewall option.
  8. The firewall needs to have a secret key configured, which is used to encrypt
  9. the cookie's content. It also has several options with default values which
  10. are shown here:
  11. .. configuration-block::
  12. .. code-block:: yaml
  13. # app/config/security.yml
  14. firewalls:
  15. main:
  16. remember_me:
  17. key: aSecretKey
  18. lifetime: 3600
  19. path: /
  20. domain: ~ # Defaults to the current domain from $_SERVER
  21. .. code-block:: xml
  22. <!-- app/config/security.xml -->
  23. <config>
  24. <firewall>
  25. <remember-me
  26. key="aSecretKey"
  27. lifetime="3600"
  28. path="/"
  29. domain="" <!-- Defaults to the current domain from $_SERVER -->
  30. />
  31. </firewall>
  32. </config>
  33. .. code-block:: php
  34. // app/config/security.php
  35. $container->loadFromExtension('security', array(
  36. 'firewalls' => array(
  37. 'main' => array('remember_me' => array(
  38. 'key' => '/login_check',
  39. 'lifetime' => 3600,
  40. 'path' => '/',
  41. 'domain' => '', // Defaults to the current domain from $_SERVER
  42. )),
  43. ),
  44. ));
  45. It's a good idea to provide the user with the option to use or not use the
  46. remember me functionality, as it will not always be appropriate. The usual
  47. way of doing this is to add a checkbox to the login form. By giving the checkbox
  48. the name ``_remember_me``, the cookie will automatically be set when the checkbox
  49. is checked and the user successfully logs in. So, your specific login form
  50. might ultimately look like this:
  51. .. configuration-block::
  52. .. code-block:: html+jinja
  53. {# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
  54. {% if error %}
  55. <div>{{ error.message }}</div>
  56. {% endif %}
  57. <form action="{{ path('login_check') }}" method="post">
  58. <label for="username">Username:</label>
  59. <input type="text" id="username" name="_username" value="{{ last_username }}" />
  60. <label for="password">Password:</label>
  61. <input type="password" id="password" name="_password" />
  62. <input type="checkbox" id="remember_me" name="_remember_me" checked />
  63. <label for="remember_me">Keep me logged in</label>
  64. <input type="submit" name="login" />
  65. </form>
  66. .. code-block:: html+php
  67. <?php // src/Acme/SecurityBundle/Resources/views/Security/login.html.php ?>
  68. <?php if ($error): ?>
  69. <div><?php echo $error->getMessage() ?></div>
  70. <?php endif; ?>
  71. <form action="<?php echo $view['router']->generate('login_check') ?>" method="post">
  72. <label for="username">Username:</label>
  73. <input type="text" id="username"
  74. name="_username" value="<?php echo $last_username ?>" />
  75. <label for="password">Password:</label>
  76. <input type="password" id="password" name="_password" />
  77. <input type="checkbox" id="remember_me" name="_remember_me" checked />
  78. <label for="remember_me">Keep me logged in</label>
  79. <input type="submit" name="login" />
  80. </form>
  81. The user will then automatically be logged in on subsequent visits while
  82. the cookie remains valid.
  83. Forcing the User to Re-authenticate before accessing certain Resources
  84. ----------------------------------------------------------------------
  85. When the user returns to your site, he/she is authenticated automatically based
  86. on the information stored in the remember me cookie. This allows the user
  87. to access protected resources as if the user had actually authenticated upon
  88. visiting the site.
  89. In some cases, however, you may want to force the user to actually re-authenticate
  90. before accessing certain resources. For example, you might allow a "remember me"
  91. user to see basic account information, but then require them to actually
  92. re-authenticate before modifying that information.
  93. The security component provides an easy way to do this. In addition to roles
  94. explicitly assigned to them, users are automatically given one of the following
  95. roles depending on how they are authenticated:
  96. * ``IS_AUTHENTICATED_ANONYMOUSLY`` - automatically assigned to a user who is
  97. in a firewall protected part of the site but who has not actually logged in.
  98. This is only possible if anonymous access has been allowed.
  99. * ``IS_AUTHENTICATED_REMEMBERED`` - automatically assigned to a user who
  100. was authenticated via a remember me cookie.
  101. * ``IS_AUTHENTICATED_FULLY`` - automatically assigned to a user that has
  102. provided their login details during the current session.
  103. You can use these to control access beyond the explicitly assigned roles.
  104. .. note::
  105. If you have the ``IS_AUTHENTICATED_REMEMBERED`` role, then you also
  106. have the ``IS_AUTHENTICATED_ANONYMOUSLY`` role. If you have the ``IS_AUTHENTICATED_FULLY``
  107. role, then you also have the other two roles. In other words, these roles
  108. represent three levels of increasing "strength" of authentication.
  109. You can use these additional roles for finer grained control over access to
  110. parts of a site. For example, you may want you user to be able to view their
  111. account at ``/account`` when authenticated by cookie but to have to provide
  112. their login details to be able to edit the account details. You can do this
  113. by securing specific controller actions using these roles. The edit action
  114. in the controller could be secured using the service context.
  115. In the following example, the action is only allowed if the user has the
  116. ``IS_AUTHENTICATED_FULLY`` role.
  117. .. code-block:: php
  118. use Symfony\Component\Security\Core\Exception\AccessDeniedException
  119. // ...
  120. public function editAction()
  121. {
  122. if (false === $this->get('security.context')->isGranted(
  123. 'IS_AUTHENTICATED_FULLY'
  124. )) {
  125. throw new AccessDeniedException();
  126. }
  127. // ...
  128. }
  129. You can also choose to install and use the optional JMSSecurityExtraBundle_,
  130. which can secure your controller using annotations:
  131. .. code-block:: php
  132. use JMS\SecurityExtraBundle\Annotation\Secure;
  133. /**
  134. * @Secure(roles="IS_AUTHENTICATED_FULLY")
  135. */
  136. public function editAction($name)
  137. {
  138. // ...
  139. }
  140. .. tip::
  141. If you also had an access control in your security configuration that
  142. required the user to have a ``ROLE_USER`` role in order to access any
  143. of the account area, then you'd have the following situation:
  144. * If a non-authenticated (or anonymously authenticated user) tries to
  145. access the account area, the user will be asked to authenticate.
  146. * Once the user has entered his username and password, assuming the
  147. user receives the ``ROLE_USER`` role per your configuration, the user
  148. will have the ``IS_AUTHENTICATED_FULLY`` role and be able to access
  149. any page in the account section, including the ``editAction`` controller.
  150. * If the user's session ends, when the user returns to the site, he will
  151. be able to access every account page - except for the edit page - without
  152. being forced to re-authenticate. However, when he tries to access the
  153. ``editAction`` controller, he will be forced to re-authenticate, since
  154. he is not, yet, fully authenticated.
  155. For more information on securing services or methods in this way,
  156. see :doc:`/cookbook/security/securing_services`.
  157. .. _JMSSecurityExtraBundle: https://github.com/schmittjoh/JMSSecurityExtraBundle