o
    ni                     @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlZd dlm	Z	 eej
dZdZdeded	ed
efddZdeded	efddZdeded	ed
efddZdeded
efddZdS )    N)datetime	timedelta)Path)AuthenticationErrorz ~/.config/wandb/credentials.jsonz%Y-%m-%d %H:%M:%Sbase_url
token_filecredentials_filereturnc                 C   s(   |  s
t| || t| ||}|d S )a  Retrieve an access token from the credentials file.

    If no access token exists, create a new one by exchanging the identity
    token from the token file, and save it to the credentials file.

    Args:
        base_url (str): The base URL of the server
        token_file (pathlib.Path): The path to the file containing the
        identity token
        credentials_file (pathlib.Path): The path to file used to save
        temporary access tokens

    Returns:
        str: The access token
    access_token)exists_write_credentials_file_fetch_credentials)r   r   r   data r   W/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/lib/credentials.pyr
      s   r
   c                 C   sb   t | |}d| |ii}t|d}tj||dd t|d W d   dS 1 s*w   Y  dS )aT  Obtain an access token from the server and write it to the credentials file.

    Args:
        base_url (str): The base URL of the server
        token_file (pathlib.Path): The path to the file containing the
        identity token
        credentials_file (pathlib.Path): The path to file used to save
        temporary access tokens
    credentialsw   indenti  N)_create_access_tokenopenjsondumposchmod)r   r   r   r   r   filer   r   r   r   (   s   

"r   c                 C   s   i }t |!}t|}d|vri |d< | |d v r |d |  }W d   n1 s*w   Y  t }d|v r?t|d t}|t krnt| |}t |d}||d | < tj||dd W d   |S 1 siw   Y  |S )a  Fetch the access token from the credentials file.

    If the access token has expired, fetch a new one from the server and save it
    to the credentials file.

    Args:
        base_url (str): The base URL of the server
        token_file (pathlib.Path): The path to the file containing the
        identity token
        credentials_file (pathlib.Path): The path to file used to save
        temporary access tokens

    Returns:
        dict: The credentials including the access token.
    r   N
expires_atr   r   r   )	r   r   loadr   utcnowstrptime_expires_at_fmtr   r   )r   r   r   credsr   r   r   r   r   r   r   ;   s*   



r   c              
   C   s  zt |}|  }W d   n1 sw   Y  W n' ty0 } ztd| |d}~w tyC } ztd| |d}~ww |  d}d|d}ddi}tj|||d	}|jd
krktd|j d|j	 |
 }	t tt|	d d }
|
t|	d< |	d= |	S )a  Exchange an identity token for an access token from the server.

    Args:
        base_url (str): The base URL of the server.
        token_file (pathlib.Path): The path to the file containing the
        identity token

    Returns:
        dict: The access token and its expiration.

    Raises:
        FileNotFoundError: If the token file is not found.
        OSError: If there is an issue reading the token file.
        AuthenticationError: If the server fails to provide an access token.
    NzIdentity token file not found: z-Failed to read the identity token from file: z/oidc/tokenz+urn:ietf:params:oauth:grant-type:jwt-bearer)
grant_type	assertionzContent-Typez!application/x-www-form-urlencoded)r   headers   z!Failed to retrieve access token: z, 
expires_in)secondsr   )r   readstripFileNotFoundErrorOSErrorrequestspoststatus_coder   textr   r   r   r   floatstrftimer!   )r   r   r   tokeneurlr   r%   response	resp_jsonr   r   r   r   r   `   s>   


r   )r   r   r   r   pathlibr   requests.utilsr-   wandb.errorsr   path
expanduserDEFAULT_WANDB_CREDENTIALS_FILEr!   strr
   r   dictr   r   r   r   r   r   <module>   s    
%