
 Xi                 @   sb  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 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   J/home/ubuntu/projects/ifolica/build/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
 rs PYn 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 t |  j j t	 d  |  r |  j j
   q n |  j j   y | p | j } WnR t k
 rt d d  } t |  d k r | d \ } } n t d   Yn X| j j j |  |  j t <| |  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   Tr
   r   zYou have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.r/   r)   r'   )r/   hasattrr1   r&   r%   r(   r$   r   getHASH_SESSION_KEYflushZ	cycle_keyr   AttributeErrorr   len
ValueErrorr#   Zvalue_to_stringBACKEND_SESSION_KEYr   r   r-   	__class__)r'   r/   r   Zsession_auth_hashr   _r   r   r   loginX   s4    
r<   c             C   s   t  |  d d  } t | d  r4 | j r4 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   r=   r   r-   r:   r%   r3   r	   r5   Zdjango.contrib.auth.modelsr>   r/   )r'   r/   languager>   r   r   r   logout   s    	rA   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_MODELr8   r   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	 |  } 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>   Nr1   )modelsr>   r(   r%   r9   KeyErrorr   r   r   get_userr2   r3   r4   r   r1   r5   )r'   r>   r/   Zuser_idr   r   Zsession_hashZsession_hash_verifiedr   r   r   rF      s&    	rF   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    rI   c             C   s8   t  | d  r4 |  j | k r4 | j   |  j t <n  d S)aW  
    Updating a user's password logs out all sessions for the user.

    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    	rJ   z#django.contrib.auth.apps.AuthConfig)'r*   r   Zdjango.appsr   rB   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&   r9   r4   ZREDIRECT_FIELD_NAMEr   r   r   r!   r(   r+   r<   rA   r"   rF   rI   rJ   Zdefault_app_configr   r   r   r   <module>   s4   -