o
    Vh                     @   sT   d dl 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 dZdS )    N)HUB_API_ROOTHUB_WEB_ROOTPREFIXrequest_with_credentials)IS_COLABLOGGERSETTINGSemojisz/settings?tab=api+keysc                   @   sh   e Zd ZdZd Z ZZddedefddZ	dd	e
d
efddZd
efddZd
efddZdd ZdS )Autha[  
    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.
    F api_keyverbosec                 C   s   | dd }|ptdd| _| jr,| jtdkr'|r%tt d dS |  }ntr3| 	 }n| 
 }|rOtd| ji |rMtt d dS dS |r^tt dt d	 dS dS )
aP  
        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   r   r   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   r   success r   H/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/hub/auth.py__init__   s&   

zAuth.__init__   max_attemptsreturnc                 C   sv   ddl }t|D ])}tt d|d  d|  | dt d}|dd | _|  r1 d	S qt	t
t d
)z'Prompt the user to input their API key.r   NzLogin. Attempt    z of zEnter API key from  r   Tu   Failed to authenticate ❌)getpassranger   r   r   r   r   r   r   ConnectionErrorr	   )r   r   r!   attempts	input_keyr   r   r   r   I   s   zAuth.request_api_keyc                 C   sx   z#|    }r tjt d|d}| ddstdW dS td ty;   d | _| _t	
t d Y dS w )	z
        Attempt to authenticate with the server using either id_token or API key.

        Returns:
            (bool): True if authentication is successful, False otherwise.
        z/v1/auth)headersr   FzUnable to authenticate.Tz#User has not authenticated locally.zInvalid API key)get_auth_headerrequestspostr   jsonr   r#   id_tokenr   r   warningr   )r   headerrr   r   r   r   U   s   zAuth.authenticatec                 C   sj   t sdS z#tt d}|ddr$|di dd| _|   W dS td ty4   d| _Y dS 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   authnr   r   r   r   h   s   	zAuth.auth_with_cookiesc                 C   s*   | j rdd| j  iS | jrd| jiS dS )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   r   r   r   r'   ~   s
   
zAuth.get_auth_headerN)r   F)r   )__name__
__module____qualname____doc__r+   r   	model_keystrboolr   intr   r   r   r'   r   r   r   r   r
      s    -r
   )r(   ultralytics.hub.utilsr   r   r   r   ultralytics.utilsr   r   r   r	   r   r
   r   r   r   r   <module>   s
   
