
    N                         S SK Jr  S SKrS SKJrJr  S SKJrJr  S SKJ	r	  S SKJ
r
Jr  S SKr\R                  " \5      r " S S\5      r " S	 S
\R$                  5      rg)    )unicode_literalsN)generate_token	urldecode)WebApplicationClientInsecureTransportError)LegacyApplicationClient)TokenExpiredErroris_secure_transportc                   (   ^  \ rS rSrU 4S jrSrU =r$ )TokenUpdated   c                 6   > [         [        U ]  5         Xl        g N)superr   __init__token)selfr   	__class__s     3lib/third_party/requests_oauthlib/oauth2_session.pyr   TokenUpdated.__init__   s    	,&(J    r   )__name__
__module____qualname____firstlineno__r   __static_attributes____classcell__r   s   @r   r   r      s     r   r   c                     ^  \ rS rSrSr         SU 4S jjrS r\S 5       r\R                  S 5       r\R                  S 5       r\S 5       r\R                  S	 5       r\S
 5       r\R                  S 5       r\R                  S 5       r\S 5       rSS jr             SS jrS r       SS jr     SU 4S jjrS rSrU =r$ )OAuth2Session   a  Versatile OAuth 2 extension to :class:`requests.Session`.

Supports any grant type adhering to :class:`oauthlib.oauth2.Client` spec
including the four core OAuth 2 grants.

Can be used to create authorization urls, fetch tokens and access protected
resources using the :class:`requests.Session` interface you are used to.

- :class:`oauthlib.oauth2.WebApplicationClient` (default): Authorization
Code Grant
- :class:`oauthlib.oauth2.MobileApplicationClient`: Implicit Grant
- :class:`oauthlib.oauth2.LegacyApplicationClient`: Password Credentials
Grant
- :class:`oauthlib.oauth2.BackendApplicationClient`: Client Credentials
Grant

Note that the only time you will be using Implicit Grant from python is if
you are driving a user agent able to obtain URL fragments.
c
                 V  > [         [        U ]
  " S0 U
D6  U=(       d	    [        XS9U l        U=(       d    0 U l        XPl        X`l        U=(       d    [        U l	        Xl
        X0l        U=(       d    0 U l        Xl        S U l        [        5       [        5       [        5       S.U l        g)a  Construct a new OAuth 2 client session.

:param client_id: Client id obtained during registration
:param client: :class:`oauthlib.oauth2.Client` to be used. Default is
               WebApplicationClient which is useful for any
               hosted application but not mobile or desktop.
:param scope: List of scopes you wish to request access to
:param redirect_uri: Redirect URI you registered as callback
:param token: Token dictionary, must include access_token
              and token_type.
:param state: State string used to prevent CSRF. This will be given
              when creating the authorization url and must be supplied
              when parsing the authorization response.
              Can be either a string or a no argument callable.
:auto_refresh_url: Refresh token endpoint URL, must be HTTPS. Supply
                   this if you wish the client to automatically refresh
                   your access tokens.
:auto_refresh_kwargs: Extra arguments to pass to the refresh token
                      endpoint.
:token_updater: Method with one argument, token, to be used to update
                your token database on automatic token refresh. If not
                set a TokenUpdated warning will be raised when a token
                has been refreshed. This warning will carry the token
                in its token argument.
:param kwargs: Arguments to pass to the Session constructor.
r   c                     U $ r    )rs    r   <lambda>(OAuth2Session.__init__.<locals>.<lambda>\   s    !r   )access_token_responserefresh_token_responseprotected_requestNr%   )r   r!   r   r   _clientr   scoperedirect_urir   state_stateauto_refresh_urlauto_refresh_kwargstoken_updaterauthsetcompliance_hook)r   	client_idclientr1   r2   r-   r.   r   r/   r3   kwargsr   s              r   r   OAuth2Session.__init__*   s    J 
-'1&1I1)IDL"DJJ$(.DJK,28bD& DI
 "%"%% UDr   c                     U R                  5       U l        [        R                  SU R                  5        U R                  $ ! [         a?    U R                   U l        [        R                  SU R                  5         U R                  $ f = f)z6Generates a state string to be used in authorizations.zGenerated new state %s.z&Re-using previously supplied state %s.)r/   r0   logdebug	TypeErrorr   s    r   	new_stateOAuth2Session.new_statef   so    GJJLdk	ii)4;;7 ;;  GJJdk	ii8$++F;;Gs   5A ;BBc                 0    [        U R                  SS 5      $ )Nr7   getattrr,   r?   s    r   r7   OAuth2Session.client_idp   s    4<<d33r   c                 $    XR                   l        g r   r,   r7   r   values     r   r7   rE   t   s    "LLr   c                     U R                   ?g r   rG   r?   s    r   r7   rE   x   s    r   c                 0    [        U R                  SS 5      $ )Nr   rC   r?   s    r   r   OAuth2Session.token|   s    4<<$//r   c                 Z    XR                   l        U R                   R                  U5        g r   )r,   r   populate_token_attributesrH   s     r   r   rL      s    LLLL**51r   c                 0    [        U R                  SS 5      $ )Naccess_tokenrC   r?   s    r   rP   OAuth2Session.access_token   s    4<<66r   c                 $    XR                   l        g r   r,   rP   rH   s     r   rP   rQ      s     %LLr   c                     U R                   ?g r   rS   r?   s    r   rP   rQ      s    !r   c                 ,    [        U R                  5      $ )aQ  Boolean that indicates whether this session has an OAuth token

or not. If `self.authorized` is True, you can reasonably expect
OAuth-protected requests to the resource to succeed. If
`self.authorized` is False, you need the user to go through the OAuth
authentication dance before OAuth-protected requests to the resource
will succeed.
)boolrP   r?   s    r   
authorizedOAuth2Session.authorized   s     !!""r   c                     U=(       d    U R                  5       nU R                  R                  " U4U R                  U R                  US.UD6U4$ )a  Form an authorization URL.

:param url: Authorization endpoint url, must be HTTPS.
:param state: An optional state string for CSRF protection. If not
              given it will be generated for you.
:param kwargs: Extra parameters to include.
:return: authorization_url, state
)r.   r-   r/   )r@   r,   prepare_request_urir.   r-   )r   urlr/   r9   s       r   authorization_urlOAuth2Session.authorization_url   s[     %T^^%E<<++&&jj	
 
  r   c           
         [        U5      (       d
  [        5       eU(       dA  U(       a:  U R                  R                  X0R                  S9  U R                  R
                  nONU(       dG  [        U R                  [        5      (       a(  U R                  R
                  nU(       d  [        S5      e[        U R                  [        5      (       a  Uc  [        S5      eUc  [        S5      eUb  XoS'   Ub  XS'   Ub  Uc  SnOUUSLaP  U R                  nU(       a=  [        R                  S	U5        Ub  UOS
n[        R                  R                  UU5      nU(       a  Ub  XS'   U R                  R                   " S UUU R"                  US.UD6nU
=(       d    SSS.n
0 U l        UR'                  5       S:X  a>  U R)                  U[+        [-        U5      5      U	U
UUUS9n[        R                  SU5        O]UR'                  5       S:X  a>  U R/                  U[+        [-        U5      5      U	U
UUUS9n[        R                  SU5        O[        S5      e[        R                  SUR0                  5        [        R                  SUR2                  R4                  5        [        R                  SUR2                  R6                  5        [        R                  SUR4                  UR8                  5        [        R                  S[;        U R<                  S   5      5        U R<                  S    H!  n[        R                  SU5        U" U5      nM#     U R                  R?                  UR8                  U R@                  S9  U R                  R$                  U l        [        R                  SU R$                  5        U R$                  $ )!aQ  Generic method for fetching an access token from the token endpoint.

If you are using the MobileApplicationClient you will want to use
`token_from_fragment` instead of `fetch_token`.

The current implementation enforces the RFC guidelines.

:param token_url: Token endpoint URL, must use HTTPS.
:param code: Authorization code (used by WebApplicationClients).
:param authorization_response: Authorization response URL, the callback
                               URL of the request back to you. Used by
                               WebApplicationClients instead of code.
:param body: Optional application/x-www-form-urlencoded body to add the
             include in the token request. Prefer kwargs over body.
:param auth: An auth tuple or method as accepted by `requests`.
:param username: Username required by LegacyApplicationClients to appear
                 in the request body.
:param password: Password required by LegacyApplicationClients to appear
                 in the request body.
:param method: The HTTP method used to make the request. Defaults
               to POST, but may also be GET. Other methods should
               be added as needed.
:param timeout: Timeout of the request in seconds.
:param headers: Dict to default request headers with.
:param verify: Verify SSL certificate.
:param proxies: The `proxies` argument is passed onto `requests`.
:param include_client_id: Should the request body include the
                          `client_id` parameter. Default is `None`,
                          which will attempt to autodetect. This can be
                          forced to always include (True) or never
                          include (False).
:param client_secret: The `client_secret` paired to the `client_id`.
                      This is generally required unless provided in the
                      `auth` tuple. If the value is `None`, it will be
                      omitted from the request, however if the value is
                      an empty string, an empty string will be sent.
:param kwargs: Extra parameters to include in the token request.
:return: A token dict
r/   z?Please supply either code or authorization_response parameters.zQ`LegacyApplicationClient` requires both the `username` and `password` parameters.zFThe required paramter `username` was supplied, but `password` was not.usernamepasswordFTzIEncoding `client_id` "%s" with `client_secret` as Basic auth credentials. client_secret)codebodyr.   include_client_idapplication/json/application/x-www-form-urlencoded;charset=UTF-8AcceptzContent-TypePOST)datatimeoutheadersr4   verifyproxiesz$Prepared fetch token request body %sGET)paramsrm   rn   r4   ro   rp   z+Prepared fetch token request querystring %sz%The method kwarg must be POST or GET.z0Request to fetch token completed with status %s.zRequest headers were %szRequest body was %s(Response headers were %s and content %s.!Invoking %d token response hooks.r)   Invoking hook %s.r-   zObtained token %s.r%   )!r
   r   r,   parse_request_uri_responser0   rd   
isinstancer   
ValueErrorr   r7   r<   r=   requestsr4   HTTPBasicAuthprepare_request_bodyr.   r   upperpostdictr   getstatus_coderequestrn   re   textlenr6   parse_request_body_responser-   )r   	token_urlrd   authorization_responsere   r4   r`   ra   methodrm   rn   ro   rp   rf   rc   r9   r7   r&   hooks                      r   fetch_tokenOAuth2Session.fetch_token   s_   n y))"$$*
ll--
  . 5\\dj/CDD\\d > ? 	? $,, 788		 A B 	B		 3 4 	4 #Z#Z  
	"! 
$	&NN	
))+,57 ,9+D-"-,,YF$ 
	""/<<,, &&+	
 D  $IG DJ||~
))
IdO$  a 
ii6=	5	 
((
io&  a 
ii=tD>??II@!--PII'):):;II#QYY^^4II8!))QVVLII1$&&'>?@B$$%<=	ii#T*
q'a > 	LL,,QVV4::,F##DJII"DJJ/::r   c                     U R                   R                  XR                  S9  U R                   R                  U l        U R                  $ )zParse token from the URI fragment, used by MobileApplicationClients.

:param authorization_response: The full URL of the redirect back to you
:return: A token dict
r_   )r,   rw   r0   r   )r   r   s     r   token_from_fragment!OAuth2Session.token_from_fragment^  s=     	LL++kk , 3##DJ::r   c	                 0   U(       d  [        S5      e[        U5      (       d
  [        5       eU=(       d    U R                  R	                  S5      n[
        R                  SU R                  5        U	R                  U R                  5        U R                  R                  " SX2U R                  S.U	D6n[
        R                  SU5        Uc  SSS.nU R                  U[        [        U5      5      UUUUS	US
9n
[
        R                  SU
R                  5        [
        R                  SU
R                   U
R"                  5        [
        R                  S[%        U R&                  S   5      5        U R&                  S    H!  n[
        R                  SU5        U" U
5      n
M#     U R                  R)                  U
R"                  U R                  S9U l        SU R                  ;  a#  [
        R                  S5        X R                  S'   U R                  $ )a  Fetch a new access token using a refresh token.

:param token_url: The token endpoint, must be HTTPS.
:param refresh_token: The refresh_token to use.
:param body: Optional application/x-www-form-urlencoded body to add the
             include in the token request. Prefer kwargs over body.
:param auth: An auth tuple or method as accepted by `requests`.
:param timeout: Timeout of the request in seconds.
:param headers: A dict of headers to be used by `requests`.
:param verify: Verify SSL certificate.
:param proxies: The `proxies` argument will be passed to `requests`.
:param kwargs: Extra parameters to include in the token request.
:return: A token dict
z'No token endpoint set for auto_refresh.refresh_tokenz*Adding auto refresh key word arguments %s.)re   r   r-   z&Prepared refresh token request body %srg   rh   ri   T)rl   r4   rm   rn   ro   withhold_tokenrp   z2Request to refresh token completed with status %s.rs   rt   r*   ru   rv   z)No new refresh token given. Re-using old.r%   )ry   r
   r   r   r   r<   r=   r2   updater,   prepare_refresh_bodyr-   r~   r   r   r   rn   r   r   r6   r   )r   r   r   re   r4   rm   rn   ro   rp   r9   r&   r   s               r   r   OAuth2Session.refresh_tokeni  s   0 @AAy))"$$!DTZZ^^O%DMII:&&(
MM$**+<<,, LdjjLDJLDII6=&Lg
 			)D/" 	 	A IIBmmII8!))QVVLII1$&&'?@AC$$%=>	ii#T*
q'a ? 99	djj : "DJdjj(	ii;<$1jj!::r   c                   > [        U5      (       d
  [        5       eU R                  (       a  U(       d  [        R	                  S[        U R                  S   5      5        U R                  S    H%  n	[        R	                  SU	5        U	" X$U5      u  p$nM'     [        R	                  SU R                  5         U R                  R                  X!X4S9u  p$n[        R	                  SX!5        [        R	                  SXC5        [        R	                  SU5        [$        [&        U ]R  " X4XCS.UD6$ ! [         Ga    U R                  (       Ga  [        R	                  SU R                  5        UR                  SS5      n
U(       a?  U(       a8  U
c5  [        R	                  S	U5        [        R                  R                  Xg5      n
U R                  " U R                  4SU
0UD6nU R                   (       aR  [        R	                  S
UU R                   5        U R!                  U5        U R                  R                  X!X4S9u  p$n GNp[#        U5      ee f = f)z<Intercept all requests and add the OAuth 2 token if present.z-Invoking %d protected resource request hooks.r+   ru   zAdding token %s to request.)http_methodre   rn   z1Auto refresh is set, attempting to refresh at %s.r4   NzEEncoding client_id "%s" with client_secret as Basic auth credentials.zUpdating token to %s using %s.z"Requesting url %s using method %s.z Supplying headers %s and data %sz&Passing through key word arguments %s.)rn   rl   )r
   r   r   r<   r=   r   r6   r,   	add_tokenr	   r1   poprz   r4   r{   r   r3   r   r   r!   r   )r   r   r[   rl   rn   r   r7   rc   r9   r   r4   r   r   s               r   r   OAuth2Session.request  s    s##"$$zz.	ii?D(()<=>@&&':;$		%t,!#5d < 
ii-tzz:!\\33$ 4 Ad6 II2C@II0'@II6?-;$;39; ;7     
))G))+ FD)$=dlIIW ==..yHD$$T%:%:PPP%II6((*u%!%!7!7d "8 "EC$ u%%
-s   9D/ /DIIc                     XR                   ;  a  [        SUU R                   5      eU R                   U   R                  U5        g)aI  Register a hook for request/response tweaking.

Available hooks are:
    access_token_response invoked before token parsing.
    refresh_token_response invoked before refresh token parsing.
    protected_request invoked before making a request.

If you find a new hook is needed please send a GitHub PR request
or open an issue.
zHook type %s is not in %s.N)r6   ry   add)r   	hook_typer   s      r   register_compliance_hook&OAuth2Session.register_compliance_hook  sD     ,,,3Y++- -#''-r   )r,   r0   r4   r2   r1   r6   r.   r-   r/   r   r3   )	NNNNNNNNNr   )NNrb   NNNrk   NNTNNN)Nrb   NNNTN)NNFNN)r   r   r   r   __doc__r   r@   propertyr7   setterdeleterr   rP   rW   r\   r   r   r   r   r   r   r   r   s   @r   r!   r!      sy   *  $#' !:x 4 4 # #   0 0 	<<2 2 7 7 & & " " 	# 	#& )-$( $n`	 #'   DR " 4;l. .r   r!   )
__future__r   loggingoauthlib.commonr   r   oauthlib.oauth2r   r   r   r	   r
   rz   	getLoggerr   r<   Warningr   Sessionr!   r%   r   r   <module>r      sJ    '  5 H 3 B !7 ^.H$$ ^.r   