
     h-                     N   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mZ  ej:                  e      Zd
 Z d Z!d Z"d Z# G d de      Z$ G d d      Z% G d d      Z& G d d      Z' G d d      Z(y)    N)datetime	timedelta)
NamedTupleOptional)tzutc)UNSIGNED)total_seconds)Config)ClientErrorInvalidConfigErrorTokenRetrievalError)CachedPropertyJSONFileCacheSSOTokenLoadercreate_nested_clientget_token_from_environmentc                  <    t        j                  t                     S N)r   nowr        k/home/developers/rajanand/mypropertyqr-fmb-refixing-v2/venv/lib/python3.12/site-packages/botocore/tokens.py_utc_nowr   *   s    <<  r   c                 F    t        |       t        |       g}t        |      S )N)	providers)ScopedEnvTokenProviderSSOTokenProviderTokenProviderChain)sessionr   s     r   create_token_resolverr    .   s&    w'!I 	22r   c                 H    t        | t              r| j                  d      S | S )Nz%Y-%m-%dT%H:%M:%SZ)
isinstancer   strftimeobjs    r   _serialize_utc_timestampr&   6   s!    #x ||011Jr   c                 8    t        j                  | t              S )N)default)jsondumpsr&   r$   s    r   _sso_json_dumpsr+   <   s    ::c#;<<r   c                   ,    e Zd ZU eed<   dZee   ed<   y)FrozenAuthTokentokenN
expiration)__name__
__module____qualname__str__annotations__r/   r   r   r   r   r   r-   r-   @   s    J%)J")r   r-   c                   @    e Zd ZdZdZdZefdZd Zd Z	d Z
d Zd	 Zy
)DeferredRefreshableToken  iX  <   c                 |    || _         || _        || _        t        j                         | _        d | _        d | _        y r   )_time_fetcher_refresh_usingmethod	threadingLock_refresh_lock_frozen_token_next_refresh)selfr<   refresh_usingtime_fetchers       r   __init__z!DeferredRefreshableToken.__init__N   s:    )+ '^^-!!r   c                 :    | j                          | j                  S r   )_refreshr@   rB   s    r   get_frozen_tokenz)DeferredRefreshableToken.get_frozen_tokenX   s    !!!r   c                     | j                         }|sy |dk(  }| j                  j                  |      r,	 | j                          | j                  j	                          y y # | j                  j	                          w xY w)N	mandatory)_should_refreshr?   acquire_protected_refreshrelease)rB   refresh_typeblock_for_refreshs      r   rG   z!DeferredRefreshableToken._refresh\   sv    ++- )K7%%&78-'')""**,	 9 ""**,s   A! !A=c                 Z   | j                         }|sy 	 | j                         }|t        | j                        z   | _        | j                         | _        | j                         rt        | j                  d      y # t        $ r! t        j                  d|d       |dk(  r Y Qw xY w)Nsecondsz5Refreshing token failed during the %s refresh period.Texc_inforK   z$Token has expired and refresh failed)provider	error_msg)rL   r:   r   _attempt_timeoutrA   r;   r@   	Exceptionloggerwarning_is_expiredr   r<   )rB   rP   r   s      r   rN   z+DeferredRefreshableToken._protected_refreshj   s     ++-	$$&C!$y9N9N'O!OD!%!4!4!6D %@    	NNG  
 {* +	s   AB   'B*)B*c                     | j                   y| j                   j                  }t        || j                         z
        }|dk  S )NFr   )r@   r/   r	   r:   )rB   r/   	remainings      r   r]   z$DeferredRefreshableToken._is_expired   sD    %''22
!*t/A/A/C"CD	A~r   c                     | j                   y| j                   j                  }|y | j                         }|| j                  k  ry t	        ||z
        }|| j
                  k  ry|| j                  k  ryy )NrK   advisory)r@   r/   r:   rA   r	   _mandatory_refresh_timeout_advisory_refresh_timeout)rB   r/   r   r_   s       r   rL   z(DeferredRefreshableToken._should_refresh   s~    %''22
  "###!*s"23	t666777r   N)r0   r1   r2   rc   rb   rY   r   rE   rI   rG   rN   r]   rL   r   r   r   r6   r6   E   s7     !(!(;C ""-8r   r6   c                       e Zd ZddZd Zy)r   Nc                     |g }|| _         y r   )
_providers)rB   r   s     r   rE   zTokenProviderChain.__init__   s    I#r   c                 V    | j                   D ]  } |j                  di |}||c S  y )Nr   )rf   
load_token)rB   kwargsrW   r.   s       r   rh   zTokenProviderChain.load_token   s:     	H'H''1&1E 	 r   r   )r0   r1   r2   rE   rh   r   r   r   r   r      s    $
r   r   c                       e Zd ZdZdZej                  j                  ej                  j                  dddd            Z	ddgZ
dZeZd	ed	fd
Zd Zed        Zed        Zd Zd Zd Zd Zy	)r   ssor7   ~z.awscachesso_start_url
sso_regionrefresh_tokenNc                     || _         |!| j                  | j                  t              }|| _        || _        t        | j
                        | _        |xs | j                   j                  d      xs d| _	        y )N)
dumps_func)rm   profiler(   )
_sessionDEFAULT_CACHE_CLS_SSO_TOKEN_CACHE_DIRr+   _now_cacher   _token_loaderget_config_variable_profile_name)rB   r   rm   rD   profile_names        r   rE   zSSOTokenProvider.__init__   s|      =**))* + E !	+$++> }}00; 	r   c                    | j                   j                  }|j                  di       }|j                  di       }|j                  | j                  i       }d|vry |d   }|j                  |d       }|sd| j                   d| d}t	        |      g }| j
                  D ]  }	|	|vs|j                  |	        |rd| j                   d| d	}t	        |      ||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.)rX   zZ" is configured to use the SSO token provider but is missing the following configuration: .ro   rn   )session_namero   rn   )rt   full_configgetr{   r   _SSO_CONFIG_VARSappend)
rB   loaded_configr~   r   profile_configsso_session_name
sso_configrX   missing_configsvars
             r   _load_sso_configz!SSOTokenProvider._load_sso_config   s3   11 $$Z4$((<!d&8&8"=.)-8!%%&6=
 2 23 4++;*< =01 
 %y99(( 	,C*$&&s+	,  2 23 4N"#1& 
 %y99 -$\2'8
 	
r   c                 "    | j                         S r   )r   rH   s    r   _sso_configzSSOTokenProvider._sso_config   s    $$&&r   c                 n    t        | j                  d   t              }t        | j                  d|      S )Nro   )region_namesignature_versionzsso-oidc)config)r
   r   r   r   rt   )rB   r   s     r   _clientzSSOTokenProvider._client   s2    ((6&
 $DMM:fMMr   c                 L   | j                   j                  | j                  |d   |d   |d         }t        |d         }| j                  d   | j                  d   |d	   | j                         |z   |d   |d   |d
   d}d|v r|d   |d<   t        j                  d       |S )NclientIdclientSecretrefreshToken)	grantTyper   r   r   	expiresInrS   rn   ro   accessTokenregistrationExpiresAt)startUrlregionr   	expiresAtr   r   r   zSSO Token refresh succeeded)r   create_token_GRANT_TYPEr   r   rw   r[   info)rB   r.   response
expires_in	new_tokens        r   _attempt_create_tokenz&SSOTokenProvider._attempt_create_token  s    <<,,&&:&~.~.	 - 
 x'<=
((9&&|4#M2z1j)!.1%*+B%C	
	 X%(0(@In%12r   c                    d}|D cg c]	  }||vs| }}|rd| }t         j                  |       y t        j                  j	                  |d         }t        || j                         z
        dk  rt         j                  d|       y 	 | j                  |      S c c}w # t        $ r t         j                  dd       Y y w xY w)	N)r   r   r   r   z+Unable to refresh SSO token: missing keys: r   r   z$SSO token registration expired at %sz SSO token refresh attempt failedTrU   )
r[   r   dateutilparserparser	   rw   r   r   r\   )rB   r.   keyskmissing_keysmsgexpirys          r   _refresh_access_tokenz&SSOTokenProvider._refresh_access_token  s    
 $(:a1E>::?~NCKK&&u-D'EF$))+-.!3KK>G	--e44 ;  	NN=NM	s   	BBB# # CCc                    | j                   d   }| j                   d   }t        j                  d|       | j                  ||      }t        j
                  j                  |d         }t        j                  d|       t        || j                         z
        }|| j                  k  r8| j                  |      }|%|}|d   }| j                  j                  |||       t        |d   |      S )	Nrn   r   zLoading cached SSO token for %s)r   r   zCached SSO token expires at %sr   )r/   )r   r[   r   ry   r   r   r   debugr	   rw   _REFRESH_WINDOWr   
save_tokenr-   )rB   	start_urlr   
token_dictr/   r_   new_token_dicts          r   
_refresherzSSOTokenProvider._refresher5  s    $$_5	''75|D''	'M
__**:k+BC
5zB!*tyy{":;	t+++!77
CN)+
'4
""--z .  }%*
 	
r   c                 t    | j                   y t        | j                  | j                  | j                        S )N)rD   )r   r6   METHODr   rw   )rB   ri   s     r   rh   zSSOTokenProvider.load_tokenK  s1    #'KKtyy
 	
r   )r0   r1   r2   r   r   ospath
expanduserjoinrv   r   r   r   ru   r   rE   r   r   r   r   r   r   r   rh   r   r   r   r   r      s    FO77--
S&%1 	 "K% "t
$%
N ' ' N N.0
,
r   r   c                   "    e Zd ZdZdZddZd Zy)r   zn
    Token provider that loads tokens from environment variables scoped to
    a specific `signing_name`.
    envNc                 D    || _         |t        j                  }|| _        y r   )rt   r   environ)rB   r   r   s      r   rE   zScopedEnvTokenProvider.__init__\  s    ?jjGr   c                     |j                  d      }|y t        || j                        }| t        j	                  d       t        |      S y )Nsigning_namez%Found token in environment variables.)r   r   r   r[   r   r-   )rB   ri   r   r.   s       r   rh   z!ScopedEnvTokenProvider.load_tokenb  sL    zz.1*<FKK?@"5)) r   r   )r0   r1   r2   __doc__r   rE   rh   r   r   r   r   r   T  s    
 F	*r   r   ))r)   loggingr   r=   r   r   typingr   r   dateutil.parserr   dateutil.tzr   botocorer   botocore.compatr	   botocore.configr
   botocore.exceptionsr   r   r   botocore.utilsr   r   r   r   r   	getLoggerr0   r[   r   r    r&   r+   r-   r6   r   r   r   r   r   r   <module>r      s      	  ( '    ) " 
  
		8	$!3=*j *
^ ^B ]
 ]
@* *r   