
    9-eI"                     L   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlZd dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ  ej        e           Z!de"de"de"ddfdZ#de"dee"         ddfdZ$de"de"de"de"de"ddfdZ%	 d*de"deedf         de"fdZ&d*dee         de"fdZ'd*dee         de"fd Z(ded!e)ddfd"Z*de"de"fd#Z+dede)fd$Z,	 d+dee         d!e)d&e)ddfd'Z-d*dee         ddfd(Z.de)fd)Z/dS ),    N)Optional)Union)	urlencode)__version__)	APIConfig)
AuthConfig)console)AuthenticationError)InvalidTokenError)TokenNotFoundError)capture_auth_code)	JWKClient)	TokenInfoaccess_tokenalgorithm_usedexpected_hashreturnc                    t          j        |          j        j        }t	          j        |          }|                    |                     d                     |                                }|dt          |          dz           }t          j        |                              d                              d          }||k    rt          j                    dS )zValidate the JWT token.

    We need to compute the hash of the access token and compare it with the hash that is present in the JWT.
    This is to ensure that the token is not tampered with.

    zutf-8N   =)jwtget_algorithm_by_namehash_algnamehashlibnewupdateencodedigestlenbase64urlsafe_b64encodedecoderstripInvalidSignatureError)r   r   r   hashlib_alg_namehashr   digest_truncatedcomputed_hashs           ;lib/python3.11/site-packages/anaconda_cloud_auth/actions.py_validate_access_tokenr+      s     0@@IN;'((DKK##G,,---[[]]F
 2V!123 	 !12299'BBII#NN  %%'))) &%    id_tokenc                    |d S t                      }t          |j        j                  }|                    |          }	 t          j        ||j        |j        j        |j	                  }n.# t
          j
        j        $ r}t          d|           d }~ww xY wt          j        |          d         }| t          d          	 t          | ||d                    d S # t
          j        $ r t          d          w xY w)N)key
algorithmsaudiencezError decoding token: algz!No access token found to validateat_hashz!Access token has an invalid hash.)r   r   oidcjwks_uriget_signing_key_from_jwtr   r#   r/   %id_token_signing_alg_values_supported	client_id
exceptions
PyJWTErrorr   get_unverified_headerr   r+   r%   )r   r-   auth_configjwks_clientsigning_keyid_infoer   s           r*   _validate_token_inforA   <   s-   ,,KK,566K66x@@K	>*"'M *	
 
 
 >$ > > > < < <===>
 .x88?N !DEEEE|^WY=OPPPPP$ E E E CDDDEs$   ,A/ /BBB	C" "Dr8   authorization_endpointredirect_uristatecode_challengec           	          t          | dd|||d          }t          |          }| d| }t                              d|            t	          j        |           dS )z,Open the authentication flow in the browser.codez#openid email profile offline_accessS256)r8   response_typescoperD   rC   rE   code_challenge_method?zOpening auth URL: N)dictr   loggerdebug
webbrowseropen)r8   rB   rC   rD   rE   paramsencoded_paramsurls           r*   _send_auth_code_requestrU   ]   s     3!%$  F v&&N#
6
6n
6
6C
LL+c++,,,OCr,   refresh_tokenr<   c                     |t                      }t          j        |j        j        d| |j        d          }|                                 |                                }|d         }|S )zRefresh and save the tokens.NrV   )
grant_typerV   r8   datar   )r   requestspostr4   token_endpointr8   raise_for_statusjson)rV   r<   responseresponse_datar   s        r*   refresh_access_tokenrb   w   sy      ll}')*$.
 
  H MMOOM 0Lr,   c           
         | t                      } | j        j        }| j        j        }| j        }| j        }t          t          j                              }t          j
        d          \  }}t          |||||           t          ||          }t                              d           t          j        |t#          d||||                    }	|	                                }
d|
v r;t'          d	|
                    d           d
|
                    d                     |
                    d          }|
                    d          }t+          ||           |S )zZDo the browser-based auth flow and return the short-lived access_token and id_token tuple.N   )code_verifier_lengthz-Authentication successful! Getting JWT token.authorization_code)rX   r8   rG   rC   code_verifierrY   errorzError getting JWT: z - error_descriptionr   r-   )r   r4   r]   rB   r8   rC   struuiduuid4pkcegenerate_pkce_pairrU   r   rN   rO   r[   r\   rM   r_   r
   getrA   )r<   r]   rB   r8   rC   rD   rg   rE   	auth_coder`   resultr   r-   s                r*   _do_auth_flowrr      st    ll %4N(-D%I+L
E$($;QT$U$U$U!M>)<  
 ",66I
LL@AAA }+%'
 
 
	 	 	H ]]__F&![&**W"5"5[[&**EX:Y:Y[[
 
 	
 ::n--Lzz*%%Hx000r,   c                 Z   t          j        dt          d           | t                      } t	          j        d          }t	          j        dd          }t          j        | j        j	        d	||d
          }|
                                }|                                 |d         }|S )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   )
stacklevelNzPlease enter your email: zPlease enter your password: T)passwordru   )rX   usernameru   rY   r   )warningswarnUserWarningr   r	   inputr[   r\   r4   r]   r_   r^   )r<   rv   ru   r`   ra   r   s         r*   _login_with_usernamer{      s    MU     ll}899H};dKKKH}'$  
 
  H MMOOM 0Lr,   basicc                     |rt          |           }nt          |           }t          |          }t          || j                  }|                                 d S )Nr<   )api_keydomain)r{   rr   _get_api_keyr   r   save)r<   r|   r   r   
token_infos        r*   	_do_loginr      sd     >+DDD$===<((G7;3EFFFJOOr,   c                 <   t                      }dd|  i}|j        }|||d<   t          j        d|j         dt          ddgdt           g	          |
          }|j        dk    rt          j	        d           t          |                                d         S )NAuthorizationzBearer zX-AAU-CLIENTzhttps://z/api/iam/api-keysz
cloud:readzcloud:writezanaconda-cloud-auth/v)scopestags)r_   headers   zError retrieving an API keyr   )r   	aau_tokenr[   r\   r   rM   r   status_coder	   printr
   r_   )r   configr   r   r`   s        r*   r   r      s    [[F 8, 8 89G I"+}36=333 -07+778
 
 
   H s""3444!!==??9%%r,   c                 l    	 t          j        | j                  j         }n# t          $ r d}Y nw xY w|S )NF)r   loadr   expiredr   )r<   valids     r*   _api_key_is_validr      sL    N;#566>>    Ls   " 11Fforcec                 p    | t                      } |st          |           st          | |           dS dS )zGLog into Anaconda.cloud and store the token information in the keyring.Nr~   )r<   r|   )r   r   r   )r<   r|   r   s      r*   loginr      sQ      ll 8%+>>> 8k7777778 8r,   c                     | t                      } 	 t          j        | j                  }|                                 dS # t
          $ r Y dS w xY w)zLog out of Anaconda.cloud.Nr   )r   r   r   r   deleter   r<   r   s     r*   logoutr     se     ll^;+=>>>
   s   .A 
AAc                      t                      } 	 t          j        | j                  }n# t          $ r d }Y nw xY w|d uS )Nr   )r   r   r   r   r   r   s     r*   is_logged_inr     sX    ,,K^;+=>>>

   


 T!!s   + ::)N)NFF)0r!   r   loggingrk   rw   rP   typingr   r   urllib.parser   r   rm   r[   anaconda_cloud_authr   anaconda_cloud_auth.configr   r   anaconda_cloud_auth.consoler	   anaconda_cloud_auth.exceptionsr
   r   r   anaconda_cloud_auth.handlersr   anaconda_cloud_auth.jwtr   anaconda_cloud_auth.tokenr   	getLogger__name__rN   rj   r+   rA   rU   rb   rr   r{   boolr   r   r   r   r   r    r,   r*   <module>r      s                        " " " " " " 



   + + + + + + 0 0 0 0 0 0 1 1 1 1 1 1 / / / / / / > > > > > > < < < < < < = = = = = = : : : : : : - - - - - - / / / / / /		8	$	$**'**;>*	* * * *>Es Ehsm E E E E EB  	
  
   6 @D %*:t+;%<   *+ +x
3 +s + + + +\ hz&: c    8: d t    &s &s & & & &.: $     RW8 8*%8598JN8	8 8 8 8 ,     "d " " " " " "r,   