o
    "ch                     @  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                   @  .   e Zd ZdddddZddddddZdS )r   Nsecretpayloadstr | bytesheadersr   r   
str | Nonereturnr   c                C  s8   |du 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!   W/home/air/goalskill/back/venv/lib/python3.10/site-packages/openai/resources/webhooks.pyunwrap   s   zWebhooks.unwrap,  r   	tolerancer&   intNonec                  sn  |du r| j j}|du rtdt|d}t|d}t|d}zt|}W n ty1   tddw tt }	|	| |krCtdd||	| krNtddg }
| D ]}|d	re|
	|d
d  qT|
	| qT|drzt
|dd }n| }t|tr|dn|}| d| d| }t
t|| tj   t fdd|
D st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      | ]	}t  |V  qd S Nhmaccompare_digest.0sigexpected_signaturer!   r"   	<genexpr>p       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&   signature_header	timestamp
webhook_idtimestamp_secondsnow
signaturespartdecoded_secretbodysigned_payloadr!   r?   r"   r   ,   sR   







zWebhooks.verify_signaturer   r   r   r   r   r   r   r   
r   r   r   r   r   r   r&   r'   r   r(   __name__
__module____qualname__r#   r   r!   r!   r!   r"   r      s    c                   @  r   )r   Nr   r   r   r   r   r   r   r   r   c                C  sP   |du r| j j}| j|||d t|tr|dn|}ttttt	
|dS )r   Nr   r5   r   )r   r   r   rM   rN   rO   r   r   r   r   r   )r    r   r   r   r_   r!   r!   r"   r#   w   s   zAsyncWebhooks.unwrapr$   r%   r&   r'   r(   c                  sn  |du r| j j}|du rtddt|d}t|d}t|d}zt|}W n ty2   tddw tt }	|	| |krDtdd||	| krOtddg }
| D ]}|d	rf|
	|d
d  qU|
	| qU|dr{t
|dd }n| }t|tr|dn|}| d| d| }t
t|| tj   t fdd|
D stddS )r)   Nr*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   c                 3  r7   r8   r9   r<   r?   r!   r"   rA      rB   z1AsyncWebhooks.verify_signature.<locals>.<genexpr>rC   rD   rV   r!   r?   r"   r      sN   







zAsyncWebhooks.verify_signaturera   rb   rc   r!   r!   r!   r"   r   v   s    )
__future__r   r:   r   rF   rJ   rR   typingr   _typesr   _utilsr   _modelsr   	_resourcer   r	   _exceptionsr
   #types.webhooks.unwrap_webhook_eventr   __all__r   r   r!   r!   r!   r"   <module>   s    `