
    5pj/                        S r SSKrSSKJr  SSKJrJr  SSKrSSKJ	r	  SSK
JrJr  SS	KJrJr  S
rSr " S S\5      r " S S\SS9rS\4S jrSS.S\S\/ S4   S-  S\4S jjrS\S\4S jrS\R0                  S\4S jrg)aA  Device Code OAuth (RFC 8628) for browser-based login, plus access token refresh.

The flow: the client requests a device code, displays a URL and a short user code, the user
authorizes in a browser, and the client polls ``POST {ENDPOINT}/oauth/token`` until a token is
issued. Access tokens may come with a refresh token, used to renew them transparently (see
``utils/_auth.py::get_token``).

This module is self-contained protocol logic: no printing, no persistence. Interactive flows
live in ``_login.py`` (human/library) and ``cli/auth.py`` (machine-readable event stream).
    N)Callable)	TypedDictcast   )	constants)DeviceCodeErrorOAuthErrorCode   )get_sessionhf_raise_for_statusz,urn:ietf:params:oauth:grant-type:device_coderefresh_tokenc                   R    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   Srg	)
DeviceCodeInfo(   device_code	user_codeverification_uriverification_uri_completeinterval
expires_in N__name__
__module____qualname____firstlineno__str__annotations__int__static_attributes__r       _/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/huggingface_hub/utils/_oauth_device.pyr   r   (   s#    N""MOr!   r   c                   >    \ rS rSr% \\S'   \\S'   \\S'   \\S'   Srg)OAuthTokenResponse1   access_tokenr   r   
token_typer   Nr   r   r!   r"   r$   r$   1   s    OOr!   r$   F)totalreturnc                      [        5       R                  [        R                   S3S[        R                  0[        R
                  S9n [        U 5        U R                  5       nUR                  SS5        UR                  S	S
5        UR                  S5      (       d  US   US'   [        [        U5      $ ! [        R                   a%  n[        S[        R                   SU 35      UeSnAff = f)a  Request a device code from the Hub's OAuth device authorization endpoint.

The returned dict is normalized: `interval`, `expires_in` and `verification_uri_complete`
are always set (server values, or sensible defaults).

Raises:
    [`DeviceCodeError`]: If the request fails.
z/oauth/device	client_iddatatimeoutz#Failed to request device code from z/oauth/device: Nr      r   i  r   r   )r   postr   ENDPOINTDEVICE_CODE_OAUTH_CLIENT_IDHF_HUB_DOWNLOAD_TIMEOUTr   httpx	HTTPErrorr   json
setdefaultgetr   r   )responseeinfos      r"   request_device_coder<   8   s    s=%%!!"-0yDDE55 & 

 	H% ==?D 	OOJ"OOL#&88/00,01C,D()%% ?? s CIDVDVCWWfghfijkqrrss   AB7 7C0 C++C0)
on_pendingdevice_infor=   c          	         U S   n[         R                  " 5       U S   -   n[         R                  " 5       U:  Ga  Sn [        5       R                  [        R
                   S3[        U S   [        R                  S.[        R                  S9nUR                  S:  a  UR                  5       nUb  S	U;   a  [        [        U5      $ UR!                  S
5      =c    O=["        R$                  :X  a    Ub  U" 5         O=["        R&                  :X  a    US-  nO{=["        R(                  :X  a    [+        S["        R(                  S9e=["        R,                  :X  a    [+        S["        R,                  S9en[+        SU SUR!                  SS5       3US9e[         R.                  " U5        [         R                  " 5       U:  a  GM  [+        S["        R(                  S9e! [        R                  [        4 a     GNKf = f)aG  Poll the token endpoint until the user authorizes the device.

Args:
    device_info (`DeviceCodeInfo`):
        The device authorization response from [`request_device_code`].
    on_pending (`Callable`, *optional*):
        Called after each "authorization pending" response (e.g. to print a progress dot).

Returns:
    `OAuthTokenResponse`: the full token response: `access_token`, and optionally
    `refresh_token` and `expires_in`.

Raises:
    [`DeviceCodeError`]: If authorization is denied, the device code expires, or the server
        returns an unexpected OAuth error.
r   r   N/oauth/tokenr   )
grant_typer   r+   r,     r&   errorr/   z&Device code expired. Please try again.
error_codez+Authorization was denied. Please try again.zOAuth error:  - error_description z0Device code expired (timeout). Please try again.)time	monotonicr   r0   r   r1   _DEVICE_CODE_GRANT_TYPEr2   r3   status_coder6   r4   r5   
ValueErrorr   r$   r8   r	   AUTHORIZATION_PENDING	SLOW_DOWNEXPIRED_TOKENr   ACCESS_DENIEDsleep)r>   r=   r   deadliner-   r9   rC   s          r"   poll_device_tokenrT   T   s   & :&H~~+l";;H
..
X
% 	"}))%%&l3"9#.}#=!*!F!F
 "99 * H ##c)}} %.55((7#9^99!-"-^--MH1^11)@^MiMi  2^11)ER`RnRn  )'wc$((;NPR2S1TUbg  	

8[ ..
X
%^ LYgYuYu
vv= , 		s   A1G G;:G;c           	          [        5       R                  [        R                   S3[        U [        R
                  S.[        R                  S9n[        U5      nSU;   a  [        [        U5      $ UR                  S5      n[        SU=(       d    UR                   SUR                  S	S
5       3US9e! [        R                   a  n[        SU 35      UeSnAff = f)ax  Exchange a refresh token for a new access token.

Returns:
    `OAuthTokenResponse`: the full token response: `access_token`, and optionally a rotated
    `refresh_token` and `expires_in`.

Raises:
    [`DeviceCodeError`]: If the server rejects the refresh (`error_code="invalid_grant"` when
        the refresh token is expired or revoked) or returns an unexpected response.
r@   )rA   r   r+   r,   z Failed to refresh access token: Nr&   rC   rF   rG   rH   rD   )r   r0   r   r1   _REFRESH_TOKEN_GRANT_TYPEr2   r3   r4   r5   r   _parse_token_responser   r$   r8   rL   )r   r9   r:   r-   rC   s        r"   refresh_access_tokenrX      s    M=%%!!",/7!.&BB 55 & 

 !*D&--HHWE

*5+HH4H4H*ITXXVikmMnLop  ?? M @DE1LMs   AB5 5C	CCr9   c                      U R                  5       $ ! [         a?  n[        S[        R                   SU R
                   SU R                  S S  35      UeS nAff = f)NzFailed to parse response from z/oauth/token (status z): rB   )r6   rM   r   r   r1   rL   text)r9   r:   s     r"   rW   rW      sm    }} ,Y-?-?,@ A++,Cds0C/DF
 	s    
A:AA)__doc__rI   collections.abcr   typingr   r   r4   rH   r   errorsr   r	   _httpr   r   rK   rV   r   r$   r<   rT   r   rX   ResponsedictrW   r   r!   r"   <module>rb      s   	  $ "   4 3 I + Y % &^ &: MQDwDw08T0BT0IDwDwN   0B  FENN t r!   