
    #	h:                     F   S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrSSKJ	r	  \R                  " \5      rSS jr\rS r " S S\5      r " S S	\5      r " S
 S\5      r " S S\5      r " S S\5      rSS jrS r " S S\5      r " S S\	R4                  5      rg)    N   )oauth2c                     U S[        U 5      * S-  -  -  n [        U 5      n [        R                  " U 5      nU(       a  UR	                  U5      nU$ )a  Decode a part of the JWT.

JWT is encoded by padding-less base64url,
based on `JWS specs <https://tools.ietf.org/html/rfc7515#appendix-C>`_.

:param encoding:
    If you are going to decode the first 2 parts of a JWT, i.e. the header
    or the payload, the default value "utf-8" would work fine.
    If you are going to decode the last part i.e. the signature part,
    it is a binary string so you should use `None` as encoding here.
=   )lenstrbase64urlsafe_b64decodedecode)rawencodingoutputs      E/var/www/html/env/lib/python3.13/site-packages/msal/oauth2cli/oidc.pydecode_partr      sT     33s8)a-  C
 	C %%c*Fx(M    c                 X    [         R                  " S[         R                  " U 5      5      $ )Nz%Y-%m-%d %H:%M:%S)timestrftime	localtime)epochs    r   _epoch_to_localr   '   s    ==,dnnU.CDDr   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )IdTokenError*   zMIn unlikely event of an ID token is malformed, this exception will be raised.c                   > [         [        U ]  U< S[        U5      < S[        R
                  " [        UUR                  S5      (       a  [        US   5      OS UR                  S5      (       a  [        US   5      OS S9SS9< 35        g )Nz Current epoch = z#.  The id_token was approximately: iatexp)r   r      indent)superr   __init__r   jsondumpsdictgetselfreasonnowclaims	__class__s       r   r#   IdTokenError.__init__,   ss    lD*OC($**T6<jj6G6GOF5M2T6<jj6G6GOF5M2T6 	+	r    )__name__
__module____qualname____firstlineno____doc__r#   __static_attributes____classcell__r-   s   @r   r   r   *   s    W r   r   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )_IdTokenTimeError5   z>Make sure your computer's time and time zone are both correct.c                 N   > [         [        U ]  US-   U R                  -   X#5        g )N )r"   r9   r#   _SUGGESTIONr(   s       r   r#   _IdTokenTimeError.__init__7   s%    /d>N>N0NPS\r   c                 @    [         R                  [        U 5      5        g N)loggerwarningr	   )r)   s    r   log_IdTokenTimeError.log9   s     	s4y!r   r/   )	r0   r1   r2   r3   r=   r#   rC   r5   r6   r7   s   @r   r9   r9   5   s    RK]
" 
"r   r9   c                       \ rS rSrSrg)IdTokenIssuerErrorE   r/   Nr0   r1   r2   r3   r5   r/   r   r   rF   rF   E       r   rF   c                       \ rS rSrSrg)IdTokenAudienceErrorH   r/   NrH   r/   r   r   rK   rK   H   rI   r   rK   c                       \ rS rSrSrg)IdTokenNonceErrorK   r/   NrH   r/   r   r   rN   rN   K   rI   r   rN   c                    [         R                  " [        U R                  S5      S   5      5      n[	        U=(       d    [
        R
                  " 5       5      nSnXg-   UR                  SUS-
  5      :  a  [        SXe5      R                  5         U(       a  X%S   :w  a  [        SU-  UU5      eU(       a=  [        US   [        5      (       a  XS   ;   OXS   :H  nU(       d  [        S	U-  UU5      eXg-
  US
   :  a  [        SXe5      R                  5         U(       a!  X5R                  S5      :w  a  [        SUU5      eU$ )ax  Decodes and validates an id_token and returns its claims as a dictionary.

ID token claims would at least contain: "iss", "sub", "aud", "exp", "iat",
per `specs <https://openid.net/specs/openid-connect-core-1_0.html#IDToken>`_
and it may contain other optional content such as "preferred_username",
`maybe more <https://openid.net/specs/openid-connect-core-1_0.html#Claims>`_
.r   x   nbfz!0. The ID token is not yet valid.issz2. The Issuer Identifier for the OpenID Provider, "%s", (which is typically obtained during Discovery), MUST exactly match the value of the iss (issuer) Claim.audz|3. The aud (audience) claim must contain this client's client_id "%s", case-sensitively. Was your client_id in wrong casing?r   z 9. The ID token already expires.noncezX11. Nonce must be the same value as the one that was sent in the Authentication Request.)r$   loadsr   splitintr   r'   r9   rC   rF   
isinstancelistrK   rN   )	id_token	client_idissuerrV   r+   decoded_nowskew	valid_auds	            r   decode_id_tokenrc   N   sN    jjX^^C%8%;<=Gs!diik"DD{W[[q11 	=tMQQS&EN* FHNO  	 3=END4" 4"I/'0EN'B 	&N    {WU^#<dLPPR++g..F	 	 Nr   c                 h    [         R                  " U R                  S5      5      R                  5       $ )Nascii)hashlibsha256encode	hexdigest)rV   s    r   _nonce_hashrj      s#    >>%,,w/0::<<r   c                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
Prompt   zThis class defines the constant strings for prompt parameter.

The values are based on
https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest
noneloginconsentselect_accountcreater/   N)r0   r1   r2   r3   r4   NONELOGINCONSENTSELECT_ACCOUNTCREATEr5   r/   r   r   rl   rl      s"    
 DEG%NFr   rl   c                      ^  \ rS rSrSrSS jrU 4S jrSU 4S jjrSU 4S jjr SU 4S jjr	U 4S jr
       SU 4S	 jjrS
rU =r$ )Client   z^OpenID Connect is a layer on top of the OAuth2.

See its specs at https://openid.net/connect/
c                 ^    [        XU R                  U R                  R                  S5      S9$ )zSee :func:`~decode_id_token`.r^   )rV   r]   r^   )rc   r]   configurationr'   )r)   r\   rV   s      r   rc   Client.decode_id_token   s.    nnT-?-?-C-CH-MO 	Or   c                 r   > [         [        U ]
  " U/UQ70 UD6nSU;   a  U R                  US   5      US'   U$ )zThe result will also contain one more key "id_token_claims",
whose value will be a dictionary returned by :func:`~decode_id_token`.
r\   id_token_claims)r"   ry   _obtain_tokenrc   )r)   
grant_typeargskwargsretr-   s        r   r   Client._obtain_token   sG     FD/
LTLVL%)%9%9#j/%JC!"
r   c                 h   > [         R                  " S[        5        [        [        U ]  " U4SU0UD6$ )af  Generate an authorization uri to be visited by resource owner.

Return value and all other parameters are the same as
:func:`oauth2.Client.build_auth_request_uri`, plus new parameter(s):

:param nonce:
    A hard-to-guess string used to mitigate replay attacks. See also
    `OIDC specs <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.
z%Use initiate_auth_code_flow() insteadrV   )warningswarnDeprecationWarningr"   ry   build_auth_request_uri)r)   response_typerV   r   r-   s       r   r   Client.build_auth_request_uri   s<     	=?QRVT92!&2*02 	2r   c                    > [         R                  " S[        5        [        [        U ]  " U40 UD6nUR                  S0 5      R                  S5      nSU;   a   U(       a  X%:w  a  [        SU< SU< S35      eU$ )a  Get a token via authorization code. a.k.a. Authorization Code Grant.

Return value and all other parameters are the same as
:func:`oauth2.Client.obtain_token_by_authorization_code`,
plus new parameter(s):

:param nonce:
    If you provided a nonce when calling :func:`build_auth_request_uri`,
    same nonce should also be provided here, so that we'll validate it.
    An exception will be raised if the nonce in id token mismatches.
z,Use obtain_token_by_auth_code_flow() insteadr   rV   The nonce in id token ("z") should match your nonce (""))r   r   r   r"   ry   "obtain_token_by_authorization_coder'   
ValueError)r)   coderV   r   resultnonce_in_id_tokenr-   s         r   r   )Client.obtain_token_by_authorization_code   s~     	:<N	PvtG"JJ'8"=AA'J&5U5O"E+, , r   c                   > SUR                  SS5      ;   a  [        S5      eU(       a  [        U5      O/ nSU;  a  UR                  S5        SR	                  [
        R                  " [        R                  S5      5      n[        [        U ].  " S
U[        U5      S.UD6nXES'   UR                  S	5      b  US	   US	'   U$ )a|  Initiate an auth code flow.

It provides nonce protection automatically.

:param list scope:
    A list of strings, e.g. ["profile", "email", ...].
    This method will automatically send ["openid"] to the wire,
    although it won't modify your input list.

See :func:`oauth2.Client.initiate_auth_code_flow` in parent class
for descriptions on other parameters and return value.
r\   r    z+response_type="id_token ..." is not allowedopenid   )scoperV   rV   max_ager/   )r'   r   r[   appendjoinrandomsamplestringascii_lettersr"   ry   initiate_auth_code_flowrj   )r)   r   r   _scoperV   flowr-   s         r   r   Client.initiate_auth_code_flow   s      OR88 JKK %e26! MM(#f&:&:B?@VT: >E 2>6<>W::i ,$Y/DOr   c                   > [         [        U ]
  " X40 UD6nSU;   a  UR                  S0 5      R                  S5      n[	        US   5      nXV:w  a  [        SU< SU< S35      eUR                  S5      b  UR                  S0 5      R                  S5      nU(       d  [        S5      e[        [        R                  " 5       5      nS	n	X-
  XqS   -   :  a4  [        S
R                  UUS   U[        R                  " US   SS9S95      eU$ )a  Validate the auth_response being redirected back, and then obtain tokens,
including ID token which can be used for user sign in.

Internally, it implements nonce to mitigate replay attack.
It also implements PKCE to mitigate the auth code interception attack.

See :func:`oauth2.Client.obtain_token_by_auth_code_flow` in parent class
for descriptions on other parameters and return value.
r   rV   r   z") should match our nonce ("r   r   	auth_timez<13. max_age was requested, ID token should contain auth_timerR   z13. auth_time ({auth_time}) was requested, by using max_age ({max_age}) parameter, and now ({now}) too much time has elasped since last end-user authentication. The ID token was: {id_token}r   r    )r   r   r+   r\   )r"   ry   obtain_token_by_auth_code_flowr'   rj   RuntimeErrorrY   r   formatr$   r%   )r)   auth_code_flowauth_responser   r   r   expected_hashr   r+   ra   r-   s             r   r   %Client.obtain_token_by_auth_code_flow   s+    vtC5-35& &

+<b A E Eg N'w(?@M 1"&78 8 !!),8"JJ'8"=AA+N	 &VX X$))+&:	9,E EE&; <B6"+ .y 9!%F3D,Ea!P	 <B <
 
 r   c           
      4  > [        [        U[        [        45      (       a  SR	                  U5      OUUUUUUUS9R                  5        V	V
s0 s H  u  pU
c  M
  X_M     nn	n
[        [        U ]"  " SS[        UR                  S0 5      40 UD60UD6$ s  sn
n	f )a  A native app can use this method to obtain token via a local browser.

Internally, it implements nonce to mitigate replay attack.
It also implements PKCE to mitigate the auth code interception attack.

:param string display: Defined in
    `OIDC <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.
:param string prompt: Defined in
    `OIDC <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.
    You can find the valid string values defined in :class:`oidc.Prompt`.

:param int max_age: Defined in
    `OIDC <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.
:param string ui_locales: Defined in
    `OIDC <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.
:param string id_token_hint: Defined in
    `OIDC <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.
:param string login_hint: Defined in
    `OIDC <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.
:param string acr_values: Defined in
    `OIDC <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.

See :func:`oauth2.Client.obtain_token_by_browser` in parent class
for descriptions on other parameters and return value.
r<   )promptdisplayr   
ui_localesid_token_hint
login_hint
acr_valuesauth_paramsr/   )
r&   rZ   r[   tupler   itemsr"   ry   obtain_token_by_browserpop)r)   r   r   r   r   r   r   r   r   kvfiltered_paramsr-   s               r   r   Client.obtain_token_by_browser"  s    H ,0'1&4-'H'H388F#f!'!!, eg, ( ,41  13 , ( VT: VZZr:NoN 	(s   	BBr/   r@   )NNNNNNN)r0   r1   r2   r3   r4   rc   r   r   r   r   r   r   r5   r6   r7   s   @r   ry   ry      sO    
O22 #J'V / /r   ry   )zutf-8)NNNN)r$   r
   r   r   r   r   rf   loggingr   r   	getLoggerr0   rA   r   base64decoder   r   r   r9   rF   rK   rN   rc   rj   objectrl   ry   r/   r   r   <module>r      s             
		8	$, E	< 	" " 	 		< 		 	7t=

V 
wV]] wr   