Ë
    ÂÓ^j/  ã                   ó  — d Z ddlZddlmZ ddlmZmZ ddlZddlm	Z	 ddl
mZmZ dd	lmZmZ d
ZdZ G d„ de«      Z G d„ ded¬«      Zdefd„Zddœdedeg df   dz  defd„Zdedefd„Zdej0                  defd„Zy)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                   óJ   — e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   y)ÚDeviceCodeInfoÚdevice_codeÚ	user_codeÚverification_uriÚverification_uri_completeÚintervalÚ
expires_inN©Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__Úint© ó    ún/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/huggingface_hub/utils/_oauth_device.pyr   r   (   s#   … ØÓØƒNØÓØ"Ó"ØƒMØ„Or   r   c                   ó6   — e Zd ZU eed<   eed<   eed<   eed<   y)ÚOAuthTokenResponseÚaccess_tokenr   r   Ú
token_typeNr   r   r   r   r!   r!   1   s   … ØÓØÓØƒOØ„Or   r!   F)ÚtotalÚreturnc                  óà  — 	 t        «       j                  t        j                  › d�dt        j                  it        j
                  ¬«      } t        | «       | j                  «       }|j                  dd«       |j                  d	d
«       |j                  d«      s|d   |d<   t        t        |«      S # t        j                  $ r%}t        dt        j                  › d|› �«      |‚d}~ww xY w)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_coder8   8   så   € ðsÜ“=×%Ñ%Ü×!Ñ!Ð" -Ð0Øœy×DÑDÐEÜ×5Ñ5ð &ó 
ˆô
 	˜HÔ%ð �=‰=‹?€Dð 	‡O�O�J Ô"Ø‡O�O�L #Ô&Ø�8‰8Ð/Ô0Ø,0Ð1CÑ,DˆÐ(Ñ)Ü” Ó%Ð%øô �?‰?ò sÜÐ CÄI×DVÑDVÐCWÐWfÐghÐfiÐjÓkÐqrÐrûðsús   ‚AB5 Â5C-Ã C(Ã(C-)Ú
on_pendingÚdevice_infor9   c          	      óö  — | d   }t        j                  «       | d   z   }t        j                  «       |k  �rˆd}	 t        «       j                  t        j
                  › d�t        | d   t        j                  dœt        j                  ¬«      }|j                  dk  r|j                  «       }|�âd	|v rt        t        |«      S |j!                  d
«      x€ nºxt"        j$                  k(  r |�¤ |«        nœxt"        j&                  k(  r |dz  }n‚xt"        j(                  k(  r t+        dt"        j(                  ¬«      ‚xt"        j,                  k(  r t+        dt"        j,                  ¬«      ‚}t+        d|› d|j!                  dd«      › �|¬«      ‚t        j.                  |«       t        j                  «       |k  r�Œˆt+        dt"        j(                  ¬«      ‚# t        j                  t        f$ r Y �ŒIw xY w)aw  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   r,   r   r-   Ú_DEVICE_CODE_GRANT_TYPEr.   r/   Ústatus_coder2   r0   r1   Ú
ValueErrorr   r!   r4   r	   ÚAUTHORIZATION_PENDINGÚ	SLOW_DOWNÚEXPIRED_TOKENr   ÚACCESS_DENIEDÚsleep)r:   r9   r   Údeadliner)   r5   r?   s          r   Úpoll_device_tokenrP   T   sÑ  € ð& ˜:Ñ&€HÜ�~‰~Ó +¨lÑ";Ñ;€HÜ
�.‰.Ó
˜XÓ
%ð ˆð	Ü"“}×)Ñ)Ü×%Ñ%Ð& lÐ3ä"9Ø#.¨}Ñ#=Ü!*×!FÑ!Fñô
 "×9Ñ9ð *ó ˆHð ×#Ñ# cÒ)Ø—}‘}“�ð ÐØ Ñ%ÜÔ.°Ó5Ð5à—(‘(˜7Ó#ÚØØ9”^×9Õ9Ø!Ð-Ù"�Ø-”^×-Õ-Ø ‘M‘HØ1”^×1Õ1Ü)Ø@Ì^×MiÑMiôð ð 2”^×1Õ1Ü)ØEÔR`×RnÑRnôð ð Ü)Ø'¨ w¨c°$·(±(Ð;NÐPRÓ2SÐ1TÐUÐbgôð ô 	�
‰
�8Ôô[ �.‰.Ó
˜XÔ
%ô^ ÐLÔYg×YuÑYuÔ
vÐvøô= —‘¤Ð,ò 	Úð	ús   »A3G ÇG8Ç7G8c           	      ó¼  — 	 t        «       j                  t        j                  › d�t        | t        j
                  dœt        j                  ¬«      }t        |«      }d|v rt        t        |«      S |j                  d«      }t        d|xs |j                  › d|j                  d	d
«      › �|¬«      ‚# t        j                  $ r}t        d|› �«      |‚d}~ww xY w)a”  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<   )r=   r   r'   r(   z Failed to refresh access token: Nr"   r?   rB   rC   rD   r@   )r   r,   r   r-   Ú_REFRESH_TOKEN_GRANT_TYPEr.   r/   r0   r1   r   Ú_parse_token_responser   r!   r4   rH   )r   r5   r6   r)   r?   s        r   Úrefresh_access_tokenrT   ›   sæ   € ðMÜ“=×%Ñ%Ü×!Ñ!Ð" ,Ð/ä7Ø!.Ü&×BÑBñô ×5Ñ5ð &ó 

ˆô ! Ó*€DØ˜ÑÜÔ&¨Ó-Ð-Ø�H‰H�WÓ€EÜ
Ø
*¨5Ò+H°H×4HÑ4HÐ*IÈÈTÏXÉXÐViÐkmÓMnÐLoÐpØôð øô �?‰?ò MÜÐ @ÀÀÐDÓEÈ1ÐLûðMús   ‚AB4 Â4CÃCÃCr5   c                 óº   — 	 | j                  «       S # t        $ r?}t        dt        j                  › d| j
                  › d| j                  d d › �«      |‚d }~ww xY w)NzFailed to parse response from z/oauth/token (status z): r>   )r2   rI   r   r   r-   rH   Útext)r5   r6   s     r   rS   rS   ¾   sm   € ðØ�}‰}‹ÐøÜò ÜØ,¬Y×-?Ñ-?Ð,@ð AØ×+Ñ+Ð,¨C°·±¸d¸sÐ0CÐ/DðFó
ð ð	ûðús   ‚ ’	A›:AÁA)Ú__doc__rE   Úcollections.abcr   Útypingr   r   r0   rD   r   Úerrorsr   r	   Ú_httpr   r   rG   rR   r   r!   r8   rP   r   rT   ÚResponseÚdictrS   r   r   r   ú<module>r^      s½   ðñ	ó Ý $ß "ã å ß 4ß 3ð IÐ Ø+Ð ô�Yô ô˜¨%õ ð&˜^ó &ð: MQòDwØðDwØ08¸¸T¸Ñ0BÀTÑ0IðDwàóDwðN ¨ð  Ð0Bó  ðF E§N¡Nð °tô r   