o
     i                     @   s   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	d
Zddedeeef fddZG dd dZG dd dZdS )zFirebase App Check module.    )AnyDictN)PyJWKClientExpiredSignatureErrorInvalidTokenErrorDecodeError)InvalidAudienceErrorInvalidIssuerErrorInvalidSignatureError)_utils
_app_checkreturnc                 C   s   t | ttS N)r   get_app_service_APP_CHECK_ATTRIBUTE_AppCheckService)app r   x/var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/lib/python3.10/site-packages/firebase_admin/app_check.py_get_app_check_service   s   r   tokenc                 C   s   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   r   r   r   r      s   r   c                   @   sv   e Zd ZdZdZdZdZdZdZde	
 iZdd Zded	eeef fd
dZded	dfddZdedefd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/jwksNzx-goog-api-clientc                 C   s:   |j | _| jstdd|j  | _t| jd| 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headers)
project_id_project_id
ValueError_scoped_project_idr   	_JWKS_URL_APP_CHECK_HEADERS_jwks_client)selfr   r   r   r   __init__:   s   
z_AppCheckService.__init__r   r   c              
   C   sz   t d| z| j|}| t| | ||j}W n t	t
fy3 } ztd| |d}~w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	exceptionr   r   r   r   I   s   z_AppCheckService.verify_tokenr   c                 C   s<   | 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)r-   r   )r!   r   	algorithmr   r   r   r(   \   s   
z)_AppCheckService._has_valid_token_headersr.   c              
   C   s0  i }zt j||dg| jd}W n] ty  } ztd|d}~w ty5 } z
td| j d|d}~w tyI } z	td| j |d}~w tyY } ztd|d}~w t	yl } ztd	| |d}~ww |
d
}t|tr|| j|vrtd|
d| jstdtd|
d |S )z.Decodes and verifies the token from App Check.r4   )
algorithmsaudiencez6The provided App Check token has an invalid signature.NzbThe provided App Check token has an incorrect "aud" (audience) claim. Expected payload to include r5   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: 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   r-   
isinstancelist
startswithr%   r&   )r!   r   r.   payloadr0   r8   r   r   r   r+   i   st   

z#_AppCheckService._decode_and_verify)__name__
__module____qualname____doc__r<   r   r   r   r    r   get_metrics_headerr   r"   strr   r   r   r(   r+   r   r   r   r   r   -   s    r   c                   @   s&   e Zd ZdZededefddZdS )r%   zA collection of data validation utilities.

    Methods provided in this class raise ``ValueErrors`` if any validations fail.
    labelvaluec                 C   s>   |du rt | d| dt|tst | d| ddS )z&Checks if the given value is a string.Nz "z" must be a non-empty string.z" must be a string.)r   r=   rF   )clsrG   rH   r   r   r   r&      s
   
z_Validators.check_stringN)rA   rB   rC   rD   classmethodrF   r   r&   r   r   r   r   r%      s    r%   r   )rD   typingr   r   r)   r   r   r   r   r   r	   r
   firebase_adminr   r   r   rF   r   r   r%   r   r   r   r   <module>   s   h