Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import inspect
  2. import re
  3. from django.apps import apps as django_apps
  4. from django.conf import settings
  5. from django.core.exceptions import ImproperlyConfigured, PermissionDenied
  6. from django.middleware.csrf import rotate_token
  7. from django.utils.crypto import constant_time_compare
  8. from django.utils.module_loading import import_string
  9. from django.utils.translation import LANGUAGE_SESSION_KEY
  10. from .signals import user_logged_in, user_logged_out, user_login_failed
  11. SESSION_KEY = '_auth_user_id'
  12. BACKEND_SESSION_KEY = '_auth_user_backend'
  13. HASH_SESSION_KEY = '_auth_user_hash'
  14. REDIRECT_FIELD_NAME = 'next'
  15. def load_backend(path):
  16. return import_string(path)()
  17. def _get_backends(return_tuples=False):
  18. backends = []
  19. for backend_path in settings.AUTHENTICATION_BACKENDS:
  20. backend = load_backend(backend_path)
  21. backends.append((backend, backend_path) if return_tuples else backend)
  22. if not backends:
  23. raise ImproperlyConfigured(
  24. 'No authentication backends have been defined. Does '
  25. 'AUTHENTICATION_BACKENDS contain anything?'
  26. )
  27. return backends
  28. def get_backends():
  29. return _get_backends(return_tuples=False)
  30. def _clean_credentials(credentials):
  31. """
  32. Clean a dictionary of credentials of potentially sensitive info before
  33. sending to less secure functions.
  34. Not comprehensive - intended for user_login_failed signal
  35. """
  36. SENSITIVE_CREDENTIALS = re.compile('api|token|key|secret|password|signature', re.I)
  37. CLEANSED_SUBSTITUTE = '********************'
  38. for key in credentials:
  39. if SENSITIVE_CREDENTIALS.search(key):
  40. credentials[key] = CLEANSED_SUBSTITUTE
  41. return credentials
  42. def _get_user_session_key(request):
  43. # This value in the session is always serialized to a string, so we need
  44. # to convert it back to Python whenever we access it.
  45. return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
  46. def authenticate(request=None, **credentials):
  47. """
  48. If the given credentials are valid, return a User object.
  49. """
  50. for backend, backend_path in _get_backends(return_tuples=True):
  51. try:
  52. inspect.getcallargs(backend.authenticate, request, **credentials)
  53. except TypeError:
  54. # This backend doesn't accept these credentials as arguments. Try the next one.
  55. continue
  56. try:
  57. user = backend.authenticate(request, **credentials)
  58. except PermissionDenied:
  59. # This backend says to stop in our tracks - this user should not be allowed in at all.
  60. break
  61. if user is None:
  62. continue
  63. # Annotate the user object with the path of the backend.
  64. user.backend = backend_path
  65. return user
  66. # The credentials supplied are invalid to all backends, fire signal
  67. user_login_failed.send(sender=__name__, credentials=_clean_credentials(credentials), request=request)
  68. def login(request, user, backend=None):
  69. """
  70. Persist a user id and a backend in the request. This way a user doesn't
  71. have to reauthenticate on every request. Note that data set during
  72. the anonymous session is retained when the user logs in.
  73. """
  74. session_auth_hash = ''
  75. if user is None:
  76. user = request.user
  77. if hasattr(user, 'get_session_auth_hash'):
  78. session_auth_hash = user.get_session_auth_hash()
  79. if SESSION_KEY in request.session:
  80. if _get_user_session_key(request) != user.pk or (
  81. session_auth_hash and
  82. not constant_time_compare(request.session.get(HASH_SESSION_KEY, ''), session_auth_hash)):
  83. # To avoid reusing another user's session, create a new, empty
  84. # session if the existing session corresponds to a different
  85. # authenticated user.
  86. request.session.flush()
  87. else:
  88. request.session.cycle_key()
  89. try:
  90. backend = backend or user.backend
  91. except AttributeError:
  92. backends = _get_backends(return_tuples=True)
  93. if len(backends) == 1:
  94. _, backend = backends[0]
  95. else:
  96. raise ValueError(
  97. 'You have multiple authentication backends configured and '
  98. 'therefore must provide the `backend` argument or set the '
  99. '`backend` attribute on the user.'
  100. )
  101. else:
  102. if not isinstance(backend, str):
  103. raise TypeError('backend must be a dotted import path string (got %r).' % backend)
  104. request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
  105. request.session[BACKEND_SESSION_KEY] = backend
  106. request.session[HASH_SESSION_KEY] = session_auth_hash
  107. if hasattr(request, 'user'):
  108. request.user = user
  109. rotate_token(request)
  110. user_logged_in.send(sender=user.__class__, request=request, user=user)
  111. def logout(request):
  112. """
  113. Remove the authenticated user's ID from the request and flush their session
  114. data.
  115. """
  116. # Dispatch the signal before the user is logged out so the receivers have a
  117. # chance to find out *who* logged out.
  118. user = getattr(request, 'user', None)
  119. if not getattr(user, 'is_authenticated', True):
  120. user = None
  121. user_logged_out.send(sender=user.__class__, request=request, user=user)
  122. # remember language choice saved to session
  123. language = request.session.get(LANGUAGE_SESSION_KEY)
  124. request.session.flush()
  125. if language is not None:
  126. request.session[LANGUAGE_SESSION_KEY] = language
  127. if hasattr(request, 'user'):
  128. from django.contrib.auth.models import AnonymousUser
  129. request.user = AnonymousUser()
  130. def get_user_model():
  131. """
  132. Return the User model that is active in this project.
  133. """
  134. try:
  135. return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  136. except ValueError:
  137. raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
  138. except LookupError:
  139. raise ImproperlyConfigured(
  140. "AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
  141. )
  142. def get_user(request):
  143. """
  144. Return the user model instance associated with the given request session.
  145. If no user is retrieved, return an instance of `AnonymousUser`.
  146. """
  147. from .models import AnonymousUser
  148. user = None
  149. try:
  150. user_id = _get_user_session_key(request)
  151. backend_path = request.session[BACKEND_SESSION_KEY]
  152. except KeyError:
  153. pass
  154. else:
  155. if backend_path in settings.AUTHENTICATION_BACKENDS:
  156. backend = load_backend(backend_path)
  157. user = backend.get_user(user_id)
  158. # Verify the session
  159. if hasattr(user, 'get_session_auth_hash'):
  160. session_hash = request.session.get(HASH_SESSION_KEY)
  161. session_hash_verified = session_hash and constant_time_compare(
  162. session_hash,
  163. user.get_session_auth_hash()
  164. )
  165. if not session_hash_verified:
  166. request.session.flush()
  167. user = None
  168. return user or AnonymousUser()
  169. def get_permission_codename(action, opts):
  170. """
  171. Return the codename of the permission for the specified action.
  172. """
  173. return '%s_%s' % (action, opts.model_name)
  174. def update_session_auth_hash(request, user):
  175. """
  176. Updating a user's password logs out all sessions for the user.
  177. Take the current request and the updated user object from which the new
  178. session hash will be derived and update the session hash appropriately to
  179. prevent a password change from logging out the session from which the
  180. password was changed.
  181. """
  182. request.session.cycle_key()
  183. if hasattr(user, 'get_session_auth_hash') and request.user == user:
  184. request.session[HASH_SESSION_KEY] = user.get_session_auth_hash()
  185. default_app_config = 'django.contrib.auth.apps.AuthConfig'