
    (phM                     n   S 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  SSKJr  SSKJ	r	  SSK
rSSKrSSKrSSKJr  SSKJr  SS	KJr  S
rSrSrSr\" \R.                  " SS9R1                  5       5      r\" \R.                  " SS9R1                  5       5      r\" \R.                  " SS9R1                  5       5      rSr\" / SQ5      rSrSr Sr!Sr" " S S\RF                  RH                  RJ                  5      r& " S S5      r' " S S5      r( " S  S!\	RR                  5      r* " S" S#5      r+ " S$ S%5      r, " S& S'\RZ                  5      r. " S( S)\RZ                  5      r/ " S* S+\R`                  5      r1 " S, S-\R`                  5      r2 " S. S/\Rf                  5      r4 " S0 S1\45      r5 " S2 S3\45      r6g)4z1Firebase token minting and validation sub module.    N)credentials)iam)jwt)	transport)
exceptions)_auth_utils)_http_clientzhttps://securetoken.google.com/zXhttps://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.comz$https://session.firebase.google.com/zEhttps://www.googleapis.com/identitytoolkit/v3/relyingparty/publicKeys   )minutes   )days   )hourszYhttps://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit)acramrat_hashaud	auth_timeazpcnfc_hashexpfirebaseiatissjtinbfnoncesubzZhttp://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/emailRS256nonez"firebase-auth-emulator@example.comc                   $    \ rS rSrSrS rS rSrg)_EmulatedSigner?   Nc                     g N selfs    L/var/www/html/venv/lib/python3.13/site-packages/firebase_admin/_token_gen.py__init___EmulatedSigner.__init__B   s        c                     g)Nr-   r'   r)   messages     r*   sign_EmulatedSigner.signE   s    r-   r'   )__name__
__module____qualname____firstlineno__key_idr+   r1   __static_attributes__r'   r-   r*   r#   r#   ?   s    Fr-   r#   c                       \ rS rSrSr\4S jr\S 5       r\S 5       r	\S 5       r
\S 5       r\S 5       r\S	 5       rS
rg)_SigningProviderI   z2Stores a reference to a google.auth.crypto.Signer.c                 (    Xl         X l        X0l        g r&   )_signer_signer_email_alg)r)   signersigner_emailalgs       r*   r+   _SigningProvider.__init__L   s    )	r-   c                     U R                   $ r&   )r=   r(   s    r*   r@   _SigningProvider.signerQ   s    ||r-   c                     U R                   $ r&   )r>   r(   s    r*   rA   _SigningProvider.signer_emailU   s    !!!r-   c                     U R                   $ r&   )r?   r(   s    r*   rB   _SigningProvider.algY   s    yyr-   c                 B    [        UR                  UR                  5      $ r&   )r:   r@   rA   )clsgoogle_creds     r*   from_credential _SigningProvider.from_credential]   s     2 2K4L4LMMr-   c                 F    [         R                  " XU5      n[        XC5      $ r&   )r   Signerr:   )rK   requestrL   service_accountr@   s        r*   from_iam_SigningProvider.from_iama   s    G/B88r-   c                 <    [        [        5       [        [        5      $ r&   )r:   r#   AUTH_EMULATOR_EMAILALGORITHM_NONE)rK   s    r*   for_emulator_SigningProvider.for_emulatorf   s     13FWWr-   )r?   r=   r>   N)r3   r4   r5   r6   __doc__ALGORITHM_RS256r+   propertyr@   rA   rB   classmethodrM   rS   rX   r8   r'   r-   r*   r:   r:   I   s    <1@ 
   " "   N N 9 9 X Xr-   r:   c                   L    \ rS rSrSrSrSS jrS r\S 5       r	SS jr
S	 rS
rg)TokenGeneratork   z,Generates custom tokens and session cookies.z)https://identitytoolkit.googleapis.com/v1Nc                     Xl         X l        [        R                  R	                  5       U l        U=(       d    U R                  nSR                  XAR                  5      U l	        S U l
        g )Nz{0}/projects/{1})apphttp_clientr   requestsRequestrQ   ID_TOOLKIT_URLformat
project_idbase_url_signing_provider)r)   rb   rc   url_override
url_prefixs        r*   r+   TokenGenerator.__init__p   sQ    & ))113!8T%8%8
*11*nnM!%r-   c                 Z   [         R                  " 5       (       a  [        R                  5       $ U R                  R
                  R                  5       n[        U[        R                  R                  R                  5      (       a  [        R                  U5      $ U R                  R                  R                  S5      nU(       a   [        R                  U R                   X5      $ [        U["        R$                  5      (       a  [        R                  U5      $ U R!                  [&        SS0S9nUR(                  S:w  a2  [+        SR-                  UR.                  R1                  5       5      5      eUR.                  R1                  5       n[        R                  U R                   X5      $ )zPInitializes a signing provider by following the go/firebase-admin-sign protocol.serviceAccountIdzMetadata-FlavorGoogle)urlheaders   z2Failed to contact the local metadata service: {0}.)r   is_emulatedr:   rX   rb   
credentialget_credential
isinstancegoogleoauth2rR   CredentialsrM   optionsgetrS   rQ   r   SigningMETADATA_SERVICE_URLstatus
ValueErrorrg   datadecode)r)   rL   rR   resps       r*   _init_signing_provider%TokenGenerator._init_signing_providerx   s>   ""$$#0022hh))88:k6==#@#@#L#LMM#33K@@ ((**../AB#,,T\\;XX k;#6#677#33K@@ || 4?PRZ>[|\;;#DKKDIIL\L\L^_a a))**,(({TTr-   c                     U R                   (       d"   U R                  5       U l         U R                   $ U R                   $ ! [         a!  nSn[        SR	                  X5      5      eSnAff = f)z@Initializes and returns the SigningProvider instance to be used.z@https://firebase.google.com/docs/auth/admin/create-custom-tokenszFailed to determine service account: {0}. Make sure to initialize the SDK with service account credentials or specify a service account ID with iam.serviceAccounts.signBlob permission. Please refer to {1} for more details on creating custom tokens.N)rj   r   	Exceptionr   rg   )r)   errorrq   s      r*   signing_providerTokenGenerator.signing_provider   sw     %%M)-)D)D)F& %%%t%%%  MX 9 :@9K	M MMs   A   
A+
A&&A+c                 t   Ub  [        U[        5      (       d  [        S5      e[        UR	                  5       5      [
        -  nU(       a[  [        U5      S:  a!  SR                  SR                  U5      5      nO SR                  SR                  U5      5      n[        U5      eU(       a$  [        U[        5      (       a  [        U5      S:  a  [        S5      eU R                  n[        [        R                  " 5       5      nUR                  UR                  [        UUU[        -   S	.nU(       a  X8S
'   Ub  X(S'   SUR                   0n	 ["        R$                  " UR&                  XS9$ ! [(        R*                  R,                  R.                   a!  n
SR                  U
5      n[1        X5      eSn
A
ff = f)z.Builds and signs a Firebase custom auth token.Nz%developer_claims must be a dictionaryr   z:Developer claims {0} are reserved and cannot be specified.z, z8Developer claim {0} is reserved and cannot be specified.   z2uid must be a string between 1 and 128 characters.)r   r   r   uidr   r   	tenant_idclaimsrB   )headerz Failed to sign custom token. {0})rw   dictr   setkeysRESERVED_CLAIMSlenrg   joinstrr   inttimerA   FIREBASE_AUDIENCEMAX_TOKEN_LIFETIME_SECONDSrB   r   encoder@   rx   authr   TransportErrorTokenSignError)r)   r   developer_claimsr   disallowed_keyserror_messager   nowpayloadr   r   msgs               r*   create_custom_token"TokenGenerator.create_custom_token   s   '.55 !HII!"2"7"7"9:_LO'!+&<<BF)-?)C=E "&<<BF)-?)C=E " !//*S#..#c(S.QRR00$))+#00#00$33
 #,K ' 0H)--.	-::.55wNN{{%%44 	-4;;EBC ,,	-s   E. .(F7F22F7c                    [        U[        5      (       a  UR                  S5      OUn[        U[        5      (       a  U(       d  [	        SR                  U5      5      e[        U[        R                  5      (       a  [        UR                  5       5      n[        U[        5      (       d  [        U[        5      (       d  [	        SR                  U5      5      eU[        :  a  [	        SR                  U[        5      5      eU[        :  a  [	        SR                  U[        5      5      eSR                  U R                  5      nUUS.n U R                  R                  SX4S	9u  pVU(       a  UR!                  S
5      (       d  ["        R$                  " SUS9eUR!                  S
5      $ ! [&        R(                  R*                   a  n["        R,                  " U5      eSnAff = f)z4Creates a session cookie from the provided ID token.utf-8zDIllegal ID token provided: {0}. ID token must be a non-empty string.zIllegal expiry duration: {0}.zDIllegal expiry duration: {0}. Duration must be at least {1} seconds.zCIllegal expiry duration: {0}. Duration must be at most {1} seconds.z{0}:createSessionCookie)idTokenvalidDurationpost)jsonsessionCookiez Failed to create session cookie.)http_responseN)rw   bytesr   r   r   rg   datetime	timedeltar   total_secondsbool#MIN_SESSION_COOKIE_DURATION_SECONDS#MAX_SESSION_COOKIE_DURATION_SECONDSri   rc   body_and_responser|   r   UnexpectedResponseErrorrd   r   RequestExceptionhandle_auth_backend_error)r)   id_token
expires_inrq   r   body	http_respr   s           r*   create_session_cookie$TokenGenerator.create_session_cookie   s   /9(E/J/J8??7+PX(C(( &*, , j("4"455Z5578Jj$''z*c/J/J<CCJOPP;; ((.z;^(_a a;; ((.z;^(_a a (..t}}='
	-"..@@@[OD txx88!996iQ Q88O,, ""33 	?77>>	?s   F- -G&G!!G&)rj   rb   ri   rc   rQ   r&   )NN)r3   r4   r5   r6   rZ   rf   r+   r   r\   r   r   r   r8   r'   r-   r*   r_   r_   k   s4    6@N&U: & &*-Z -r-   r_   c                   L    \ rS rSrSrS	S jr\S 5       r\S 5       rS
S jr	Sr
g)CertificateFetchRequest   zqA google-auth transport that supports HTTP cache-control.

Also injects a timeout to each outgoing HTTP request.
Nc                     [         R                  " [        R                  " 5       5      U l        [
        R                  R                  U R                  5      U l        Xl	        g r&   )
cachecontrolCacheControlrd   Session_sessionr   re   session	_delegate_timeout_seconds)r)   timeout_secondss     r*   r+    CertificateFetchRequest.__init__   s?    $11(2B2B2DE"++33DLLA /r-   c                     U R                   $ r&   )r   r(   s    r*   r   CertificateFetchRequest.session   s    }}r-   c                     U R                   $ r&   )r   r(   s    r*   r   'CertificateFetchRequest.timeout_seconds  s    $$$r-   c                 X    U=(       d    U R                   nU R                  " U4X#XES.UD6$ )N)methodr   rr   timeout)r   r   )r)   rq   r   r   rr   r   kwargss          r*   __call__ CertificateFetchRequest.__call__  s<    1T11~~W7WOUW 	Wr-   )r   r   r   r&   )GETNNN)r3   r4   r5   r6   rZ   r+   r\   r   r   r   r8   r'   r-   r*   r   r      s:    
0
   % %Wr-   r   c                   2    \ rS rSrSrS rSS jrSS jrSrg)	TokenVerifieri  z'Verifies ID tokens and session cookies.c                 H   UR                   R                  S[        R                  5      n[	        U5      U l        [        UR                  SSS[        [        [        R                  [        S9U l        [        UR                  SSS[        [        [         ["        S9U l        g )NhttpTimeoutzID tokenzverify_id_token()z<https://firebase.google.com/docs/auth/admin/verify-id-tokens)rh   
short_name	operationdoc_urlcert_urlissuerinvalid_token_errorexpired_token_errorzsession cookiezverify_session_cookie())r{   r|   r	   DEFAULT_TIMEOUT_SECONDSr   rQ   _JWTVerifierrh   ID_TOKEN_CERT_URIID_TOKEN_ISSUER_PREFIXr   InvalidIdTokenErrorExpiredIdTokenErrorid_token_verifierCOOKIE_CERT_URICOOKIE_ISSUER_PREFIXInvalidSessionCookieErrorExpiredSessionCookieErrorcookie_verifier)r)   rb   r   s      r*   r+   TokenVerifier.__init__  s    ++//-1U1UV.w7!-~~*)R&) + ? ? 3"5  ,~~2B/R$' 9 9 ;r-   c                 N    U R                   R                  XR                  U5      $ r&   )r   verifyrQ   )r)   r   clock_skew_secondss      r*   verify_id_tokenTokenVerifier.verify_id_token$  s     %%,,X||EWXXr-   c                 N    U R                   R                  XR                  U5      $ r&   )r   r   rQ   )r)   cookier   s      r*   verify_session_cookie#TokenVerifier.verify_session_cookie'  s     ##**6<<ASTTr-   )r   r   rQ   Nr   )	r3   r4   r5   r6   rZ   r+   r   r   r8   r'   r-   r*   r   r     s    1;(YUr-   r   c                   .    \ rS rSrSrS rSS jrS rSrg)	r   i+  z6Verifies Firebase JWTs (ID tokens or session cookies).c                 (   UR                  S5      U l        UR                  S5      U l        UR                  S5      U l        UR                  S5      U l        UR                  S5      U l        UR                  S5      U l        U R                  S   R                  5       S;   a!  S	R                  U R                  5      U l	        O S
R                  U R                  5      U l	        UR                  S5      U l
        UR                  S5      U l        g )Nrh   r   r   r   r   r   r   aeiouzan {0}za {0}r   r   )poprh   r   r   rq   r   r   lowerrg   articled_short_name_invalid_token_error_expired_token_error)r)   r   s     r*   r+   _JWTVerifier.__init__.  s     **\2 **\2K0::i(

:.jj*??1##%0'/t'GD$'.~~doo'FD$$*JJ/D$E!$*JJ/D$E!r-   c                    [        U[        5      (       a  UR                  S5      OUn[        U[        5      (       a  U(       d%  [	        SR                  U R                  U5      5      eU R                  (       d$  [	        SR                  U R                  5      5      eUS:  d  US:  a  [	        SR                  U5      5      eU R                  U5      u  pEUR                  S5      nUR                  S5      nUR                  S	5      nU R                  U R                  -   n	S
R                  U R                  5      n
SR                  U R                  U R                  5      n[        R                  " 5       nSnU[        :X  a(  SR                  U R                  U R                   5      nGOU(       d  UR                  S5      (       d  UR                  S5      S:X  aS  UR                  S5      S:X  a>  SUR                  S0 5      ;   a(  SR                  U R                  U R                   5      nGOESR                  U R                  5      nGO(U(       dB  UR                  S5      S:w  a-  SR                  U R                  UR                  S5      U5      nOXpR                  :w  a)  SR                  U R                  U R                  UX5      nOXi:w  a  SR                  U R                  XX5      nOUb  [        U[        5      (       d  SR                  U R                  U5      nOOU(       d  SR                  U R                  U5      nO+[#        U5      S:  a  SR                  U R                  U5      nU(       a  U R%                  U5      e U(       a  UnO?[&        R(                  R*                  R-                  UUU R                  U R.                  US9nUS	   US'   U$ ! [&        R0                  R2                  R4                   a  n[7        [        U5      US9eSnAf[         aF  nS [        U5      ;   a  U R9                  [        U5      US9eU R%                  [        U5      US9eSnAff = f)!z5Verifies the signature and data for the provided JWT.r   z:Illegal {0} provided: {1}. {0} must be a non-empty string.a  Failed to ascertain project ID from the credential or the environment. Project ID is required to call {0}. Initialize the app with a credentials.Certificate or set your Firebase project ID as an app option. Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.r   <   zKIllegal clock_skew_seconds value: {0}. Must be between 0 and 60, inclusive.r   r   r   zlMake sure the {0} comes from the same Firebase project as the service account used to authenticate this SDK.z+See {0} for details on how to retrieve {1}.Nz.{0} expects {1}, but was given a custom token.kidrB   HS256vr   dz5{0} expects {1}, but was given a legacy custom token.z Firebase {0} has no "kid" claim.r    zIFirebase {0} has incorrect algorithm. Expected "RS256" but got "{1}". {2}zXFirebase {0} has incorrect "aud" (audience) claim. Expected "{1}" but got "{2}". {3} {4}zVFirebase {0} has incorrect "iss" (issuer) claim. Expected "{1}" but got "{2}". {3} {4}z.Firebase {0} has no "sub" (subject) claim. {1}z;Firebase {0} has an empty string "sub" (subject) claim. {1}r   zHFirebase {0} has a "sub" (subject) claim longer than 128 characters. {1})rQ   audience	certs_urlclock_skew_in_secondscausezToken expired)rw   r   r   r   r   rg   r   rh   r   _decode_unverifiedr|   r   rq   r   rt   r   r   r   r  rx   ry   r   verify_tokenr   r   r   r   CertificateFetchErrorr  )r)   tokenrQ   r   r   r   r   r
  subjectexpected_issuerproject_id_match_msgverify_id_token_msgemulatedr   verified_claimsr   s                   r*   r   _JWTVerifier.verify<  s   )3E3)?)?W%U%''u &%8: : = >DVDNN=S	U U !%7"%<]*+- - 11%8U#;;u%++e$++7((.t(? 	 :@@4??[ 	 **,((0H0HI  &**U"3"3zz% G+11"'7;;sB+?"?#VDNND4L4LM  !C I I$// Zfjj/7:#VDOOVZZ5FH[\  (%%+VDOOT__h,@&W  &%%+VDOO_,@&W  _Jw$<$<fT__.AB  fT__.AB  \CfT__.AB  ++M::	E")"(--"8"8"E"E#!__"mm*< #F #> &5U%;OE"""{{%%44 	A'E
%@@ 	E#e*,//E
%/HH++CJe+DD	Es&   AO$ $(Q2PQ2,AQ--Q2c                      [         R                  " U5      n[         R                  " USS9nX#4$ ! [         a  nU R	                  [        U5      US9eS nAff = f)NF)r   r  )r   decode_headerr   r   r  r   )r)   r  r   r   r   s        r*   r  _JWTVerifier._decode_unverified  s\    	E&&u-Fjju5G?" 	E++CJe+DD	Es   -0 
AAA)	r  r  r   r   r   r   rh   r   rq   Nr   )	r3   r4   r5   r6   rZ   r+   r   r  r8   r'   r-   r*   r   r   +  s    @F^E@Er-   r   c                       \ rS rSrSrS rSrg)r   i  z7Unexpected error while signing a Firebase custom token.c                 D    [         R                  R                  XU5        g r&   r   UnknownErrorr+   r)   r0   r  s      r*   r+   TokenSignError.__init__      ((>r-   r'   Nr3   r4   r5   r6   rZ   r+   r8   r'   r-   r*   r   r     s
    A?r-   r   c                       \ rS rSrSrS rSrg)r  i  zHFailed to fetch some public key certificates required to verify a token.c                 D    [         R                  R                  XU5        g r&   r  r!  s      r*   r+   CertificateFetchError.__init__  r#  r-   r'   Nr$  r'   r-   r*   r  r    s
    R?r-   r  c                       \ rS rSrSrS rSrg)r   i  z!The provided ID token is expired.c                 D    [         R                  R                  XU5        g r&   r   r   r+   r!  s      r*   r+   ExpiredIdTokenError.__init__  s    ''00Fr-   r'   Nr$  r'   r-   r*   r   r     s    +Gr-   r   c                       \ rS rSrSrS rSrg)RevokedIdTokenErrori  z'The provided ID token has been revoked.c                 B    [         R                  R                  X5        g r&   r*  r/   s     r*   r+   RevokedIdTokenError.__init__  s    ''00?r-   r'   Nr$  r'   r-   r*   r-  r-    s    1@r-   r-  c                   "    \ rS rSrSrSS jrSrg)r   i  z;The provided string is not a valid Firebase session cookie.Nc                 D    [         R                  R                  XU5        g r&   )r   InvalidArgumentErrorr+   r!  s      r*   r+   "InvalidSessionCookieError.__init__  s    ''00Fr-   r'   r&   r$  r'   r-   r*   r   r     s    EGr-   r   c                       \ rS rSrSrS rSrg)r   i  z'The provided session cookie is expired.c                 0    [         R                  XU5        g r&   r   r+   r!  s      r*   r+   "ExpiredSessionCookieError.__init__  s    !**4%@r-   r'   Nr$  r'   r-   r*   r   r     s    1Ar-   r   c                       \ rS rSrSrS rSrg)RevokedSessionCookieErrori  z-The provided session cookie has been revoked.c                 .    [         R                  X5        g r&   r6  r/   s     r*   r+   "RevokedSessionCookieError.__init__  s    !**49r-   r'   Nr$  r'   r-   r*   r9  r9    s
    7:r-   r9  )7rZ   r   r   r   rd   google.authr   r   r   r   google.auth.exceptionsrx   google.oauth2.id_tokengoogle.oauth2.service_accountfirebase_adminr   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   r[   rW   rV   r   cryptrP   r#   r:   r_   re   r   r   r   r   r   r  r   r   r-  r2  r   r   r9  r'   r-   r*   <module>rB     s   8     #   !   $ % & ' ; >  > Y&)(*<*<Q*G*U*U*W&X #&)(*<*<"*E*S*S*U&V # !!3!3!!<!J!J!LM C   9  ; fkk''.. X XDF- F-RWi// W2U U<wE wEt?Z,, ??J33 ?G+99 G@+99 @G
 ? ? GA 9 A: 9 :r-   