
                             S r SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SrSrSS jrS rS rS rg)a2  This module contains the views used by the OAuth2 flows.

Their are two views used by the OAuth2 flow, the authorize and the callback
view. The authorize view kicks off the three-legged OAuth flow, and the
callback view validates the flow and if successful stores the credentials
in the configured storage.    N)http)	shortcuts)settings)urlresolvers)redirect)parse)client)django_util)get_storage)signalsgoogle_oauth2_csrf_tokenzgoogle_oauth2_flow_{0}c                    [         R                  " [        R                  " S5      5      R	                  5       nX0R
                  [        '   [        R                  " UUS.5      n[        R                  " [        R                  R                  [        R                  R                  UUU R                  [         R"                  " S5      5      S9n[$        R'                  U5      n[(        R                  " U5      U R
                  U'   U$ )a-  Creates a Web Server Flow

Args:
    request: A Django request object.
    scopes: the request oauth2 scopes.
    return_url: The URL to return to after the flow is complete. Defaults
        to the path of the current request.

Returns:
    An OAuth2 flow object that has been stored in the session.
i   )
csrf_token
return_urlzgoogle_oauth:callback)	client_idclient_secretscopestateredirect_uri)hashlibsha256osurandom	hexdigestsession	_CSRF_KEYjsondumpsr	   OAuth2WebServerFlowr
   oauth2_settingsr   r   build_absolute_urir   reverse	_FLOW_KEYformatpickle)requestscopesr   r   r   flowflow_keys          9lib/third_party/oauth2client/contrib/django_util/views.py
_make_flowr+   +   s     

4 01;;=J!+OOIJJ   E
 %%--77!11??//  !89;<D 
+H &T 2GOOHK    c                     UR                   R                  [        R                  U 5      S5      nUc  S$ [        R
                  " U5      $ )ad  Looks up the flow in session to recover information about requested
scopes.

Args:
    csrf_token: The token passed in the callback request that should
        match the one previously generated and stored in the request on the
        initial authorization view.

Returns:
    The OAuth2 Flow object associated with this flow based on the
    CSRF token.
N)r   getr#   r$   r%   loads)r   r&   flow_pickles      r*   _get_flow_for_tokenr1   N   s>     //%%i&6&6z&BDIK&4EFLL,EEr,   c                 ^   SU R                   ;   a[  U R                   R                  SU R                   R                  SS5      5      n[        R                  " SR	                  U5      5      $  U R                   S   nU R                   S   n U R                  [           n [        R                  " U5      nUS	   nUS
   nXd:w  a  [        R                  " S5      $ [        X`5      nU(       d  [        R                  " S5      $  UR                  U5      n	[        U 5      R!                  U	5        ["        R$                  R'                  ["        R$                  X	S9  [(        R*                  " U5      $ ! [
         a    [        R                  " S5      s $ f = f! [
         a    [        R                  " S5      s $ f = f! [        [
        4 a    [        R                  " S5      s $ f = f! [        R                   a/  n
[        R                  " SR	                  U
5      5      s Sn
A
$ Sn
A
ff = f)a}  View that handles the user's return from OAuth2 provider.

This view verifies the CSRF state and OAuth authorization code, and on
success stores the credentials obtained in the storage provider,
and redirects to the return_url specified in the authorize view and
stored in the session.

Args:
    request: Django request.

Returns:
     A redirect response back to the return_url.
errorerror_description zAuthorization failed {0}r   codez+Request missing state or authorization codez"No existing session for this flow.r   r   zInvalid state parameter.zInvalid CSRF token.zMissing Oauth2 flow.zAn error has occurred: {0}N)senderr&   credentials)GETr.   r   HttpResponseBadRequestr$   KeyErrorr   r   r   r/   
ValueErrorr1   step2_exchanger	   FlowExchangeErrorr   putr   oauth2_authorizedsendr   r   )r&   reasonencoded_stater6   server_csrfr   client_csrfr   r(   r8   exchange_errors              r*   oauth2_callbackrG   _   s    '++"!=?**&--f57 	7;G,{{6"
2ooi0
G

=)L)<(
 !**+@AA{4D**+ABBA))$/
 [)""'*C*C+2 # M j))I  ;**9; 	;;  2**02 	22 ! G**+EFFG ## A**(//?A 	AAsT   -E1 F   F= G) 1 FF F:9F:=&G&%G&)H,=$H'!H,'H,c           	         U R                   R                  SS5      n[        R                  R                  (       a  U R
                  R                  5       (       dK  [        SR                  [        R                  [        R                  " U R                  5       5      5      5      $ [        U 5      R                  5       b  [        U5      $ U R                   R                  S[        R                  R                   5      nU(       d  U R"                  R                  SS5      n[%        XUS9nUR'                  5       n[(        R                  " U5      $ )a  View to start the OAuth2 Authorization flow.

 This view starts the OAuth2 authorization flow. If scopes is passed in
 as a  GET URL parameter, it will authorize those scopes, otherwise the
 default scopes specified in settings. The return_url can also be
 specified as a GET parameter, otherwise the referer header will be
 checked, and if that isn't found it will return to the root path.

Args:
   request: The Django request object.

Returns:
     A redirect to Google OAuth2 Authorization.
r   Nz{0}?next={1}r'   HTTP_REFERER/)r&   r'   r   )r9   r.   r
   r    storage_modeluseris_authenticatedr   r$   r   	LOGIN_URLr   quoteget_full_pathr   getlistr'   METAr+   step1_get_authorize_urlr   )r&   r   r'   r(   auth_urls        r*   oauth2_authorizerU      s     t4J ""00||,,..N11""EKK0E0E0G$HJ K K !%%'3J''[[  ;+F+F+M+MNF\\%%nc:
gLD++-Hh''r,   )N)__doc__r   r   r   r%   djangor   r   django.confr   django.corer   django.shortcutsr   six.moves.urllibr   oauth2clientr	   oauth2client.contribr
    oauth2client.contrib.django_utilr   r   r   r#   r+   r1   rG   rU    r,   r*   <module>r`      sW      	      $ % "  , 8 4&	$	 FF";*|!(r,   