3
ãdeç3  ã               @   sV  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„ ƒ dZdZdd	„ Zd
d„ Zd7dd„Zddedfdd„Zdd„ Zdd„ Zdd„ Zd8dd„Zdd„ Zdd„ ZdZd d!ed"œZd#gZd$d%„ Zd&d'„ Zd(Zej ed)d*œƒZ!d+eej"e!j#d,ƒƒj$d,ƒj%d-ƒd"œZ&d.d/„ Z'd0d1„ Z(d2d3„ Z)d4d5„ Z*e+d6k�rRe*ƒ  dS )9a  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               C   s   t jrttdƒjt jƒ ƒS d S )NÚw)Úglobal_cacheZhas_state_changedÚopenÚ_token_cache_filenameÚwriteÚ	serialize© r   r   úD/var/www/agendate/envp3/lib/python3.6/site-packages/msal/__main__.pyÚ<lambda>   s    r
   z$04b07795-8ddb-461a-bbee-02f9e1bf7b46z$04f0c124-f2bc-4f59-8241-bf6df9866bbdc             C   s   t tj| ddd�ƒ d S )Né   T)ÚindentÚ	sort_keys)ÚprintÚjsonÚdumps)Zblobr   r   r	   Ú
print_json   s    r   c             C   s   t dj| ƒƒdkS )Nz9{} (N/n/F/f or empty means False, otherwise it is True): ÚNÚnÚFÚfÚ )r   r   r   r   r   )ÚinputÚformat)Úmessager   r   r	   Ú_input_boolean   s    
r   c             C   s   t | j|d�ƒjƒ p|S )N)Údefault)r   r   Ústrip)r   r   r   r   r	   Ú_input"   s    r   zYour options:z    Your choice? Fc       	      C   sº   | st dƒ‚|rt|ƒ x,t| dd�D ]\}}tdj|||ƒƒƒ q&W |rRtdƒ xbt|ƒ}y4t|ƒ}d|  ko|t| ƒkn  rŽ| |d  S W qT tk
r°   |r¬|r¬|S Y qTX qTW d S )Nzoptions must not be emptyé   )Ústartz
    {}: {}z'    Or you can just type in your input.)ÚAssertionErrorr   Ú	enumerater   r   ÚintÚlenÚ
ValueError)	ÚoptionsÚheaderÚfooterÚoption_rendererÚaccept_nonempty_stringÚiÚoZraw_dataÚchoicer   r   r	   Ú_select_options%   s     r-   c              C   s.   t ddddgddd�jƒ } d| kr*td	ƒ‚| S )
Nz$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)r&   r)   z4https://pas.windows.net/CheckMyAccess/Linux/.defaultz9SSH Cert scope shall be tested by its dedicated functions)r-   Úsplitr$   )Úscopesr   r   r	   Ú_input_scopes:   s    r0   c             C   s*   | j ƒ }|rt|dd„ dd�S tdƒ d S )Nc             S   s   dj | d | d ƒS )Nz{}, came from {}ÚusernameZaccount_source)r   )Úar   r   r	   r
   M   s    z!_select_account.<locals>.<lambda>z0Account(s) already signed in inside MSAL Python:)r(   r&   zRNo account available inside MSAL Python. Use other methods to acquire token first.)Úget_accountsr-   r   )ÚappZaccountsr   r   r	   Ú_select_accountH   s    r5   c             C   s*   t | ƒ}|r&t| jtƒ |tdƒd�ƒ dS )zIacquire_token_silent() - with an account already signed into MSAL Python.z!Bypass MSAL Python's token cache?)ÚaccountÚforce_refreshN)r5   r   Zacquire_token_silent_with_errorr0   r   )r4   r6   r   r   r	   Ú_acquire_token_silentS   s    r8   c          	   C   sä   |pt ƒ }tdddœdddœdddœgdd	„ d
d�d }|dkrFd}n8tdgdd„ | jƒ D ƒ ddd�}t|tƒrz|d n|}| j|| j| jtt	gkd|||p i d�}|rØd|krØ|j
di ƒj
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.Zselect_accountz.select_account. Prompt with an account picker.c             S   s   | d S )Nr:   r   )r+   r   r   r	   r
   e   s    z,_acquire_token_interactive.<locals>.<lambda>zPrompt behavior?)r(   r&   r9   c             S   s   g | ]}|d  ‘qS )r1   r   )Ú.0r2   r   r   r	   ú
<listcomp>k   s    z._acquire_token_interactive.<locals>.<listcomp>zŸlogin_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.)T)r&   r)   r1   iÒ  )Zparent_window_handleZenable_msa_passthroughÚportÚpromptÚ
login_hintÚdataZid_token_claimsZpreferred_usernamez-Signed-in user "%s" does not match login_hint)r0   r-   r3   Ú
isinstanceÚdictZacquire_token_interactiveZCONSOLE_WINDOW_HANDLEÚ	client_idÚ
_AZURE_CLIÚ_VISUAL_STUDIOÚgetÚloggingÚwarningr   )r4   r/   rA   r?   r@   Zraw_login_hintÚresultZsigned_in_userr   r   r	   Ú_acquire_token_interactive]   s6    

rK   c             C   s$   t | jtdƒtjdƒtƒ d�ƒ dS )z«acquire_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: )r/   N)r   Z"acquire_token_by_username_passwordr   Úgetpassr0   )r4   r   r   r	   Ú#_acquire_token_by_username_password€   s    rM   c             C   sB   | j tƒ d�}t|d ƒ tjjƒ  tdƒ | j|ƒ}t|ƒ dS )zNacquire_token_by_device_flow() - Note that this one does not go through broker)r/   r   zNAfter you completed the step above, press ENTER in this console to continue...N)	Zinitiate_device_flowr0   r   ÚsysÚstdoutÚflushr   Zacquire_token_by_device_flowr   )r4   ZflowrJ   r   r   r	   Ú_acquire_token_by_device_flow…   s    

rQ   aw  {"kty":"RSA", "n":"2tNr73xwcj6lH7bqRZrFzgSLj7OeLfbn8216uOMDHuaZ6TEUBDN8Uz0ve8jAlKsP9CQFCSVoSNovdE-fs7c15MxEGHjDcNKLWonznximj8pDGZQjVdfK-7mG6P6z-lgVcLuYu5JcWU_PeEqIKg5llOaz-qeQ4LEDS4T1D2qWRGpAra4rJX1-kmrWmX_XIamq30C9EIO0gGuT4rc2hJBWQ-4-FnE1NXmy125wfT3NdotAJGq5lMIfhjfglDbJCwhc8Oe17ORjO3FsB5CLuBRpYmP7Nzn66lRY3Fe11Xz8AEBl3anKFSJcTvlMnFtu3EpD-eiaHfTgRBU7CztGQqVbiQ", "e":"AQAB"}zssh-certÚkey1)Ú
token_typeZkey_idZreq_cnfz4https://pas.windows.net/CheckMyAccess/Linux/.defaultc             C   sJ   t | ƒ}|rF| jt|ttdƒd�}t|ƒ |rF|jdƒdkrFtjdƒ dS )zFAcquire an SSH Cert silently- This typically only works with Azure CLIz!Bypass MSAL Python's token cache?)rA   r7   rS   zssh-certzUnable to acquire an ssh-cert.N)	r5   Zacquire_token_silentÚ_SSH_CERT_SCOPEÚ_SSH_CERT_DATAr   r   rG   rH   Úerror)r4   r6   rJ   r   r   r	   Ú_acquire_ssh_cert_silently’   s    rW   c             C   s*   t | ttd�}|jdƒdkr&tjdƒ dS )zLAcquire an SSH Cert interactively - This typically only works with Azure CLI)r/   rA   rS   zssh-certzUnable to acquire an ssh-certN)rK   rT   rU   rG   rH   rV   )r4   rJ   r   r   r	   Ú_acquire_ssh_cert_interactive    s    rX   z+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA-AAAAAAAAÚsw)ZkidZxms_kslÚpopzutf-8ú=c             C   s8   dg}t | |td�}t|ƒ |jdƒdkr4tjdƒ dS )zLAcquire a POP token interactively - This typically only works with Azure CLIz-6256c85f-0aad-4d50-b960-e6e9b21efe35/.default)r/   rA   rS   rZ   zUnable to acquire a pop tokenN)rK   Ú	_POP_DATAr   rG   rH   rV   )r4   Z	POP_SCOPErJ   r   r   r	   Ú_acquire_pop_token_interactive¯   s
    r]   c             C   s,   t | ƒ}|r(| j|ƒ tdj|d ƒƒ 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 Pythonr1   N)r5   Zremove_accountr   r   )r4   r6   r   r   r	   Ú_remove_account·   s    
r^   c             C   s(   | j r
dnd}tdj|ƒƒ tjƒ  dS )Z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)Z_enable_brokerr   r   rN   Úexit)r4   Zbug_linkr   r   r	   Ú_exit¾   s    
r`   c              C   s¨  t djtjƒƒ tdddœddjtƒdœgdd„ d	d
d�} | d dkr€tjjtƒr€yt	j
ttdƒjƒ ƒ W n tk
r~   Y nX ttddœtddœdddœgdd„ ddd�}tdƒ}tdƒ}|rÌ|rÌtdƒnd
}tjt|tƒræ|d n|tdddddgddd �||t	d!�}|�rtjtjd"� x„ttttttttttg	d#d„ d$d%�}y||ƒ W nL t k
�r€ } ztj!d&|ƒ W Y d d }~X n t"k
�rœ   t d'ƒ Y nX �q W d S )(Nz4Welcome to the Msal Python {} Tester (Experimental)
Úemptyz<Start with an empty token cache. Suitable for one-off tests.)r,   ÚdescZreusezŒReuse the previous token cache {} (if any) which was created during last test app exit. Useful for testing acquire_token_silent() repeatedlyc             S   s   | d S )Nrb   r   )r+   r   r   r	   r
   ×   s    z_main.<locals>.<lambda>z1What token cache state do you want to begin with?F)r(   r&   r)   r,   Úrz+Azure CLI (Correctly configured for MSA-PT))rD   Únamez/Visual Studio (Correctly configured for MSA-PT)z$95de633a-083e-42f5-b444-a4295d8e9314zAWhiteboard Services (Non MSA-PT app. Accepts AAD & MSA accounts.)c             S   s   | d S )Nrd   r   )r2   r   r   r	   r
   ä   s    zGImpersonate this app (or you can type in the client_id of your own app)TzWEnable broker? It will error out later if your app has not registered some redirect URIzEnable MSAL Python's DEBUG log?zEnable PII in broker's log?rD   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&   r)   )Ú	authorityZenable_broker_on_windowsÚenable_pii_logZtoken_cache)Úlevelc             S   s   | j S )N)Ú__doc__)r   r   r   r	   r
     s    zMSAL Python APIs:)r(   r&   zInvalid input: %sZAborted)#r   r   ÚmsalÚ__version__r-   r   ÚosÚpathÚexistsr   Zdeserializer   ÚreadÚIOErrorrE   rF   r   ZPublicClientApplicationrB   rC   rH   ÚbasicConfigÚDEBUGr8   rK   rM   rQ   rW   rX   r]   r^   r`   r$   rV   ÚKeyboardInterrupt)Zcache_choiceZ
chosen_appZenable_brokerZenable_debug_logrf   r4   ÚfuncÚer   r   r	   Ú_mainÈ   st    

ru   Ú__main__)N)NN),rh   Úbase64rL   r   rH   rN   rk   Úatexitri   r   ZSerializableTokenCacher   ÚregisterrE   rF   r   r   r   Ústrr-   r0   r5   r8   rK   rM   rQ   Z_JWK1rU   rT   rW   rX   Z_POP_KEY_IDr   Z_RAW_REQ_CNFÚurlsafe_b64encodeÚencodeÚdecodeÚrstripr\   r]   r^   r`   ru   Ú__name__r   r   r   r	   Ú<module>   sB   @


#	 
G
