§
    }Štj/  ã                   ó  — 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j        defd„ZdS )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                   óL   — e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   dS )ÚDeviceCodeInfoÚdevice_codeÚ	user_codeÚverification_uriÚverification_uri_completeÚintervalÚ
expires_inN©Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__Úint© ó    úa/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/huggingface_hub/utils/_oauth_device.pyr   r   (   sO   € € € € € € ØÐÐÑØ€N€N�NØÐÐÑØ"Ð"Ð"Ñ"Ø€M€M�MØ€O€O�O€O€Or   r   c                   ó8   — e Zd ZU eed<   eed<   eed<   eed<   dS )ÚOAuthTokenResponseÚaccess_tokenr   r   Ú
token_typeNr   r   r   r   r!   r!   1   s:   € € € € € € ØÐÐÑØÐÐÑØ€O€O�OØ€O€O�O€O€Or   r!   F)ÚtotalÚreturnc                  ó
  — 	 t          ¦   «                              t          j        › d�dt          j        it          j        ¬¦  «        } t          | ¦  «         n7# t          j        $ r%}t          dt          j        › d|› �¦  «        |‚d}~ww xY w|  
                    ¦   «         }|                     dd¦  «         |                     d	d
¦  «         |                     d¦  «        s|d         |d<   t          t          |¦  «        S )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Ð0Ð0Ø�yÔDÐEÝÔ5ð &ñ 
ô 
ˆõ
 	˜HÑ%Ô%Ð%Ð%øÝŒ?ð sð sð sÝÐjÅIÔDVÐjÐjÐghÐjÐjÑkÔkÐqrÐrøøøøðsøøøà�=Š=‰?Œ?€Dð 	‡O‚O�J Ñ"Ô"Ð"Ø‡O‚O�L #Ñ&Ô&Ð&Ø�8Š8Ð/Ñ0Ô0ð EØ,0Ð1CÔ,DˆÐ(Ñ)Ý• Ñ%Ô%Ð%s   ‚AA ÁBÁ( BÂB)Ú
on_pendingÚdevice_infor9   c          	      ó  — | d         }t          j        ¦   «         | d         z   }t          j        ¦   «         |k     �r«d}	 t          ¦   «                              t          j        › d�t          | d         t          j        dœt          j        ¬¦  «        }|j	        dk     r| 
                    ¦   «         }n# t          j        t          f$ r Y nw xY w|�êd	|v rt          t          |¦  «        S |                     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|                     dd¦  «        › �|¬¦  «        ‚t          j        |¦  «         t          j        ¦   «         |k     �°«t+          dt"          j        ¬¦  «        ‚)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Ò
%Ñ
%ð ˆð	Ý"‘}”}×)Ò)ÝÔ%Ð3Ð3Ð3å"9Ø#.¨}Ô#=Ý!*Ô!Fðð õ
 "Ô9ð *ñ ô ˆHð Ô# cÒ)Ð)Ø—}’}‘”�øøÝ”¥Ð,ð 	ð 	ð 	ØˆDð	øøøð ÐØ Ð%Ð%ÝÕ.°Ñ5Ô5Ð5à—(’(˜7Ñ#Ô#Ø�T�TØØ9•^Ô9Ò9Ð9Ð9Ø!Ð-Ø"˜
™œ˜øØ-•^Ô-Ò-Ð-Ð-Ø ‘M�H�HØ1•^Ô1Ò1Ð1Ð1Ý)Ø@Í^ÔMiðñ ô ð ð 2•^Ô1Ò1Ð1Ð1Ý)ØEÕR`ÔRnðñ ô ð ð Ý)ØU¨ÐUÐU°$·(²(Ð;NÐPRÑ2SÔ2SÐUÐUÐbgðñ ô ð õ 	Œ
�8ÑÔÐõ[ Œ.Ñ
Ô
˜XÒ
%Ñ
%õ^ ÐLÕYgÔYuÐ
vÑ
vÔ
vÐvs   Á A3B4 Â4CÃCc           	      óÔ  — 	 t          ¦   «                              t          j        › d�t          | t          j        dœt          j        ¬¦  «        }n*# t          j        $ r}t          d|› �¦  «        |‚d}~ww xY wt          |¦  «        }d|v rt          t          |¦  «        S |                     d¦  «        }t          d|p|j        › d|                     d	d
¦  «        › �|¬¦  «        ‚)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ðð õ Ô5ð &ñ 

ô 

ˆˆøõ Œ?ð Mð Mð MÝÐDÀÐDÐDÑEÔEÈ1ÐLøøøøðMøøøå  Ñ*Ô*€DØ˜ÐÐÝÕ&¨Ñ-Ô-Ð-Ø�HŠH�WÑÔ€EÝ
Øp¨5Ð+H°HÔ4HÐpÐpÈTÏXÊXÐViÐkmÑMnÔMnÐpÐpØðñ ô ð s   ‚AA ÁA8Á A3Á3A8r5   c                 óº   — 	 |                       ¦   «         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   ¾   sŽ   € ðØ�}Š}‰ŒÐøÝð ð ð ÝðF­YÔ-?ð Fð FØÔ+ðFð FØ08´¸d¸s¸dÔ0CðFð Fñ
ô 
ð ð	øøøøðøøøs   ‚ –
A 5AÁ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Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3ð IÐ Ø+Ð ðð ð ð ð �Yñ ô ð ðð ð ð ð ˜¨%ð ñ ô ð ð&˜^ð &ð &ð &ð &ð: MQðDwð Dwð DwØðDwØ08¸¸T¸Ô0BÀTÑ0IðDwàðDwð Dwð Dwð DwðN ¨ð  Ð0Bð  ð  ð  ð  ðF E¤Nð °tð ð ð ð ð ð r   