ó
O'—^c           @  s  d  Z  d d l m Z d d l Z d d l 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 m Z d	 „  Z d
 e	 f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   u+   
Provides various authentication policies.
iÿÿÿÿ(   t   unicode_literalsN(   t   authenticatet   get_user_model(   t   CsrfViewMiddleware(   t	   text_type(   t   ugettext_lazy(   t   HTTP_HEADER_ENCODINGt
   exceptionsc         C  s:   |  j  j d d ƒ } t | t ƒ r6 | j t ƒ } n  | S(   u‰   
    Return request's 'Authorization:' header, as a bytestring.

    Hide some test client ickyness where the header can be unicode.
    u   HTTP_AUTHORIZATIONt    (   t   METAt   gett
   isinstanceR   t   encodeR   (   t   requestt   auth(    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyt   get_authorization_header   s    t	   CSRFCheckc           B  s   e  Z d  „  Z RS(   c         C  s   | S(   N(    (   t   selfR   t   reason(    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyt   _reject   s    (   t   __name__t
   __module__R   (    (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR      s   t   BaseAuthenticationc           B  s    e  Z d  Z d „  Z d „  Z RS(   uF   
    All authentication classes should extend BaseAuthentication.
    c         C  s   t  d ƒ ‚ d S(   uS   
        Authenticate the request and return a two-tuple of (user, token).
        u#   .authenticate() must be overridden.N(   t   NotImplementedError(   R   R   (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR   )   s    c         C  s   d S(   uç   
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        N(    (   R   R   (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyt   authenticate_header/   s    (   R   R   t   __doc__R   R   (    (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR   $   s   	t   BasicAuthenticationc           B  s/   e  Z d  Z d Z d „  Z d „  Z d „  Z RS(   u>   
    HTTP Basic authentication against username/password.
    u   apic         C  s  t  | ƒ j ƒ  } | s/ | d j ƒ  d k r3 d	 St | ƒ d k rc t d ƒ } t j | ƒ ‚ n0 t | ƒ d k r“ t d ƒ } t j | ƒ ‚ n  y) t j	 | d ƒ j
 t ƒ j d ƒ } Wn8 t t t j f k
 rö t d ƒ } t j | ƒ ‚ n X| d | d } } |  j | | ƒ S(
   uœ   
        Returns a `User` if a correct username and password have been supplied
        using HTTP Basic authentication.  Otherwise returns `None`.
        i    t   basici   u.   Invalid basic header. No credentials provided.i   uC   Invalid basic header. Credentials string should not contain spaces.u   :u?   Invalid basic header. Credentials not correctly base64 encoded.N(   R   t   splitt   lowert   Nonet   lent   _R   t   AuthenticationFailedt   base64t	   b64decodet   decodeR   t	   partitiont	   TypeErrort   UnicodeDecodeErrort   binasciit   Errort   authenticate_credentials(   R   R   R   t   msgt
   auth_partst   useridt   password(    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR   >   s     )c         C  su   i | t  ƒ  j 6| d 6} t |   } | d k rJ t j t d ƒ ƒ ‚ n  | j sk t j t d ƒ ƒ ‚ n  | d f S(   uU   
        Authenticate the userid and password against username and password.
        u   passwordu   Invalid username/password.u   User inactive or deleted.N(   R   t   USERNAME_FIELDR   R   R   R!   R    t	   is_active(   R   R-   R.   t   credentialst   user(    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR*   X   s    
	c         C  s   d |  j  S(   Nu   Basic realm="%s"(   t   www_authenticate_realm(   R   R   (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR   j   s    (   R   R   R   R3   R   R*   R   (    (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR   8   s
   		t   SessionAuthenticationc           B  s    e  Z d  Z d „  Z d „  Z RS(   u<   
    Use Django's session framework for authentication.
    c         C  sA   t  | j d d ƒ } | s& | j r* d S|  j | ƒ | d f S(   u{   
        Returns a `User` if the request session currently has a logged in user.
        Otherwise returns `None`.
        u   userN(   t   getattrt   _requestR   R0   t   enforce_csrf(   R   R   R2   (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR   s   s
    c         C  s;   t  ƒ  j | d d i  ƒ } | r7 t j d | ƒ ‚ n  d S(   uK   
        Enforce CSRF validation for session based authentication.
        u   CSRF Failed: %sN(    (   R   t   process_viewR   R   t   PermissionDenied(   R   R   R   (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR7   …   s    (   R   R   R   R   R7   (    (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR4   n   s   	t   TokenAuthenticationc           B  s>   e  Z d  Z d Z d Z d „  Z d „  Z d „  Z d „  Z	 RS(   u  
    Simple token based authentication.

    Clients should authenticate by passing the token key in the "Authorization"
    HTTP header, prepended with the string "Token ".  For example:

        Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
    u   Tokenc         C  s*   |  j  d  k	 r |  j  Sd d l m } | S(   Niÿÿÿÿ(   t   Token(   t   modelR   t   rest_framework.authtoken.modelsR;   (   R   R;   (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyt	   get_modelœ   s    c         C  sò   t  | ƒ j ƒ  } | s> | d j ƒ  |  j j ƒ  j ƒ  k rB d  St | ƒ d k rr t d ƒ } t j	 | ƒ ‚ n0 t | ƒ d k r¢ t d ƒ } t j	 | ƒ ‚ n  y | d j
 ƒ  } Wn, t k
 rä t d ƒ } t j	 | ƒ ‚ n X|  j | ƒ S(   Ni    i   u.   Invalid token header. No credentials provided.i   u=   Invalid token header. Token string should not contain spaces.uI   Invalid token header. Token string should not contain invalid characters.(   R   R   R   t   keywordR   R   R   R    R   R!   R$   t   UnicodeErrorR*   (   R   R   R   R+   t   token(    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR   ©   s    ,c         C  s‹   |  j  ƒ  } y" | j j d ƒ j d | ƒ } Wn) | j k
 rY t j t d ƒ ƒ ‚ n X| j j	 s~ t j t d ƒ ƒ ‚ n  | j | f S(   Nu   usert   keyu   Invalid token.u   User inactive or deleted.(
   R>   t   objectst   select_relatedR
   t   DoesNotExistR   R!   R    R2   R0   (   R   RB   R<   RA   (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR*   ¾   s    "c         C  s   |  j  S(   N(   R?   (   R   R   (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR   Ê   s    N(
   R   R   R   R?   R   R<   R>   R   R*   R   (    (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyR:      s   			(   R   t
   __future__R    R"   R(   t   django.contrib.authR   R   t   django.middleware.csrfR   t   django.utils.sixR   t   django.utils.translationR   R    t   rest_frameworkR   R   R   R   t   objectR   R   R4   R:   (    (    (    s?   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/authentication.pyt   <module>   s   	6!