o
    L)j                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ G dd	 d	eZG d
d deZdS )    N)OrderedDict)sha256)Eventsecure_compare)SignatureVerificationError)_APIRequestorc                   @   s"   e Zd ZdZeedfddZdS )Webhooki,  Nc                 C   sf   t | dr
| d} t| ||| tj| td}tj|t	j
|p"tjddd}|jdkr1td|S )	Ndecodeutf-8)object_pairs_hook)api_keyV1)values	requestorapi_modezv2.core.eventzYou passed a thin event notification to Webhook.construct_event, which expects a webhook payload. Use StripeClient.parse_event_notification instead.)hasattrr
   WebhookSignatureverify_headerjsonloadsr   r   _construct_fromr   _global_with_optionsstriper   object
ValueError)payload
sig_headersecret	tolerancer   dataevent r"   t/var/www/html/finance-dev.cargoinsureonline.com/_shared/backend-venv/lib/python3.10/site-packages/stripe/_webhook.pyconstruct_event   s    


zWebhook.construct_event)__name__
__module____qualname__DEFAULT_TOLERANCEstaticmethodr$   r"   r"   r"   r#   r	      s
    r	   c                   @   s6   e Zd ZdZedd Zedd Zed	ddZdS )
r   v1c                 C   s$   t j|d| dtd}| S )Nr   )msg	digestmod)hmacnewencoder   	hexdigest)r   r   macr"   r"   r#   _compute_signature.   s   z#WebhookSignature._compute_signaturec                    sD   dd |  dD }tdd |D d } fdd|D }||fS )Nc                 S   s   g | ]}| d dqS )=   )split.0ir"   r"   r#   
<listcomp>9   s    zBWebhookSignature._get_timestamp_and_signatures.<locals>.<listcomp>,c                 S   s    g | ]}|d  dkr|d qS )r   t   r"   r6   r"   r"   r#   r9   :        r   c                    s    g | ]}|d   kr|d qS )r   r<   r"   r6   schemer"   r#   r9   ;   r=   )r5   int)headerr?   
list_items	timestamp
signaturesr"   r>   r#   _get_timestamp_and_signatures7   s   z.WebhookSignature._get_timestamp_and_signaturesNc                    s   z|  || j\}}W n ty   td||w |s$td| j ||d||f }| || t fdd|D sAtd|||rS|t | k rStd| ||dS )	Nz6Unable to extract timestamp and signatures from headerz+No signatures found with expected scheme %sz%d.%sc                 3   s    | ]}t  |V  qd S Nr   )r7   sexpected_sigr"   r#   	<genexpr>U   s    z1WebhookSignature.verify_header.<locals>.<genexpr>z?No signatures found matching the expected signature for payloadz)Timestamp outside the tolerance zone (%d)T)rE   EXPECTED_SCHEME	Exceptionr   r2   anytime)clsr   rA   r   r   rC   rD   signed_payloadr"   rH   r#   r   >   sD   zWebhookSignature.verify_headerrF   )	r%   r&   r'   rK   r)   r2   rE   classmethodr   r"   r"   r"   r#   r   +   s    

r   )r-   r   rN   collectionsr   hashlibr   r   stripe._eventr   stripe._utilr   stripe._errorr   stripe._api_requestorr   r   r	   r   r"   r"   r"   r#   <module>   s    