o
    wÔ+fÂ  ã                   @   sB   d dl mZ d dlmZ d dlmZ ddlmZ G dd„ dƒZdS )	é    )ÚEcdsa)Ú	PublicKey)Ú	Signatureé   )ÚEventWebhookHeaderc                   @   s,   e Zd ZdZd	dd„Zdd„ Zd	dd„ZdS )
ÚEventWebhookz¤
    This class allows you to use the Event Webhook feature. Read the docs for
    more details: https://sendgrid.com/docs/for-developers/tracking-events/event
    Nc                 C   s   |r
|   |¡| _dS || _dS )zž
        Construct the Event Webhook verifier object
        :param public_key: verification key under Mail Settings
        :type public_key: string
        N)Úconvert_public_key_to_ecdsaÚ
public_key©Úselfr	   © r   úY/var/www/html/venv/lib/python3.10/site-packages/sendgrid/helpers/eventwebhook/__init__.pyÚ__init__   s   zEventWebhook.__init__c                 C   s   t  d| d ¡S )zñ
        Convert the public key string to a ECPublicKey.

        :param public_key: verification key under Mail Settings
        :type public_key string
        :return: public key using the ECDSA algorithm
        :rtype PublicKey
        z
-----BEGIN PUBLIC KEY-----
z
-----END PUBLIC KEY-----
)r   ÚfromPemr
   r   r   r   r      s   	z(EventWebhook.convert_public_key_to_ecdsac                 C   s*   || }t  |¡}|p| j}t |||¡S )a"  
        Verify signed event webhook requests.

        :param payload: event payload in the request body
        :type payload: string
        :param signature: value obtained from the 'X-Twilio-Email-Event-Webhook-Signature' header
        :type signature: string
        :param timestamp: value obtained from the 'X-Twilio-Email-Event-Webhook-Timestamp' header
        :type timestamp: string
        :param public_key: elliptic curve public key
        :type public_key: PublicKey
        :return: true or false if signature is valid
        )r   Ú
fromBase64r	   r   Úverify)r   ÚpayloadÚ	signatureÚ	timestampr	   Útimestamped_payloadÚdecoded_signatureÚkeyr   r   r   Úverify_signature    s   

zEventWebhook.verify_signature)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s
    
r   N)	Úellipticcurve.ecdsar   Úellipticcurve.publicKeyr   Úellipticcurve.signaturer   Úeventwebhook_headerr   r   r   r   r   r   Ú<module>   s
    