
    iX*                        S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJr  S SK	r	S SK
Jr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  \ R:                  " \5      r S,S\ S\ S\\   S\ 4S jjr!S\ S\ S\SS4S jr"S\ S\S\ 4S jr#S\ S\ S\S\ 4S jr$S,S\\   S\ 4S jjr%S,S\\   S\ 4S jjr&S,S\\   S\ 4S jjr'S\S\(SS4S jr) S,S \ S\\   S\ 4S! jjr*S\S\(4S" jr+ S,S#\\\ \4      S\4S$ jjr,  S-S#\\\ \4      S\\   S\4S% jjr-    S.SS&.S#\\\ \4      S'\\\(\ 4      S\(S(\(S\\   SS4S) jjjr. S,SS&.S#\\\ \4      S\\   SS4S* jjjr/S,S#\\\ \4      S\(4S+ jjr0g)/    N)Optional)Union)	urlencode)__version__)
BaseClient)AnacondaAuthConfig)AnacondaAuthSite)DeviceCodeFlow)AuthenticationError)DeviceFlowError)TokenNotFoundError)capture_auth_code)	TokenInfo)consolecode_challengestateconfigreturnc           
          Uc
  [        5       nUR                  R                  nUR                  nUR                  n[        USSUUU SS9n[        U5      nU SU 3nU$ )z)Build the authorization code request URL.codez#openid email profile offline_accessS256)	client_idresponse_typescoper   redirect_urir   code_challenge_method?)r   oidcauthorization_endpointr   r   dictr   )	r   r   r   r   r   r   paramsencoded_paramsurls	            5lib/python3.13/site-packages/anaconda_auth/actions.pymake_auth_code_request_urlr%      sy    
 ~#%#[[??  I&&L3!%$F v&N#$An%5
6CJ    c                 H    [        XU5      n[        R                  " U5        g)z,Open the authentication flow in the browser.N)r%   
webbrowseropen)r   r   r   r#   s       r$   _send_auth_code_requestr*   5   s     %^F
CCOOCr&   refresh_tokenc                     [        US9nUR                  UR                  R                  SU UR                  S.SS9nUR                  5         UR                  5       nUS   nU$ )zRefresh and save the tokens.siter+   )
grant_typer+   r   Fdataauthaccess_token)r   postr   token_endpointr   raise_for_statusjson)r+   r   clientresponseresponse_datar3   s         r$   refresh_access_tokenr;   =   sn    V$F{{"")*))

   H MMOM 0Lr&   	auth_codecode_verifierc                 X   UR                   R                  nUR                  nUR                  n[	        US9nUR                  U[        SUU UUS9SS9nUR                  5       nSU;   a/  [        SUR                  S5       SUR                  S	5       35      eUR                  S
5      n	U	$ )zPRequest an access token using the provided authorization code and code verifier.r-   authorization_code)r/   r   r   r   r=   Fr0   errorzError getting JWT: z - error_descriptionr3   )
r   r5   r   r   r   r4   r    r7   r   get)
r<   r=   r   r5   r   r   r8   r9   resultr3   s
             r$   request_access_tokenrD   P   s     [[//N  I&&LV$F{{+%'
   
H ]]_F&!!&**W"5!6c&**EX:Y9Z[
 	
 ::n-Lr&   c                    U =(       d
    [        5       n [        U S9nUR                  5       n[        R                  " S5        [        R                  " S5        [        R                  " 5         [        R                  " UR
                  5        [        R                  " 5         [        R                  " S5        [        R                  " 5         [        R                  " UR                  5        [        R                  " 5          [        R                  " UR                  5        [        R                  " S5      n UR                  5         UR                  5       nUR                  5         [        R                  " S5        US   $ ! [         a     Nnf = f! [         a    UR                  5         e [          a    UR                  5         e f = f)z'Login using OAuth 2.0 device code flow.r   zPAttempting to automatically open the authorization page in your default browser.zuIf the browser does not open or you wish to use a different device to authorize this request, open the following URL:zThen enter the code:z,Waiting for authorization (CTRL-C to cancel)u   ✓ Login successful!r3   )r   r
   initiate_device_authorizationr   printverification_uri	user_coder(   r)   verification_uri_complete	Exceptionstatusstartpoll_for_tokenstopKeyboardInterruptr   )r   device_flowdevice_authorizationrM   token_responses        r$   _do_device_flowrU   o   sR   +)+F !/K 'DDF MMZ MM MMOMM&778MMOMM()MMOMM&001MMO,FFG ^^JKF$335-.n--     s    F ;A
F 
FF6Gc                    U =(       d
    [        5       n [        [        R                  " 5       5      n[        R
                  " SS9u  p#[        X1U 5        [        U R                  XS9n[        R                  S5        [        XBU 5      $ )zZDo the browser-based auth flow and return the short-lived access_token and id_token tuple.   )code_verifier_length)r   r   z-Authentication successful! Getting JWT token.)r   struuiduuid4pkcegenerate_pkce_pairr*   r   r   loggerdebugrD   )r   r   r=   r   r<   s        r$   _do_auth_flowr`      sn    +)+F

E$($;$;QT$U!MN6: "&"5"5URI
LL@A  	&AAr&   c                 X   [         R                  " S[        SS9  U c
  [        5       n [        R
                  " S5      n[        R
                  " SSS9n[        U S9nUR                  U R                  R                  S	UUS
.SS9nUR                  5       nUR                  5         US   nU$ )zIPrompt for username and password and log in with the password grant flow.zKBasic login with username/password is deprecated and will be disabled soon.r   )
stacklevelzPlease enter your email: zPlease enter your password: T)passwordr-   rc   )r/   usernamerc   Fr0   r3   )warningswarnUserWarningr   r   inputr   r4   r   r5   r7   r6   )r   rd   rc   r8   r9   r:   r3   s          r$   _login_with_usernameri      s    MMU ~#%}}89H}};dKHV$F{{""$  

   H MMOM 0Lr&   basicc                     U(       a
  [        U S9nO$U R                  (       a
  [        U S9nO	[        U S9n[	        UU S9n[        X0R                  S9nUR                  5         g )NrF   )api_keydomain)ri   use_device_flowrU   r`   get_api_keyr   rm   save)r   rj   r3   rl   
token_infos        r$   	_do_loginrr      sV    +6:			&f5$F3G
 7==AJOOr&   r3   c           	         U=(       d
    [        5       n[        US9nSSU  30nUR                  nUb  XCS'   SUR                   S3SUR                   S3/nU H9  nUR                  U[        / SQS	[         3/S
9USS9nUR                  S:X  d  M9    O   [        R                  " S5        [        eUR                  5       S   $ )Nr-   AuthorizationzBearer zX-AAU-CLIENTzhttps://z/api/auth/api-keysz/api/iam/api-keys)z
cloud:readzcloud:writez	repo:readzanaconda-auth/v)scopestagsF)r7   headersr2      zError retrieving an API keyrl   )r   r   	aau_tokenauth_domainrm   r4   r    r   status_coder   rH   r   r7   )r3   r   r8   rw   ry   urlsr#   r9   s           r$   ro   ro      s     +)+FV$F', 89G  I"+ 6%%&&89
6==/!23D ;;A'}56   
 3&  	34!!==?9%%r&   c                      [         R                  " U R                  5      R                  (       + nU$ ! [         a    Sn U$ f = f)NF)r   loadrm   expiredr   )r   valids     r$   _api_key_is_validr     sD    NN6==1999 L  Ls   /3 AAr.   c                 p    [        U [        5      (       a  U nU$ U (       a  [        U S9nU$ [        5       nU$ )Nr-   )
isinstancer	   r   r.   r   s     r$   _get_configr     sC     $()) M 
#. M $%Mr&   c                     U(       a  U b  [        S5      eU(       a  [        R                  " S[        5        U$ [	        U 5      $ )Nz/You cannot set both site= and config= argumentszRThe config= keyword argument is deprecated, please use site=str | AnacondaAuthSite)
ValueErrorre   rf   DeprecationWarningr   r   s     r$   _site_or_configr     s?     $"JKK`	
 4  r&   rF   
ssl_verifyforcec                ~    [        XS9nUb  UR                  SU0SS9nU(       d  [        US9(       d
  [        XRS9  gg)zJLog into Anaconda Platform and store the token information in the keyring.r   Nr   T)updatedeeprF   )r   rj   )r   
model_copyr   rr   )r.   r   rj   r   r   site_configs         r$   loginr   -  sM     "t;K!,, *-D - 
 %[92 :r&   c                    [        XS9n [        R                  " UR                  S9nUR	                  5         UR                  S:w  a  g [        R                  " SS9nUR	                  5         g! [
         a     NCf = f! [
         a     gf = f)zLog out of Anaconda Platform.r   rm   zanaconda.comNzanaconda.cloud)r   r   r~   rm   deleter   )r.   r   r   rq   s       r$   logoutr   A  s     "t;K^^;+=+=>
 ^+ 	^^+;<
    s#   .A0 $B  0
A=<A= 
BBc                     [        U [        5      (       a  U nO	[        U S9n [        R                  " UR
                  S9nUS L$ ! [         a    S n US L$ f = f)Nr-   r   )r   r	   r   r   r~   rm   r   )r.   r   rq   s      r$   is_logged_inr   _  sd    $())#.^^6==9
 T!!  
T!!s   A AA)N)NN)NNFF)1loggingrZ   re   r(   typingr   r   urllib.parser   r\   anaconda_authr   anaconda_auth.clientr   anaconda_auth.configr   r	   anaconda_auth.device_flowr
   anaconda_auth.exceptionsr   r   r   anaconda_auth.handlersr   anaconda_auth.tokenr   anaconda_cli_base.consoler   	getLogger__name__r^   rY   r%   r*   r;   rD   rU   r`   ri   boolrr   ro   r   r   r   r   r   r    r&   r$   <module>r      s         "  % + 3 1 4 8 4 7 4 ) -			8	$ KO #-56F-G6 #-=	 5E # &#&0@>.H%56 .# .bB(#34 B B"*:!; s >& t  & *.!&!&%&!& 	!&H. 4  48
5../
0 48)-!
5../
0!%&! !$ 48-1	3 *.3
5../
03tSy)*3 3 	3 %&3 
3* 48 *.
5../
0 %& 
	<"xc+;&; <= " "r&   