ó
O'—^c           @  s½   d  d l  m Z d  d l Z d  d l m Z d  d l m Z m Z m Z d d l	 m
 Z
 m Z d d l m Z d d l m Z m Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d S(   iÿÿÿÿ(   t   unicode_literalsN(   t   oauth2(   t	   urlencodet
   urlencodedt   quotei   (   t   OAuthToolkitErrort   FatalClientError(   t   oauth2_settings(   t   urlparset
   urlunparset   OAuthLibCorec           B  st   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z RS(   u   
    TODO: add docs
    c         C  s"   | p t  j t  j ƒ  ƒ |  _ d S(   uM   
        :params server: An instance of oauthlib.oauth2.Server class
        N(   R   t   OAUTH2_SERVER_CLASSt   OAUTH2_VALIDATOR_CLASSt   server(   t   selfR   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyt   __init__   s    c         C  s|   t  t | j ƒ  ƒ ƒ } t d „  | d Dƒ ƒ j t ƒ } x4 | D], } | d j | t | d d ƒƒ | d <qB Wt | ƒ S(   uŽ   
        Django considers "safe" some characters that aren't so for oauthlib. We have to search for
        them and properly escape.
        c         s  s   |  ] } | Vq d  S(   N(    (   t   .0t   c(    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pys	   <genexpr>   s    i   t   safet    (	   t   listR   t   get_full_patht   sett
   differenceR   t   replaceR   R	   (   R   t   requestt   parsedt   unsafeR   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyt   _get_escaped_full_path   s
    #*c         C  s   d S(   uP  
        Produce extra credentials for token response. This dictionary will be
        merged with the response.
        See also: `oauthlib.oauth2.rfc6749.TokenEndpoint.create_token_response`

        :param request: The current django.http.HttpRequest object
        :return: dictionary of extra credentials or None (default)
        N(   t   None(   R   R   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyt   _get_extra_credentials#   s    	c         C  sL   |  j  | ƒ } | j } |  j | ƒ } t |  j | ƒ ƒ } | | | | f S(   uÒ   
        Extract parameters from the Django request object. Such parameters will then be passed to
        OAuthLib to build its own Request object. The body should be encoded using OAuthLib urlencoded
        (   R   t   methodt   extract_headersR   t   extract_body(   R   R   t   urit   http_methodt   headerst   body(    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyt   _extract_params.   s
    	c         C  s\   | j  j ƒ  } d | k r% | d =n  d | k r; | d =n  d | k rX | d | d <n  | S(   u¿   
        Extracts headers from the Django request object
        :param request: The current django.http.HttpRequest object
        :return: a dictionary with OAuthLib needed headers
        u
   wsgi.inputu   wsgi.errorsu   HTTP_AUTHORIZATIONu   Authorization(   t   METAt   copy(   R   R   R$   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR    9   s    

c         C  s   | j  j ƒ  S(   u´   
        Extracts the POST body from the Django request object
        :param request: The current django.http.HttpRequest object
        :return: provided POST parameters
        (   t   POSTt   items(   R   R   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR!   I   s    c   	      C  s£   yS |  j  | ƒ \ } } } } |  j j | d | d | d | ƒ\ } } | | f SWnI t j k
 rz } t d | ƒ ‚ n% t j k
 rž } t d | ƒ ‚ n Xd S(   u¬   
        A wrapper method that calls validate_authorization_request on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        R#   R%   R$   t   errorN(   R&   R   t   validate_authorization_requestR   R   t   OAuth2ErrorR   (	   R   R   R"   R#   R%   R$   t   scopest   credentialsR+   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR,   Q   s    	!c   
      C  sÚ   yv | s t  j ƒ  ‚ n  | j | d <|  j j d | d d | d | ƒ \ } } } | j d d	 ƒ } | | | | f SWn] t  j k
 r§ }	 t d |	 d | d ƒ ‚ n/ t  j k
 rÕ }	 t	 d |	 d | d ƒ ‚ n Xd	 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 list 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   userR"   u   redirect_uriR.   R/   u   LocationR+   t   redirect_uriN(
   R   t   AccessDeniedErrort   userR   t   create_authorization_responset   getR   R   R-   R   (
   R   R   R.   R/   t   allowR$   R%   t   statusR"   R+   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR3   c   s    "c         C  ss   |  j  | ƒ \ } } } } |  j | ƒ } |  j j | | | | | ƒ \ } } } | j d d ƒ } | | | | f S(   u£   
        A wrapper method that calls create_token_response on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        u   LocationN(   R&   R   R   t   create_token_responseR4   R   (   R   R   R"   R#   R%   R$   t   extra_credentialsR6   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR7   €   s    c         C  sa   |  j  | ƒ \ } } } } |  j j | | | | ƒ \ } } } | j d d ƒ } | | | | f S(   u²   
        A wrapper method that calls create_revocation_response on a
        `server_class` instance.

        :param request: The current django.http.HttpRequest object
        u   LocationN(   R&   R   t   create_revocation_responseR4   R   (   R   R   R"   R#   R%   R$   R6   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR9      s
    	c   	      C  sL   |  j  | ƒ \ } } } } |  j j | | | | d | ƒ\ } } | | f S(   uó   
        A wrapper method that calls verify_request on `server_class` instance.

        :param request: The current django.http.HttpRequest object
        :param scopes: A list of scopes required to verify so that request is verified
        R.   (   R&   R   t   verify_request(	   R   R   R.   R"   R#   R%   R$   t   validt   r(    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR:   ž   s    'N(   t   __name__t
   __module__t   __doc__R   R   R   R   R&   R    R!   R,   R3   R7   R9   R:   (    (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR
      s   									t   JSONOAuthLibCorec           B  s   e  Z d  Z d „  Z RS(   ui   
    Extends the default OAuthLibCore to parse correctly requests with application/json Content-Type
    c         C  sC   y% t  j | j j d ƒ ƒ j ƒ  } Wn t k
 r> d } n X| S(   uÃ   
        Extracts the JSON body from the Django request object
        :param request: The current django.http.HttpRequest object
        :return: provided POST parameters "urlencodable"
        u   utf-8u    (   t   jsont   loadsR%   t   decodeR*   t
   ValueError(   R   R   R%   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR!   ¯   s
    %
(   R=   R>   R?   R!   (    (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyR@   «   s   c          C  s(   t  j ƒ  }  t  j |  ƒ } t  j | ƒ S(   uu   
    Utility function that take a request and returns an instance of
    `oauth2_provider.backends.OAuthLibCore`
    (   R   R   R   t   OAUTH2_BACKEND_CLASS(   t	   validatorR   (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyt   get_oauthlib_core½   s    (   t
   __future__R    RA   t   oauthlibR   t   oauthlib.commonR   R   R   t
   exceptionsR   R   t   settingsR   t   compatR   R	   t   objectR
   R@   RG   (    (    (    sA   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/oauth2_backends.pyt   <module>   s   ž