
    /j                    f    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dS )    )annotations)HUB_API_ROOTHUB_WEB_ROOTPREFIXrequest_with_credentials)IS_COLABLOGGERSETTINGSemojisz/settings?tab=api+keysc                  H    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dS )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()
    F api_keystrverboseboolc                V   |                     dd          d         }|pt          j        dd          | _        | j        rR| j        t          j        d          k    r |rt	          j        t           d           dS |                                 }n0t          r| 	                                }n| 
                                }|r=t          j        d| j        i           |rt	          j        t           d           dS dS |r&t	          j        t           d	t           d
           dS dS )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   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   successs       Y/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/hub/auth.py__init__zAuth.__init__(   sT    --Q''* =(,y""="= < 	-|x|I6666 >K6 < < <=== ++-- 	-,,..GG **,,G  	eOY5666 JvHHHIIIIIJ J 	eK6ccKcccddddd	e 	e       max_attemptsintreturnc                j   ddl }t          |          D ]|}t          j        t           d|dz    d|            |                     dt
           d          }|                    dd          d         | _        |                                 r d	S }t          t          t           d
                    )zPrompt 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   r$   r)   attempts	input_keys        r    r   zAuth.request_api_keyT   s     	l++ 	 	HK6RR(Q,RRLRRSSS(Lk(L(L(LMMI$??32215DL  "" ttf%J%J%JKKLLLr"   c                |   ddl }	 |                                 x}rX|                    t           d|          }|                                                    dd          st          d          dS t          d	          # t          $ r. dx| _        | _        t          j
        t           d
           Y dS w xY w)zAttempt 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   r0   headerrs       r    r   zAuth.authenticateg   s     	
	--///v MM\";";";VMLLvvxx||Iu55 E)*CDDDt!"GHHH 	 	 	+00DMDLNf55566655	s   A,B 4B 4B;:B;c                H   t           sdS 	 t          t           d          }|                    dd          rE|                    di                               dd          | _        |                                  dS t          d          # t          $ r d| _        Y dS w xY w)	zAttempt 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   r4   r   r+   )r   authns     r    r   zAuth.auth_with_cookies{   s      	5		,-K-K-KLLEyyE**  %		&" 5 5 9 9)T J J!!###t!"STTT 	 	 	!DM55	s   A0B =B B! B!c                L    | j         rdd| j          iS | j        r	d| j        iS dS )zGet 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)r4   r   )r   s    r    r1   zAuth.get_auth_header   sE     = 	/#%>t}%>%>??\ 	/..	/ 	/r"   N)r   F)r   r   r   r   )r#   )r$   r%   r&   r   )r&   r   )__name__
__module____qualname____doc__r4   r   	model_keyr!   r   r   r   r1    r"   r    r   r      s         4 &+*H*w*e *e *e *e *eXM M M M 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    # " " " " " ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ @ @ @ @ @ @ @ @ @ @ @ @555N/ N/ N/ N/ N/ N/ N/ N/ N/ N/r"   