ó
pÁìVc           @  sù   d  Z  d d l m 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 m Z d d l 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   ugettext_lazy(   t   HTTP_HEADER_ENCODINGt
   exceptions(   t   Tokenc         C  s@   |  j  j d d ƒ } t | t d ƒ ƒ r< | 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    u    (   t   METAt   gett
   isinstancet   typet   encodeR   (   t   requestt   auth(    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/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(    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyt   _reject   s    (   t   __name__t
   __module__R   (    (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/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   (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/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   (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyt   authenticate_header.   s    (   R   R   t   __doc__R   R   (    (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/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 ƒ } Wn2 t t 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   authenticate_credentials(   R   R   R   t   msgt
   auth_partst   useridt   password(    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/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(    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR)   W   s    
	c         C  s   d |  j  S(   Nu   Basic realm="%s"(   t   www_authenticate_realm(   R   R   (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR   i   s    (   R   R   R   R2   R   R)   R   (    (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR   7   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   R/   t   enforce_csrf(   R   R   R1   (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR   r   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   (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR6   „   s    (   R   R   R   R   R6   (    (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR3   m   s   	t   TokenAuthenticationc           B  s/   e  Z d  Z e 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
    c         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 | d j ƒ  } Wn, t	 k
 rÕ t d ƒ } t j | ƒ ‚ n X|  j
 | ƒ S(   Ni    t   tokeni   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   R   R    R!   R   R"   R%   t   UnicodeErrorR)   (   R   R   R   R*   R:   (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR       s    c         C  s…   y% |  j  j j d ƒ j d | ƒ } Wn, |  j  j k
 rS t j t d ƒ ƒ ‚ n X| j j	 sx t j t d ƒ ƒ ‚ n  | j | f S(   Nu   usert   keyu   Invalid token.u   User inactive or deleted.(
   t   modelt   objectst   select_relatedR
   t   DoesNotExistR   R"   R!   R1   R/   (   R   R<   R:   (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR)   µ   s    %c         C  s   d S(   Nu   Token(    (   R   R   (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR   À   s    (   R   R   R   R   R=   R   R)   R   (    (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyR9   Ž   s
   		(   R   t
   __future__R    R#   t   django.contrib.authR   R   t   django.middleware.csrfR   t   django.utils.translationR   R!   t   rest_frameworkR   R   t   rest_framework.authtoken.modelsR   R   R   t   objectR   R   R3   R9   (    (    (    sw   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/djangorestframework-3.3.2-py2.7.egg/rest_framework/authentication.pyt   <module>   s   	6!