
    #	hP                         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  SSKJrJ	r	  SSK
Jr  \R                  " \5      rSrS rS r " S	 S
\5      r " S S\5      rg)    N   )canonicalize)decode_partdecode_id_token)Clientbrokerc                      [        U40 U D6U:H  $ N)dict)smallbigs     B/var/www/html/env/lib/python3.13/site-packages/msal/token_cache.pyis_subdict_ofr      s    u$$    c                 D    U R                  SU R                  S5      5      $ )Npreferred_usernameupn)get)id_token_claimss    r   _get_usernamer      s&    E"$ $r   c            
           \ rS rSrSr " S S5      r " S S5      rS r SS	 jrSS
 jr	SS jr
\SS\S\S\S\4S jj5       rSSS.S jjrSSS.S jjrSS jrS rSS jrSS jrS rS rS rS rS rSrg) 
TokenCache   a  This is considered as a base class containing minimal cache behavior.

Although it maintains tokens using unified schema across all MSAL libraries,
this class does not serialize/persist them.
See subclass :class:`SerializableTokenCache` for details on serialization.
c                   (    \ rS rSrSrSrSrSrSrSr	g)	TokenCache.CredentialType   AccessTokenRefreshTokenAccountIdTokenAppMetadata N)
__name__
__module____qualname____firstlineno__ACCESS_TOKENREFRESH_TOKENACCOUNTID_TOKENAPP_METADATA__static_attributes__r"   r   r   CredentialTyper      s    $&$r   r-   c                       \ rS rSrSrSrSrg)TokenCache.AuthorityType&   ADFSMSSTSr"   N)r#   r$   r%   r&   r1   r2   r,   r"   r   r   AuthorityTyper/   &   s    r   r3   c                 n  ^  [         R                  " 5       T l        0 T l        T R                  R
                    SU 4S jjT R                  R                    SU 4S jjT R                  R                    SU 4S jjT R                  R                  SS jT R                  R                  S	S j0T l
        g )
Nc           	         > SR                  U =(       d    SU=(       d    STR                  R                  U=(       d    SSU=(       d    S/5      R                  5       $ N- )joinr-   r(   lower)home_account_idenvironment	client_idtarget!ignored_payload_from_a_real_tokenselfs        r   <lambda>%TokenCache.__init__.<locals>.<lambda>1   sR    HH'-2#)r++99!R"  !57#r   c           	         > SR                  U =(       d    SU=(       d    STR                  R                  U=(       d    SU=(       d    SU=(       d    S/5      R                  5       $ r6   )r9   r-   r'   r:   )r;   r<   r=   realmr>   r?   r@   s         r   rA   rB   <   sX    
 HH'-2#)r++88!R"  !57#r   c                    > SR                  U =(       d    SU=(       d    STR                  R                  U=(       d    SU=(       d    SS/5      R                  5       $ r6   )r9   r-   r*   r:   )r;   r<   r=   rD   r?   r@   s        r   rA   rB   K   sR    HH'-2#)r++44!R  !57#r   c                 |    SR                  U =(       d    SU=(       d    SU=(       d    S/5      R                  5       $ r6   )r9   r:   )r;   r<   rD   !ignored_payload_from_a_real_entrys       r   rA   rB   V   s8    HH'-2#)r  !57	#r   c                 J    SR                  U =(       d    SU=(       d    S5      $ )Nzappmetadata-{}-{}r8   )format)r<   r=   kwargss      r   rA   rB   ^   s    '..{/@b)/rRr   )NNNN)NNNNN)NNNNN)	threadingRLock_lock_cacher-   r(   r'   r*   r)   r+   
key_makersr@   s   `r   __init__TokenCache.__init__*   s    __&
 --IM#	# ,,IM+/# ((IM"	# ''# ,,Sc3r   Nc                     U R                  U R                  R                  U R                  [        R                  R                     " UUUUSR                  U5      S9US9$ )N )r;   r<   r=   rD   r>   default)_getr-   r'   rP   r   r9   )r@   r;   r<   r=   rD   r>   rW   s          r   _get_access_tokenTokenCache._get_access_tokenb   sc    
 yy,,OOJ55BBC /'#xx'   	 		r   c                     U R                  U R                  R                  U R                  [        R                  R                     " UUS9US9$ )N)r<   r=   rV   )rX   r-   r+   rP   r   )r@   r<   r=   rW   s       r   _get_app_metadataTokenCache._get_app_metadatar   sO    yy,,OOJ55BBC'#    	r   c                     U R                      U R                  R                  U0 5      R                  X#5      sS S S 5        $ ! , (       d  f       g = fr
   )rN   rO   r   )r@   credential_typekeyrW   s       r   rX   TokenCache._get{   s0    ZZ;;???B7;;CI ZZs   +A
Aentryquery
target_setreturnc                     [        U=(       d    0 U 5      =(       a5    U(       a,  U[        U R                  SS5      R                  5       5      :*  $ S$ )Nr>   r8   T)r   setr   split)rb   rc   rd   s      r   _is_matchingTokenCache._is_matching   sH    U[b%0 % #eii"5;;=>>	%#	%r   nowc             #     #    [        U=(       d    / 5      n[        U[        5      (       d   S5       eSnXR                  R                  :X  av  [        U[
        5      (       aa  SU;   a[  SU;   aU  SU;   aO  SU;   aI  U(       aB  U R                  US   US   US   US   U5      nU(       a  U R                  XS5      (       a  Uv   [        U5      nU R                     [        Uc  [        R                  " 5       OU5      n/ nU R                  R                  U0 5      R                  5        Hc  nXR                  R                  :X  a%  [        US   5      U:  a  UR                  U5        MA  X:w  d  MH  U R                  XUS9(       d  M_  Uv   Me     U H  n	U R!                  U	5        M     SSS5        g! , (       d  f       g= f7f)	zReturns a generator of matching entries.

It is O(1) for AT hits, and O(n) for other types.
Note that it holds a lock during the entire search.
zInvalid parameter typeNr;   r<   r=   rD   
expires_on)rd   )sorted
isinstancelistr-   r'   r   rY   ri   rg   rN   inttimerO   r   valuesappend	remove_at)
r@   r_   r>   rc   rl   preferred_resultrd   expired_access_tokensrb   ats
             r   searchTokenCache.search   s     "%&$''A)AA'22???5$''!U*}/Eu$E)9f#55'(%*>k"E'NF <  D$5$5 % % '&[
ZZS[diikc:C%!
 "=DDF#':':'G'GGE,/036)007-))%:)NNK G ,r" ,' ZZs+   CGBF53F5
"F5,	G5
G?Gc          	      j    [         R                  " S[        5        [        U R	                  XX4S95      $ )z Equivalent to list(search(...)).z7Use list(search(...)) instead to explicitly get a list.)r>   rc   rl   )warningswarnDeprecationWarningrq   rz   )r@   r_   r>   rc   rl   s        r   findTokenCache.find   s.    E	  DKKeKUVVr   c           
          S n[        UU" UR                  S0 5      S5      U" UR                  S0 5      S5      S9n[        R                  S[        R
                  " USS	[        S
95        U R                  XS9$ )z:Handle a token obtaining event, and add tokens into cache.c                 h    U R                  5        VVs0 s H  u  p#X"U;   a  SOU_M     snn$ s  snnf )Nz********)items)
dictionarysensitive_fieldskvs       r   make_clean_copy'TokenCache.add.<locals>.make_clean_copy   sA     ',,..DA &6!6:A=.  s   .data)passwordclient_secretrefresh_token	assertionresponse)r   access_tokenr   id_tokenusername)r   r   zevent=%s   T)indent	sort_keysrW   rk   )r   r   loggerdebugjsondumpsstr_TokenCache__add)r@   eventrl   r   clean_events        r   addTokenCache.add   s    	
  62!6 9  %UYYz2%> A 	
 	Z "
 	 zz%z))r   c                     SU;   aB  [         R                  " [        US   5      5      nSU;   a  SU;   a  USR                  " S0 UD64$ U(       a  US   nSU0U4$ 0 S4$ )z&Return client_info and home_account_idclient_infouidutidz{uid}.{utid}subNr"   )r   loadsr   rI   )r@   r   r   r   r   s        r   __parse_accountTokenCache.__parse_account   so    H$**[-1H%IJK#+(="N$9$9$HK$HHH!%(C3<$$4xr   c                 	   S =p4SU;   a  [        US   5      u  pSnSU;   a  US   nUR                  S0 5      nUR                  S0 5      nUR                  S5      nUR                  S5      n	UR                  S5      n
UR                  S5      =(       d    U
(       a  [        XS	   S
9O0 nU R                  Xk5      u  pSR	                  [        UR                  S5      =(       d    / 5      5      nU R                     [        Uc  [        R                  " 5       OU5      nU(       Ga>  UR                  S5      (       a  [        UR                  S5      5      U-
  OSn[        UR                  SU5      5      n[        UR                  SU5      5      nU R                  R                  UUUUR                  S	5      UUUR                  SS5      [        U5      [        UU-   5      [        UU-   5      S.nUR                  U Vs0 s H  nUS;   d  M  UUU   _M     sn5        SU;   a  US   n[        UU-   5      US'   U R                  U R                  R                  UU5        U(       Ga0  UR                  S5      (       Gd  UUUUR                  SUR                  SUR                  S5      5      5      [        U5      =(       d2    UR                  S5      =(       d    UR                  S5      =(       d    SUR                  SUS:X  a  U R                  R                   OU R                  R"                  5      S.n[$        S S![&        R(                  S"   4nUR                  S#5      U;   a  US#   US$'   U R                  U R                  R*                  UU5        U
(       aS  U R                  R,                  U
UUUUR                  S	5      S%.nU R                  U R                  R,                  UU5        U	(       ak  U R                  R.                  U	UUUR                  S	5      U[        U5      S&.nS'U;   a  US'   US('   U R                  U R                  R.                  UU5        UR                  S	5      US).nS'U;   a  UR                  S'5      US('   U R                  U R                  R0                  UU5        S S S 5        g s  snf ! , (       d  f       g = f)*Ntoken_endpointr<   r   r   r   r   r   r   r=   )r=   rU   scopern   iX  
expires_inext_expires_in
token_typeBearer)r_   secretr;   r<   r=   r>   rD   r   	cached_atrn   extended_expires_on>   key_id
refresh_in
refresh_onskip_account_creation_account_idoidr   r   r8   authority_typeadfs)r;   r<   rD   local_account_idr   r   authorization_coder   
GRANT_TYPE
grant_typeaccount_source)r_   r   r;   r<   rD   r=   )r_   r   r;   r<   r=   r>   last_modification_timefoci	family_id)r=   r<   )r   r   r   _TokenCache__parse_accountr9   ro   rN   rr   rs   r-   r'   r   updatemodifyr   r3   r1   r2   _GRANT_TYPE_BROKERr   DEVICE_FLOWr)   r*   r(   r+   )r@   r   rl   r<   rD   _r   r   r   r   r   r   r   r;   r>   default_expires_inr   r   ry   r   r   account%grant_types_that_establish_an_accountidtrtapp_metadatas                             r   __addTokenCache.__add   s    #"u$$07G1H$I!AEE!.K99Z,yy$||N3 _5<<
+",,'89 YGOOHk0BCUW 	 (,';';H'V$&7!3!9r:;ZZS[diikc:C "l33 \23c99< # !LL/ABD
!$<< *-"/ (,':':'G'G*'6#.!&;!7$""*,,|X"F!$S"%cJ&6"7+.s^/C+D 		t t!q = 8:1d1g:t  
  8+!),!7J'*3+;'<B|$D//<<b"E599-D#E#E'6#."(-		%'++E?3F3Fu3MN)P !.o > !88J/! 99Z0! &+ii(38F?**//!%!3!3!9!9';& '(<j&&|4965 99\*.SS05l0CG,-D//77'J'+':':'C'C&'6#."!&;!7 D//88#sC'+':':'H'H+'6#.!&;!7$.1#h X%&.v&6B{OD//==r2F #YY{3*L !,4LL,@[)KK++88,Uy Z01 Zs&   ;DS
S

S
J#SS
Sc                 6   U R                   U   " S0 UD6nU R                     U(       a+  U R                  R                  U0 5      n[	        U40 UD6XT'   O,U R                  R                  U0 5      R                  US 5        S S S 5        g ! , (       d  f       g = f)Nr"   )rP   rN   rO   
setdefaultr   pop)r@   r_   	old_entrynew_key_value_pairsr`   entriess         r   r   TokenCache.modifyT  s~     ooo.;;ZZ"++00"E# +) + &&;??TJ ZZs   AB


Bc                     UR                  S5      U R                  R                  :X  d   eU R                  U R                  R                  U5      $ Nr_   )r   r-   r(   r   )r@   rt_items     r   	remove_rtTokenCache.remove_rtf  sC    {{,-1D1D1R1RRRR{{4..<<gFFr   c                     UR                  S5      U R                  R                  :X  d   eU R                  U R                  R                  UU[	        [        [        R                  " 5       5      5      S.5      $ )Nr_   )r   r   )r   r-   r(   r   r   rr   rs   )r@   r   new_rts      r   	update_rtTokenCache.update_rtj  sc    {{,-1D1D1R1RRRR{{4..<<g&)#diik*:&;H  	r   c                     UR                  S5      U R                  R                  :X  d   eU R                  U R                  R                  U5      $ r   )r   r-   r'   r   )r@   at_items     r   rv   TokenCache.remove_atq  sC    {{,-1D1D1Q1QQQQ{{4..;;WEEr   c                     UR                  S5      U R                  R                  :X  d   eU R                  U R                  R                  U5      $ r   )r   r-   r*   r   )r@   idt_items     r   
remove_idtTokenCache.remove_idtu  sC    ||-.$2E2E2N2NNNN{{4..77BBr   c                 ^    SU;   d   eU R                  U R                  R                  U5      $ )Nr   )r   r-   r)   )r@   account_items     r   remove_accountTokenCache.remove_accounty  s-    <///{{4..66EEr   )rO   rN   rP   r
   rK   )r#   r$   r%   r&   __doc__r-   r3   rR   rY   r\   rX   staticmethodr   rg   boolri   rz   r   r   r   r   r   r   r   rv   r   r   r,   r"   r   r   r   r      s    % % 6v  J %D % %3 %$ % %
-#d -#^WD W*4pVdK$GFCFr   r   c                   L   ^  \ rS rSrSrSrU 4S jrS	U 4S jjrS rS r	Sr
U =r$ )
SerializableTokenCachei~  a  This serialization can be a starting point to implement your own persistence.

This class does NOT actually persist the cache on disk/db/etc..
Depending on your need,
the following simple recipe for file-based, unencrypted persistence may be sufficient::

    import os, atexit, msal
    cache_filename = os.path.join(  # Persist cache into this file
        os.getenv(
            # Automatically wipe out the cache from Linux when user's ssh session ends.
            # See also https://github.com/AzureAD/microsoft-authentication-library-for-python/issues/690
            "XDG_RUNTIME_DIR", ""),
        "my_cache.bin")
    cache = msal.SerializableTokenCache()
    if os.path.exists(cache_filename):
        cache.deserialize(open(cache_filename, "r").read())
    atexit.register(lambda:
        open(cache_filename, "w").write(cache.serialize())
        # Hint: The following optional line persists only when state changed
        if cache.has_state_changed else None
        )
    app = msal.ClientApplication(..., token_cache=cache)
    ...

Alternatively, you may use a more sophisticated cache persistence library,
`MSAL Extensions <https://github.com/AzureAD/microsoft-authentication-extensions-for-python>`_,
which provides token cache persistence with encryption, and more.

:var bool has_state_changed:
    Indicates whether the cache state in the memory has changed since last
    :func:`~serialize` or :func:`~deserialize` call.
Fc                 >   > [         [        U ]
  " U40 UD6  SU l        g NT)superr   r   has_state_changed)r@   r   rJ   	__class__s      r   r   SerializableTokenCache.add  s     $d/@@!%r   c                 <   > [         [        U ]  XU5        SU l        g r   )r   r   r   r   )r@   r_   r   r   r   s       r   r   SerializableTokenCache.modify  s!    $d2(;	=!%r   c                     U R                      U(       a  [        R                  " U5      O0 U l        SU l        SSS5        g! , (       d  f       g= f)zEDeserialize the cache from a state previously obtained by serialize()FN)rN   r   r   rO   r   )r@   states     r   deserialize"SerializableTokenCache.deserialize  s1     ZZ/4$**U+"DK%*D" ZZs   ,A
Ac                     U R                      SU l        [        R                  " U R                  SS9sSSS5        $ ! , (       d  f       g= f)z0Serialize the current cache state into a string.Fr   )r   N)rN   r   r   r   rO   rQ   s    r   	serialize SerializableTokenCache.serialize  s/     ZZ%*D"::dkk!4 ZZs	   &=
A)rO   r   r
   )r#   r$   r%   r&   r   r   r   r   r   r   r,   __classcell__)r   s   @r   r   r   ~  s*    @ &&
+5 5r   r   )r   rL   rs   loggingr}   	authorityr   oauth2cli.oidcr   r   oauth2cli.oauth2r   	getLoggerr#   r   r   r   r   objectr   r   r"   r   r   <module>r     s\         # 8 $ 
		8	$ %$
dF dFN85Z 85r   