
                         V    d Z ddlZddlmZ ddlmZ dZd Zd Z	 ddZ	d	 Z
ddi fd
Zy)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                 D    d| v rt        j                  | d   d         | S )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           	           | dj                  t        |      dt        j                  |      ddj                  |      i      \  }}t        j                  |      }t        |       |S )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	JAw zz'"H8Or   c                 4    d|i}|r||d<   t        | d||      S )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&   =   s3      &'@AD3C/0hl4 4r   c                 J    ||d|d}t        | dj                  |      ||      S )a  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                     d|||d}|r||d<   |r||d<   t         j                  j                  |      }	 | |d|	|      \  }
}|
|fS )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   so    , &&&	J $
7!
6<<!!*-D$	Hg
 Wr   )N)__doc__r   	six.movesr   google_reauthr   r   r   r    r&   r0   r=    r   r
   <module>rB      sB        9	 40N: T2)r   