
    h                     R    S SK JrJrJrJr  S SKJrJrJrJ	r	  \ S3r
 " S S5      rg)    )HUB_API_ROOTHUB_WEB_ROOTPREFIXrequest_with_credentials)IS_COLABLOGGERSETTINGSemojisz/settings?tab=api+keysc                   r    \ rS rSrSrS=r=rrSS\S\	4S jjr
SS\S\	4S	 jjrS\	4S
 jrS\	4S jrS rSrg)Auth	   a*  
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verbosec                    UR                  SS5      S   nU=(       d    [        R                  " SS5      U l        U R                  (       aZ  U R                  [        R                  " S5      :X  a%  U(       a  [        R
                  " [         S35        gU R                  5       nO,[        (       a  U R                  5       nOU R                  5       nU(       aH  [        R                  " SU R                  05        U(       a  [        R
                  " [         S35        ggU(       a%  [        R
                  " [         S	[         S
35        gg)a  
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    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       N/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/hub/auth.py__init__Auth.__init__'   s     --Q'* =(,,y""= <<||x||I66KK6(*; <= ++-X,,.G **,G OOY56vh&GHI KK6("3K=@bcd     max_attemptsreturnc                 B   SSK n[        U5       Hr  n[        R                  " [         SUS-    SU 35        UR                  S[
         S35      nUR                  SS5      S   U l        U R                  5       (       d  Mr    g	   [        [        [         S
35      5      e)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   r"   r&   attempts	input_keys        r   r   Auth.request_api_keyT   s     	l+HKK6(/(Q,tL>RS*=k]!(LMI$??3215DL  "" , fx/I%JKLLr!   c                 V   SSK n U R                  5       =n(       aH  UR                  [         S3US9nUR	                  5       R                  SS5      (       d  [        S5      eg[        S	5      e! [         a.    S=U l        U l        [        R                  " [         S
35         gf = f)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   Auth.authenticateh   s     	
	--//v/MM\N(";VMLvvx||Iu55)*CDD!"GHH 	+00DMDLNNfX_56	s   AA0 %A0 05B('B(c                 "   [         (       d  g [        [         S35      nUR                  SS5      (       a8  UR                  S0 5      R                  SS5      U l        U R                  5         g[        S5      e! [         a
    SU l         gf = f)	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   r2   r   r(   )r   authns     r   r   Auth.auth_with_cookies}   s     x		,~]-KLEyyE** %		&" 5 9 9)T J!!#!"STT 	!DM	s   A A: /A: :BBc                     U R                   (       a  SSU R                    30$ U R                  (       a  SU R                  0$ g)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)r2   r   )r   s    r   r/   Auth.get_auth_header   s<     ==#wt}}o%>??\\.. r!   )r   r2   N)r   F)   )__name__
__module____qualname____firstlineno____doc__r2   r   	model_keystrboolr   intr   r   r   r/   __static_attributes__ r!   r   r   r   	   sb    6 &+*H*w+e +e4 +eZMC M M(d *4 ,
/r!   r   N)ultralytics.hub.utilsr   r   r   r   ultralytics.utilsr   r   r	   r
   r   r   rJ   r!   r   <module>rM      s-    _ ^ @ @45T/ T/r!   