
    5pjM                        % S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  SSKJr  SSKJr  SSKJrJrJr  S	S
KJr  S	SKJr  S	SKJrJr  SrSrS\S\SS4S jrSq\
" 5       rSq \S-  \!S'   \RD                  " \#5      r$S\S-  4S jr%S\S-  4S jr&S\S-  4S jr'S\S-  4S jr( " S S\5      r)\
" 5       r*Sq+\)S-  \!S'   Sr,S\S-  4S jr- " S S\5      r.\
" 5       r/Sq0\.S-  \!S '   S!r1Sr2Sq3S\S-  4S" jr4S#\S\4S$ jr5S%\SS4S& jr6S'\7\\4   S\8S-  4S( jr9S\7\\4   4S) jr:S\7\\7\\4   4   4S* jr;S+\7\\7\\4   4   SS4S, jr<S-\S\S-  4S. jr=SSS/.S#\S-\S0\S-  S1\8S-  SS4
S2 jjr>S#\S-  S\S-  4S3 jr?g)4zVContains a helper to get the token from machine (env variable, secret or config file).    N)Path)Lock)	TypedDict   )	constants)DeviceCodeErrorOAuthErrorCode	OIDCError   )WeakFileLock)refresh_access_token)is_colab_enterpriseis_google_colabi  i  pathcontentreturnc                    U R                   R                  SS[        S9  [        R                  " [        U 5      [        R                  [        R                  -  [        R                  -  [        5      n[        R                  " US5       nUR                  U5        SSS5         U R                  [        5        U R                   R                  [        5        g! , (       d  f       ND= f! [        [        4 a     gf = f)ziWrite content to file, restricting both the file and its parent directory to owner-only on POSIX systems.T)parentsexist_okmodewN)parentmkdir_SECRET_DIR_MODEosopenstrO_WRONLYO_CREATO_TRUNC_SECRET_FILE_MODEfdopenwritechmodOSErrorNotImplementedError)r   r   fdfs       W/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/huggingface_hub/utils/_auth.py_write_secretr*   %   s    KKdT8HI	TBKK"**4rzzACT	UB	2s	q	 


$%*+	 
	
 () s   C'4C- 
C*-D ?D F_GOOGLE_COLAB_SECRETc                  |    [        5       =(       d,    [        5       =(       d    [        5       =(       d
    [        5       $ )a%  
Get token if user is logged in.

Note: in most cases, you should use [`huggingface_hub.utils.build_hf_headers`] instead. This method is only useful
      if you want to retrieve the token for other purposes than sending an HTTP request.

If `HF_OIDC_RESOURCE` is set (Trusted Publishers, typically in CI), a short-lived token obtained via OIDC token
exchange takes precedence. Otherwise the token is retrieved from the `HF_TOKEN` environment variable, then from the
token file in the Hugging Face home folder. Returns None if user is not logged in. To log in, use [`login`] or
`hf auth login`.

OAuth tokens obtained with the browser-based login come with a refresh token: when such a token is close to
expiry, it is transparently refreshed and persisted before being returned.

Note: if `HF_OIDC_RESOURCE` is set but the OIDC token exchange fails, this raises instead of returning `None`,
opting into OIDC is explicit, so a failure surfaces as a clear error rather than a silent fallback.

Returns:
    `str` or `None`: The token, `None` if it doesn't exist.
)_get_token_from_oidc_get_token_from_environment_get_token_from_file_refreshed_get_token_from_google_colab     r)   	get_tokenr3   :   s2    , 	 	*&(	*)+	* ()	r2   c                  \   [        5       (       a  [        5       (       a  g[           [        (       a  [        sSSS5        $  SSKJn   SSKJn   U R                  S5      n[        U5      qS
q[        sSSS5        $ ! [         a     SSS5        gf = f! U R                   a    [        R                  " S5        Sq NSU R                   a    [         R#                  S5        Sq N{U a/  n[        R                  " S[%        U5       S	35        Sq SnANSnAff = f! , (       d  f       g= f)zGet token from Google Colab secrets vault using `google.colab.userdata.get(...)`.

Token is read from the vault only once per session and then stored in a global variable to avoid re-requesting
access to the vault.
Nr   )userdata)ErrorHF_TOKENz
Access to the secret `HF_TOKEN` has not been granted on this notebook.
You will not be requested again.
Please restart the session if you want to be prompted again.zThe secret `HF_TOKEN` does not exist in your Colab secrets. Run `huggingface_hub.login()` to authenticate (recommended but still optional to access public models or datasets).z@
Error while fetching `HF_TOKEN` secret value from your vault: 'z'.T)r   r   _GOOGLE_COLAB_SECRET_LOCK_IS_GOOGLE_COLAB_CHECKEDr+   google.colabr5   google.colab.errorsr6   ImportErrorget_clean_tokenNotebookAccessErrorwarningswarnSecretNotFoundErrorloggerinfor   )r5   
ColabErrortokenes       r)   r0   r0   W   s     3 5 5 
# $#' 
#	"	-?	(LL,E#/#6 0 $( #Q 
#	"  	 
#	"	 ++ 	( MMQ
 $( ++ 	( KKf $(  	(MM]^abc^d]eeghi#' 	(E 
#	"sk   DA<B+D<
BDBD(D<D>&D$D&D+%DDDD
D+c                      [        [        R                  R                  S5      =(       d    [        R                  R                  S5      5      $ )Nr7   HUGGING_FACE_HUB_TOKEN)r>   r   environr=   r1   r2   r)   r.   r.      s-    

z2^bjjnnE]6^__r2   c                       [        [        [        R                  5      R	                  5       5      $ ! [
         a     g f = f)N)r>   r   r   HF_TOKEN_PATH	read_textFileNotFoundErrorr1   r2   r)   _get_token_from_filerO      s8    D!8!89CCEFF s   /2 
??c                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)_OidcTokenCache   resourcerF   
expires_atr1   N__name__
__module____qualname____firstlineno__r   __annotations__float__static_attributes__r1   r2   r)   rQ   rQ      s    MJr2   rQ   _OIDC_TOKEN_CACHEi,  c                  >   [         R                  R                  S5      n U (       d  gSSKJnJn  [           [        R                  " 5       n[        b,  [        S   U :X  a  U[        S   :  a  [        S   sSSS5        $ [         R                  R                  S5      =(       d    SnUc  U" 5       c  [        S	5      eU" XS
9nUS   n[        UR                  SS5      5      nUb  SO[        nU UU[        Xx-
  S5      -   S.q	UsSSS5        $ ! , (       d  f       g= f)a  Get a short-lived OIDC token in CI (Trusted Publishers).

Enabled by setting `HF_OIDC_RESOURCE`, which scopes the token to a repo or user.
The ID token is read from `HF_OIDC_ID_TOKEN` if available, or minted from a supported CI provider (e.g. GitHub Actions).

Returns `None` when OIDC is not enabled.
If enabled, any failure is raised explicitly rather than falling back silently.

See `huggingface_hub._oidc` and https://huggingface.co/docs/hub/trusted-publishers.
HF_OIDC_RESOURCENr   )detect_provider
oidc_loginrS   rT   rF   HF_OIDC_ID_TOKENzHF_OIDC_RESOURCE is set but no OIDC id token is available: not running in a supported CI provider (github) and HF_OIDC_ID_TOKEN is not set. Set HF_OIDC_ID_TOKEN to the id token minted by your CI provider, or unset HF_OIDC_RESOURCE.)rS   subject_tokenaccess_token
expires_ini  r   )rS   rF   rT   )r   rJ   r=   _oidcr`   ra   _OIDC_TOKEN_LOCKtime	monotonicr]   r
   int_OIDC_REFRESH_MARGINmax)	rS   r`   ra   nowrc   resultrF   re   margins	            r)   r-   r-      s    zz~~01H3 
nn)!*-9'55$W- 
	 

'9:Bd _%6%>O  XK~&L$78
 $/5I J$7 ;;

 = 
		s   ?D?BD
Dc                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)_OAuthRefreshCache   
file_tokenresolved_token
recheck_atr1   NrU   r1   r2   r)   rq   rq      s    Or2   rq   _OAUTH_REFRESH_CACHEiQ c                  4    [        5       n U c  g[        U 5      $ )zSGet the token from `HF_TOKEN_PATH`, transparently refreshing it if close to expiry.N)rO   _refresh_oauth_token_if_neededrF   s    r)   r/   r/      s     "E})%00r2   rF   c           	        ^  [            [        R                  " 5       n[        nUb  US   T :X  a  XS   :  a  US   sSSS5        $ [        U 4S j[	        5       R                  5        5       S0 45      u  p4UR                  S5      n[        U5      nUb  Ub  Uc6  [        5       nUb  UT :w  a  UsSSS5        $ T T U[        -   S.qT sSSS5        $ U[        -
  U:  a  T T U[        -
  S.qT sSSS5        $  [        [        R                  S-   S	S
9   [	        5       R                  U0 5      nUR                  S5      T :w  a&  UR                  S5      =(       d    T n[        U5      n	O[        U5      n
U
S   nSU
;   a  [        U5      [        U
S   5      -   OSn	[!        UUU
R                  S5      =(       d    UU	S9  [        5       T :X  a#  [#        [%        [        R&                  5      U5        [(        R+                  SU S35        SSS5        WU[?        U[        -   W	(       a	  U	[        -
  OS5      S.qUsSSS5        $ ! , (       d  f       N@= f! [,         a  n[/        U[0        5      (       aC  UR2                  [4        R6                  :X  a%  [(        R9                  SU S35        [;        S5      nO[=        SU S35        U[        -   nT T US.qT s SnAsSSS5        $ SnAff = f! , (       d  f       g= f)a  Refresh an OAuth access token if it is close to expiry. Best-effort: never raises.

OAuth tokens obtained with the browser-based login are stored with a `refresh_token` and an
`expires_at` timestamp (see `_save_token`). When the active token is one of them and about to
expire, exchange the refresh token for a new access token and persist it. Any other token is
returned unchanged.
Nrs   ru   rt   c              3   \   >#    U  H!  u  pUR                  S 5      T:X  d  M  X4v   M#     g7f)hf_tokenN)r=   ).0namefieldsrF   s      r)   	<genexpr>1_refresh_oauth_token_if_needed.<locals>.<genexpr>  s.     v0RV\V`V`akVlpuVu^d^0Rs   ,
,refresh_token)rs   rt   ru   z.lock   )timeoutr|   rd   re   )rF   
token_namer   rT   zAccess token `z` has been refreshed.zYour Hugging Face access token has expired and could not be refreshed (session expired or revoked). Run `hf auth login` to re-authenticate. ()infz2Could not refresh your Hugging Face access token: z. Will retry later.r   ) _OAUTH_REFRESH_LOCKrh   rv   next_read_stored_tokens_fullitemsr=   _parse_expires_atrO   _OAUTH_RECHECK_INTERVAL_OAUTH_REFRESH_MARGINr   r   HF_STORED_TOKENS_PATHr   rj   _save_tokenr*   r   rL   rC   rD   	Exception
isinstancer   
error_coder	   INVALID_GRANTwarningr[   _warn_refresh_failure_oncerl   )rF   rm   cacher   r   r   rT   current_file_token	new_tokennew_expires_atresponserG   ru   s   `            r)   rx   rx      s    
iik$|!4!=#lH[B[)*	 
	 "v0H0J0P0P0Rv2J

 

?3&v.
!6*:L "6!7!-2D2M)# 
	( $"'!$;;$ 
 1 
	4 --3#"'(+@@$ 
 A 
	D)	 i==GQST1377
BG::j)U2 &

: 6 ?%I%6v%>N3MBH ( 8IO[_gOgSXH\4J0K%KmqN'#-&.ll?&C&T}#1 ,-6%d9+B+B&CYOKK.<Q RS+ UR $' --:H!66a	 
 o 
	J UT,  	!_--!,,.B^B^2^ ^^_]``ac #5\
 +-_`a_bbu+vw #::
275`j#k LW 
	v	w 
	st   4K+A)K+9K+K+7IC6II(K+
I	IK+I
K(A7K#K(K+#K((K++
K9messagec                 J    [         (       d  [        R                  U 5        Sq g g )NT)_OAUTH_REFRESH_WARNEDrC   r   )r   s    r)   r   r   W  s      w $ !r2   r   c                 L     [        U S   5      $ ! [        [        4 a     gf = f)zVParse the `expires_at` field of a stored-tokens section, `None` if missing or corrupt.rT   N)rj   KeyError
ValueError)r   s    r)   r   r   ^  s.    6,'((j! s    ##c            	          [        5       R                  5        V Vs0 s H  u  pXR                  SS5      _M     snn $ s  snn f )a)  
Returns the parsed INI file containing the access tokens.
The file is located at `HF_STORED_TOKENS_PATH`, defaulting to `~/.cache/huggingface/stored_tokens`.
If the file does not exist, an empty dictionary is returned.

Returns: `dict[str, str]`
    Key is the token name and value is the token.
r|    )r   r   r=   )r   r   s     r)   get_stored_tokensr   f  s=     NfMgMmMmMopMo7IzJ

:r22Moppps   >c            	         [        [        R                  5      n U R                  5       (       d  0 $ [        R
                  " SS9n UR                  U 5        UR                  5        Vs0 s H  o"[        UR                  U5      5      _M     sn$ s  snf ! [        R                   a$  n[        R                  SU 35        0 s SnA$ SnAff = f)zRead all sections of the stored tokens INI file, with all their fields.

Beside `hf_token`, sections for OAuth tokens also carry `refresh_token` and `expires_at`
(unix timestamp), used by [`get_token`] to transparently refresh them.
Ninterpolationz"Error parsing stored tokens file: )r   r   r   existsconfigparserConfigParserreadsectionsdictr   r6   rC   error)tokens_pathconfigr   rG   s       r)   r   r   r  s     y667K	&&T:FK MS__M^_M^zDj!9::M^___ 9!=>	s0   #B )$BB B C)CCCstored_tokensc                    [         R                  " SS9n[        U R                  5       5       HA  nUR	                  U5        X   R                  5        H  u  p4UR                  X#U5        M     MC     [        R                  " 5       nUR                  U5        [        [        [        R                  5      UR                  5       5        g)zBWrite all sections and their fields to the stored tokens INI file.Nr   )r   r   sortedkeysadd_sectionr   setioStringIOr#   r*   r   r   r   getvalue)r   r   r   keyvaluebufs         r)   _save_stored_tokens_fullr     s    &&T:F]//12
:&'399;JCJJz. < 3
 ++-C
LL$y667Hr2   r   c                 <    [        5       nX;  a  g[        X   5      $ )z
Get the token by name.

Args:
    token_name (`str`):
        The name of the token to get.

Returns:
    `str` or `None`: The token, `None` if it doesn't exist.

N)r   r>   )r   r   s     r)   _get_token_by_namer     s#     &'M&122r2   )r   rT   r   rT   c                    [        5       nSU 0nUb  X%S'   Ub  [        U5      US'   XTU'   [        U5        [        R	                  SU S[
        R                   35        g)a  
Save the given token.

If the stored tokens file does not exist, it will be created.
Args:
    token (`str`):
        The token to save.
    token_name (`str`):
        The name of the token.
    refresh_token (`str`, *optional*):
        OAuth refresh token used to renew the access token when it expires.
    expires_at (`int`, *optional*):
        Unix timestamp at which the access token expires.
r|   Nr   rT   zThe token `z` has been saved to )r   r   r   rC   rD   r   r   )rF   r   r   rT   r   r   s         r)   r   r     si    " -.M% F "/":| &*]+
KK+j\)=i>]>]=^_`r2   c                 |    U c  gU R                  SS5      R                  SS5      R                  5       =(       d    S$ )zmClean token by removing trailing and leading spaces and newlines.

If token is an empty string, return None.
Nr   
)replacestripry   s    r)   r>   r>     s8    
 }==r"**44::<DDr2   )@__doc__r   r   loggingr   rh   r@   pathlibr   	threadingr   typingr   r   r   errorsr   r	   r
   _fixesr   _oauth_devicer   _runtimer   r   r!   r   r   r*   r9   r8   r+   rZ   	getLoggerrV   rC   r3   r0   r.   rO   rQ   rg   r]   rk   r-   rq   r   rv   r   r   r   r/   rx   r   r   rj   r   r   r   r   r   r   r>   r1   r2   r)   <module>r      sy   ]  	  	       ? ?   / :    s t  !  F #' cDj '			8	$3: :7$cDj 7$t`S4Z `
cDj i  6 ,0 ?T) 0 0cDj 0f  f 26 (4/ 6!   1d
 1`# `# `F% % %d38n t 	q4S> 	q$sDcN':"; &
IDd38n1D,E 
I$ 
I33 33: 3& AE_caaa36:aRUX\R\a	a:Ed
 EsTz Er2   