o
    0=j  ã                   @   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)Ú_utilsZ
_app_checkÚreturnc                 C   s   t  | tt¡S ©N)r   Zget_app_serviceÚ_APP_CHECK_ATTRIBUTEÚ_AppCheckService)Úapp© r   úbC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\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.
    N)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 )Nz·A 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  )ZlifespanÚheaders)Z
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ÚsubZapp_id)Ú_ValidatorsÚcheck_stringr   Zget_signing_key_from_jwtÚ_has_valid_token_headersÚjwtZget_unverified_headerÚ_decode_and_verifyÚkeyr   r   r   Úget)r   r   Úsigning_keyZ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.ÚtypZ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r€tdƒ‚| 
d¡ | j¡s�tdƒ‚t d| 
d¡¡ |S )z.Decodes and verifies the token from App Check.r+   )Z
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 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: Zaudz>Firebase App Check token has incorrect "aud" (audience) claim.Z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'   Úpayloadr(   r.   r   r   r   r$   i   st   
üÿþ€ÿÿý€ÿÿý€ÿþ€ÿþ€ÿ
þz#_AppCheckService._decode_and_verify)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r   r   r   r   r   Z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    z‚A 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   r1   r9   )Úclsr:   r;   r   r   r   r!   ›   s
   
ÿz_Validators.check_stringN)r5   r6   r7   r8   Úclassmethodr9   r   r!   r   r   r   r   r    •   s    r    r   )r8   Útypingr   r   r#   r   r   r   r   r   r	   r
   Zfirebase_adminr   r   r   r9   r   r   r    r   r   r   r   Ú<module>   s   h