
    0j@>                     R   d Z 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ZddlZdZ	 ej
                    Z ej        d            dZdZ ej        ej        j        dd	          Zd
 Zd Zd*dZddedfdZ ed          Z ej        erej        nej                   	 ddlmZ  e              ej        d           n# e$ r  ej        d           Y nw xY wd Z d Z!d Z"d+dZ#d Z$d Z%dZ&dde&dZ'd gZ(d! Z)d" Z*d# Z+d$ Z,d% Z-d& Z.d' Z/d( Z0e1d)k    r e0             dS dS ),a  MSAL Python Tester

Usage 1: Run it on the fly.
    python -m msal
    Note: We choose to not define a console script to avoid name conflict.

Usage 2: Build an all-in-one executable file for bug bash.
    shiv -e msal.__main__._main -o msaltest-on-os-name.pyz .
    Nzmsal_cache.binc                      t           j        r?t          t          d                              t                                                     nd S )Nw)global_cachehas_state_changedopen_token_cache_filenamewrite	serialize     U/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/msal/__main__.py<lambda>r      sA     %0D	$$**<+A+A+C+CDDD+/ r   z$04b07795-8ddb-461a-bbee-02f9e1bf7b46z$04f0c124-f2bc-4f59-8241-bf6df9866bbdzhttps://example.com/endpointplaceholder)http_methodurlnoncec                 N    t          t          j        | dd                     d S )N   T)indent	sort_keys)printjsondumps)blobs    r   
print_jsonr      s'    	$*T!t
4
4
455555r   c                 J    t          d                    |                     dvS )Nz9{} (N/n/F/f or empty means False, otherwise it is True): )NnFf )inputformat)messages    r   _input_booleanr%   "   s-    CJJ7SS
 
)* *r   c                 p    t          |                     |                                                    p|S )N)default)r"   r#   strip)r$   r'   s     r   _inputr)   '   s/    00117799DWDr   zYour options:z    Your choice? Fc           	         | s
J d            |rt          |           t          | d          D ]1\  }}t          d                    | ||                               2|rt          d           	 t          |          }	 t	          |          }d|cxk    rt          |           k    rn n| |dz
           S n# t          $ r |r|r|cY S Y nw xY wa)Nzoptions must not be empty   )startz
    {}: {}z'    Or you can just type in your input.)r   	enumerater#   r"   intlen
ValueError)	optionsheaderfooteroption_rendereraccept_nonempty_stringioraw_datachoices	            r   _select_optionsr:   *   s     /////7 f'+++ : :1l!!!__Q%7%7889999 97888 ==	 ]]FF****c'll*****vz** 	  	  	   2  	  s   6B; ;CCzEnable MSAL Python's DEBUG log?)level)load_dotenvz+Loaded environment variables from .env filezSpython-dotenv is not installed. You may need to set environment variables manually.c                  x    t          g ddd                                          } d| v rt          d          | S )N)z$https://graph.microsoft.com/.defaultz%https://management.azure.com/.defaultz	User.ReadzUser.ReadBasic.Allz_Select a scope (multiple scopes can only be input by manually typing them, delimited by space):Tr2   r5   4https://pas.windows.net/CheckMyAccess/Linux/.defaultz9SSH Cert scope shall be tested by its dedicated functions)r:   splitr0   scopess    r   _input_scopesrC   J   s]     
 
 
 q#
 
 
 %''  >GGTUUUMr   c                 v    |                                  }|rt          |d d          S t          d           d S )Nc                 F    d                     | d         | d                   S )Nz{}, came from {}usernameaccount_source)r#   as    r   r   z!_select_account.<locals>.<lambda>]   s!    &8&?&?*qQaOb&c&c r   z0Account(s) already signed in inside MSAL Python:r4   r2   zRNo account available inside MSAL Python. Use other methods to acquire token first.)get_accountsr:   r   )appaccountss     r   _select_accountrN   X   sV    !!H dccE   	 	bcccccr   c           
          t          |           }|rkt          |                     t                      |t	          d          |                                 rt	          d          rt          nd                     dS dS )zIacquire_token_silent() - with an account already signed into MSAL Python.!Bypass MSAL Python's token cache?Acquire AT POP via Broker?N)accountforce_refreshauth_scheme)rN   r   acquire_token_silent_with_errorrC   r%   is_pop_supportedplaceholder_auth_schemerL   rR   s     r   _acquire_token_silentrY   c   s    c""G 366OO()LMM'')).<=Y.Z.Z// 7   	 	 	 	 	 r   c                    t          | t          j                  sJ |pt                      }t	          dddddddddgd d	
          d         }|dk    rd}nQt	          dgd |                                 D             z   dd          }t          |t                    r|d         n|}|                     || j        | j	        t          t          fv d|||pi |                                 rt          d          rt          nd          }|rHd|v rD|                    di                               d          }||k    rt!          j        d|           t%          |           |S )zUacquire_token_interactive() - User will be prompted if app opts to do select_account.NzRUnspecified. Proceed silently with a default account (if any), fallback to prompt.)valuedescriptionnonezEnone. Proceed silently with a default account (if any), or error out.select_accountz.select_account. Prompt with an account picker.c                     | d         S )Nr\   r   r7   s    r   r   z,_acquire_token_interactive.<locals>.<lambda>y   s    !M"2 r   zPrompt behavior?rJ   r[   c                     g | ]
}|d          S )rF   r   ).0rI   s     r   
<listcomp>z._acquire_token_interactive.<locals>.<listcomp>   s    @@@a
m@@@r   zlogin_hint? (If you have multiple signed-in sessions in browser/broker, and you specify a login_hint to match one of them, you will bypass the account picker.)Tr>   rF   i  rQ   )parent_window_handleenable_msa_passthroughportprompt
login_hintdatarT   id_token_claimspreferred_usernamez-Signed-in user "%s" does not match login_hint)
isinstancemsalPublicClientApplicationrC   r:   rK   dictacquire_token_interactiveCONSOLE_WINDOW_HANDLE	client_id
_AZURE_CLI_VISUAL_STUDIOrV   r%   rW   getloggingwarningr   )rL   rB   ri   rg   rh   raw_login_hintresultsigned_in_users           r   _acquire_token_interactiver{   p   s   c4788888&}F'{||)pqq"3cdd

 32!# # # $+,F !!!

(F@@S-=-=-?-?@@@@ u#'  
 4>nd3S3Sg^J//Yg
** 6"}1   *4:2##%%*89U*V*V++ + 
 
F  ]'611$5r::>>?STTZ''OK^\\\vMr   c                     t          |                     t          d          t          j        d          t	                                           dS )zacquire_token_by_username_password() - See constraints here: https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#constraints-for-ropcz
username: z
password: rA   N)r   "acquire_token_by_username_passwordr)   getpassrC   rL   s    r   #_acquire_token_by_username_passwordr      sY    s55|gol;;MOO 6 U U V V V V Vr   c                 N   t          | t          j                  sJ |                     t	                                }t          |d                    t          j                                         t          d           | 
                    |          }t          |           dS )zNacquire_token_by_device_flow() - Note that this one does not go through brokerrA   r$   zNAfter you completed the step above, press ENTER in this console to continue...N)rl   rm   rn   initiate_device_flowrC   r   sysstdoutflushr"   acquire_token_by_device_flowr   )rL   flowry   s      r   _acquire_token_by_device_flowr      s    c4788888##=??#;;D	$y/J	
Z[[[--d33Fvr   aw  {"kty":"RSA", "n":"2tNr73xwcj6lH7bqRZrFzgSLj7OeLfbn8216uOMDHuaZ6TEUBDN8Uz0ve8jAlKsP9CQFCSVoSNovdE-fs7c15MxEGHjDcNKLWonznximj8pDGZQjVdfK-7mG6P6z-lgVcLuYu5JcWU_PeEqIKg5llOaz-qeQ4LEDS4T1D2qWRGpAra4rJX1-kmrWmX_XIamq30C9EIO0gGuT4rc2hJBWQ-4-FnE1NXmy125wfT3NdotAJGq5lMIfhjfglDbJCwhc8Oe17ORjO3FsB5CLuBRpYmP7Nzn66lRY3Fe11Xz8AEBl3anKFSJcTvlMnFtu3EpD-eiaHfTgRBU7CztGQqVbiQ", "e":"AQAB"}ssh-certkey1)
token_typekey_idreq_cnfr?   c                 H   t          | t          j                  sJ t          |           }|rp|                     t
          |t          t          d                    }t          |           |r1|	                    d          dk    rt          j        d           dS dS dS dS )zFAcquire an SSH Cert silently- This typically only works with Azure CLIrP   )ri   rS   r   r   zUnable to acquire an ssh-cert.N)rl   rm   rn   rN   acquire_token_silent_SSH_CERT_SCOPE_SSH_CERT_DATAr%   r   ru   rv   error)rL   rR   ry   s      r   _acquire_ssh_cert_silentlyr      s    c4788888c""G 	<))()LMM	 *   	6 	<fjj..*<<M:;;;;;	< 	<	< 	<<<r   c                     t          | t          j                  sJ t          | t          t
                    }|                    d          dk    rt          j        d           dS dS )zLAcquire an SSH Cert interactively - This typically only works with Azure CLI)rB   ri   r   r   zUnable to acquire an ssh-certN)	rl   rm   rn   r{   r   r   ru   rv   r   )rL   ry   s     r   _acquire_ssh_cert_interactiver      se    c4788888'O.YYYFzz,:--566666 .-r   c                     t          | t          j                  sJ dg}t          | |          }|                    d          dk    rt          j        d           dS dS )zLAcquire a POP token interactively - This typically only works with Azure CLIz-6256c85f-0aad-4d50-b960-e6e9b21efe35/.defaultrA   r   popzUnable to acquire a pop tokenN)rl   rm   rn   r{   ru   rv   r   )rL   	POP_SCOPEry   s      r   _acquire_pop_token_interactiver      sk    c4788888@AI'I>>>Fzz,5((566666 )(r   c                     t          |           }|r?|                     |           t          d                    |d                              dS dS )zoremove_account() - Invalidate account and/or token(s) from cache, so that acquire_token_silent() would be resetz@Account "{}" and/or its token(s) are signed out from MSAL PythonrF   N)rN   remove_accountr   r#   rX   s     r   _remove_accountr      sb    c""G n7###PWWX_`jXkllmmmmmn nr   c                     t          | t          j                  sJ t          |                     t                                           dS )zKCCA.acquire_token_for_client() - Rerun this will get same token from cache.rA   N)rl   rm   ConfidentialClientApplicationr   acquire_token_for_clientrC   r   s    r   _acquire_token_for_clientr      sD    c4=>>>>>s++=??+CCDDDDDr   c                 f    t          | t          j                  sJ |                                  dS )zECCA.remove_tokens_for_client() - Run this to evict tokens from cache.N)rl   rm   r   remove_tokens_for_clientr   s    r   _remove_tokens_for_clientr      s3    c4=>>>>>  """""r   c                     | j         rdnd}t          d                    |                     t          j                     dS )Exitzjhttps://identitydivision.visualstudio.com/Engineering/_queries/query/79b3a352-a775-406f-87cd-a487c382a8ed/zXhttps://github.com/AzureAD/microsoft-authentication-library-for-python/issues/new/choosez2Bye. If you found a bug, please report it here: {}N)_enable_brokerr   r#   r   exit)rL   bug_links     r   _exitr      sL     	cttb 
 

>
E
Eh
O
OPPPHJJJJJr   c                     t          d                    t          j                             t	          ddddd                    t
                    dgd dd	
          } | d         dk    rut          j                            t
                    rQ	 t          
                    t          t
          d                                                     n# t          $ r Y nw xY wt	          t          ddt          dddddt          j        d          t          j        d          ddgd dd
          }t#          |t$                    od|v }|r|d         r|d         st'          d          | ot)          d          }|rt*          rt)          d          nd	}t	          g ddd           }|r$|                    d!          st)          d"          nd }|s?t          j        t#          |t$                    r|d         n||||||||t          #	  	        n*t          j        |d         |d         |||t          $          }t2          gt#          |t          j                  r t4          t6          t8          t:          t<          gng z   t>          t@          gz   t#          |t          j                  rtB          tD          gng z   }	 t	          |tF          gz   d% d&'          }		  |	|           nm# t&          $ r}
tI          j%        d(|
           Y d }
~
nId }
~
wtL          $ r t          d)           Y n+tN          $ r}
tI          j%        d*|
           Y d }
~
nd }
~
ww xY w)+Nz4Welcome to the Msal Python {} Tester (Experimental)
emptyz<Start with an empty token cache. Suitable for one-off tests.)r9   descreusezReuse the previous token cache {} (if any) which was created during last test app exit. Useful for testing acquire_token_silent() repeatedlyc                     | d         S )Nr   r   r`   s    r   r   z_main.<locals>.<lambda>   
    !F) r   z1What token cache state do you want to begin with?F)r4   r2   r5   r9   rz+Azure CLI (Correctly configured for MSA-PT))rr   namez/Visual Studio (Correctly configured for MSA-PT)z$95de633a-083e-42f5-b444-a4295d8e9314zAWhiteboard Services (Non MSA-PT app. Accepts AAD & MSA accounts.)	CLIENT_IDCLIENT_SECRETzoA confidential client app (CCA) whose settings are defined in environment variables CLIENT_ID and CLIENT_SECRET)rr   client_secretr   c                     | d         S )Nr   r   rH   s    r   r   z_main.<locals>.<lambda>  r   r   zUImpersonate this app (or you can type in the client_id of your own public client app)Tr   rr   zAYou need to set environment variables CLIENT_ID and CLIENT_SECRETzYEnable broker? (It will error out later if your app has not registered some redirect URI)zEnable PII in broker's log?)z(https://login.microsoftonline.com/commonz/https://login.microsoftonline.com/organizationsz;https://login.microsoftonline.com/microsoft.onmicrosoft.comz:https://login.microsoftonline.com/msidlab4.onmicrosoft.comz+https://login.microsoftonline.com/consumerszKInput authority (Note that MSA-PT apps would NOT use the /common authority)r>   z!https://login.microsoftonline.comzYou input an unusual authority which might fail the Instance Discovery. Now, do you want to perform Instance Discovery on your input authority?)	authorityinstance_discoveryenable_broker_on_windowsenable_broker_on_macenable_broker_on_linuxenable_broker_on_wslenable_pii_logtoken_cache)client_credentialr   r   r   r   c                     | j         S N)__doc__)r    s    r   r   z_main.<locals>.<lambda>E  s    ai r   zMSAL Python APIs:rJ   zInvalid input: %sAbortedz	Error: %s)(r   r#   rm   __version__r:   r   ospathexistsr   deserializer   readIOErrorrs   rt   getenvrl   ro   r0   r%   enable_debug_log
startswithrn   r   rY   r{   r   r   r   r   r   r   r   r   r   rv   r   KeyboardInterrupt	Exception)cache_choice
chosen_appis_ccaenable_brokerr   r   r   rL   methods_to_be_testedfunces              r   _mainr      s   	
A
H
HIY
Z
Z[[["!V 
 "KKQ6-L/ L/	 $
 ,+B$& & &L  H((RW^^<Q-R-R(	$$T*?%E%E%J%J%L%LMMMM 	 	 	D	  *WXX$._``<  GJ  	K  	K;//Y77G	
 	
	
"
 ,+O#% % %J 
D))Ko.KF ^z+. ^:o3N ^\]]]Z 
^ 5U &
 &
M GTsXhs^$ABBBnsN !
 !
 !
 ]#	
 	
 	
I ;  )33/ 1  1;	R
 
 
 7;	  
$
&#-j$#?#?O
;Z-!.*,*% 

 

 

 

 "?;$_5-% 
 
 
 & "
 C!=>>G&)&)*  EG	
 0

 C!CDDM%%  KM
"* E7*//8KM M M	*DIIII 	2 	2 	2M-q11111111  	 	 	) 	* 	* 	*M+q))))))))	**s<   ?C 
CC K, ,
M6LM.	M7MM__main__r   )NN)2r   base64r~   r   rv   r   r   atexitrm   r   SerializableTokenCacher   registerrs   rt   PopAuthSchemeHTTP_GETrW   r   r%   r)   strr:   r   basicConfigDEBUGINFOdotenvr<   infoImportErrorrw   rC   rN   rY   r{   r   r   _JWK1r   r   r   r   r   r   r   r   r   r   __name__r   r   r   <module>r      s    = < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < <( *t*,,  0 0    4
7,$,"+&
   6 6 6* * *
E E E E (0CUX$       * ">"CDD   +;M'-- N N N N?""""""KMMMGL>???? ? ? ?GO	>? ? ? ? ??
  	d 	d 	d  % % % %NV V V
   	F *fOOIJ< < <7 7 77 7 7n n nE E E
# # #
  j* j* j*X z	EGGGGG s   # C CC