o
    ùT·j   ã                   @  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mZ ddlm	Z	 ddl
mZ ddd„ZdS )é    )ÚannotationsNé   )ÚHeadersLike)Úget_required_header)ÚInvalidWebhookSignatureErrorÚpayloadústr | bytesÚheadersr   ÚsecretÚstrÚ	toleranceÚintÚreturnÚboolc                  s@  t |dƒ}t |dƒ}t |dƒ}zt|ƒ}W n ty!   tdƒd‚w tt ¡ ƒ}|| |kr3tdƒd‚||| kr>tdƒd‚g }	| ¡ D ]}
|
 d¡rU|	 |
d	d… ¡ qD|	 |
¡ qD| d
¡rjt 	|dd… ¡}n| 
¡ }t| tƒrx|  d¡n| }|› d|› d|› �}t t || 
¡ tj¡ ¡ ¡ ¡ ‰ t‡ fdd„|	D ƒƒS )z?Validate the replay window and compare the supplied signatures.zwebhook-signaturezwebhook-timestampz
webhook-idz Invalid webhook timestamp formatNzWebhook timestamp is too oldzWebhook timestamp is too newzv1,é   Úwhsec_é   zutf-8Ú.c                 3  s   � | ]	}t  ˆ |¡V  qd S )N)ÚhmacÚcompare_digest)Ú.0Úsig©Úexpected_signature© úW/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/openai/lib/_webhooks.pyÚ	<genexpr>@   s   € z,webhook_signature_matches.<locals>.<genexpr>)r   r   Ú
ValueErrorr   ÚtimeÚsplitÚ
startswithÚappendÚbase64Ú	b64decodeÚencodeÚ
isinstanceÚbytesÚdecodeÚ	b64encoder   ÚnewÚhashlibÚsha256ÚdigestÚany)r   r	   r
   r   Úsignature_headerÚ	timestampÚ
webhook_idÚtimestamp_secondsÚnowÚ
signaturesÚpartÚdecoded_secretÚbodyÚsigned_payloadr   r   r   Úwebhook_signature_matches   s:   



ÿ



ÿþr8   )
r   r   r	   r   r
   r   r   r   r   r   )Ú
__future__r   r   r   r"   r*   Ú_typesr   Ú_utilsr   Ú_exceptionsr   r8   r   r   r   r   Ú<module>   s    