ó
O'^c           @  sŲ   d  d l  m Z d  d l Z d  d l m Z d  d l m Z d d l m Z d d l	 m
 Z
 e j d  Z d	 d
 d g Z d e f d     YZ d e f d     YZ d e f d     YZ d e e f d     YZ d S(   iĸĸĸĸ(   t   unicode_literalsN(   t   ImproperlyConfigured(   t   HttpResponseForbiddeni   (   t   FatalClientError(   t   oauth2_settingsu   oauth2_provideru   GETu   HEADu   OPTIONSt   OAuthLibMixinc           B  sŠ   e  Z d  Z d Z d Z d Z e d    Z e d    Z	 e d    Z
 e d    Z e d    Z d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z RS(   u  
    This mixin decouples Django OAuth Toolkit from OAuthLib.

    Users can configure the Server, Validator and OAuthlibCore
    classes used by this mixin by setting the following class
    variables:

      * server_class
      * validator_class
      * oauthlib_backend_class

    c         C  s)   |  j  d k r t d   n |  j  Sd S(   u9   
        Return the OAuthlib server class to use
        ui   OAuthLibMixin requires either a definition of 'server_class' or an implementation of 'get_server_class()'N(   t   server_classt   NoneR   (   t   cls(    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyt   get_server_class"   s    c         C  s)   |  j  d k r t d   n |  j  Sd S(   uI   
        Return the RequestValidator implementation class to use
        uo   OAuthLibMixin requires either a definition of 'validator_class' or an implementation of 'get_validator_class()'N(   t   validator_classR   R   (   R   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyt   get_validator_class.   s    c         C  s)   |  j  d k r t d   n |  j  Sd S(   uE   
        Return the OAuthLibCore implementation class to use
        u}   OAuthLibMixin requires either a definition of 'oauthlib_backend_class' or an implementation of 'get_oauthlib_backend_class()'N(   t   oauthlib_backend_classR   R   (   R   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyt   get_oauthlib_backend_class:   s    c         C  s%   |  j    } |  j   } | |    S(   uj   
        Return an instance of `server_class` initialized with a `validator_class`
        object
        (   R	   R   (   R   R   R
   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyt
   get_serverF   s    c         C  s@   t  |  d  s9 |  j   } |  j   } | |  |  _ n  |  j S(   uf   
        Cache and return `OAuthlibCore` instance so it will be created only on first request
        u   _oauthlib_core(   t   hasattrR   R   t   _oauthlib_core(   R   t   servert
   core_class(    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyt   get_oauthlib_coreP   s
    c         C  s   |  j    } | j |  S(   uŽ   
        A wrapper method that calls validate_authorization_request on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        (   R   t   validate_authorization_request(   t   selft   requestt   core(    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR   [   s    c         C  s=   | r | j  d  n g  } |  j   } | j | | | |  S(   uā  
        A wrapper method that calls create_authorization_response on `server_class`
        instance.

        :param request: The current django.http.HttpRequest object
        :param scopes: A space-separated string of provided scopes
        :param credentials: Authorization credentials dictionary containing
                           `client_id`, `state`, `redirect_uri`, `response_type`
        :param allow: True if the user authorize the client, otherwise False
        u    (   t   splitR   t   create_authorization_response(   R   R   t   scopest   credentialst   allowR   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR   d   s    c         C  s   |  j    } | j |  S(   uĢ   
        A wrapper method that calls create_token_response on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        (   R   t   create_token_response(   R   R   R   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR   u   s    c         C  s   |  j    } | j |  S(   uī   
        A wrapper method that calls create_revocation_response on the
        `server_class` instance.

        :param request: The current django.http.HttpRequest object
        (   R   t   create_revocation_response(   R   R   R   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR   ~   s    c         C  s%   |  j    } | j | d |  j   S(   u   
        A wrapper method that calls verify_request on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        R   (   R   t   verify_requestt
   get_scopes(   R   R   R   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR      s    c         C  s   g  S(   u}   
        This should return the list of scopes required to access the resources. By default it returns an empty list
        (    (   R   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR       s    c         K  s   | j  } | j p d } d | k r* d n d } i | d 6d j | j | | j  d 6} | j |  t | t  r~ t } n t } | | f S(   u   
        Return an error to be displayed to the resource owner if anything goes awry.

        :param error: :attr:`OAuthToolkitError`
        u    u   ?u   &u   erroru	   {0}{1}{2}u   url(	   t   oauthlib_errort   redirect_urit   formatt
   urlencodedt   updatet
   isinstanceR   t   Falset   True(   R   t   errort   kwargsR!   R"   t	   separatort   error_responset   redirect(    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR,      s    		N(   t   __name__t
   __module__t   __doc__R   R   R
   R   t   classmethodR	   R   R   R   R   R   R   R   R   R   R    R,   (    (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR      s    
						
			t   ScopedResourceMixinc           B  s   e  Z d  Z d Z d   Z RS(   uB   
    Helper mixin that implements "scopes handling" behaviour
    c         O  s)   |  j  d k r t d   n |  j  Sd S(   u   
        Return the scopes needed to access the resource

        :param args: Support scopes injections from the outside (not yet implemented)
        uo   ProtectedResourceMixin requires either a definition of 'required_scopes' or an implementation of 'get_scopes()'N(   t   required_scopesR   R   (   R   t   argsR*   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR    ·   s    N(   R.   R/   R0   R   R3   R    (    (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR2   ą   s   t   ProtectedResourceMixinc           B  s   e  Z d  Z d   Z RS(   u{   
    Helper mixin that implements OAuth2 protection on request dispatch,
    specially useful for Django Generic Views
    c         O  s   | j  j   d k r1 t t |   j | | |  S|  j |  \ } } | rt | j | _ t t |   j | | |  St   Sd  S(   Nu   OPTIONS(	   t   methodt   uppert   superR5   t   dispatchR   t   usert   resource_ownerR   (   R   R   R4   R*   t   validt   r(    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR9   Ę   s    (   R.   R/   R0   R9   (    (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR5   Å   s   t   ReadWriteScopedResourceMixinc           B  s5   e  Z d  Z g  Z d Z d   Z d   Z d   Z RS(   uG   
    Helper mixin that implements "read and write scopes" behavior
    c         O  sj   t  j } t  j t  j g } t |  j t |   sN t d j |    n  t t	 |   j
 |  | |  S(   Nun   ReadWriteScopedResourceMixin requires following scopes {0} to be in OAUTH2_PROVIDER['SCOPES'] list in settings(   R   t   _SCOPESt
   READ_SCOPEt   WRITE_SCOPEt   sett   issubsetR   R#   R8   R>   t   __new__(   R   R4   R*   t   provided_scopest   read_write_scopes(    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyRD   ß   s    	c         O  sL   | j  j   t k r$ t j |  _ n t j |  _ t t |   j	 | | |  S(   N(
   R6   R7   t   SAFE_HTTP_METHODSR   R@   t   read_write_scopeRA   R8   R>   R9   (   R   R   R4   R*   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR9   ë   s    c         O  s)   t  t |   j | |   } | |  j g S(   N(   R8   R>   R    RH   (   R   R4   R*   R   (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR    ó   s    N(	   R.   R/   R0   R3   R   RH   RD   R9   R    (    (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyR>   Ø   s   		(   t
   __future__R    t   loggingt   django.core.exceptionsR   t   django.httpR   t
   exceptionsR   t   settingsR   t	   getLoggert   logRG   t   objectR   R2   R5   R>   (    (    (    s>   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/mixins.pyt   <module>   s    