Ë
    Fêñi’  ã                  ó^   — d dl mZ d dlmZmZmZmZ d dlmZm	Z	m
Z
mZ e› d�Z G d„ d«      Zy)é    )Úannotations)ÚHUB_API_ROOTÚHUB_WEB_ROOTÚPREFIXÚrequest_with_credentials)ÚIS_COLABÚLOGGERÚSETTINGSÚemojisz/settings?tab=api+keysc                  óF   — e Zd ZdZdxZxZZd	d
d„Zddd„Zdd„Z	dd„Z
d„ Zy)ÚAuthau  Manages authentication processes including API key handling, cookie-based authentication, and header generation.

    The class supports different methods of authentication:
    1. Directly using an API key.
    2. Authenticating using browser cookies (specifically in Google Colab).
    3. Prompting the user to enter an API key.

    Attributes:
        id_token (str | bool): Token used for identity verification, initialized as False.
        api_key (str | bool): API key for authentication, initialized as False.
        model_key (bool): Placeholder for model key, initialized as False.

    Methods:
        authenticate: Attempt to authenticate with the server using either id_token or API key.
        auth_with_cookies: Attempt to fetch authentication via cookies and set id_token.
        get_auth_header: Get the authentication header for making API requests.
        request_api_key: Prompt the user to input their API key.

    Examples:
        Initialize Auth with an API key
        >>> auth = Auth(api_key="your_api_key_here")

        Initialize Auth without API key (will prompt for input)
        >>> auth = Auth()
    Fc                óH  — |j                  dd«      d   }|xs t        j                  dd«      | _        | j                  rR| j                  t        j                  d«      k(  r|rt	        j
                  t        › d�«       y| j                  «       }n't        r| j                  «       }n| j                  «       }|rAt        j                  d| j                  i«       |rt	        j
                  t        › d�«       yy|r$t	        j
                  t        › d	t        › d
�«       yy)aG  Initialize Auth class and authenticate user.

        Handles API key validation, Google Colab authentication, and new key requests. Updates SETTINGS upon successful
        authentication.

        Args:
            api_key (str): API key or combined key_id format.
            verbose (bool): Enable verbose logging.
        Ú_é   r   Úapi_keyÚ u   Authenticated âœ…Nu!   New authentication successful âœ…zGet API key from z" and then run 'yolo login API_KEY')Úsplitr
   Úgetr   r	   Úinfor   Úauthenticater   Úauth_with_cookiesÚrequest_api_keyÚupdateÚAPI_KEY_URL)Úselfr   ÚverboseÚsuccesss       úV/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/hub/auth.pyÚ__init__zAuth.__init__(   sô   € ð —-‘-  QÓ'¨Ñ*ˆð Ò=¤(§,¡,¨y¸"Ó"=ˆŒð �<Š<à�|‰|œxŸ|™|¨IÓ6Ò6áÜ—K‘K¤6 (Ð*;Ð <Ô=Øð ×+Ñ+Ó-‘åà×,Ñ,Ó.‰Gð ×*Ñ*Ó,ˆGñ Ü�O‰O˜Y¨¯©Ð5Ô6áÜ—‘œv˜hÐ&GÐHÕIð áÜ�K‰Kœ6˜(Ð"3´K°=Ð@bÐcÕdð ó    c                ó2  — ddl }t        |«      D ]k  }t        j                  t        › d|dz   › d|› �«       |j                  dt
        › d�«      }|j                  dd«      d   | _        | j                  «       sŒk y	 t        t        t        › d
�«      «      ‚)zçPrompt the user to input their API key.

        Args:
            max_attempts (int): Maximum number of authentication attempts.

        Returns:
            (bool): True if authentication is successful, False otherwise.
        r   NzLogin. Attempt r   z of zEnter API key from ú r   Tu   Failed to authenticate â�Œ)ÚgetpassÚranger	   r   r   r   r   r   r   ÚConnectionErrorr   )r   Úmax_attemptsr#   ÚattemptsÚ	input_keys        r   r   zAuth.request_api_keyT   s”   € ó 	ä˜lÓ+ò 	ˆHÜ�K‰Kœ6˜( /°(¸Q±,°¸tÀLÀ>ÐRÔSØŸ™Ð*=¼k¸]È!Ð(LÓMˆIØ$Ÿ?™?¨3°Ó2°1Ñ5ˆDŒLØ× Ñ Õ"Ùð	ô œf¬ xÐ/IÐ%JÓKÓLÐLr    c                óD  — ddl }	 | j                  «       x}rF|j                  t        › d�|¬«      }|j	                  «       j                  dd«      st        d«      ‚yt        d	«      ‚# t        $ r- dx| _        | _        t        j                  t        › d
�«       Y yw xY w)z¯Attempt to authenticate with the server using either id_token or API key.

        Returns:
            (bool): True if authentication is successful, False otherwise.
        r   Nz/v1/auth)Úheadersr   FzUnable to authenticate.Tz#User has not authenticated locally.zInvalid API key)ÚrequestsÚget_auth_headerÚpostr   Újsonr   r%   Úid_tokenr   r	   Úwarningr   )r   r+   ÚheaderÚrs       r   r   zAuth.authenticateg   s›   € ó 	ð
	Ø×-Ñ-Ó/Ð/ˆvÐ/Ø—M‘M¤\ N°(Ð";ÀV�MÓL�Ø—v‘v“x—|‘| I¨uÔ5Ü)Ð*CÓDÐDØÜ!Ð"GÓHÐHøÜò 	Ø+0Ð0ˆDŒM˜DœLÜ�N‰Nœf˜X _Ð5Ô6Ùð	ús   †AA) ÁA) Á)3BÂBc                ó  — t         sy	 t        t        › d�«      }|j                  dd«      r8|j                  di «      j                  dd«      | _        | j                  «        yt        d«      ‚# t        $ r
 d| _        Y yw xY w)	zîAttempt to fetch authentication via cookies and set id_token.

        User must be logged in to HUB and running in a supported browser.

        Returns:
            (bool): True if authentication is successful, False otherwise.
        Fz/v1/auth/autor   ÚdataÚidTokenNTz/Unable to fetch browser authentication details.)r   r   r   r   r/   r   r%   )r   Úauthns     r   r   zAuth.auth_with_cookies{   s�   € õ Øð		Ü,´¨~¸]Ð-KÓLˆEØ�y‰y˜ EÔ*Ø %§	¡	¨&°"Ó 5× 9Ñ 9¸)ÀTÓ J�”Ø×!Ñ!Ô#ØÜ!Ð"SÓTÐTøÜò 	Ø!ˆDŒMÙð	ús   ‰AA0 Á%A0 Á0BÂBc                ór   — | j                   rdd| j                   › �iS | j                  rd| j                  iS y)zµGet the authentication header for making API requests.

        Returns:
            (dict | None): The authentication header if id_token or API key is set, None otherwise.
        ÚauthorizationzBearer z	x-api-keyN)r/   r   )r   s    r   r,   zAuth.get_auth_header�   s<   € ð �=Š=Ø# w¨t¯}©}¨oÐ%>Ð?Ð?Ø�\Š\Ø §¡Ð.Ð.ð r    N)r   F)r   Ústrr   Úbool)é   )r&   ÚintÚreturnr:   )r=   r:   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r   Ú	model_keyr   r   r   r   r,   © r    r   r   r      s4   „ ñð4 &+Ð*€HÐ*ˆw˜ô*eôXMó&ó(ó*	/r    r   N)Ú
__future__r   Úultralytics.hub.utilsr   r   r   r   Úultralytics.utilsr   r	   r
   r   r   r   rC   r    r   ú<module>rG      s/   ðõ #ç ^Ó ^ß @Ó @à�Ð4Ð5€÷N/ò N/r    