ó
O'—^c           @   sX  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 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 d d l m Z e  j d ƒ Z d e e e f d „  ƒ  YZ d e e f d „  ƒ  YZ d e e e f d „  ƒ  YZ d e e e f d „  ƒ  YZ  d S(   iÿÿÿÿN(   t   HttpResponse(   t   sensitive_post_parameters(   t   Viewt   FormView(   t   timezone(   t   method_decorator(   t   LoginRequiredMixint   CsrfExemptMixini   (   t   oauth2_settings(   t   OAuthToolkitError(   t	   AllowForm(   t   HttpResponseUriRedirect(   t   get_application_modeli   (   t   OAuthLibMixint   oauth2_providert   BaseAuthorizationViewc           B   s    e  Z d  Z d „  Z d „  Z RS(   s  
    Implements a generic endpoint to handle *Authorization Requests* as in :rfc:`4.1.1`. The view
    does not implement any strategy to determine *authorize/do not authorize* logic.
    The endpoint is used in the following flows:

    * Authorization code
    * Implicit grant

    c         O   s%   i  |  _  t t |  ƒ j | | | Ž S(   N(   t   oauth2_datat   superR   t   dispatch(   t   selft   requestt   argst   kwargs(    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyR      s    	c         K   sU   t  t |  ƒ j | |  \ } } | r5 t | d ƒ S| d j } |  j | d | ƒS(   s   
        Handle errors either by redirecting to redirect_uri with a json in the body containing
        error details or providing an error response
        t   urlt   errort   status(   R   R   t   error_responseR   t   status_codet   render_to_response(   R   R   R   t   redirectR   R   (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyR   #   s
    !(   t   __name__t
   __module__t   __doc__R   R   (    (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyR      s   		t   AuthorizationViewc           B   sV   e  Z d  Z d Z e Z e j Z e j	 Z
 e j Z e Z d „  Z d „  Z d „  Z RS(   sÆ  
    Implements and endpoint to handle *Authorization Requests* as in :rfc:`4.1.1` and prompting the
    user with a form to determine if she authorizes the client application to access her data.
    This endpoint is reached two times during the authorization process:
    * first receive a ``GET`` request from user asking authorization for a certain client
    application, a form is served possibly showing some useful info and prompting for
    *authorize/do not authorize*.

    * then receive a ``POST`` request possibly after user authorized the access

    Some informations contained in the ``GET`` request and needed to create a Grant token during
    the ``POST`` request would be lost between the two steps above, so they are temporary stored in
    hidden fields on the form.
    A possible alternative could be keeping such informations in the session.

    The endpoint is used in the followin flows:
    * Authorization code
    * Implicit grant
    s   oauth2_provider/authorize.htmlc         C   s–   |  j  j d |  j  j d g  ƒ ƒ } i |  j  j d d  ƒ d 6d j | ƒ d 6|  j  j d d  ƒ d 6|  j  j d d  ƒ d 6|  j  j d d  ƒ d 6} | S(   Nt   scopet   scopest   redirect_urit    t	   client_idt   statet   response_type(   R   t   gett   Nonet   join(   R   R#   t   initial_data(    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyt   get_initialN   s    $c   
   	   C   s  yâ i | j  j d ƒ d 6| j  j d ƒ d 6| j  j d d  ƒ d 6| j  j d d  ƒ d 6} | j  j d ƒ } | j  j d ƒ } |  j d |  j d | d	 | d | ƒ \ } } } } | |  _ t j d
 j |  j ƒ ƒ t	 |  j ƒ SWn t
 k
 r}	 |  j |	 ƒ SXd  S(   NR&   R$   R(   R'   R"   t   allowR   R#   t   credentialss    Success url for the request: {0}(   t   cleaned_dataR)   R*   t   create_authorization_responseR   t   success_urlt   logt   debugt   formatR   R	   R   (
   R   t   formR/   R#   R.   t   urit   headerst   bodyR   R   (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyt
   form_validZ   s    	*	c      
   O   sæ  yÂ|  j  | ƒ \ } } g  | D] } t j | ^ q | d <| | d <t ƒ  j j d | d ƒ } | | d <| j | ƒ | |  _ |  j |  j	 ƒ  ƒ } | | d <| j
 j d t j ƒ }	 | j r|  j d |  j d d j | ƒ d	 | d
 t ƒ \ }
 } } } t |
 ƒ S|	 d k r«| j j j d | d d t j ƒ  ƒ j ƒ  } xf | D][ } | j | ƒ rI|  j d |  j d d j | ƒ d	 | d
 t ƒ \ }
 } } } t |
 ƒ SqIWn  |  j |  j |   ƒ SWn t k
 rá} |  j | ƒ SXd  S(   Nt   scopes_descriptionsR#   R&   t   applicationR6   t   approval_promptR   R%   R/   R.   t   autot   expires__gt(   t   validate_authorization_requestR   t   SCOPESR   t   objectsR)   t   updateR   t   get_formt   get_form_classt   GETt   REQUEST_APPROVAL_PROMPTt   skip_authorizationR1   R   R+   t   TrueR   t   usert   accesstoken_sett   filterR   t   nowt   allt   allow_scopesR   t   get_context_dataR	   R   (   R   R   R   R   R#   R/   R"   R<   R6   t   require_approvalR7   R8   R9   R   t   tokenst   tokenR   (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyR)   n   s8    $

	
		
	(   R   R   R    t   template_nameR
   t
   form_classR   t   OAUTH2_SERVER_CLASSt   server_classt   OAUTH2_VALIDATOR_CLASSt   validator_classt   OAUTH2_BACKEND_CLASSt   oauthlib_backend_classt   Falset   skip_authorization_completelyR-   R:   R)   (    (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyR!   1   s   					t	   TokenViewc           B   sD   e  Z d  Z e j Z e j Z e j Z	 e
 e d ƒ ƒ d „  ƒ Z RS(   s¬   
    Implements an endpoint to provide access tokens

    The endpoint is used in the following flows:
    * Authorization code
    * Password
    * Client credentials
    t   passwordc         O   s[   |  j  | ƒ \ } } } } t d | d | ƒ } x$ | j ƒ  D] \ }	 }
 |
 | |	 <q= W| S(   Nt   contentR   (   t   create_token_responseR    t   items(   R   R   R   R   R   R8   R9   R   t   responset   kt   v(    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyt   post¨   s
    (   R   R   R    R   RV   RW   RX   RY   RZ   R[   R   R   Rf   (    (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyR^   ›   s
   			t   RevokeTokenViewc           B   s2   e  Z d  Z e j Z e j Z e j Z	 d „  Z
 RS(   sC   
    Implements an endpoint to revoke access or refresh tokens
    c         O   sa   |  j  | ƒ \ } } } } t d | p* d d | ƒ } x$ | j ƒ  D] \ }	 }
 |
 | |	 <qC W| S(   NR`   t    R   (   t   create_revocation_responseR    Rb   (   R   R   R   R   R   R8   R9   R   Rc   Rd   Re   (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyRf   º   s
    (   R   R   R    R   RV   RW   RX   RY   RZ   R[   Rf   (    (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyRg   ²   s
   			(!   t   loggingt   django.httpR    t   django.views.decorators.debugR   t   django.views.genericR   R   t   django.utilsR   t   django.utils.decoratorsR   t   braces.viewsR   R   t   settingsR   t
   exceptionsR	   t   formsR
   t   httpR   t   modelsR   t   mixinsR   t	   getLoggerR3   R   R!   R^   Rg   (    (    (    s<   /tmp/pip-unpacked-wheel-ndW12l/oauth2_provider/views/base.pyt   <module>   s"   j