
    0j9                        d dl Z 	 d dlmZ n# e$ r	 d dlmZ Y nw xY wd dlZ ej        e          ZdZdZ	dZ
dZdZdZdZ eed	d
de	dddedeeeg          Zg dZdZd Zd Z G d de          Z G d de          Zd Zd Zd ZdS )    N)urlparsezlogin.microsoftonline.uszlogin.chinacloudapi.cnzlogin.microsoftonline.comzlogin.sovcloud-identity.frzlogin.sovcloud-identity.dezlogin.sovcloud-identity.sgzlogin.microsoft.comzlogin.windows.netzsts.windows.netz login.partner.microsoftonline.cnzlogin.microsoftonline.dezlogin-us.microsoftonline.comzlogin.usgovcloudapi.net)zb2clogin.comzb2clogin.cnzb2clogin.uszb2clogin.dezciamlogin.comz.ciamlogin.comc                 &    | t           v r| nt          S )N)WELL_KNOWN_AUTHORITY_HOSTS
WORLD_WIDEinstances    V/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/msal/authority.py_get_instance_discovery_hostr
   -   s    #===88:M    c                 F    d                     t          |                     S )Nz$https://{}/common/discovery/instance)formatr
   r   s    r	    _get_instance_discovery_endpointr   1   s%    188$X..0 0 0r   c                       e Zd Zd Zd ZdS )AuthorityBuilderc                 n    |                     d          | _        |                    d          | _        dS )zA helper to save caller from doing string concatenation.

        Usage is documented in :func:`application.ClientApplication.__init__`.
        /N)rstrip	_instancestrip_tenant)selfr   tenants      r	   __init__zAuthorityBuilder.__init__7   s.    
 "--||C((r   c                 B    d                     | j        | j                  S )Nzhttps://{}/{})r   r   r   )r   s    r	   __str__zAuthorityBuilder.__str__?   s    %%dndlCCCr   N)__name__
__module____qualname__r   r    r   r	   r   r   6   s7        ) ) )D D D D Dr   r   c                   P    e Zd ZdZ eg           Z	 	 	 d	dZd Zd Zd
dZ	d Z
dS )	AuthorityzThis class represents an (already-validated) authority.

    Once constructed, it contains members named "*_endpoint" for this instance.
    TODO: It will also cache the previously-validated authority instances.
    TNc                    || _         || _        |r|                     |          }n|                     |||          }	 t	          || j                   }nM# t
          $ r@ |rd                    |          nd                    |          dz   }t          |          w xY w|                    d          | _        |d         | _	        |d         | _
        |                    d          | _        t          | j
                  \  }	}	| _        | j        r=|                                 s+t          d	                    | j        |
                    dS dS )a`  Creates an authority instance, and also validates it.

        :param validate_authority:
            The Authority validation process actually checks two parts:
            instance (a.k.a. host) and tenant. We always do a tenant discovery.
            This parameter only controls whether an instance discovery will be
            performed.
        zUnable to get OIDC authority configuration for {url} because its OIDC Discovery endpoint is unavailable at {url}/.well-known/openid-configuration )urlzUnable to get authority configuration for {}. Authority would typically be in a format of https://login.microsoftonline.com/your_tenant or https://tenant_name.ciamlogin.com or https://tenant_name.b2clogin.com/tenant.onmicrosoft.com/policy. z> Also please double check your tenant name or GUID is correct.issuerauthorization_endpointtoken_endpointdevice_authorization_endpointav  The issuer '{iss}' does not match the authority '{auth}' or a known pattern. When using the 'oidc_authority' parameter in ClientApplication, the authority will be validated against the issuer from {auth}/.well-known/openid-configuration .If using a known Entra authority (e.g. login.microsoftonline.com) the 'authority' parameter should be used instead of 'oidc_authority'. )issauthN)_http_client_oidc_authority_url_initialize_oidc_authority_initialize_entra_authoritytenant_discovery
ValueErrorr   get_issuerr%   r&   r'   canonicalizer   has_valid_issuer)
r   authority_urlhttp_clientvalidate_authorityinstance_discoveryoidc_authority_urltenant_discovery_endpointopenid_configerror_message_s
             r	   r   zAuthority.__init__K   s    (#5  	G(,(G(G")$ )$%% )-(H(H13E)G )G%	,,)!# #MM  	, 	, 	,
 &	' ::@&EW&:X:X:XV
 &&TUM ]+++	, %((22&34L&M#+,<=-:->->?^-_-_*()<==1dk # 	AD,A,A,C,C 	A f,>f??A A A	A 	A 	A 	As   A A
Bc                     t          |          \  }| _        }|                                dk    | _        d| _        d| _        |dz   S )NadfsTz!/.well-known/openid-configuration)r2   r   loweris_adfs_is_b2c_is_known_to_developer)r   r8   	authorityr   s       r	   r,   z$Authority._initialize_oidc_authority   sJ    +78J+K+K(	4=&||~~/ '+#!$GGGr   c                 ,    t          |t                    rt          |          }t          |          \  } _        } j                            t                    }|                                dk    o|  _        |j	        
                    d          }t           fdt          D                       p?t          |          dk    o,|d                                                             d           _         j        p	 j        p|  _         j        t"          v }|dv rt%           j                  n|}	|	rq|so j        sht'          d                     j        |j	                   j        |	          }
|
                    d	          d
k    rt/          d|z            |
d         }ng|                    d                    |rt          |j	                  dk    r|n|j	         j        rdnd                                                    }|S )Nr>   r   c              3   R   K   | ]!}j                             d |z             V  "dS .N)r   endswith).0dr   s     r	   	<genexpr>z8Authority._initialize_entra_authority.<locals>.<genexpr>   sH        01DM""37++     r         b2c_)NTz"https://{}{}/oauth2/v2.0/authorizeerrorinvalid_instancezinvalid_instance: The authority you provided, %s, is not known. If it is a valid domain name known to you, you can turn off this check by passing in instance_discovery=Falser9   z2{prefix}{version}/.well-known/openid-configuration    z/v2.0)prefixversion)path)
isinstancer   strr2   r   rH   _CIAM_DOMAIN_SUFFIXr?   r@   rU   splitanyWELL_KNOWN_B2C_HOSTSlen
startswithrA   rB   r   r   _instance_discoveryr   r*   r0   r/   _replacegeturl)r   r4   r6   r7   rC   r   is_ciampartsis_known_to_microsoftinstance_discovery_endpointpayloadr9   s   `           r	   r-   z%Authority._initialize_entra_authority   sa    m%566 	/..M+7+F+F(	4=&-(()<==||~~/?K$$S))    5I     Ke**/IeAhnn&6&6&A&A&&I&I 	 '+l&\dl&\J\F\# $1K K $|33 'GM' ' '9K 	$ ' 	%	)-)D	)4;;M9>3 3!+	- -G
 {{7##'999 /
 $$% % % )00K(L%%(1(:(:IPP%, ,Y^1D1D1I1I66&^"&,;BBG Q   ); ) ) &(( & )(r   c                 Z   | j         | j        j        vr|p8| j                            d                    | j         |          d|d          }|j        dk    r-|                                 t          j	        |j
                  S | j        j                            | j                    i S )Nz<https://{netloc}/common/userrealm/{username}?api-version=1.0)netlocusernamezapplication/json)Acceptzclient-request-id)headersi  )r   	__class__%_domains_without_user_realm_discoveryr*   r0   r   status_coderaise_for_statusjsonloadstextadd)r   rh   correlation_idresponseresps        r	   user_realm_discoveryzAuthority.user_realm_discovery   s     = TTT @t044NUU=8 V = =#5.<> >  5  @  @D
 3&&%%'''z$),,,N@DDT]SSS	r   c                    | j         r| j        sdS | j                             d          | j                            d          k    rdS t          | j                   }t          | j                  }|j        r|j                                        ndsdS t          v rdS                     d          }|dk    rN|dz   d         }dd|         vr5|t          v rdS |j        r|j                                        nd}||k    rdS |j        |j        k    r|j	        |j	        k    rdS t          fd	t          D                       rdS dS )
a]  
        Returns True if the issuer from OIDC discovery is valid for this authority.

        An issuer is valid if one of the following is true:
        - It exactly matches the authority URL (with/without trailing slash)
        - It has the same scheme and host as the authority (path can be different)
        - The issuer host is a well-known Microsoft authority host
        - The issuer host is a regional variant of a well-known host (e.g., westus2.login.microsoft.com)
        - For CIAM, hosts that end with well-known B2C hosts (e.g., tenant.b2clogin.com) are accepted as valid issuers
        Fr   TNrG   r   rQ   rR   c              3   H   K   | ]}                     d |z             V  dS rF   )rH   )rI   hissuer_hosts     r	   rK   z-Authority.has_valid_issuer.<locals>.<genexpr>   s5      KK{##C!G,,KKKKKKr   )r1   r+   r   r   hostnamer?   r   findschemerg   rZ   r[   )r   issuer_parsedauthority_parsed	dot_indexpotential_baseauthority_hostrz   s         @r	   r3   zAuthority.has_valid_issuer   s    | 	4#; 	5 <s##t'?'F'Fs'K'KKK4 ..#D$<==8E8NXm,22444TX 	5 4444  $$S))	q==(Q8N+jyj111!%???4 GWF_!g!1!:!@!@!B!B!Beg!^334 #}';;;#}';;;4
 KKKK6JKKKKK 	4ur   )TNN)NN)r   r   r   __doc__setrl   r   r,   r-   rv   r3   r   r   r	   r!   r!   C   s         
 -0CGG)  $##	7A 7A 7A 7ApH H H-) -) -)^    7 7 7 7 7r   r!   c                    t          |           }|j        dk    r|j        r|j                            d          }t          |          dk    r|d         r|d         nd }|j                            t                    rG|r|n8d                    |j        	                    t          d          d                   }||j        |fS t          |          dk    r|d         r||j        |d         fS t          d| z            )Nhttpsr   rM   rQ   z{}.onmicrosoft.comr   a  Your given address (%s) should consist of an https url with hostname and a minimum of one segment in a path: e.g. https://login.microsoftonline.com/{tenant} or https://{tenant_name}.ciamlogin.com/{tenant} or https://{tenant_name}.b2clogin.com/{tenant_name}.onmicrosoft.com/policy)r   r}   r{   rU   rY   r\   rH   rX   r   rsplitr/   )authority_or_auth_endpointrC   rb   
first_partr   s        r	   r2   r2     s   344I7""y'9"$$S))!$UqU1XU1XX4
&&':;; 	9#- FZZ3G3N3N"))*=qAA!D4F 4FF i0&88u::??uQx?i0%(::
	U
 %	%& & &r   c                 Z     |j         |fd| ddi|}t          j        |j                  S )Nparamsz1.0)r%   zapi-version)r0   ro   rp   rq   )r#   r5   rd   kwargsru   s        r	   r^   r^     sK    ;?# *-eDD  D :di   r   c                 P    |j         | fi |}|j        dk    rt          j        |j                  S d|j        cxk    rdk     r1n n.t          d                    | |j        |j                            |                                 t          d|j        |j        fz            )N   i  i  z7OIDC Discovery failed on {}. HTTP status: {}, Error: {}z)Unable to complete OIDC Discovery: %d, %s)	r0   rm   ro   rp   rq   r/   r   rn   RuntimeError)r9   r5   r   ru   s       r	   r.   r.   "  s    ;?4????D3z$)$$$
d$$$$$$$$$ RYY%I    	 	
3t7G6SSU U Ur   )ro   urllib.parser   ImportErrorlogging	getLoggerr   loggerAZURE_US_GOVERNMENTDEPRECATED_AZURE_CHINAAZURE_PUBLICAZURE_GOV_FRAZURE_GOV_DEAZURE_GOV_SGr   	frozensetr   r[   rX   r
   r   objectr   r!   r2   r^   r.   r   r   r	   <module>r      s   "%%%%%%% " " "!!!!!!!!" 		8	$	$ 1 1 *+++(
&Y&"(        ' N N N0 0 0

D 
D 
D 
D 
Dv 
D 
D 
D       B& & &.! ! !U U U U Us    