
    F                         d Z ddlZddl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	 ddlm
Z
  ej                  e      Z G d d	ej                        Zy)
zTwo factor Oauth2Credentials.    N)_helpers)client)	transport)errors)reauthc                   T     e Zd ZdZ fdZed        Zed        Zd Z	 	 ddZ	 xZ
S )Oauth2WithReauthCredentialsa8  Credentials object that extends OAuth2Credentials with reauth support.

    This class provides the same functionality as OAuth2Credentials, but adds
    the support for reauthentication and rapt tokens. These credentials should
    behave the same as OAuth2Credentials when the credentials don't use rauth.
    c                 Z    |j                  dd      | _        t        t        |   |i | y)zrCreate an instance of Oauth2WithReauthCredentials.

        A Oauth2WithReauthCredentials has an extra rapt_token.
rapt_tokenN)popr   superr	   __init__)selfargskwargs	__class__s      5platform/bq/third_party/google_reauth/reauth_creds.pyr   z$Oauth2WithReauthCredentials.__init__)   s,    
 !**\48)494J6J    c           
         t        j                  t        j                  |            }|j	                  d      rRt        |d   t        j                        s5	 t        j                  j                  |d   t        j                        |d<   i }dD ]  }|j	                  |d      }||||<     | |d   |d   |d   |d   |d   |d   |d	   fi |}|d
   |_        |S # t        $ r d|d<   Y cw xY w)z
Overrides.token_expiryN)
revoke_uriid_tokenid_token_jwttoken_responsescopestoken_info_urir   access_token	client_idclient_secretrefresh_token	token_uri
user_agentinvalid)jsonloadsr   _from_bytesget
isinstancedatetimestrptimer   EXPIRY_FORMAT
ValueErrorr#   )cls	json_datadatar   paramvalueretvals          r   	from_jsonz%Oauth2WithReauthCredentials.from_json1   s    zz(..y9:XXn%^ 4h6G6GH,'/'8'8'A'A(&*>*>(@^$
 $E HHUD)E  %u$ 
~

{





~

{

|
	
 	
 i-  ,'+^$,s   4C C0/C0c                 D    |j                         }| j                  |      S )zAInstantiate a Oauth2WithReauthCredentials from OAuth2Credentials.)to_jsonr3   )r-   originalr$   s      r   from_OAuth2Credentialsz2Oauth2WithReauthCredentials.from_OAuth2CredentialsS   s!     !}}T""r   c                 .   | j                         }t        j                  d       fd}	  | j                  t	        j
                  || j                  | j                  | j                  | j                  | j                  t        | j                        |        y# t        j                  t        j                  f$ rT}d| _        | j"                  r| j"                  j%                  |        t'        j                  ||j(                        d}~ww xY w)an  Refresh the access_token using the refresh_token.

        Args:
            http: An object to be used to make HTTP requests.
            rapt_refreshed: If we did or did not already refreshed the rapt
                            token.

        Raises:
            oauth2client_4_0.client.HttpAccessTokenRefreshError: if the refresh
                fails.
        zRefreshing access_tokenc                 p    t        j                  | |||      \  }}t        j                  |      }||fS )N)methodbodyheaders)r   requestr   r&   )urir:   r;   r<   responsecontenthttps         r   http_requestzEOauth2WithReauthCredentials._do_refresh_request.<locals>.http_requesti   sA     ) 1 1c&7!,Hg **73GW$$r   )raptr   r<   T)statusN)!_generate_refresh_request_headers_LOGGERinfo_updater   refresh_access_tokenr   r   r    r!   r   listr   r   ReauthAccessTokenRefreshErrorHttpAccessTokenRefreshErrorr#   store
locked_putr   rD   )r   rA   r<   rB   es    `   r   _do_refresh_requestz/Oauth2WithReauthCredentials._do_refresh_requestY   s     88:./	%	IDLL&55 NN&&&&NN,#% & 44224 	IDLzz

%%d+44QqxxHH	Is   A/B #D ADDc                    |r|| _         || _        || _        |r|n| j                  | _        |r<t	        j
                  t        |            }|t        j                         z   | _	        nd | _	        || _
        |rt        j                  |      nd | _        d| _        | j                  r| j                  j                  |        y y )N)secondsF)r   r   r   r    r)   	timedeltaintr   _UTCNOWr   r   _extract_id_tokenr   r#   rM   rN   )r   rC   r@   r   r    
expires_inr   deltas           r   rH   z#Oauth2WithReauthCredentials._update   s    "DO%(*M0B0B 	&&s:?E %(8 8D $D$2:F$$X. 	 ::JJ!!$' r   )NNN)__name__
__module____qualname____doc__r   classmethodr3   r7   rP   rH   __classcell__)r   s   @r   r	   r	   !   sM    K  B # #
&IP BF*.(r   r	   )r\   r)   r$   loggingoauth2client_4_0r   r   r   google_reauthr   r   	getLoggerrY   rF   OAuth2Credentialsr	    r   r   <module>re      sL     $    % # &     '

H
%s(&":": s(r   