
    Ѯh                         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y)zFirebase App Check module.    )AnyDictN)PyJWKClientExpiredSignatureErrorInvalidTokenErrorDecodeError)InvalidAudienceErrorInvalidIssuerErrorInvalidSignatureError)_utils
_app_checkreturnc                 @    t        j                  | t        t              S N)r   get_app_service_APP_CHECK_ATTRIBUTE_AppCheckService)apps    j/home/www/academy-backend.kofcorporation.com/venv/lib/python3.12/site-packages/firebase_admin/app_check.py_get_app_check_servicer      s    !!#';=MNN    tokenc                 6    t        |      j                  |       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                       e Zd ZdZdZdZdZdZdZd e	j                         i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y)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                     |j                   | _        | j                  st        d      d|j                   z   | _        t	        | j
                  d| j                        | _        y )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   s     r   __init__z_AppCheckService.__init__:   s[    >>=> >
 #.">'NNUD4K4KMr   r   r   c                 ^   t         j                  d|       	 | j                  j                  |      }| j	                  t        j                  |             | j                  ||j                        }|j                  d      |d<   |S # t        t        f$ r}t        d|       |d}~ww xY w)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_tokenI   s      !2E:
	!++DDUKK))#*C*CE*JK"55e[__MO %4$7$7$>! ";/ 	!;I;G !	!s   AB	 	B,B''B,r   c                     |j                  d      dk7  rt        d      |j                  d      }|dk7  rt        d| d      y)	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)r3   r!   )r&   r   	algorithms      r   r.   z)_AppCheckService._has_valid_token_headers\   sZ     ;;u&XYYKK&	**3A7   r   r4   c                    i }	 t        j                  ||dg| j                        }|j                  d
      }t        |t              r| j                  |vrt	        d      |j                  d      j                  | j                        st	        d      t        j                  d|j                  d             |S # t        $ r}t	        d      |d}~wt
        $ r}t	        d| j                   d      |d}~wt        $ r}t	        d| j                         |d}~wt        $ r}t	        d      |d}~wt        $ r}t	        d	|       |d}~ww xY w)z.Decodes and verifies the token from App Check.r;   )
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: 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   r3   
isinstancelist
startswithr+   r,   )r&   r   r4   payloadr6   r@   s         r   r1   z#_AppCheckService._decode_and_verifyi   s   	!jj#900	G: ;;u%(D)T-D-DH-T]^^{{5!,,T-C-CDQRR  @KK	  A % 	!H ! $ 	!//3/F/F.GqJ !! " 	!--1-C-C,DF !! % 	!; ! ! 	!:9+F !	!sG   $B> >	ECEC99EDE*D66EEE)__name__
__module____qualname____doc__rD   r#   r    r"   r%   r   get_metrics_headerr$   r'   strr   r   r   r.   r1    r   r   r   r   -   s    IBAIKL 	6V668M# $sCx. &  * *# *r   r   c                   *    e Zd ZdZededefd       Zy)r+   zA collection of data validation utilities.

    Methods provided in this class raise ``ValueErrors`` if any validations fail.
    labelvaluec                 l    |t        | d| d      t        |t              st        | d| d      y)z&Checks if the given value is a string.Nz "z" must be a non-empty string.z" must be a string.)r!   rE   rN   )clsrQ   rR   s      r   r,   z_Validators.check_string   sJ     =wb/LMNN%%wb/BCDD &r   N)rI   rJ   rK   rL   classmethodrN   r   r,   rO   r   r   r+   r+      s-    
 E ES E Er   r+   r   )rL   typingr   r   r/   r   r   r   r   r	   r
   r   firebase_adminr   r   r   rN   r   r   r+   rO   r   r   <module>rX      sg    !  
 R R O O !# O3 O; ;$sCx. ;"f fPE Er   