

_VT                 @   s_  d  d l  Z  d  d l Z d  d l m Z d  d l m Z d  d l m Z m	 Z	 d  d l
 m Z d  d l m Z d  d l m Z d  d l m Z d	 d
 l m Z m Z m Z d Z d Z d Z d Z d d   Z d d d  Z d d   Z d d   Z d d   Z d d   Z d d   Z  d d   Z! d  d!   Z" d" d#   Z# d$ d%   Z$ d& d'   Z% d( Z& d S))    N)apps)settings)ImproperlyConfiguredPermissionDenied)rotate_token)constant_time_compare)import_string)LANGUAGE_SESSION_KEY   )user_logged_inuser_logged_outuser_login_failedZ_auth_user_idZ_auth_user_backendZ_auth_user_hashnextc             C   s   t  |     S)N)r   )path r   >/tmp/pip-build-ghmbqnp_/Django/django/contrib/auth/__init__.pyload_backend   s    r   Fc             C   s^   g  } x< t  j D]1 } t |  } | j |  r: | | f n |  q W| sZ t d   n  | S)Nz\No authentication backends have been defined. Does AUTHENTICATION_BACKENDS contain anything?)r   AUTHENTICATION_BACKENDSr   appendr   )return_tuplesbackendsbackend_pathbackendr   r   r   _get_backends   s    #r   c               C   s   t  d d  S)Nr   F)r   r   r   r   r   get_backends%   s    r   c             C   sL   t  j d t  j  } d } x* |  D]" } | j |  r" | |  | <q" q" W|  S)z
    Cleans a dictionary of credentials of potentially sensitive info before
    sending to less secure functions.

    Not comprehensive - intended for user_login_failed signal
    z'api|token|key|secret|password|signaturez********************)recompileIsearch)credentialsZSENSITIVE_CREDENTIALSZCLEANSED_SUBSTITUTEkeyr   r   r   _clean_credentials)   s    r!   c             C   s   t    j j j |  j t  S)N)get_user_model_metapkZ	to_pythonsessionSESSION_KEY)requestr   r   r   _get_user_session_key8   s    r(   c              K   s   x t  d d  D] \ } } y t j | j |   Wn t k
 rJ w Yn Xy | j |    } Wn t k
 rv d SYn X| d k r q n  | | _ | SWt j d t	 d t
 |    d S)zC
    If the given credentials are valid, return a User object.
    r   TNsenderr   )r   inspectZgetcallargsauthenticate	TypeErrorr   r   r   send__name__r!   )r   r   r   userr   r   r   r+   >   s    		r+   c             C   s  d } | d k r |  j  } n  t | d  r< | j   } n  t |  j k r t |   | j k s~ | r |  j j t  | k r |  j j	   q n |  j j
   | j j j |  |  j t <| j |  j t <| |  j t <t |  d  r | |  _  n  t |   t j d | j d |  d |  d S)z
    Persist a user id and a backend in the request. This way a user doesn't
    have to reauthenticate on every request. Note that data set during
    the anonymous session is retained when the user logs in.
     Nget_session_auth_hashr/   r)   r'   )r/   hasattrr1   r&   r%   r(   r$   getHASH_SESSION_KEYflushZ	cycle_keyr#   Zvalue_to_stringr   BACKEND_SESSION_KEYr   r   r-   	__class__)r'   r/   Zsession_auth_hashr   r   r   loginY   s$    
r8   c             C   s   t  |  d d  } t | d  r7 | j   r7 d } n  t j d | j d |  d |  |  j j t  } |  j j	   | d k	 r | |  j t <n  t |  d  r d d l
 m } |   |  _ n  d S)zb
    Removes the authenticated user's ID from the request and flushes their
    session data.
    r/   Nis_authenticatedr)   r'   r   )AnonymousUser)getattrr2   r9   r   r-   r7   r%   r3   r	   r5   Zdjango.contrib.auth.modelsr:   r/   )r'   r/   languager:   r   r   r   logoutx   s    	r=   c               C   s]   y t  j t j  SWnB t k
 r4 t d   Yn% t k
 rX t d t j   Yn Xd S)z@
    Returns the User model that is active in this project.
    z:AUTH_USER_MODEL must be of the form 'app_label.model_name'z@AUTH_USER_MODEL refers to model '%s' that has not been installedN)django_appsZ	get_modelr   ZAUTH_USER_MODEL
ValueErrorr   LookupErrorr   r   r   r   r"      s    r"   c             C   s   d d l  m } d } y t |   } |  j t } Wn t k
 rG Yn X| t j k r t |  } | j	 |  } d t j
 k r t | d  r |  j j t  } | o t | | j    } | s |  j j   d } q q n  | p |   S)z
    Returns the user model instance associated with the given request session.
    If no user is retrieved an instance of `AnonymousUser` is returned.
    r
   )r:   Nz>django.contrib.auth.middleware.SessionAuthenticationMiddlewarer1   )modelsr:   r(   r%   r6   KeyErrorr   r   r   get_userZMIDDLEWARE_CLASSESr2   r3   r4   r   r1   r5   )r'   r:   r/   Zuser_idr   r   Zsession_hashZsession_hash_verifiedr   r   r   rC      s(    	rC   c             C   s   d |  | j  f S)zJ
    Returns the codename of the permission for the specified action.
    z%s_%s)Z
model_name)actionoptsr   r   r   get_permission_codename   s    rF   c             C   s8   t  | d  r4 |  j | k r4 | j   |  j t <n  d S)a  
    Updating a user's password logs out all sessions for the user if
    django.contrib.auth.middleware.SessionAuthenticationMiddleware is enabled.

    This function takes the current request and the updated user object from
    which the new session hash will be derived and updates the session hash
    appropriately to prevent a password change from logging out the session
    from which the password was changed.
    r1   N)r2   r/   r1   r%   r4   )r'   r/   r   r   r   update_session_auth_hash   s    
rG   z#django.contrib.auth.apps.AuthConfig)'r*   r   Zdjango.appsr   r>   Zdjango.confr   Zdjango.core.exceptionsr   r   Zdjango.middleware.csrfr   Zdjango.utils.cryptor   Zdjango.utils.module_loadingr   Zdjango.utils.translationr	   Zsignalsr   r   r   r&   r6   r4   ZREDIRECT_FIELD_NAMEr   r   r   r!   r(   r+   r8   r=   r"   rC   rF   rG   Zdefault_app_configr   r   r   r   <module>   s4   