
    FPh                     Z    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      Zy)    N)HUB_API_ROOTHUB_WEB_ROOTPREFIXrequest_with_credentials)LOGGERSETTINGSemojisis_colabz/settings?tab=api+keysc                   J    e Zd ZdZdxZxZZd
dZddZde	fdZ
de	fdZd Zy	)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.
    Fc                 L   |j                  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                yy)	z
        Initialize the Auth class with an optional API key.

        Args:
            api_key (str, optional): May be an API key or a combination API key and model ID, i.e. key_id
        _r   api_key u   Authenticated ✅Nu!   New authentication successful ✅zRetrieve API key from )splitr   getr   r   infor   authenticater
   auth_with_cookiesrequest_api_keyupdateAPI_KEY_URL)selfr   verbosesuccesss       _C:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/hub/auth.py__init__zAuth.__init__   s     --$Q' =(,,y""= <<||x||I66KK6(*; <= ++-Z,,.G **,G OOY56vh&GHI KK6("8FG     c                 0   ddl }t        |      D ]j  }t        j                  t         d|dz    d|        |j                  dt
         d      }|j                  d      d   | _        | j                         sj y	 t        t        t         d
            )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   max_attemptsr"   attempts	input_keys        r   r   zAuth.request_api_keyD   s     	l+HKK6(/(Q,tL>RS*=k]!(LMI$??3/2DL  " , fx/I%JKLLr   returnc                 D   	 | j                         x}rJt        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.
        z/v1/auth)headersr   FzUnable to authenticate.Tz#User has not authenticated locally.u   Invalid API key ⚠️)get_auth_headerrequestspostr   jsonr   r$   id_tokenr   r   warningr   )r   headerrs      r   r   zAuth.authenticateS   s    
	--//v/MM\N(";VLvvx||Iu5)*CDD!"GHH 	+00DMDLNNfX%;<=	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_cookiesf   s     z		,~]-KLEyyE* %		&" 5 9 9)T J!!#!"STT 	!DM	s   AA4 )A4 4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): 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<     ==#wt}}o%>??\\.. r   N)r   F)   )__name__
__module____qualname____doc__r/   r   	model_keyr   r   boolr   r   r+    r   r   r   r      sB     &+*H*w'HRMd &4 *
/r   r   )r,   ultralytics.hub.utilsr   r   r   r   ultralytics.utilsr   r   r	   r
   r   r   r@   r   r   <module>rC      s/     ^ ^ @ @45z/ z/r   