
    9i2                        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         G d de      Z G d de      ZdZd Zd ZddZd Zd Zd Z	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 d dZddZ d Z!y# eef$ r6 dddd	j!                  ej"                        Zer ed
e d       ed      w xY w)!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j                  |t        j                        |       S N)	pymsalruntimeLogLevelTRACEloggerdebugDEBUGFATALcriticalget)messagelevels     >/var/www/html/venv/lib/python3.12/site-packages/msal/broker.py<lambda>r      se    $$fll$$fll
 	$$fooD
 #eV\\
"7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y)RedirectUriErrorN__name__
__module____qualname__ r   r   r   r   (       r   r   c                       e Zd Zy)TokenTypeErrorNr   r   r   r   r"   r"   ,   r    r   r"   z$msauth.com.msauth.unsignedapp://authc                 L   | j                         }d|v sd|v rt        dj                  |t                    ddj                  || j	                         | j                         | j                               | j	                         | j                         | j                         dS )NAADSTS50011AADSTS7000218zMsalRuntime needs the current app to register these redirect_uri
(1) ms-appx-web://Microsoft.AAD.BrokerPlugin/{}
(2) {}
(3) https://login.microsoftonline.com/common/oauth2/nativeclientbroker_errorz'{}. Status: {}, Error code: {}, Tag: {})errorerror_description_broker_status_broker_error_code_broker_tag)get_contextr   format_redirect_uri_on_mac
get_statusget_error_codeget_tag)r'   	client_idcontexts      r   _convert_errorr4   8   s    !GW$')D EKF+E-	. 	.   GMM 4 4 6I  **,#224}}

 

r   c                 J   t        j                         }t        j                  | ||fd       |j                  j	                          |j
                  j                         }|r!t        j                  dt        |d             y|j
                  j                         }|r|S y)zEReturn an instance of MSALRuntimeError or MSALRuntimeAccount, or Nonec                 $    |j                  |       S r   completeresultcallback_datas     r   r   z%_read_account_by_id.<locals>.<lambda>Y       M4J4J64R r   zread_account_by_id() error: %sN)r   CallbackDataread_account_by_idsignalwaitr:   	get_errorr   r   r4   get_account)
account_idcorrelation_idr;   r'   accounts        r   _read_account_by_idrF   S   s    !..0M$$%2R

   **,E5~eT7RS""..0Gr   c                    | j                         }|j                  dd        | j                         }|rt        t	        ||      |      S | j                         r#t        j                  | j                               ni }| j                         }|sJ d       | j                         r!| j                         j                         d   n| j                         | j                         t        t        j                               z
  | j!                         ||j#                         |j%                         | j                         rdn|xs ddj'                         D ci c]
  \  }}|r|| }	}}|	d   j)                  d	      }
|	d
   j+                         dk(  r"|
s t-        dj/                  |	d   d d             | j1                         }|rdj3                  |      |	d<   t        |	|      S c c}}w )Nwam_telemetry)_msalruntime_telemetryz*Account is expected to be always availabler   popbearer)access_token
expires_inid_tokenid_token_claimsclient_info_account_id
token_typerL   AAAArR   zssh-certz'Broker could not get an SSH Cert: {}...    scope)get_telemetry_datarJ   rA   dictr4   get_id_tokenjsonloadsrB   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:   r2   expected_token_type	telemetryr'   rO   rE   kvreturn_valuelikely_a_certgranted_scopess               r   _convert_resultrr   f   s   ))+IMM/4(EN5)4YWW;A;N;N;Pdjj!4!4!67VXO  "G@@@7 **, ++-282I2I2K99;c$))+>NN++-*..0--/446+8&
 %'TQ  qD L  !0;;FCML!'')Z7FMM(!,. / 	/..0N # 8WY??)s    G1c                  <    t        t        j                               S r   )struuiduuid4r   r   r   _get_new_correlation_idrw      s    tzz|r   c                 (    | j                  dd       y )Nmsal_request_typeconsumer_passthrough)set_additional_parameter)paramss    r   _enable_msa_ptr}      s    
##$79OPr   c                     t        j                  | |      }|j                  dt               |j                  dt               |S )Nmsal_client_skumsal_client_ver)r   MSALRuntimeAuthParametersr{   r   r   )r2   	authorityr|   s      r   _build_msal_runtime_auth_paramsr      s;    44Y	JF
##$5s;
##$5{CMr   c                    t        ||       }|j                  |       |r|j                  |       |rP|j                  |j                  |j
                  j                  |j
                  j                  |j                         t        j                         }	|j                         D ]#  \  }
}|	|j                  |
t        |             % |rt        |       t        j                  ||xs
 t!               |	fd       |	j"                  j%                          t'        |	j(                  ||j+                  d            S )Nc                 $    |j                  |       S r   r7   r9   s     r   r   z"_signin_silently.<locals>.<lambda>   r<   r   rR   rk   )r   set_requested_scopesset_decoded_claimsset_pop_params_http_method_urlnetlocpath_noncer   r=   rf   r{   rt   r}   signin_silentlyrw   r?   r@   rr   r:   r   )r   r2   scopesrD   claimsenable_msa_ptauth_schemekwargsr|   r;   rm   rn   s               r   _signin_silentlyr      s   
 -Y	BF
'!!&)$$k&6&6&=&={?O?O?T?T	  "..0M 71=++As1v67 v!!313%2RT iVZZ=UW Wr   c
                    t        ||       }|j                  |       |j                  t        j                  dk(  rt
        nd       |r5|dk(  r|r.d }t        j                  d       nt        j                  d|       ||j                  dd       |rt        |       |	rP|j                  |	j                  |	j                  j                  |	j                  j                  |	j                         |
j!                         D ]#  \  }}|	|j                  |t#        |             % |r|j%                  |       t'        j(                  d	      }t'        j*                  |xs* t'        j,                         xs t'        j.                         ||xs
 t1               ||fd
       |j2                  j5                          t7        |j8                  ||
j;                  d            S )Nr   z<https://login.microsoftonline.com/common/oauth2/nativeclient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                 $    |j                  |       S r   r7   r9   s     r   r   z'_signin_interactively.<locals>.<lambda>   r<   r   rR   r   )r   r   set_redirect_urisysplatformr.   r   warningr{   r}   r   r   r   r   r   r   rf   rt   r   r   r=   signin_interactivelyget_console_windowget_desktop_windowrw   r?   r@   rr   r:   r   )r   r2   r   parent_window_handleprompt
login_hintr   rD   r   r   r   r|   rm   rn   r;   s                  r   _signin_interactivelyr      s    -Y	BF
'
 # 8F %% "
lmNNFO#''(96Bv$$k&6&6&=&={?O?O?T?T	   71=++As1v67 !!&)!..dCM&&h @ @ BhmFfFfFh313%2RT iVZZ=UW Wr   c                    |xs
 t               }t        ||      }|y t        ||       }	|	j                  |       |r|	j	                  |       |rP|	j                  |j                  |j                  j                  |j                  j                  |j                         |j                         D ]#  \  }
}|	|	j                  |
t        |             % t        j                         }t        j                   |	|||fd       |j"                  j%                          t'        |j(                  ||j+                  d            S )Nc                 $    |j                  |       S r   r7   r9   s     r   r   z)_acquire_token_silently.<locals>.<lambda>  r<   r   rR   r   )rw   rF   r   r   r   r   r   r   r   r   r   rf   r{   rt   r   r=   acquire_token_silentlyr?   r@   rr   r:   r   )r   r2   rC   r   r   rD   r   r   rE   r|   rm   rn   r;   s                r   _acquire_token_silentlyr      s+    $@'>'@N!*n=G,Y	BF
'!!&)$$k&6&6&=&={?O?O?T?T	   71=++As1v67 "..0M((%2R	T
 iVZZ=UW Wr   c                     |xs
 t               }t        ||      }|y t        j                         }t        j                  | |||fd       |j
                  j                          |j                  j                         }|rt        ||       S y )Nc                 $    |j                  |       S r   r7   r9   s     r   r   z#_signout_silently.<locals>.<lambda>  r<   r   )
rw   rF   r   r=   signout_silentlyr?   r@   r:   rA   r4   )r2   rC   rD   rE   r;   r'   s         r   _signout_silentlyr     s    #@'>'@N!*n=G!..0M""%2R	T
   **,EeY// r   c                  .    t        j                  d       y )Nr   )r   set_is_pii_enabledr   r   r   _enable_pii_logr     s    $$Q'r   r   )NNFN)NNNNFN)NNN)"__doc__rZ   loggingr   rb   ru   skur   r   	getLoggerr   r   r   register_logging_callbackImportErrorAttributeErrorr   r   min_ver
ValueErrorr   r"   r.   r4   rF   rr   rw   r}   r   r   r   r   r   r   r   r   r   <module>r      sE     
   !			8	$V+M++ -, -2	z 		Z 	 > 
6&@BQ CGW> 5Wr OSW@0 (A 	^$ V  
c#,,	 
 I'RVWY 	Y TUUVs   B =C