o
    NDi-                     @   s  d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm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mZmZ d d	lmZmZmZmZ eeZd
d Zdd Z dd Z!dd Z"G dd deZ#G dd dZ$G dd dZ%G dd dZ&G dd dZ'dS )    N)datetime	timedelta)
NamedTupleOptional)tzutc)UNSIGNED)total_seconds)Config)ClientErrorInvalidConfigErrorTokenRetrievalError)CachedPropertyJSONFileCacheSSOTokenLoaderget_token_from_environmentc                   C   s   t t S N)r   nowr    r   r   H/var/www/Datamplify/venv/lib/python3.10/site-packages/botocore/tokens.py_utc_now)   s   r   c                 C   s   t | t| g}t|dS )N)	providers)ScopedEnvTokenProviderSSOTokenProviderTokenProviderChain)sessionr   r   r   r   create_token_resolver-   s   
r   c                 C   s   t | tr
| dS | S )Nz%Y-%m-%dT%H:%M:%SZ)
isinstancer   strftimeobjr   r   r   _serialize_utc_timestamp5   s   

r    c                 C   s   t j| tdS )N)default)jsondumpsr    r   r   r   r   _sso_json_dumps;   s   r$   c                   @   s&   e Zd ZU eed< dZee ed< dS )FrozenAuthTokentokenN
expiration)__name__
__module____qualname__str__annotations__r'   r   r   r   r   r   r   r%   ?   s   
 r%   c                   @   sL   e Zd ZdZdZdZefddZdd Zdd	 Z	d
d Z
dd Zdd ZdS )DeferredRefreshableToken  iX  <   c                 C   s,   || _ || _|| _t | _d | _d | _d S r   )_time_fetcher_refresh_usingmethod	threadingLock_refresh_lock_frozen_token_next_refresh)selfr2   refresh_usingtime_fetcherr   r   r   __init__M   s   

z!DeferredRefreshableToken.__init__c                 C   s   |    | jS r   )_refreshr6   r8   r   r   r   get_frozen_tokenW   s   z)DeferredRefreshableToken.get_frozen_tokenc                 C   sN   |   }|sd S |dk}| j|r%z|   W | j  d S | j  w d S )N	mandatory)_should_refreshr5   acquire_protected_refreshrelease)r8   refresh_typeblock_for_refreshr   r   r   r<   [   s   
z!DeferredRefreshableToken._refreshc                 C   s   |   }|sd S z|  }|t| jd | _|  | _W n ty2   tj	d|dd |dkr0 Y nw | 
 r>t| jddd S )Nsecondsz5Refreshing token failed during the %s refresh period.Texc_infor?   z$Token has expired and refresh failed)provider	error_msg)r@   r0   r   _attempt_timeoutr7   r1   r6   	Exceptionloggerwarning_is_expiredr   r2   )r8   rD   r   r   r   r   rB   i   s.   
z+DeferredRefreshableToken._protected_refreshc                 C   s.   | j d u rdS | j j}t||   }|dkS )NFr   )r6   r'   r   r0   )r8   r'   	remainingr   r   r   rP      s
   
z$DeferredRefreshableToken._is_expiredc                 C   sd   | j d u rdS | j j}|d u rd S |  }|| jk rd S t|| }|| jk r)dS || jk r0dS d S )Nr?   advisory)r6   r'   r0   r7   r   _mandatory_refresh_timeout_advisory_refresh_timeout)r8   r'   r   rQ   r   r   r   r@      s   



z(DeferredRefreshableToken._should_refreshN)r(   r)   r*   rT   rS   rL   r   r;   r>   r<   rB   rP   r@   r   r   r   r   r-   D   s    
r-   c                   @   s   e Zd ZdddZdd ZdS )r   Nc                 C   s   |d u rg }|| _ d S r   )
_providers)r8   r   r   r   r   r;      s   
zTokenProviderChain.__init__c                 K   s0   | j D ]}|jdi |}|d ur|  S qd S )Nr   )rU   
load_token)r8   kwargsrJ   r&   r   r   r   rV      s   
zTokenProviderChain.load_tokenr   )r(   r)   r*   r;   rV   r   r   r   r   r      s    
r   c                   @   s   e Zd ZdZdZejejddddZ	ddgZ
dZeZd	ed	fd
dZdd Zedd Zedd Zdd Zdd Zdd Zdd Zd	S )r   ssor.   ~z.awscachesso_start_url
sso_regionrefresh_tokenNc                 C   sR   || _ |d u r| j| jtd}|| _|| _t| jd| _|p%| j dp%d| _	d S )N)
dumps_func)rZ   profiler!   )
_sessionDEFAULT_CACHE_CLS_SSO_TOKEN_CACHE_DIRr$   _now_cacher   _token_loaderget_config_variable_profile_name)r8   r   rZ   r:   profile_namer   r   r   r;      s   
zSSOTokenProvider.__init__c           
      C   s   | j j}|di }|di }|| ji }d|vrd S |d }||d }|s8d| j d| d}t|dg }| jD ]}	|	|vrH||	 q=|rZd| j d| d	}t|d||d
 |d dS )Nprofilessso_sessionssso_sessionzThe profile "z7" is configured to use the SSO token provider but the "z+" sso_session configuration does not exist.)rK   zZ" is configured to use the SSO token provider but is missing the following configuration: .r\   r[   )session_namer\   r[   )r`   full_configgetrg   r   _SSO_CONFIG_VARSappend)
r8   loaded_configri   rj   profile_configsso_session_name
sso_configrK   missing_configsvarr   r   r   _load_sso_config   s:   





z!SSOTokenProvider._load_sso_configc                 C   s   |   S r   )rx   r=   r   r   r   _sso_config   s   zSSOTokenProvider._sso_configc                 C   s"   t | jd td}| jjd|dS )Nr\   )region_namesignature_versionzsso-oidc)config)r	   ry   r   r`   create_client)r8   r|   r   r   r   _client   s
   zSSOTokenProvider._clientc                 C   s   | j j| j|d |d |d d}t|d d}| jd | jd |d	 |  | |d |d |d
 d}d|v r>|d |d< td |S )NclientIdclientSecretrefreshToken)	grantTyper   r   r   	expiresInrF   r[   r\   accessTokenregistrationExpiresAt)startUrlregionr   	expiresAtr   r   r   zSSO Token refresh succeeded)r~   create_token_GRANT_TYPEr   ry   rc   rN   info)r8   r&   response
expires_in	new_tokenr   r   r   _attempt_create_token  s&   


z&SSOTokenProvider._attempt_create_tokenc                    s   d} fdd|D }|rd| }t | d S tj d }t||   dkr5t d|  d S z|  W S  tyL   t j	dd	d
 Y d S w )N)r   r   r   r   c                    s   g | ]}| vr|qS r   r   ).0kr&   r   r   
<listcomp>#  s    z:SSOTokenProvider._refresh_access_token.<locals>.<listcomp>z+Unable to refresh SSO token: missing keys: r   r   z"SSO token registration expired at z SSO token refresh attempt failedTrH   )
rN   r   dateutilparserparser   rc   r   r
   rO   )r8   r&   keysmissing_keysmsgexpiryr   r   r   _refresh_access_token  s    

z&SSOTokenProvider._refresh_access_tokenc                 C   s   | j d }| j d }td|  | j||d}tj|d }td|  t|| 	  }|| j
k rN| |}|d urN|}|d }| jj|||d t|d |dS )	Nr[   rm   zLoading cached SSO token for )rm   r   zCached SSO token expires at r   )r'   )ry   rN   r   re   r   r   r   debugr   rc   _REFRESH_WINDOWr   
save_tokenr%   )r8   	start_urlrm   
token_dictr'   rQ   new_token_dictr   r   r   
_refresher4  s$   



zSSOTokenProvider._refresherc                 K   s"   | j d u rd S t| j| j| jdS )N)r:   )ry   r-   METHODr   rc   )r8   rW   r   r   r   rV   J  s
   
zSSOTokenProvider.load_token)r(   r)   r*   r   r   ospath
expanduserjoinrb   rp   r   r   ra   r   r;   rx   r   ry   r~   r   r   r   rV   r   r   r   r   r      s,    
'

r   c                   @   s&   e Zd ZdZdZdddZdd ZdS )	r   zn
    Token provider that loads tokens from environment variables scoped to
    a specific `signing_name`.
    envNc                 C   s   || _ |d u r
tj}|| _d S r   )r`   r   environ)r8   r   r   r   r   r   r;   [  s   
zScopedEnvTokenProvider.__init__c                 K   s@   | d}|d u rd S t|| j}|d urtd t|S d S )Nsigning_namez%Found token in environment variables.)ro   r   r   rN   r   r%   )r8   rW   r   r&   r   r   r   rV   a  s   

z!ScopedEnvTokenProvider.load_tokenr   )r(   r)   r*   __doc__r   r;   rV   r   r   r   r   r   S  s
    
r   )(r"   loggingr   r3   r   r   typingr   r   dateutil.parserr   dateutil.tzr   botocorer   botocore.compatr   botocore.configr	   botocore.exceptionsr
   r   r   botocore.utilsr   r   r   r   	getLoggerr(   rN   r   r   r    r$   r%   r-   r   r   r   r   r   r   r   <module>   s0   
a !