U
    ~fh                     @  s   d dl mZ d dlZd dlZ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mZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddgZG dd deZG dd deZdS )    )annotationsN)cast   )HeadersLike)get_required_header)construct_type)SyncAPIResourceAsyncAPIResource)InvalidWebhookSignatureError)UnwrapWebhookEventWebhooksAsyncWebhooksc                   @  sD   e Zd Zddddddddd	Zdd
dddddddddZdS )r   Nsecretstr | bytesr   
str | Noner   payloadheadersr   returnc                C  s8   |dkr| j j}| j|||d ttttt|dS )KValidates that the given payload was sent by OpenAI and parses the payload.Nr   r   r   type_value)_clientwebhook_secretverify_signaturer   r   r   jsonloads)selfr   r   r    r!   =/tmp/pip-unpacked-wheel-rjiviquo/openai/resources/webhooks.pyunwrap   s    zWebhooks.unwrap,  r   	toleranceintNoner   r   r   r&   r   c                  sx  |dkr| j j}|dkr tdt|d}t|d}t|d}zt|}W n tk
rh   tddY nX tt }	|	| |krtdd||	| krtddg }
| D ],}|d	r|
	|d
d  q|
	| q|drt
|dd }n| }t|tr|dn|}| d| d| }t
t|| tj   t fdd|
D sttdddS )\  Validates whether or not the webhook payload was sent by OpenAI.

        Args:
            payload: The webhook payload
            headers: The webhook headers
            secret: The webhook secret (optional, will use client secret if not provided)
            tolerance: Maximum age of the webhook in seconds (default: 300 = 5 minutes)
        NThe webhook secret must either be set using the env var, OPENAI_WEBHOOK_SECRET, on the client class, OpenAI(webhook_secret='123'), or passed to this functionwebhook-signaturewebhook-timestamp
webhook-id Invalid webhook timestamp formatWebhook timestamp is too oldWebhook timestamp is too newv1,   whsec_   utf-8.c                 3  s   | ]}t  |V  qd S Nhmaccompare_digest.0sigZexpected_signaturer!   r"   	<genexpr>p   s     z,Webhooks.verify_signature.<locals>.<genexpr>AThe given webhook signature does not match the expected signaturer   r   
ValueErrorr   r'   r
   timesplit
startswithappendbase64	b64decodeencode
isinstancebytesdecode	b64encoder:   newhashlibsha256digestanyr    r   r   r   r&   Zsignature_header	timestampZ
webhook_idZtimestamp_secondsnowZ
signaturespartZdecoded_secretbodyZsigned_payloadr!   r?   r"   r   ,   sJ    






zWebhooks.verify_signature__name__
__module____qualname__r#   r   r!   r!   r!   r"   r      s
   c                   @  sD   e Zd Zddddddddd	Zdd
dddddddddZdS )r   Nr   r   r   r   r   r   c                C  sP   |dkr| j j}| j|||d t|tr4|dn|}ttttt	
|dS )r   Nr   r6   r   )r   r   r   rK   rL   rM   r   r   r   r   r   )r    r   r   r   rX   r!   r!   r"   r#   w   s    zAsyncWebhooks.unwrapr$   r%   r'   r(   r)   c                  sx  |dkr| j j}|dkr"tddt|d}t|d}t|d}zt|}W n tk
rj   tddY nX tt }	|	| |krtdd||	| krtddg }
| D ],}|d	r|
	|d
d  q|
	| q|drt
|dd }n| }t|tr|dn|}| d| d| }t
t|| tj   t fdd|
D sttddS )r*   Nr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   c                 3  s   | ]}t  |V  qd S r8   r9   r<   r?   r!   r"   r@      s     z1AsyncWebhooks.verify_signature.<locals>.<genexpr>rA   rB   rT   r!   r?   r"   r      sF    






zAsyncWebhooks.verify_signaturerY   r!   r!   r!   r"   r   v   s
   )
__future__r   r:   r   rD   rH   rP   typingr   _typesr   _utilsr   Z_modelsr   Z	_resourcer   r	   _exceptionsr
   Z#types.webhooks.unwrap_webhook_eventr   __all__r   r   r!   r!   r!   r"   <module>   s   `