a
    yf                     @   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                   @   sT   e Zd ZdZd Z ZZdddZdddZe	d	d
dZ
e	d	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 or bool): Token used for identity verification, initialized as False.
        api_key (str or bool): API key for authentication, initialized as False.
        model_key (bool): Placeholder for model key, initialized as False.
    F c                 C   s   | dd }|ptdd| _| jrX| jtdkrN|rJtt d dS |  }ntrf| 	 }n| 
 }|rtd| ji |rtt d n|rtt dt d	 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   api_keyr   u   Authenticated ✅Nu!   New authentication successful ✅zGet API key from z& and then run 'yolo hub login API_KEY')splitr   getr   r   infor   authenticater   auth_with_cookiesrequest_api_keyupdateAPI_KEY_URL)selfr   verbosesuccess r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/hub/auth.py__init__   s"    

zAuth.__init__   c                 C   sz   ddl }t|D ]R}tt d|d  d|  | dt d}|dd | _|  r d	S qt	t
t d
dS )zX
        Prompt the user to input their API key.

        Returns the model ID.
        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   Zmax_attemptsr   ZattemptsZ	input_keyr   r   r   r   I   s    zAuth.request_api_key)returnc                 C   s   zJ|    }r@tjt d|d}| dds:tdW dS tdW n0 tyz   d | _| _t	
t d Y dS 0 d	S )
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.u   Invalid API key ⚠️N)get_auth_headerrequestspostr   jsonr   r!   id_tokenr   r   warningr   )r   headerrr   r   r   r   Y   s    zAuth.authenticatec                 C   sr   t sdS zJtt d}|ddrH|di dd| _|   W dS tdW n tyl   d| _Y dS 0 dS )	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   dataZidTokenNTz/Unable to fetch browser authentication details.)r   r   r   r   r(   r   r!   )r   Zauthnr   r   r   r   l   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): 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   Z	model_keyr   r   boolr   r   r$   r   r   r   r   r
      s   
-
r
   )r%   Zultralytics.hub.utilsr   r   r   r   Zultralytics.utilsr   r   r   r	   r   r
   r   r   r   r   <module>   s   
