
    f                         d Z ddlmZmZ ddlZddlmZmZmZmZ ddlm	Z	m
Z
mZ ddlmZ dZdefd	Zdd
edeeef         fdZ G d d          Z G d d          ZdS )zFirebase App Check module.    )AnyDictN)PyJWKClientExpiredSignatureErrorInvalidTokenErrorDecodeError)InvalidAudienceErrorInvalidIssuerErrorInvalidSignatureError)_utils
_app_checkreturnc                 B    t          j        | t          t                    S N)r   get_app_service_APP_CHECK_ATTRIBUTE_AppCheckService)apps    R/var/www/html/Alfredo/env/lib/python3.11/site-packages/firebase_admin/app_check.py_get_app_check_servicer      s    !#';=MNNN    tokenc                 F    t          |                              |           S )a  Verifies a Firebase App Check token.

    Args:
        token: A token from App Check.
        app: An App instance (optional).

    Returns:
        Dict[str, Any]: The token's decoded claims.

    Raises:
        ValueError: If the app's ``project_id`` is invalid or unspecified,
        or if the token's headers or payload are invalid.
        PyJWKClientError: If PyJWKClient fails to fetch a valid signing key.
    )r   verify_token)r   r   s     r   r   r      s      "#&&33E:::r   c                   l    e Zd ZdZdZdZdZdZdZd Z	de
dee
ef         fdZd	eddfd
Zde
de
fdZdS )r   z?Service class that implements Firebase App Check functionality.z(https://firebaseappcheck.googleapis.com/z/https://firebaseappcheck.googleapis.com/v1/jwksNc                     |j         | _        | j        st          d          d|j         z   | _        t	          | j        d          | _        d S )NzA project ID must be specified to access the App Check service. Either set the projectId option, use service account credentials, or set the GOOGLE_CLOUD_PROJECT environment variable.z	projects/i`T  )lifespan)
project_id_project_id
ValueError_scoped_project_idr   	_JWKS_URL_jwks_client)selfr   s     r   __init__z_AppCheckService.__init__6   s\    > 	>=> > >
 #.">'GGGr   r   r   c                 ~   t                               d|           	 | j                            |          }|                     t          j        |                     |                     ||j                  }n+# t          t          f$ r}t          d|           d}~ww xY w|                    d          |d<   |S )z$Verifies a Firebase App Check token.zapp check tokenz)Verifying App Check token failed. Error: Nsubapp_id)_Validatorscheck_stringr#   get_signing_key_from_jwt_has_valid_token_headersjwtget_unverified_header_decode_and_verifykeyr   r   r    get)r$   r   signing_keyverified_claims	exceptions        r   r   z_AppCheckService.verify_tokenD   s      !2E:::
	+DDUKKK))#*CE*J*JKKK"55e[_MMOO!;/ 	 	 	GIGG  	
 %4$7$7$>$>!s   AA: :B"BB"headersc                     |                     d          dk    rt          d          |                     d          }|dk    rt          d| d          dS )	z9Checks whether the token has valid headers for App Check.typJWTz9The provided App Check token has an incorrect type headeralgRS256zQThe provided App Check token has an incorrect alg header. Expected RS256 but got .N)r1   r    )r$   r5   	algorithms      r   r,   z)_AppCheckService._has_valid_token_headersW   sw     ;;u&&XYYYKK&&	7*37 7 7    r   r2   c                    i }	 t          j        ||dg| j                  }n# t          $ r t	          d          t
          $ r t	          d| j         d          t          $ r t	          d| j                   t          $ r t	          d          t          $ r}t	          d|           d	}~ww xY w|
                    d
          }t          |t                    r	| j        |vrt	          d          |
                    d                              | j                  st	          d          t                              d|
                    d                     |S )z.Decodes and verifies the token from App Check.r:   )
algorithmsaudiencez6The provided App Check token has an invalid signature.zbThe provided App Check token has an incorrect "aud" (audience) claim. Expected payload to include r;   z^The provided App Check token has an incorrect "iss" (issuer) claim. Expected claim to include z)The provided App Check token has expired.z(Decoding App Check token failed. Error: Naudz>Firebase App Check token has incorrect "aud" (audience) claim.issz2Token does not contain the correct "iss" (issuer).z2The provided App Check token "sub" (subject) claimr'   )r-   decoder!   r   r    r	   r
   _APP_CHECK_ISSUERr   r   r1   
isinstancelist
startswithr)   r*   )r$   r   r2   payloadr4   r?   s         r   r/   z#_AppCheckService._decode_and_verifyd   s   	j#90	  GG % 	 	 	H   $ 	 	 	J/3/FJ J J   " 	 	 	F-1-CF F   % 	 	 	;   ! 	 	 	F9FF  	
 ;;u%%(D)) 	_T-DH-T-T]^^^{{5!!,,T-CDD 	SQRRR  @KK	  	  	  s   # A;B5B00B5)__name__
__module____qualname____doc__rC   r"   r   r!   r#   r%   strr   r   r   r,   r/    r   r   r   r   -   s        IIBAIKLH H H# $sCx.    &     * *# * * * * * *r   r   c                   2    e Zd ZdZededefd            ZdS )r)   zA collection of data validation utilities.

    Methods provided in this class raise ``ValueErrors`` if any validations fail.
    labelvaluec                     |#t          d                    ||                    t          |t                    s#t          d                    ||                    dS )z&Checks if the given value is a string.Nz%{0} "{1}" must be a non-empty string.z{0} "{1}" must be a string.)r    formatrD   rL   )clsrO   rP   s      r   r*   z_Validators.check_string   sg     =DKKESXYYZZZ%%% 	Q:AA%OOPPP	Q 	Qr   N)rH   rI   rJ   rK   classmethodrL   r   r*   rM   r   r   r)   r)      sT         
 Q QS Q Q Q [Q Q Qr   r)   r   )rK   typingr   r   r-   r   r   r   r   r	   r
   r   firebase_adminr   r   r   rL   r   r   r)   rM   r   r   <module>rW      sD   !           



 R R R R R R R R R R R R O O O O O O O O O O ! ! ! ! ! !# O3 O O O O; ; ;$sCx. ; ; ; ;"a a a a a a a aFQ Q Q Q Q Q Q Q Q Qr   