
    2i9                        d dl Z 	 d dlmZ 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y# e$ r	 d dlmZ Y rw xY w)    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| S t        S )N)WELL_KNOWN_AUTHORITY_HOSTS
WORLD_WIDEinstances    D/var/www/html/qr/venv/lib/python3.12/site-packages/msal/authority.py_get_instance_discovery_hostr
   -   s    #==8M:M    c                 6    dj                  t        |             S )Nz$https://{}/common/discovery/instance)formatr
   r   s    r	    _get_instance_discovery_endpointr   1   s    188$X.0 0r   c                       e Zd Zd Zd Zy)AuthorityBuilderc                 \    |j                  d      | _        |j                  d      | _        y)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                 N    dj                  | j                  | j                        S )Nzhttps://{}/{})r   r   r   )r   s    r	   __str__zAuthorityBuilder.__str__?   s    %%dnndllCCr   N)__name__
__module____qualname__r   r    r   r	   r   r   6   s    )Dr   r   c                   H    e Zd ZdZ eg       Z	 	 	 ddZd Zd Zd	dZ	d Z
y)
	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.
    Nc                 ^   || _         || _        |r| j                  |      }n| j                  |||      }	 t	        || j                         }|j                  d      | _        |d   | _	        |d   | _
        |j                  d      | _        t        | j                        \  }	}	| _        | j                  r7| j                         s&t        d	j                  | j                  |
            yy# t
        $ r4 |rdj                  |      ndj                  |      dz   }t        |      w xY w)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   sZ    (#5 (,(G(G")$% )-(H(H13E)G%	,,)!!#M" %((2&34L&M#+,<=-:->->?^-_*()<)<=1dk ##D,A,A,C f,>f?A A -D#+  	,
 &::@&EW&:XV
 &TUM ]++	,s   C/ /=D,c                     t        |      \  }| _        }|j                         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   sE    +78J+K(	4=&||~/ '+#!$GGGr   c                 
    t        |t              rt        |      }t        |      \  } _        } j                  j                  t              }|j                         dk(  xr |  _        |j                  j                  d      }t         fdt        D              xs2 t        |      dk(  xr" |d   j                         j                  d       _         j                  xs  j                  xs |  _         j                  t"        v }|dv rt%         j                        n|}	|	rr|sp j                   sdt'        dj)                   j                  |j                         j*                  |	      }
|
j-                  d	      d
k(  rt/        d|z        |
d   }|S |j1                  dj)                  |rt        |j                        dk  r|n|j                   j                  rdnd            j3                         }|S )Nr>   r   c              3   Z   K   | ]"  }j                   j                  d |z          $ yw.N)r   endswith).0dr   s     r	   	<genexpr>z8Authority._initialize_entra_authority.<locals>.<genexpr>   s)      01DMM""37+s   (+      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   s    m%56.M+7+F(	4=&--(()<=||~/?K$$S) 5I  Ke*/IeAhnn&6&A&A&&I 	 '+ll&\dll&\J\F\# $1K K $|3 'GMM'9K 	$ '%)D)D)4;;MM9>>3!!+	-G
 {{7#'99 /
 $$% % )00K(L% )( )2(:(:IPP%,Y^^1D1I6&^^"&,,BG Q  ); ) &( & )(r   c                    | j                   | j                  j                  vr|xs; | j                  j	                  dj                  | j                   |      d|d      }|j                  dk7  r/|j                          t        j                  |j                        S | j                  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044NUU==8 V =#5.<>  5  @D
 3&%%'zz$)),,NN@@DDT]]S	r   c                    | j                   r| j                  sy| j                   j                  d      | j                  j                  d      k(  ryt        | j                         }t        | j                        }|j                  r|j                  j                         ndsyt        v ryj                  d      }|dkD  rF|dz   d }dd| vr7|t        v ry|j                  r|j                  j                         nd}||k(  ry|j                  |j                  k(  r|j                  |j                  k(  ryt        fd	t        D              ryy)
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   F   K   | ]  }j                  d |z           ywrF   )rH   )rI   hissuer_hosts     r	   rK   z-Authority.has_valid_issuer.<locals>.<genexpr>   s      K{##C!G,Ks   !)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   sM    ||4#;#; <<s#t'?'?'F'Fs'KK .#D$<$<=8E8N8Nm,,224TX 44  $$S)	q=(Q8N+jy11!%?? GWF_F_!1!:!:!@!@!Beg!^3 ##}';';;##}';';;
 K6JKKr   )TNN)NN)r   r   r   __doc__setrl   r   r,   r-   rv   r3   r   r   r	   r!   r!   C   s;    
 -0G)  $##	7ApH-)^ 7r   r!   c                    t        |       }|j                  dk(  r|j                  r|j                  j	                  d      }t        |      dk\  r
|d   r|d   nd }|j                  j                  t              rE|r|n1dj                  |j                  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4I7"y'9'9$$S)!$UqU1XU1X4
&&':;#-Z3G3N3N""))*=qA!D4FF i00&88u:?uQxi00%(::
	U
 %	%& &r   c                 p     |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^     sA    ;??#*-eD D ::dii  r   c                 |    |j                   | fi |}|j                  dk(  rt        j                  |j                        S d|j                  cxk  rdk  r3n n0t        dj                  | |j                  |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zz$))$$
d$$ RYY%II  	 	
3t7G7G6SS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 *+++(
&&"(     ' N0

Dv 
D B&.!U}  "!"s   A1 1A?>A?