
    E0j7/                        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
mZmZm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  ej        d
          Ze                    ej                    ej                    Ze                    e           ee e e e e e!e!gdf         Z"ee e e e e e!e!gdf         Z#de!de!fdZ$ G d de          Z%dS )    N)urlparse	urlencode)CallableOptional)CredentialsRefreshResultRefreshCachedSupplier)HttpOptions)TeaCore)ICredentialsProvider)parameter_helper)CredentialExceptioncredentials
expirationreturnc                     | dk     r5t          t          j        t          j                                        dz   S | dz
  S )Nr     i  )inttimemktime	localtime)r   s    o/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/alibabacloud_credentials/provider/oauth.py_get_stale_timer      s;    A~~4;t~//0011G;;    c                       e Zd ZdZdZddddddddddededed	ed
ededee	         dee
         fdZdefdZdefdZddZddZdee         fdZdee         fdZdefdZdefdZdS )OAuthCredentialsProvideri  i'  Nr   )	client_idsign_in_urlaccess_tokenaccess_token_expirerefresh_tokenhttp_optionstoken_update_callbacktoken_update_callback_asyncr   r   r   r    r!   r"   r#   r$   c                   |st          d          |st          d          || _        || _        || _        || _        || _        || _        || _        ||nt                      | _	        | j	        j
        | j	        j
        nt          j        | j	        j        | j	        j        nt          j        | j	        j        d| _        t#          | j        | j                  | _        d S )Nzthe ClientId is emptyzthe url for sign-in is empty)connectTimeoutreadTimeout
httpsProxy)refresh_callablerefresh_callable_async)
ValueError
_client_id_sign_in_url_access_token_access_token_expire_refresh_token_token_update_callback_token_update_callback_asyncr
   _http_optionsconnect_timeoutr   DEFAULT_CONNECT_TIMEOUTread_timeoutDEFAULT_READ_TIMEOUTproxy_runtime_optionsr	   _refresh_credentials_refresh_credentials_async_credentials_cache)	selfr   r   r   r    r!   r"   r#   r$   s	            r   __init__z!OAuthCredentialsProvider.__init__#   s     	64555 	=;<<<#')$7!+&;#,G)-9-E\\;==DHDVDfDrd0@@  yQ  yi>B>P>]>i4-::  pH  p],2!
 !

 #8!6#'#B#
 #
 #
r   r   c                 4    | j                                         S N)r<   
_sync_callr=   s    r   get_credentialsz(OAuthCredentialsProvider.get_credentialsF   s    &11333r   c                 D   K   | j                                          d {V S r@   )r<   _async_callrB   s    r   get_credentials_asyncz.OAuthCredentialsProvider.get_credentials_asyncI   s-      ,88:::::::::r   c                    t          t          j        t          j                                        }t	          | j                  }t          j                    }|j        |j	        d<   |j
        |_
        |j        |_        d|_        d|_        d| j        | j        t          j                    d}t%          |          |_        d|j	        d<   t)          j        || j                  }|j        dk    r2t1          d	|j         d
|j                            d                     t5          j        |j                            d                    }d|vsd|vr*t1          d|j                            d                     |                    d          }|                    d          }|                    dd          }	||	z   }
|| _        || _        |
| _        d S NhostPOSTz	/v1/tokenr!   )
grant_typer!   r   	Timestampz!application/x-www-form-urlencodedContent-Type   z,failed to refresh OAuth token, status code: z, response: utf-8r   zfailed to refresh OAuth token: 
expires_inr   )r   r   r   r   r   r-   phget_new_requesthostnameheadersportschemeprotocolmethodpathnamer0   r,   get_iso_8061_dater   bodyr   	do_actionr9   status_coder   decodejsonloadsgetr.   r/   r=   current_timertea_request	body_dataresponsedicnew_access_tokennew_refresh_tokenrP   new_access_token_expires              r   _try_refresh_oauth_tokenz1OAuthCredentialsProvider._try_refresh_oauth_tokenL   s   4;t~'7'78899T&''(**&'jF#6 x#* *!0-//	
 
	 %Y//.QN+$[$2GHH3&&%  'WU]Ui  'W  'Ww  xE  xL  xL  MT  xU  xU  'W  'W  X  X  X j--g6677$$s(B(B%&gH\H\]dHeHe&g&ghhh 77>22GGO44WW\400
".";-/$;!!!r   c                   K   t          t          j        t          j                                        }t	          | j                  }t          j                    }|j        |j	        d<   |j
        |_
        |j        |_        d|_        d|_        d| j        | j        t          j                    d}t%          |          |_        d|j	        d<   t)          j        || j                   d {V }|j        dk    r2t1          d	|j         d
|j                            d                     t5          j        |j                            d                    }d|vsd|vr*t1          d|j                            d                     |                    d          }|                    d          }|                    dd          }	||	z   }
|| _        || _        |
| _        d S rH   )r   r   r   r   r   r-   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r0   r,   rZ   r   r[   r   async_do_actionr9   r]   r   r^   r_   r`   ra   r.   r/   rb   s              r   _try_refresh_oauth_token_asyncz7OAuthCredentialsProvider._try_refresh_oauth_token_asyncu   s     4;t~'7'78899T&''(**&'jF#6 x#* *!0-//	
 
	 %Y//.QN+ 0d>STTTTTTTT3&&%  'WU]Ui  'W  'Ww  xE  xL  xL  MT  xU  xU  'W  'W  X  X  X j--g6677$$s(B(B%&gH\H\]dHeHe&g&ghhh 77>22GGO44WW\400
".";-/$;!!!r   c           	      B   | j         rd| j        I| j        dk    s>| j        t          t	          j        t	          j                                        z
  dk    r|                                  t          | j	                  }t          j                    }|j        |j        d<   |j        |_        |j        |_        d|_        d|_        d|j        d<   d| j         |j        d	<   t'          j        || j                  }|j        d
k    r2t/          d|j         d|j                            d                     t5          j        |j                            d                    }d|v r*t/          d|j                            d                     d|vsd|vsd|vr*t/          d|j                            d                     t	          j        |                    d          d          }t=          j        |          }tA          |                    d          |                    d          |                    d          || !                                          }| j"        r	 | "                    | j         | j        |#                                |$                                |%                                | j        |           n4# tL          $ r'}tN          (                    d|            Y d }~nd }~ww xY wtS          |tU          |                    S Nr   i  rI   rJ   z/v1/exchangezapplication/jsonrM   zBearer AuthorizationrN   z4error refreshing credentials from OAuth, http_code: z
, result: rO   errorz0error retrieving credentials from OAuth result: AccessKeyIdAccessKeySecretSecurityToken
Expirationz%Y-%m-%dT%H:%M:%SZ)access_key_idaccess_key_secretsecurity_tokenr   provider_namez.failed to update OAuth tokens in config file: )value
stale_time)+r0   r.   r/   r   r   r   r   rl   r   r-   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r   r\   r9   r]   r   r[   r^   r_   r`   strptimera   calendartimegmr   get_provider_namer1   get_access_key_idget_access_key_secretget_security_token	Exceptionlogwarningr   r   	r=   rd   re   rg   rh   
time_arrayr   r   es	            r   r:   z-OAuthCredentialsProvider._refresh_credentials   s    	,"*d.G1.L.LPTPiloDN,,--m/ m/ Q/26Q7 Q7))+++T&''(**&'jF#6 x#-.@N+/M9K/M/MO,$[$2GHH3&&% GxG[  G  Ggogtg{g{  }D  hE  hE  G  GH H H j--g6677c>>%b8=CWCWX_C`C`bbd d d ##'8'C'C^aGaGa%b8=CWCWX_C`C`bbd d d ]377<#8#8:NOO
_Z00
!''-00!gg&78877?33!0022
 
 
 & 	RR++'&113355772244-     R R RPQPPQQQQQQQQR ;(7
(C(CE E E 	Es   .A K 
L K;;L c           	      j  K   | j         rj| j        I| j        dk    s>| j        t          t	          j        t	          j                                        z
  dk    r|                                  d {V  t          | j	                  }t          j                    }|j        |j        d<   |j        |_        |j        |_        d|_        d|_        d|j        d<   d| j         |j        d	<   t'          j        || j                   d {V }|j        d
k    r2t/          d|j         d|j                            d                     t5          j        |j                            d                    }d|v r*t/          d|j                            d                     d|vsd|vsd|vr*t/          d|j                            d                     t	          j        |                    d          d          }t=          j        |          }tA          |                    d          |                    d          |                    d          || !                                          }| j"        r	 | "                    | j         | j        |#                                |$                                |%                                | j        |           d {V  n4# tL          $ r'}tN          (                    d|            Y d }~nd }~ww xY wtS          |tU          |                    S rq   )+r0   r.   r/   r   r   r   r   ro   r   r-   rQ   rR   rS   rT   rU   rV   rW   rX   rY   r   rn   r9   r]   r   r[   r^   r_   r`   r~   ra   r   r   r   r   r2   r   r   r   r   r   r   r   r   r   s	            r   r;   z3OAuthCredentialsProvider._refresh_credentials_async   s      	8"*d.G1.L.LPTPiloDN,,--m/ m/ Q/26Q7 Q755777777777T&''(**&'jF#6 x#-.@N+/M9K/M/MO, 0d>STTTTTTTT3&&% GxG[  G  Ggogtg{g{  }D  hE  hE  G  GH H H j--g6677c>>%b8=CWCWX_C`C`bbd d d ##'8'C'C^aGaGa%b8=CWCWX_C`C`bbd d d ]377<#8#8:NOO
_Z00
!''-00!gg&78877?33!0022
 
 
 , 	RR77'&113355772244-           R R RPQPPQQQQQQQQR ;(7
(C(CE E E 	Es   <A&K# #
L-LLc                     | j         S )u   获取客户端ID)r,   rB   s    r   _get_client_idz'OAuthCredentialsProvider._get_client_id  s
    r   c                     dS )Noauth rB   s    r   r   z*OAuthCredentialsProvider.get_provider_name  s    wr   )r   N)__name__
__module____qualname__r5   r7   strr   r
   r   OAuthTokenUpdateCallbackOAuthTokenUpdateCallbackAsyncr>   r   rC   rF   rl   ro   r   r:   r;   r   r   r   r   r   r   r      s       "  #'$(%),-&*-1MQX\!
 !
 !
!
!!
  #!
 '*	!

 !$!
  +!
 )11I(J!
 /77T.U!
 !
 !
 !
F4 4 4 4 4;[ ; ; ; ;'< '< '< '<R'< '< '< '<R<EmK&@ <E <E <E <E|;E-2L ;E ;E ;E ;Ez    3      r   r   )&r   r_   loggingr   urllib.parser   r   typingr   r   -alibabacloud_credentials.provider.refreshabler   r   r	   alibabacloud_credentials.httpr
   Tea.corer   alibabacloud_credentials_apir   alibabacloud_credentials.utilsr   rQ   #alibabacloud_credentials.exceptionsr   	getLoggerr   setLevelINFOStreamHandlerch
addHandlerr   r   r   r   r   r   r   r   r   <module>r      s       , , , , , , , , % % % % % % % % k k k k k k k k k k 5 5 5 5 5 5       = = = = = = A A A A A A C C C C C Cg&& W\   W r    $S#sCc3$G$MN  (#sCc3)Ld)R S                3     r   