
    0jv3                        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mZmZ  ej	        e
          Z	 ddlZ ej        d            nE# eef$ r; dddd	                    ej                  Zer ed
e d           ed          w xY w G d de          Z G d de          ZdZdZd Zd Zd dZd Zd Zd Zd Z	 	 	 d!dZ	 	 	 	 	 	 d"dZ 	 	 d#dZ!d dZ"d Z#dS )$zThis module is an adaptor to the underlying broker.
It relies on PyMsalRuntime which is the package providing broker's functionality.
    N   )__version__SKUc                     t          j        j        t          j        t           j        j        t          j        t           j        j        t          j        i                    |t          j                  |           S N)	pymsalruntimeLogLevelTRACEloggerdebugDEBUGFATALcriticalget)messagelevels     S/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/msal/broker.py<lambda>r      sT    $fl$fl
 	$foD
 #eV\
"
"7D, D,     z1.20z1.31z1.33)win32darwinlinuxz;You must install dependency by: pip install "msal[broker]>=z,<2"z8Dependency pymsalruntime unavailable on current platformc                       e Zd ZdS )RedirectUriErrorN__name__
__module____qualname__ r   r   r   r   (           Dr   r   c                       e Zd ZdS )TokenTypeErrorNr   r   r   r   r"   r"   ,   r    r   r"   z$msauth.com.msauth.unsignedapp://authz<https://login.microsoftonline.com/common/oauth2/nativeclientc                    |                                  }d|v sd|v r.t          d                    |t          t                              dd                    ||                                 |                                 |                                           |                                 |                                 |                                 dS )NAADSTS50011AADSTS7000218z~MsalRuntime needs the current app to register these redirect_uri
(1) ms-appx-web://Microsoft.AAD.BrokerPlugin/{}
(2) {}
(3) {}broker_errorz'{}. Status: {}, Error code: {}, Tag: {})errorerror_description_broker_status_broker_error_code_broker_tag)get_contextr   format_default_redirect_uri_on_mac_default_redirect_uri
get_statusget_error_codeget_tag)r'   	client_idcontexts      r   _convert_errorr5   <   s    !!GW$$'))
 &8:O
P
P	R R 	R   GMM 4 4 6 6I I  **,,#2244}}

 

 

r   c                 V   t          j                    }t          j        | ||fd           |j                                         |j                                        }|r+t                              dt          |d                     dS |j        
                                }|r|S dS )zEReturn an instance of MSALRuntimeError or MSALRuntimeAccount, or Nonec                 ,    |                     |           S r   completeresultcallback_datas     r   r   z%_read_account_by_id.<locals>.<lambda>\       M4J4J64R4R r   zread_account_by_id() error: %sN)r   CallbackDataread_account_by_idsignalwaitr;   	get_errorr   r   r5   get_account)
account_idcorrelation_idr<   r'   accounts        r   _read_account_by_idrG   V   s    !.00M$%2RRR
 
 

  **,,E 5~eT7R7RSSSt"..00G 4r   c           	         |                                  }|                    dd            |                                 }|rt          t	          ||          |          S |                                 r&t          j        |                                           ni }|                                 }|s
J d            d | 	                                r,| 
                                                                d         n|                                 |                                 t          t          j                              z
  |                                 ||                                |                                | 	                                rdn|pdd                                D             }|d	                             d
          }|d                                         dk    r2|s0t-          d                    |d	         d d                             |                                 }	|	rd                    |	          |d<   t          ||          S )Nwam_telemetry)_msalruntime_telemetryz*Account is expected to be always availablec                     i | ]
\  }}|||S r   r   ).0kvs      r   
<dictcomp>z#_convert_result.<locals>.<dictcomp>s   s4       TQ Aq   r   r   popbearer)access_token
expires_inid_tokenid_token_claimsclient_info_account_id
token_typerR   AAAArX   zssh-certz'Broker could not get an SSH Cert: {}...    scope)get_telemetry_datarP   rB   dictr5   get_id_tokenjsonloadsrC   is_pop_authorizationget_authorization_headersplitget_access_tokenget_access_token_expiry_timeinttimeget_raw_id_tokenget_client_infoget_account_iditems
startswithlowerr"   r-   get_granted_scopesjoin)
r;   r3   expected_token_type	telemetryr'   rU   rF   return_valuelikely_a_certgranted_scopess
             r   _convert_resultrv   i   sS   ))++IMM/4(((E XN5)44YWWWW;A;N;N;P;PXdj!4!4!6!6777VXO  ""G@@@@@7  **,,LF++-- 282I2I2K2K99;;c$)++>N>NN++--*..00--//4466 -+8&
 &
 %''  L !0;;FCCML!''))Z777FMM(!,. . / / 	/..00N 9 # 8 8WY????r   c                  B    t          t          j                              S r   )struuiduuid4r   r   r   _get_new_correlation_idr{      s    tz||r   c                 2    |                      dd           d S )Nmsal_request_typeconsumer_passthrough)set_additional_parameterparamss    r   _enable_msa_ptr      s     
##$79OPPPPPr   c                     t          j        | |          }|                    dt                     |                    dt                     |S )Nmsal_client_skumsal_client_ver)r   MSALRuntimeAuthParametersr   r   r   )r3   	authorityr   s      r   _build_msal_runtime_auth_paramsr      sG    4Y	JJF
##$5s;;;
##$5{CCCMr   c                     t           j        dk    r|                     t                     d S |                     t                     d S )Nr   )sysplatformset_redirect_urir.   r/   r   s    r   _set_redirect_urir      sE    
|x <===== 566666r   Fc                    t          ||           }t          |           |                    |           |r|                    |           |r6|                    |j        |j        j        |j        j        |j	                   t          j                    }	|                                D ]*\  }
}|#|                    |
t          |                     +|rt          |           t          j        ||pt#                      |	fd           |	j                                         t)          |	j        ||                    d                    S )Nc                 ,    |                     |           S r   r8   r:   s     r   r   z"_signin_silently.<locals>.<lambda>   r=   r   rX   rq   )r   r   set_requested_scopesset_decoded_claimsset_pop_params_http_method_urlnetlocpath_noncer   r>   rl   r   rx   r   signin_silentlyr{   r@   rA   rv   r;   r   )r   r3   scopesrE   claimsenable_msa_ptauth_schemekwargsr   r<   rM   rN   s               r   _signin_silentlyr      sn   
 -Y	BBFf
''' *!!&)))  $k&6&={?O?T	  	  	  ".00M 7 71=++As1vv666 v!3133%2RRRT T T iVZZ=U=UW W W Wr   c
                    t          ||           }|                    |           t          |           |r@|dk    r|rd }t                              d           nt                              d|           ||                    dd           |rt          |           |	r6|                    |	j        |	j	        j
        |	j	        j        |	j                   |
                                D ]*\  }}|#|                    |t          |                     +|r|                    |           t!          j        d          }t!          j        |p%t!          j                    pt!          j                    ||pt+                      ||fd           |j                                         t1          |j        ||
                    d	          
          S )Nselect_accountzKUsing both select_account and login_hint is ambiguous. Ignoring login_hint.z)prompt=%s is not supported by this modulemsal_gui_threadtrueT)is_interactivec                 ,    |                     |           S r   r8   r:   s     r   r   z'_signin_interactively.<locals>.<lambda>   r=   r   rX   r   )r   r   r   r   warningr   r   r   r   r   r   r   r   rl   rx   r   r   r>   signin_interactivelyget_console_windowget_desktop_windowr{   r@   rA   rv   r;   r   )r   r3   r   parent_window_handleprompt
login_hintr   rE   r   r   r   r   rM   rN   r<   s                  r   _signin_interactivelyr      s     -Y	BBF
'''f P%%% n "
lmmmNNFOOO#''(96BBB v  $k&6&={?O?T	  	  	   7 71=++As1vv666 *!!&)))!.dCCCM&h @ B BhmFfFhFh3133%2RRRT T T iVZZ=U=UW W W Wr   c                    |pt                      }t          ||          }|d S t          ||           }	t          |	           |	                    |           |r|	                    |           |r6|	                    |j        |j        j	        |j        j
        |j                   |                                D ]*\  }
}|#|	                    |
t          |                     +t          j                    }t          j        |	|||fd           |j                                         t)          |j        ||                    d                    S )Nc                 ,    |                     |           S r   r8   r:   s     r   r   z)_acquire_token_silently.<locals>.<lambda>  r=   r   rX   r   )r{   rG   r   r   r   r   r   r   r   r   r   r   rl   r   rx   r   r>   acquire_token_silentlyr@   rA   rv   r;   r   )r   r3   rD   r   r   rE   r   r   rF   r   rM   rN   r<   s                r   _acquire_token_silentlyr      sy    $@'>'@'@N!*n==G,Y	BBFf
''' *!!&)))  $k&6&={?O?T	  	  	   7 71=++As1vv666!.00M(%2RRR	T T T
 iVZZ=U=UW W W Wr   c                 0   |pt                      }t          ||          }|d S t          j                    }t          j        | |||fd           |j                                         |j                                        }|rt          ||           S d S )Nc                 ,    |                     |           S r   r8   r:   s     r   r   z#_signout_silently.<locals>.<lambda>  r=   r   )
r{   rG   r   r>   signout_silentlyr@   rA   r;   rB   r5   )r3   rD   rE   rF   r<   r'   s         r   _signout_silentlyr     s    #@'>'@'@N!*n==G!.00M"%2RRR	T T T
  **,,E 0eY///0 0r   c                  .    t          j        d           d S )Nr   )r   set_is_pii_enabledr   r   r   _enable_pii_logr     s    $Q'''''r   r   )NNFN)NNNNFN)NNN)$__doc__r`   loggingr   rh   ry   skur   r   	getLoggerr   r   r   register_logging_callbackImportErrorAttributeErrorr   r   min_ver
ValueErrorr   r"   r.   r/   r5   rG   rv   r{   r   r   r   r   r   r   r   r   r   r   r   <module>r      s      



   ! ! ! ! ! ! ! !		8	$	$V+M+ -, -, - - - - 	^$ V V V   
c#,	 
  VkW'WWWY Y 	Y kTUUUV 	 	 	 	 	z 	 	 		 	 	 	 	Z 	 	 	  F  W 

 
 
4  &@ @ @ @B  Q Q Q  7 7 7 CGW W W W@ 0W 0W 0W 0Wh OSW W W WB0 0 0 0 ( ( ( ( (s   A AB