o
    0=j^#  ã                   @  sä   d Z ddlmZ ddlmZmZmZ ddlZddlmZm	Z	m
Z
mZmZmZ ddlmZmZmZ dZdZd	Zd
Zdd„ Zd d!dd„ZG dd„ deƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZG dd„ dejƒZG dd„ deƒZdS )"z™Firebase Phone Number Verification module.

This module contains functions for verifying JWTs related to the Firebase
Phone Number Verification service.
é    )Úannotations)ÚAnyÚDictÚOptionalN)ÚPyJWKClientÚInvalidSignatureErrorÚPyJWKClientErrorÚInvalidAudienceErrorÚInvalidIssuerErrorÚExpiredSignatureError)ÚAppÚ_utilsÚ
exceptionsZ_phone_number_verificationz'https://fpnv.googleapis.com/v1beta/jwksz%https://fpnv.googleapis.com/projects/ZES256c                 C  s   t  | tt¡S ©N)r   Zget_app_serviceÚ_FPNV_ATTRIBUTEÚ_FpnvService)Úapp© r   úrC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\firebase_admin/phone_number_verification.pyÚ_get_fpnv_service%   s   r   ÚtokenÚstrr   úOptional[App]ÚreturnÚPhoneNumberVerificationTokenc                 C  s   t |ƒ | ¡S )aÌ  Verifies a Firebase Phone Number Verification token.

    Args:
        token: A string containing the Firebase Phone Number Verification JWT.
        app: An App instance (optional).

    Returns:
        PhoneNumberVerificationToken: The verified token claims.

    Raises:
        ValueError: If the token is not a string or is empty.
        InvalidTokenError: If the token is invalid or malformed.
        ExpiredTokenError: If the token has expired.
    N)r   Úverify_token)r   r   r   r   r   r   (   s   r   c                      s€   e Zd ZdZ‡ fdd„Zeddd„ƒZeddd	„ƒZedd
d„ƒZeddd„ƒZ	eddd„ƒZ
eddd„ƒZedd„ ƒZ‡  ZS )r   zÕRepresents a verified Firebase Phone Number Verification token.

    This class behaves like a dictionary, allowing access to the decoded claims.
    It also provides convenience properties for common claims.
    c                   s   t ƒ  |¡ | d¡| d< d S )NÚsubÚphone_number)ÚsuperÚ__init__Úget)ÚselfÚclaims©Ú	__class__r   r   r   A   s   z%PhoneNumberVerificationToken.__init__r   r   c                 C  ó
   |   d¡S )zfReturns the phone number of the user.
        This corresponds to the 'sub' claim in the JWT.
        r   N©r    ©r!   r   r   r   r   E   s   
z)PhoneNumberVerificationToken.phone_numberc                 C  r%   )z=Returns the issuer identifier for the issuer of the response.ZissNr&   r'   r   r   r   ÚissuerL   ó   
z#PhoneNumberVerificationToken.issuerc                 C  r%   )z6Returns the audience for which this token is intended.ZaudNr&   r'   r   r   r   ÚaudienceQ   r)   z%PhoneNumberVerificationToken.audienceÚintc                 C  r%   )z1Returns the expiration time since the Unix epoch.ÚexpNr&   r'   r   r   r   r,   V   r)   z PhoneNumberVerificationToken.expc                 C  r%   )z0Returns the issued-at time since the Unix epoch.ÚiatNr&   r'   r   r   r   r-   [   r)   z PhoneNumberVerificationToken.iatc                 C  r%   )zBReturns the sub (subject) of the token, which is the phone number.r   Nr&   r'   r   r   r   r   `   r)   z PhoneNumberVerificationToken.subc                 C  s   | S )z!Returns the entire map of claims.Nr   r'   r   r   r   r"   e   s   z#PhoneNumberVerificationToken.claims)r   r   )r   r+   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r(   r*   r,   r-   r   r"   Ú__classcell__r   r   r#   r   r   :   s"    c                   @  s&   e Zd ZdZdZdd„ Zd	dd„ZdS )
r   zOService class that implements Firebase Phone Number Verification functionality.Nc                 C  s&   |j | _| jstdƒ‚t| jƒ| _d S )Nz‘Project ID is required for Firebase Phone Number Verification. Please ensure the app is initialized with a credential that contains a project ID.)Ú
project_idÚ_project_idÚ
ValueErrorÚ_FpnvTokenVerifierÚ	_verifier)r!   r   r   r   r   r   o   s   ÿz_FpnvService.__init__r   r   c                 C  s   t | j |¡ƒS )a  Verifies a Firebase Phone Number Verification token.

        Verifies the signature, expiration, and claims of the token.

        Args:
            token: A string containing the Firebase Phone Number Verification JWT.

        Returns:
            PhoneNumberVerificationToken: The verified token claims.

        Raises:
            ValueError: If the token is not a string or is empty.
            InvalidTokenError: If the token is invalid or malformed.
            ExpiredTokenError: If the token has expired.
        N)r   r8   Úverify)r!   r   r   r   r   r   y   s   z_FpnvService.verify_token)r   r   )r.   r/   r0   r1   r5   r   r   r   r   r   r   r   k   s
    
r   c                   @  s>   e Zd ZdZdZdZdd„ Zddd„Zddd„Zddd„Z	dS )r7   zWInternal class for verifying Firebase Phone Number Verification JWTs signed with ES256.Nc                 C  s   || _ ttdd�| _d S )Ni`T  )Zlifespan)r5   r   Ú_FPNV_JWKS_URLÚ_jwks_client)r!   r4   r   r   r   r   ‘   s   z_FpnvTokenVerifier.__init__r   úDict[str, Any]c              
   C  st   t  d|¡ z|  t |¡¡ | j |¡}|  ||j¡}W |S  tj	t
fy9 } zt	d|t|ddƒd�|‚d}~ww )z<Verifies the given Firebase Phone Number Verification token.z.Firebase Phone Number Verification check tokenz1Verifying phone number verification token failed.Úhttp_responseN)Úcauser=   )Ú_ValidatorsÚcheck_stringÚ_validate_headersÚjwtZget_unverified_headerr;   Zget_signing_key_from_jwtÚ_decode_and_verifyÚkeyÚInvalidTokenErrorr   Úgetattr)r!   r   Úsigning_keyr"   Ú	exceptionr   r   r   r9   •   s    ù
ýü€ÿz_FpnvTokenVerifier.verifyÚheadersr   ÚNonec                 C  sf   |  d¡du rtdƒ‚|  d¡dkrtd|  d¡›d�ƒ‚|  d¡}|tkr1td	t› d
|› d�ƒ‚dS )zValidates the headers.ZkidNzToken has no 'kid' claim.ÚtypZJWTzHThe provided token has an incorrect type header. Expected 'JWT' but got Ú.Úalgz9The provided token has an incorrect alg header. Expected z	 but got )r    rE   Ú_ALGORITHM_ES256)r!   rI   Ú	algorithmr   r   r   rA   ¥   s$   ÿÿ
ÿÿÿÿz$_FpnvTokenVerifier._validate_headersc              
   C  s  t › | j› �}ztj||tg||d�}W n] ty% } ztdƒ|‚d}~w ty9 } z	td|› d�ƒ|‚d}~w tyM } z	td|› d�ƒ|‚d}~w t	y] } zt
dƒ|‚d}~w tjyq } ztd|› �ƒ|‚d}~ww | d	¡}t|tƒr~|s‚td
ƒ‚|S )zDecodes and verifies the token.)Z
algorithmsr*   r(   z,The provided token has an invalid signature.NzEThe provided token has an incorrect "aud" (audience) claim. Expected rL   zCThe provided token has an incorrect "iss" (issuer) claim. Expected zThe provided token has expired.zDecoding token failed. Error: r   zWThe provided token has an incorrect "sub" (subject) claim. Expected a non-empty string.)Ú_FPNV_ISSUERr5   rB   ÚdecoderN   r   rE   r	   r
   r   ÚExpiredTokenErrorr    Ú
isinstancer   )r!   r   rG   Zexpected_issuerÚpayloadrH   Z	sub_claimr   r   r   rC   ·   sn   
ûÿþ€ÿÿý€ÿÿý€ÿþ€ÿþ€ÿ
ÿz%_FpnvTokenVerifier._decode_and_verify)r   r<   )rI   r   r   rJ   )
r.   r/   r0   r1   r;   r5   r   r9   rA   rC   r   r   r   r   r7   Œ   s    

r7   c                   @  s   e Zd ZdZed	dd„ƒZdS )
r?   z‚A collection of data validation utilities.

    Methods provided in this class raise ``ValueErrors`` if any validations fail.
    Úlabelr   Úvaluer   c                 C  s    t |tƒr|st|› d�ƒ‚dS )z&Checks if the given value is a string.z must be a non-empty string.N)rS   r   r6   )ÚclsrU   rV   r   r   r   r@   é   s   ÿz_Validators.check_stringN)rU   r   rV   r   )r.   r/   r0   r1   Úclassmethodr@   r   r   r   r   r?   ã   s    r?   c                   @  ó   e Zd ZdZddd„ZdS )rE   zBRaised when a Firebase Phone Number Verification token is invalid.Nc                 C  s   t j | |||¡ d S r   )r   ÚInvalidArgumentErrorr   ©r!   Úmessager>   r=   r   r   r   r   ó   s   zInvalidTokenError.__init__©NN©r.   r/   r0   r1   r   r   r   r   r   rE   ð   ó    rE   c                   @  rY   )rR   zBRaised when a Firebase Phone Number Verification token is expired.Nc                 C  s   t  | |||¡ d S r   )rE   r   r[   r   r   r   r   ù   s   zExpiredTokenError.__init__r]   r^   r   r   r   r   rR   ö   r_   rR   r   )r   r   r   r   r   r   ) r1   Ú
__future__r   Útypingr   r   r   rB   r   r   r   r	   r
   r   Zfirebase_adminr   r   r   r   r:   rP   rN   r   r   Údictr   r   r7   r?   rZ   rE   rR   r   r   r   r   Ú<module>   s$    1!W