auth.txt 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. =======================
  2. ``django.contrib.auth``
  3. =======================
  4. This document provides API reference material for the components of Django's
  5. authentication system. For more details on the usage of these components or
  6. how to customize authentication and authorization see the :doc:`authentication
  7. topic guide </topics/auth/index>`.
  8. .. currentmodule:: django.contrib.auth
  9. ``User`` model
  10. ==============
  11. Fields
  12. ------
  13. .. class:: models.User
  14. :class:`~django.contrib.auth.models.User` objects have the following
  15. fields:
  16. .. attribute:: username
  17. Required. 150 characters or fewer. Usernames may contain alphanumeric,
  18. ``_``, ``@``, ``+``, ``.`` and ``-`` characters.
  19. The ``max_length`` should be sufficient for many use cases. If you need
  20. a longer length, please use a :ref:`custom user model
  21. <specifying-custom-user-model>`. If you use MySQL with the ``utf8mb4``
  22. encoding (recommended for proper Unicode support), specify at most
  23. ``max_length=191`` because MySQL can only create unique indexes with
  24. 191 characters in that case by default.
  25. .. admonition:: Usernames and Unicode
  26. Django originally accepted only ASCII letters and numbers in
  27. usernames. Although it wasn't a deliberate choice, Unicode
  28. characters have always been accepted when using Python 3. Django
  29. 1.10 officially added Unicode support in usernames, keeping the
  30. ASCII-only behavior on Python 2, with the option to customize the
  31. behavior using :attr:`.User.username_validator`.
  32. .. attribute:: first_name
  33. Optional (:attr:`blank=True <django.db.models.Field.blank>`). 30
  34. characters or fewer.
  35. .. attribute:: last_name
  36. Optional (:attr:`blank=True <django.db.models.Field.blank>`). 150
  37. characters or fewer.
  38. .. versionchanged:: 2.0
  39. The ``max_length`` increased from 30 to 150 characters.
  40. .. attribute:: email
  41. Optional (:attr:`blank=True <django.db.models.Field.blank>`). Email
  42. address.
  43. .. attribute:: password
  44. Required. A hash of, and metadata about, the password. (Django doesn't
  45. store the raw password.) Raw passwords can be arbitrarily long and can
  46. contain any character. See the :doc:`password documentation
  47. </topics/auth/passwords>`.
  48. .. attribute:: groups
  49. Many-to-many relationship to :class:`~django.contrib.auth.models.Group`
  50. .. attribute:: user_permissions
  51. Many-to-many relationship to :class:`~django.contrib.auth.models.Permission`
  52. .. attribute:: is_staff
  53. Boolean. Designates whether this user can access the admin site.
  54. .. attribute:: is_active
  55. Boolean. Designates whether this user account should be considered
  56. active. We recommend that you set this flag to ``False`` instead of
  57. deleting accounts; that way, if your applications have any foreign keys
  58. to users, the foreign keys won't break.
  59. This doesn't necessarily control whether or not the user can log in.
  60. Authentication backends aren't required to check for the ``is_active``
  61. flag but the default backend
  62. (:class:`~django.contrib.auth.backends.ModelBackend`) and the
  63. :class:`~django.contrib.auth.backends.RemoteUserBackend` do. You can
  64. use :class:`~django.contrib.auth.backends.AllowAllUsersModelBackend`
  65. or :class:`~django.contrib.auth.backends.AllowAllUsersRemoteUserBackend`
  66. if you want to allow inactive users to login. In this case, you'll also
  67. want to customize the
  68. :class:`~django.contrib.auth.forms.AuthenticationForm` used by the
  69. :class:`~django.contrib.auth.views.LoginView` as it rejects inactive
  70. users. Be aware that the permission-checking methods such as
  71. :meth:`~django.contrib.auth.models.User.has_perm` and the
  72. authentication in the Django admin all return ``False`` for inactive
  73. users.
  74. .. attribute:: is_superuser
  75. Boolean. Designates that this user has all permissions without
  76. explicitly assigning them.
  77. .. attribute:: last_login
  78. A datetime of the user's last login.
  79. .. attribute:: date_joined
  80. A datetime designating when the account was created. Is set to the
  81. current date/time by default when the account is created.
  82. Attributes
  83. ----------
  84. .. class:: models.User
  85. .. attribute:: is_authenticated
  86. Read-only attribute which is always ``True`` (as opposed to
  87. ``AnonymousUser.is_authenticated`` which is always ``False``). This is
  88. a way to tell if the user has been authenticated. This does not imply
  89. any permissions and doesn't check if the user is active or has a valid
  90. session. Even though normally you will check this attribute on
  91. ``request.user`` to find out whether it has been populated by the
  92. :class:`~django.contrib.auth.middleware.AuthenticationMiddleware`
  93. (representing the currently logged-in user), you should know this
  94. attribute is ``True`` for any :class:`~models.User` instance.
  95. .. attribute:: is_anonymous
  96. Read-only attribute which is always ``False``. This is a way of
  97. differentiating :class:`~models.User` and :class:`~models.AnonymousUser`
  98. objects. Generally, you should prefer using
  99. :attr:`~django.contrib.auth.models.User.is_authenticated` to this
  100. attribute.
  101. .. attribute:: username_validator
  102. Points to a validator instance used to validate usernames. Defaults to
  103. :class:`validators.UnicodeUsernameValidator`.
  104. To change the default username validator, you can subclass the ``User``
  105. model and set this attribute to a different validator instance. For
  106. example, to use ASCII usernames::
  107. from django.contrib.auth.models import User
  108. from django.contrib.auth.validators import ASCIIUsernameValidator
  109. class CustomUser(User):
  110. username_validator = ASCIIUsernameValidator()
  111. class Meta:
  112. proxy = True # If no new field is added.
  113. Methods
  114. -------
  115. .. class:: models.User
  116. .. method:: get_username()
  117. Returns the username for the user. Since the ``User`` model can be
  118. swapped out, you should use this method instead of referencing the
  119. username attribute directly.
  120. .. method:: get_full_name()
  121. Returns the :attr:`~django.contrib.auth.models.User.first_name` plus
  122. the :attr:`~django.contrib.auth.models.User.last_name`, with a space in
  123. between.
  124. .. method:: get_short_name()
  125. Returns the :attr:`~django.contrib.auth.models.User.first_name`.
  126. .. method:: set_password(raw_password)
  127. Sets the user's password to the given raw string, taking care of the
  128. password hashing. Doesn't save the
  129. :class:`~django.contrib.auth.models.User` object.
  130. When the ``raw_password`` is ``None``, the password will be set to an
  131. unusable password, as if
  132. :meth:`~django.contrib.auth.models.User.set_unusable_password()`
  133. were used.
  134. .. method:: check_password(raw_password)
  135. Returns ``True`` if the given raw string is the correct password for
  136. the user. (This takes care of the password hashing in making the
  137. comparison.)
  138. .. method:: set_unusable_password()
  139. Marks the user as having no password set. This isn't the same as
  140. having a blank string for a password.
  141. :meth:`~django.contrib.auth.models.User.check_password()` for this user
  142. will never return ``True``. Doesn't save the
  143. :class:`~django.contrib.auth.models.User` object.
  144. You may need this if authentication for your application takes place
  145. against an existing external source such as an LDAP directory.
  146. .. method:: has_usable_password()
  147. Returns ``False`` if
  148. :meth:`~django.contrib.auth.models.User.set_unusable_password()` has
  149. been called for this user.
  150. .. versionchanged:: 2.1
  151. In older versions, this also returns ``False`` if the password is
  152. ``None`` or an empty string, or if the password uses a hasher
  153. that's not in the :setting:`PASSWORD_HASHERS` setting. That
  154. behavior is considered a bug as it prevents users with such
  155. passwords from requesting a password reset.
  156. .. method:: get_group_permissions(obj=None)
  157. Returns a set of permission strings that the user has, through their
  158. groups.
  159. If ``obj`` is passed in, only returns the group permissions for
  160. this specific object.
  161. .. method:: get_all_permissions(obj=None)
  162. Returns a set of permission strings that the user has, both through
  163. group and user permissions.
  164. If ``obj`` is passed in, only returns the permissions for this
  165. specific object.
  166. .. method:: has_perm(perm, obj=None)
  167. Returns ``True`` if the user has the specified permission, where perm
  168. is in the format ``"<app label>.<permission codename>"``. (see
  169. documentation on :ref:`permissions <topic-authorization>`). If the user is
  170. inactive, this method will always return ``False``.
  171. If ``obj`` is passed in, this method won't check for a permission for
  172. the model, but for this specific object.
  173. .. method:: has_perms(perm_list, obj=None)
  174. Returns ``True`` if the user has each of the specified permissions,
  175. where each perm is in the format
  176. ``"<app label>.<permission codename>"``. If the user is inactive,
  177. this method will always return ``False``.
  178. If ``obj`` is passed in, this method won't check for permissions for
  179. the model, but for the specific object.
  180. .. method:: has_module_perms(package_name)
  181. Returns ``True`` if the user has any permissions in the given package
  182. (the Django app label). If the user is inactive, this method will
  183. always return ``False``.
  184. .. method:: email_user(subject, message, from_email=None, **kwargs)
  185. Sends an email to the user. If ``from_email`` is ``None``, Django uses
  186. the :setting:`DEFAULT_FROM_EMAIL`. Any ``**kwargs`` are passed to the
  187. underlying :meth:`~django.core.mail.send_mail()` call.
  188. Manager methods
  189. ---------------
  190. .. class:: models.UserManager
  191. The :class:`~django.contrib.auth.models.User` model has a custom manager
  192. that has the following helper methods (in addition to the methods provided
  193. by :class:`~django.contrib.auth.models.BaseUserManager`):
  194. .. method:: create_user(username, email=None, password=None, **extra_fields)
  195. Creates, saves and returns a :class:`~django.contrib.auth.models.User`.
  196. The :attr:`~django.contrib.auth.models.User.username` and
  197. :attr:`~django.contrib.auth.models.User.password` are set as given. The
  198. domain portion of :attr:`~django.contrib.auth.models.User.email` is
  199. automatically converted to lowercase, and the returned
  200. :class:`~django.contrib.auth.models.User` object will have
  201. :attr:`~django.contrib.auth.models.User.is_active` set to ``True``.
  202. If no password is provided,
  203. :meth:`~django.contrib.auth.models.User.set_unusable_password()` will
  204. be called.
  205. The ``extra_fields`` keyword arguments are passed through to the
  206. :class:`~django.contrib.auth.models.User`’s ``__init__`` method to
  207. allow setting arbitrary fields on a :ref:`custom user model
  208. <auth-custom-user>`.
  209. See :ref:`Creating users <topics-auth-creating-users>` for example usage.
  210. .. method:: create_superuser(username, email, password, **extra_fields)
  211. Same as :meth:`create_user`, but sets :attr:`~models.User.is_staff` and
  212. :attr:`~models.User.is_superuser` to ``True``.
  213. ``AnonymousUser`` object
  214. ========================
  215. .. class:: models.AnonymousUser
  216. :class:`django.contrib.auth.models.AnonymousUser` is a class that
  217. implements the :class:`django.contrib.auth.models.User` interface, with
  218. these differences:
  219. * :ref:`id <automatic-primary-key-fields>` is always ``None``.
  220. * :attr:`~django.contrib.auth.models.User.username` is always the empty
  221. string.
  222. * :meth:`~django.contrib.auth.models.User.get_username()` always returns
  223. the empty string.
  224. * :attr:`~django.contrib.auth.models.User.is_anonymous` is ``True``
  225. instead of ``False``.
  226. * :attr:`~django.contrib.auth.models.User.is_authenticated` is
  227. ``False`` instead of ``True``.
  228. * :attr:`~django.contrib.auth.models.User.is_staff` and
  229. :attr:`~django.contrib.auth.models.User.is_superuser` are always
  230. ``False``.
  231. * :attr:`~django.contrib.auth.models.User.is_active` is always ``False``.
  232. * :attr:`~django.contrib.auth.models.User.groups` and
  233. :attr:`~django.contrib.auth.models.User.user_permissions` are always
  234. empty.
  235. * :meth:`~django.contrib.auth.models.User.set_password()`,
  236. :meth:`~django.contrib.auth.models.User.check_password()`,
  237. :meth:`~django.db.models.Model.save` and
  238. :meth:`~django.db.models.Model.delete()` raise :exc:`NotImplementedError`.
  239. In practice, you probably won't need to use
  240. :class:`~django.contrib.auth.models.AnonymousUser` objects on your own, but
  241. they're used by Web requests, as explained in the next section.
  242. ``Permission`` model
  243. ====================
  244. .. class:: models.Permission
  245. Fields
  246. ------
  247. :class:`~django.contrib.auth.models.Permission` objects have the following
  248. fields:
  249. .. class:: models.Permission
  250. .. attribute:: name
  251. Required. 255 characters or fewer. Example: ``'Can vote'``.
  252. .. attribute:: content_type
  253. Required. A reference to the ``django_content_type`` database table,
  254. which contains a record for each installed model.
  255. .. attribute:: codename
  256. Required. 100 characters or fewer. Example: ``'can_vote'``.
  257. Methods
  258. -------
  259. :class:`~django.contrib.auth.models.Permission` objects have the standard
  260. data-access methods like any other :doc:`Django model </ref/models/instances>`.
  261. ``Group`` model
  262. ===============
  263. .. class:: models.Group
  264. Fields
  265. ------
  266. :class:`~django.contrib.auth.models.Group` objects have the following fields:
  267. .. class:: models.Group
  268. .. attribute:: name
  269. Required. 80 characters or fewer. Any characters are permitted. Example:
  270. ``'Awesome Users'``.
  271. .. attribute:: permissions
  272. Many-to-many field to :class:`~django.contrib.auth.models.Permission`::
  273. group.permissions.set([permission_list])
  274. group.permissions.add(permission, permission, ...)
  275. group.permissions.remove(permission, permission, ...)
  276. group.permissions.clear()
  277. Validators
  278. ==========
  279. .. class:: validators.ASCIIUsernameValidator
  280. A field validator allowing only ASCII letters and numbers, in addition to
  281. ``@``, ``.``, ``+``, ``-``, and ``_``.
  282. .. class:: validators.UnicodeUsernameValidator
  283. A field validator allowing Unicode characters, in addition to ``@``, ``.``,
  284. ``+``, ``-``, and ``_``. The default validator for ``User.username``.
  285. .. _topics-auth-signals:
  286. Login and logout signals
  287. ========================
  288. .. module:: django.contrib.auth.signals
  289. The auth framework uses the following :doc:`signals </topics/signals>` that
  290. can be used for notification when a user logs in or out.
  291. .. function:: user_logged_in
  292. Sent when a user logs in successfully.
  293. Arguments sent with this signal:
  294. ``sender``
  295. The class of the user that just logged in.
  296. ``request``
  297. The current :class:`~django.http.HttpRequest` instance.
  298. ``user``
  299. The user instance that just logged in.
  300. .. function:: user_logged_out
  301. Sent when the logout method is called.
  302. ``sender``
  303. As above: the class of the user that just logged out or ``None``
  304. if the user was not authenticated.
  305. ``request``
  306. The current :class:`~django.http.HttpRequest` instance.
  307. ``user``
  308. The user instance that just logged out or ``None`` if the
  309. user was not authenticated.
  310. .. function:: user_login_failed
  311. Sent when the user failed to login successfully
  312. ``sender``
  313. The name of the module used for authentication.
  314. ``credentials``
  315. A dictionary of keyword arguments containing the user credentials that were
  316. passed to :func:`~django.contrib.auth.authenticate()` or your own custom
  317. authentication backend. Credentials matching a set of 'sensitive' patterns,
  318. (including password) will not be sent in the clear as part of the signal.
  319. ``request``
  320. The :class:`~django.http.HttpRequest` object, if one was provided to
  321. :func:`~django.contrib.auth.authenticate`.
  322. .. _authentication-backends-reference:
  323. Authentication backends
  324. =======================
  325. .. module:: django.contrib.auth.backends
  326. :synopsis: Django's built-in authentication backend classes.
  327. This section details the authentication backends that come with Django. For
  328. information on how to use them and how to write your own authentication
  329. backends, see the :ref:`Other authentication sources section
  330. <authentication-backends>` of the :doc:`User authentication guide
  331. </topics/auth/index>`.
  332. Available authentication backends
  333. ---------------------------------
  334. The following backends are available in :mod:`django.contrib.auth.backends`:
  335. .. class:: ModelBackend
  336. This is the default authentication backend used by Django. It
  337. authenticates using credentials consisting of a user identifier and
  338. password. For Django's default user model, the user identifier is the
  339. username, for custom user models it is the field specified by
  340. USERNAME_FIELD (see :doc:`Customizing Users and authentication
  341. </topics/auth/customizing>`).
  342. It also handles the default permissions model as defined for
  343. :class:`~django.contrib.auth.models.User` and
  344. :class:`~django.contrib.auth.models.PermissionsMixin`.
  345. :meth:`has_perm`, :meth:`get_all_permissions`, :meth:`get_user_permissions`,
  346. and :meth:`get_group_permissions` allow an object to be passed as a
  347. parameter for object-specific permissions, but this backend does not
  348. implement them other than returning an empty set of permissions if
  349. ``obj is not None``.
  350. .. method:: authenticate(request, username=None, password=None, **kwargs)
  351. Tries to authenticate ``username`` with ``password`` by calling
  352. :meth:`User.check_password
  353. <django.contrib.auth.models.User.check_password>`. If no ``username``
  354. is provided, it tries to fetch a username from ``kwargs`` using the
  355. key :attr:`CustomUser.USERNAME_FIELD
  356. <django.contrib.auth.models.CustomUser.USERNAME_FIELD>`. Returns an
  357. authenticated user or ``None``.
  358. ``request`` is an :class:`~django.http.HttpRequest` and may be ``None``
  359. if it wasn't provided to :func:`~django.contrib.auth.authenticate`
  360. (which passes it on to the backend).
  361. .. method:: get_user_permissions(user_obj, obj=None)
  362. Returns the set of permission strings the ``user_obj`` has from their
  363. own user permissions. Returns an empty set if
  364. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  365. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  366. .. method:: get_group_permissions(user_obj, obj=None)
  367. Returns the set of permission strings the ``user_obj`` has from the
  368. permissions of the groups they belong. Returns an empty set if
  369. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  370. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  371. .. method:: get_all_permissions(user_obj, obj=None)
  372. Returns the set of permission strings the ``user_obj`` has, including both
  373. user permissions and group permissions. Returns an empty set if
  374. :attr:`~django.contrib.auth.models.AbstractBaseUser.is_anonymous` or
  375. :attr:`~django.contrib.auth.models.CustomUser.is_active` is ``False``.
  376. .. method:: has_perm(user_obj, perm, obj=None)
  377. Uses :meth:`get_all_permissions` to check if ``user_obj`` has the
  378. permission string ``perm``. Returns ``False`` if the user is not
  379. :attr:`~django.contrib.auth.models.CustomUser.is_active`.
  380. .. method:: has_module_perms(user_obj, app_label)
  381. Returns whether the ``user_obj`` has any permissions on the app
  382. ``app_label``.
  383. .. method:: ModelBackend.user_can_authenticate()
  384. Returns whether the user is allowed to authenticate. To match the
  385. behavior of :class:`~django.contrib.auth.forms.AuthenticationForm`
  386. which :meth:`prohibits inactive users from logging in
  387. <django.contrib.auth.forms.AuthenticationForm.confirm_login_allowed>`,
  388. this method returns ``False`` for users with :attr:`is_active=False
  389. <django.contrib.auth.models.User.is_active>`. Custom user models that
  390. don't have an :attr:`~django.contrib.auth.models.CustomUser.is_active`
  391. field are allowed.
  392. .. class:: AllowAllUsersModelBackend
  393. Same as :class:`ModelBackend` except that it doesn't reject inactive users
  394. because :meth:`~ModelBackend.user_can_authenticate` always returns ``True``.
  395. When using this backend, you'll likely want to customize the
  396. :class:`~django.contrib.auth.forms.AuthenticationForm` used by the
  397. :class:`~django.contrib.auth.views.LoginView` by overriding the
  398. :meth:`~django.contrib.auth.forms.AuthenticationForm.confirm_login_allowed`
  399. method as it rejects inactive users.
  400. .. class:: RemoteUserBackend
  401. Use this backend to take advantage of external-to-Django-handled
  402. authentication. It authenticates using usernames passed in
  403. :attr:`request.META['REMOTE_USER'] <django.http.HttpRequest.META>`. See
  404. the :doc:`Authenticating against REMOTE_USER </howto/auth-remote-user>`
  405. documentation.
  406. If you need more control, you can create your own authentication backend
  407. that inherits from this class and override these attributes or methods:
  408. .. attribute:: RemoteUserBackend.create_unknown_user
  409. ``True`` or ``False``. Determines whether or not a user object is created
  410. if not already in the database Defaults to ``True``.
  411. .. method:: RemoteUserBackend.authenticate(request, remote_user)
  412. The username passed as ``remote_user`` is considered trusted. This method
  413. simply returns the user object with the given username, creating a new
  414. user object if :attr:`~RemoteUserBackend.create_unknown_user` is ``True``.
  415. Returns ``None`` if :attr:`~RemoteUserBackend.create_unknown_user` is
  416. ``False`` and a ``User`` object with the given username is not found in the
  417. database.
  418. ``request`` is an :class:`~django.http.HttpRequest` and may be ``None`` if
  419. it wasn't provided to :func:`~django.contrib.auth.authenticate` (which
  420. passes it on to the backend).
  421. .. method:: RemoteUserBackend.clean_username(username)
  422. Performs any cleaning on the ``username`` (e.g. stripping LDAP DN
  423. information) prior to using it to get or create a user object. Returns the
  424. cleaned username.
  425. .. method:: RemoteUserBackend.configure_user(user)
  426. Configures a newly created user. This method is called immediately after a
  427. new user is created, and can be used to perform custom setup actions, such
  428. as setting the user's groups based on attributes in an LDAP directory.
  429. Returns the user object.
  430. .. method:: RemoteUserBackend.user_can_authenticate()
  431. Returns whether the user is allowed to authenticate. This method returns
  432. ``False`` for users with :attr:`is_active=False
  433. <django.contrib.auth.models.User.is_active>`. Custom user models that don't
  434. have an :attr:`~django.contrib.auth.models.CustomUser.is_active` field are
  435. allowed.
  436. .. class:: AllowAllUsersRemoteUserBackend
  437. Same as :class:`RemoteUserBackend` except that it doesn't reject inactive
  438. users because :attr:`~RemoteUserBackend.user_can_authenticate` always
  439. returns ``True``.
  440. Utility functions
  441. =================
  442. .. currentmodule:: django.contrib.auth
  443. .. function:: get_user(request)
  444. Returns the user model instance associated with the given ``request``’s
  445. session.
  446. It checks if the authentication backend stored in the session is present in
  447. :setting:`AUTHENTICATION_BACKENDS`. If so, it uses the backend's
  448. ``get_user()`` method to retrieve the user model instance and then verifies
  449. the session by calling the user model's
  450. :meth:`~django.contrib.auth.models.AbstractBaseUser.get_session_auth_hash`
  451. method.
  452. Returns an instance of :class:`~django.contrib.auth.models.AnonymousUser`
  453. if the authentication backend stored in the session is no longer in
  454. :setting:`AUTHENTICATION_BACKENDS`, if a user isn't returned by the
  455. backend's ``get_user()`` method, or if the session auth hash doesn't
  456. validate.