a
    d=ic"                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z
ddlZ
ddlZ
ddlmZ ddlmZ e ZdZdZg dZG dd	 d	eZd
d ZG dd dejjZG dd dejZdd ZdS )z8Provides authentication support for TensorBoardUploader.    N)util)
tb_logging)Zopenidz.https://www.googleapis.com/auth/userinfo.emaila  
{
  "installed": {
    "client_id": "373649185512-8v619h5kft38l4456nm2dj4ubeqsrvh6.apps.googleusercontent.com",
    "project_id": "hosted-tensorboard-prod",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "pOyAuU2yq2arsM98Bw5hwYtr",
    "redirect_uris": [
      "urn:ietf:wg:oauth:2.0:oob",
      "http://localhost"
    ]
  }
}
)Ztensorboardcredentialszuploader-creds.jsonc                   @   s:   e Zd ZdZe ZefddZdd Zdd Zdd	 Z	d
S )CredentialsStorezAPrivate file store for a `google.oauth2.credentials.Credentials`.c                 C   sN   |t ju r$t }|du r$td |du r4d| _ntjj	|gt
R  | _dS )aw  Creates a CredentialsStore.

        Args:
          user_config_directory: Optional absolute path to the root directory for
            storing user configs, under which to store the credentials file. If not
            set, defaults to a platform-specific location. If set to None, the
            store is disabled (reads return None; write and clear are no-ops).
        Nz@Credentials caching disabled - no private config directory found)r   _DEFAULT_CONFIG_DIRECTORYr   Zget_user_config_directoryloggerwarning_credentials_filepathospathjoin&TENSORBOARD_CREDENTIALS_FILEPATH_PARTS)selfZuser_config_directory r   j/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/tensorboard/uploader/auth.py__init__P   s    	
zCredentialsStore.__init__c                 C   s2   | j du rdS tj| j r.tjjj| j S dS )zMReturns the current `google.oauth2.credentials.Credentials`, or
        None.N)	r	   r
   r   existsgoogleoauth2r   CredentialsZfrom_authorized_user_file)r   r   r   r   read_credentialsf   s    

z!CredentialsStore.read_credentialsc                 C   s   t |tjjjs tdt| | jdu r.dS tj	dk}t
j| j|d |j|j|j|j|jdd}t| jd}t|| W d   n1 s0    Y  dS )z>Writes a `google.oauth2.credentials.Credentials` to the store.z#Cannot write credentials of type %sNnt)privateZauthorized_user)refresh_token	token_uri	client_idclient_secretscopestypew)
isinstancer   r   r   r   	TypeErrorr   r	   r
   namer   Zmake_file_with_directoriesr   r   r   r   r   openjsondump)r   r   r   datafr   r   r   write_credentialss   s&    


z"CredentialsStore.write_credentialsc              
   C   sV   | j du rdS zt| j  W n2 tyP } z|jtjkr< W Y d}~n
d}~0 0 dS )z:Clears the store of any persisted credentials information.N)r	   r
   removeOSErrorerrnoENOENT)r   er   r   r   clear   s    
zCredentialsStore.clearN)
__name__
__module____qualname____doc__objectr   r   r   r(   r.   r   r   r   r   r   K   s   r   c                 C   s   t j| tdS )zReturns a `CustomInstalledAppFlow` for the given config.

    Args:
      client_config (Mapping[str, Any]): The client configuration in the Google
          client secrets format.

    Returns:
      CustomInstalledAppFlow: the constructed flow.
    )r   )CustomInstalledAppFlowZfrom_client_configOPENID_CONNECT_SCOPES)Zclient_configr   r   r   build_installed_app_flow   s    
r6   c                   @   s   e Zd ZdZdddZdS )r4   z4Customized version of the Installed App OAuth2 flow.Fc                 C   sF   |s>t dr>z| jddW S  tjy<   tjd Y n0 |  S )zMRun the flow using a local server if possible, otherwise the
        console.ZDISPLAYr   )portz/Falling back to console authentication flow...
)	r
   getenvZrun_local_server
webbrowserErrorsysstderrwriteZrun_console)r   Zforce_consoler   r   r   run   s    
zCustomInstalledAppFlow.runN)F)r/   r0   r1   r2   r>   r   r   r   r   r4      s   r4   c                       s(   e Zd ZdZ fddZdd Z  ZS )IdTokenAuthMetadataPluginas  A `gRPC AuthMetadataPlugin` that uses ID tokens.

    This works like the existing `google.auth.transport.grpc.AuthMetadataPlugin`
    except that instead of always using access tokens, it preferentially uses the
    `Credentials.id_token` property if available (and logs an error otherwise).

    See http://www.grpc.io/grpc/python/grpc.html#grpc.AuthMetadataPlugin
    c                    s>   t t|   t|tjjjs.tdt	| || _
|| _dS )a3  Constructs an IdTokenAuthMetadataPlugin.

        Args:
          credentials (google.auth.credentials.Credentials): The credentials to
            add to requests.
          request (google.auth.transport.Request): A HTTP transport request object
            used to refresh credentials as needed.
        z-Cannot get ID tokens from credentials type %sN)superr?   r   r    r   r   r   r   r!   r   _credentials_request)r   r   request	__class__r   r   r      s    	z"IdTokenAuthMetadataPlugin.__init__c                 C   s`   i }| j | j|j|j| t| j dd}|r@| j j||d n
td |t	|
 d dS )a  Passes authorization metadata into the given callback.

        Args:
          context (grpc.AuthMetadataContext): The RPC context.
          callback (grpc.AuthMetadataPluginCallback): The callback that will
            be invoked to pass in the authorization metadata.
        id_tokenN)tokenz#Failed to find ID token credentials)rA   Zbefore_requestrB   method_nameZservice_urlgetattrapplyr   errorlistitems)r   contextcallbackheadersrF   r   r   r   __call__   s    
z"IdTokenAuthMetadataPlugin.__call__)r/   r0   r1   r2   r   rQ   __classcell__r   r   rD   r   r?      s   	r?   c                 C   s   t jjj }tt| |S )zConstructs `grpc.CallCredentials` using
    `google.auth.Credentials.id_token`.

    Args:
      credentials (google.auth.credentials.Credentials): The credentials to use.

    Returns:
      grpc.CallCredentials: The call credentials.
    )r   auth	transportrequestsRequestgrpcZmetadata_call_credentialsr?   )r   rC   r   r   r   id_token_call_credentials   s    
rX   )r2   r+   r$   r
   r;   r9   Zgoogle_auth_oauthlib.flowZgoogle_auth_oauthlibrW   Zgoogle.authr   Zgoogle.auth.transport.requestsZgoogle.oauth2.credentialsZtensorboard.uploaderr   Ztensorboard.utilr   Z
get_loggerr   r5   ZOAUTH_CLIENT_CONFIGr   r3   r   r6   ZflowZInstalledAppFlowr4   ZAuthMetadataPluginr?   rX   r   r   r   r   <module>   s*   M1