
    3jh/                        % 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  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'   \R8                  " \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\(\\4   4S jr)S\(\\4   SS4S jr*S\S\S-  4S  jr+S!\S\SS4S" jr,S!\S-  S\S-  4S# jr-g)$zVContains a helper to get the token from machine (env variable, secret or config file).    N)Path)Lock)	TypedDict   )	constants)	OIDCError   )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       U/home/wildlama/miniconda3/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       $ )am  
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`.

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_get_token_from_google_colab     r%   	get_tokenr/   8   s2    & 	 	*&(	*!	* ()	r.   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.a  
The secret `HF_TOKEN` does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.z@
Error while fetching `HF_TOKEN` secret value from your vault: 'z'.
You are not authenticated with the Hugging Face Hub in this notebook.
If the error persists, please let us know by opening an issue on GitHub (https://github.com/huggingface/huggingface_hub/issues/new).T)r   r
   _GOOGLE_COLAB_SECRET_LOCK_IS_GOOGLE_COLAB_CHECKEDr'   google.colabr1   google.colab.errorsr2   ImportErrorget_clean_tokenNotebookAccessErrorwarningswarnSecretNotFoundErrorr   )r1   
ColabErrortokenes       r%   r,   r,   R   s'     3 5 5 
# $#' 
#	"	-?	(LL,E#/#6 < $( #] 
#	"  	 
#	"	 ++ 	( MMQ
 $( ++ 		(MM{ $(  	(MMSTWXYTZS[ \O O $( 	(G 
#	"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      $ )Nr3   HUGGING_FACE_HUB_TOKEN)r:   r   environr9   r-   r.   r%   r*   r*      s-    

z2^bjjnnE]6^__r.   c                       [        [        [        R                  5      R	                  5       5      $ ! [
         a     g f = f)N)r:   r   r   HF_TOKEN_PATH	read_textFileNotFoundErrorr-   r.   r%   r+   r+      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   resourcer@   
expires_atr-   N)__name__
__module____qualname____firstlineno__r   __annotations__float__static_attributes__r-   r.   r%   rJ   rJ      s    MJr.   rJ   _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_loginrL   rM   r@   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.)rL   subject_tokenaccess_token
expires_ini  r   )rL   r@   rM   )r   rD   r9   _oidcrX   rY   _OIDC_TOKEN_LOCKtime	monotonicrU   r   int_OIDC_REFRESH_MARGINmax)	rL   rX   rY   nowr[   resultr@   r]   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                     [        [        R                  5      n U R                  5       (       d  0 n[        R
                  " 5       n UR                  U 5        UR                  5        Vs0 s H  o3UR                  US5      _M     nnU$ s  snf ! [        R                   a%  n[        R                  SU 35        0 n SnAU$ SnAff = 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.
hf_tokenz"Error parsing stored tokens file: N)r   r   HF_STORED_TOKENS_PATHexistsconfigparserConfigParserreadsectionsr9   r2   loggererror)tokens_pathstored_tokensconfig
token_namerA   s        r%   get_stored_tokensrv      s     y667K&&(FK Z`ZiZiZklZkJVZZ
J%GGZkl 	 m 9!=>s*   #B *B
B 
B C#CCrs   c                 j   [        [        R                  5      n[        R                  " 5       n[        U R                  5       5       H)  nUR                  U5        UR                  USX   5        M+     [        R                  " 5       nUR                  U5        [        XR                  5       5        g)z
Saves the given configuration to the stored tokens file.

Args:
    stored_tokens (`dict[str, str]`):
        The stored tokens to save. Key is the token name and value is the token.
ri   N)r   r   rj   rl   rm   sortedkeysadd_sectionsetioStringIOr   r&   getvalue)rs   stored_tokens_pathrt   ru   bufs        r%   _save_stored_tokensr      s     i==> &&(F]//12
:&

:z=+DE 3 ++-C
LL$lln5r.   ru   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)rv   r:   )ru   rs   s     r%   _get_token_by_namer   	  s#     &'M&122r.   r@   c                     [        [        R                  5      n[        5       nXU'   [	        U5        [
        R                  SU SU 35        g)z
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.
zThe token `z` has been saved to N)r   r   rj   rv   r   rp   info)r@   ru   rr   rs   s       r%   _save_tokenr     sF     y667K%'M %*&
KK+j\)=k]KLr.   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 
)replacestrip)r@   s    r%   r:   r:   -  s8    
 }==r"**44::<DDr.   ).__doc__rl   r|   loggingr   r`   r<   pathlibr   	threadingr   typingr   r   r   errorsr   _runtimer
   r   r   r   r   r&   r5   r4   r'   rR   	getLoggerrN   rp   r/   r,   r*   r+   rJ   r_   rU   rc   r)   dictrv   r   r   r   r:   r-   r.   r%   <module>r      s   ]  	  	        :    s t  !  F #' cDj '			8	$3: 4=$cDj =$@`S4Z `
cDj i  6 ,0 ?T) 0 0cDj 0f4S> ,6tCH~ 6$ 6*33 33: 3$Ms M M M$Ed
 EsTz Er.   