
    ]j                     p    d dl Z d dlZd dlZd dlmZmZ d dlZd dlZd dlZddl	m
Z
 ddlmZ  G d d      Zy)    N)datetimetimezone   )ApiError)TokenRefreshResponsec                       e Zd ZdZddedefdZddededefd	Zdeddfd
Ze	defd       Z
dej                  defdZdefdZy)TokensClientExtz*Client for managing authentication tokens.Nbase_urlapi_keyc                     || _         || _        || _        | j                  |d       | _        d | _        d | _        t        j                         | _	        y )NT)raise_if_expired)
	_base_url_api_key_client_wrapper_is_valid_jwt_token_use_legacy_token_access_token_access_token_expiration	threadingLock_token_refresh_lock)selfr
   r   client_wrappers       L/root/env/lib/python3.12/site-packages/label_studio_sdk/tokens/client_ext.py__init__zTokensClientExt.__init__   sU    !-%)%=%=gX\%=%]!] 48CG%#,>>#3     tokenr   returnc                 f   	 t        j                  |ddi      }|j                  d      }|t	        dddi      t        j                  |t        j                        }|t        j                  t        j                        k  r|rt	        ddd	i      yy
# t         j                  $ r Y yw xY w)z^Check if a token is a valid JWT token by attempting to decode its header and check expiration.verify_signatureFoptionsexpi  detailz]API key does not have an expiration set, and is not valid. Please obtain a new refresh token.status_codebodyz7API key has expired. Please obtain a new refresh token.T)
jwtdecodeInvalidTokenErrorgetr   r   fromtimestampr   utcnow)r   r   r   decoded
expirationexpiration_times         r   r   z#TokensClientExt._is_valid_jwt_token   s    	jj1CU0KLG [['
   A  #00X\\JX\\(,,77 #"$]^ 
 % $$ 		s   B B0/B0c                     	 t        j                  |ddi      }|j                  d      }|)t        j                  |t
        j                        | _        || _	        y# t         j                  $ r
 Y || _	        yw xY w)z3Set the access token and cache its expiration time.r    Fr!   r#   N)
r(   r)   r+   r   r,   r   r-   r   r*   r   )r   r   r/   r0   s       r   _set_access_tokenz!TokensClientExt._set_access_token6   sy    	jj1CU0KLG U+J%080F0FzS[S_S_0`- # $$ 	"	s   AA A<;A<c                    | j                   r| j                  S | j                  r| j                  | j                        ss| j                  5  | j                  r| j                  | j                        s+| j                         }| j                  |j                         ddd       | j                  S | j                  S # 1 sw Y   | j                  S xY w)z6Get the current access token, refreshing if necessary.N)r   r   r   r   r   refreshr3   access)r   token_responses     r   r   zTokensClientExt.api_keyA   s     !!==  ""D,D,DTEWEW,X)) B**D4L4LTM_M_4`%)\\^N**>+@+@A	B !!!t!!!B !!!s   AB??Cexisting_clientc                    |j                   |j                  |j                  |j                  |j                  |j
                  |j                  |j                  |j                  |j                  |j                  |j                  j                  j                  j                  t        j                   k7  |j                  j                  j"                  |j                  j                  j$                  t'        j(                  |j                  j                  j*                  |j                  j                  j,                  |j                  j                  j.                        dS )zExtract parameters from an existing client to create a new one.

        Args:
            existing_client: The existing client to extract parameters from.

        Returns:
            dict: Parameters for creating a new client.
        )max_connectionsmax_keepalive_connectionskeepalive_expiry)authparamsheaderscookiestimeoutfollow_redirectsmax_redirectsevent_hooksr
   	trust_envdefault_encodingverifyhttp1http2limits)r=   r>   r?   r@   rA   rB   rC   rD   r
   rE   _default_encoding
_transport_pool_ssl_contextverify_modessl	CERT_NONE_http1_http2httpxLimits_max_connections_max_keepalive_connections_keepalive_expiry)r   r8   s     r   _get_client_paramsz"TokensClientExt._get_client_paramsR   s    $((%,,&..&..&.. / @ @,::*66'00(22 / A A%0066CCOOSVS`S``$//55<<$//55<<ll / : : @ @ Q Q*9*D*D*J*J*e*e!0!;!;!A!A!S!S
 	
r   c                    | j                   j                  j                  }t        |t        j                        rM|j                  dt        j                  j                  | j                   d      d| j                  iddi      }n}| j                  |      }t        j                  di |5 }|j                  dt        j                  j                  | j                   dd      d| j                  iddi      }ddd       j                  d	k(  r#t        j                  |j                               S t!        |j                  |j                         
      # 1 sw Y   `xY w)z7Refresh the access token and return the token response.POSTzapi/token/refresh/r5   zContent-Typezapplication/json)methodurljsonr?   /N   r%    )r   httpx_client
isinstancerT   Clientrequesturllibparseurljoinr   r   rY   r&   r   	parse_objr^   r   )r   r8   responseclient_paramssync_clients        r   r5   zTokensClientExt.refreshq   s8   ..;;HH ou||4&..LL((DNN+;>RS/');<	 / H !33ODM.. +&..!,,/?q-ACWX#T]]3+-?@	 /  3&'11(--/BBx';';(--/RR s   .AEE%)N)F)__name__
__module____qualname____doc__strr   boolr   r3   propertyr   rT   AsyncClientdictrY   r   r5   ra   r   r   r	   r	      s    4
4 
4s 
4  QU 0	#s 	#t 	# " " " 
%2C2C 
 
>S- Sr   r	   )r   urllib.parserf   typingr   r   rP   rT   r(   core.api_errorr   types.token_refresh_responser   r	   ra   r   r   <module>rz      s.       ' 
  
 % ?~S ~Sr   