
                         Z    S r SSKrSSKJr  SSKJr  SrS rS r SS jr	S	 r
SS0 4S
 jrg)aR  Client for interacting with the Reauth HTTP API.

This module provides the ability to do the following with the API:

1. Get a list of challenges needed to obtain additional authorization.
2. Send the result of the challenge to obtain a rapt token.
3. A modified version of the standard OAuth2.0 refresh grant that takes a rapt
   token.
    N)urllib)errorsz)https://reauth.googleapis.com/v2/sessionsc                 J    SU ;   a  [         R                  " U S   S   5      eU $ )zRaise an exception if msg has errors.

Args:
    msg: parsed json from http response.

Returns: input response.
Raises: ReauthAPIError
errormessage)r   ReauthAPIError)msgs    7platform/bq/third_party/google_reauth/_reauth_client.py_handle_errorsr   #   s*     #~##CL$;<<J    c           	          U " SR                  [        U5      S[        R                  " U5      SSR                  U5      0S9u  pE[        R                  " U5      n[        U5        U$ )Nz{0}{1}POSTAuthorizationz
Bearer {0}urimethodbodyheaders)format_REAUTH_APIjsondumpsloadsr   )http_requestpathr   access_token_contentresponses          r
   _endpoint_requestr    1   s[    OOK.ZZ ,"5"5l"CD	JA zz'"H8Or   c                 :    SU0nU(       a  X4S'   [        U SXB5      $ )a  Does initial request to reauth API to get the challenges.

Args:
    http_request (Callable): callable to run http requests. Accepts uri,
        method, body and headers. Returns a tuple: (response, content)
    supported_challenge_types (Sequence[str]): list of challenge names
        supported by the manager.
    access_token (str): Access token with reauth scopes.
    requested_scopes (list[str]): Authorized scopes for the credentials.

Returns:
    dict: The response from the reauth API.
supportedChallengeTypes oauthScopesForDomainPolicyLookupz:start)r    )r   supported_challenge_typesr   requested_scopesr   s        r
   get_challengesr&   =   s/      &'@AD3C/0h4 4r   c                 H    UUSUS.n[        U SR                  U5      XT5      $ )aa  Attempt to refresh access token by sending next challenge result.

Args:
    http_request (Callable): callable to run http requests. Accepts uri,
        method, body and headers. Returns a tuple: (response, content)
    session_id (str): session id returned by the initial reauth call.
    challenge_id (str): challenge id returned by the initial reauth call.
    client_input: dict with a challenge-specific client input. For example:
        ``{'credential': password}`` for password challenge.
    access_token (str): Access token with reauth scopes.

Returns:
    dict: The response from the reauth API.
RESPOND)	sessionIdchallengeIdactionproposalResponsez/{0}:continue)r    r   )r   
session_idchallenge_idclient_inputr   r   s         r
   send_challenge_resultr0   U   s:    "  #(	D o,,Z8$N Nr   c                     SUUUS.nU(       a  XXS'   U(       a  XhS'   [         R                  R                  U5      n	U " USU	US9u  pX4$ )a  Implements the OAuth 2.0 Refresh Grant with the addition of the reauth
token.

Args:
    http_request (Callable): callable to run http requests. Accepts uri,
        method, body and headers. Returns a tuple: (response, content)
    client_id (str): client id to get access token for reauth scope.
    client_secret (str): client secret for the client_id
    refresh_token (str): refresh token to refresh access token
    token_uri (str): uri to refresh access token
    scopes (str): scopes required by the client application as a
        comma-joined list.
    rapt (str): RAPT token
    headers (dict): headers for http request

Returns:
    Tuple[str, dict]: http response and parsed response content.
refresh_token)
grant_type	client_idclient_secretr2   scoperaptr   r   )r   parse	urlencode)r   r4   r5   r2   	token_uriscopesr7   r   
parametersr   r   r   s               r
   refresh_grantr=   p   sg    , &&&	J $7!6<<!!*-D$	H
 r   )N)__doc__r   	six.movesr   google_reauthr   r   r   r    r&   r0   r=    r   r
   <module>rB      sB        9	 40N: T2)r   