
    U th                        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e          Z G 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ZdddddZdS )r   Nsecretpayloadstr | bytesheadersr   r   
str | Nonereturnr   c          	         || j         j        }|                     |||           t          t          t          t          t          j        |                              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   s       m/var/www/html/mycamper/aliexpress-site/backend/venv/lib/python3.11/site-packages/openai/resources/webhooks.pyunwrapzWebhooks.unwrap   se     >\0FgwvNNN(j))  
 
 	
    ,  r   	tolerancer(   intNonec                  || j         j        }|t          d          t          |d          }t          |d          }t          |d          }	 t	          |          }n# t          $ r t          d          dw xY wt	          t          j                              }	|	|z
  |k    rt          d          d||	|z   k    rt          d          dg }
|                                D ]J}|                    d	          r|
	                    |d
d                    5|
	                    |           K|                    d          rt          j        |dd                   }n|                                }t          |t                    r|                    d          n|}| d| d| }t          j        t#          j        ||                                t&          j                                                                                            t-          fd|
D                       st          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  B   K   | ]}t          j        |          V  d S Nhmaccompare_digest.0sigexpected_signatures     r#   	<genexpr>z,Webhooks.verify_signature.<locals>.<genexpr>p   1      VVC4&'93??VVVVVVr%   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_payloadrB   s                  @r#   r   zWebhooks.verify_signature,   s     >\0F>`  
 /w8KLL'1DEE	(,??
	] #I 	] 	] 	]./QRRX\\	] $)++""Y.../MNNTXXsY.../MNNTXX
 !#
$**,, 	( 	(Du%% (!!$qrr(++++!!$'''' X&& 	-#-fQRRj99NN#]]__N*4We*D*DQw~~g&&&' ';;;;T;;#-H^^%:%:%<%<gnMMTTVV
 

&(( 	
 VVVV:VVVVV 	.S 	 	s   A" "A=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      se         "
 
 
 
 
 
4 "G G G G G G G Gr%   c                  ,    e Zd ZddddZdddddZdS )r   Nr   r   r   r   r   r   r   r   r   c          	        || j         j        }|                     |||           t          |t                    r|                    d          n|}t          t          t          t          t          j
        |                              S )r   Nr   r8   r   )r   r   r   rO   rP   rQ   r   r   r   r    r!   )r"   r   r   r   ra   s        r#   r$   zAsyncWebhooks.unwrapw   s     >\0FgwvNNN*4We*D*DQw~~g&&&'(j&&  
 
 	
r%   r&   r'   r(   r)   r*   c                  || j         j        }|t          d          dt          |d          }t          |d          }t          |d          }	 t	          |          }n# t          $ r t          d          dw xY wt	          t          j                              }	|	|z
  |k    rt          d          d||	|z   k    rt          d          dg }
|                                D ]J}|                    d	          r|
	                    |d
d                    5|
	                    |           K|                    d          rt          j        |dd                   }n|                                }t          |t                    r|                    d          n|}| d| d| }t          j        t#          j        ||                                t&          j                                                                                            t-          fd|
D                       st          d          dS )r,   Nr-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   c              3  B   K   | ]}t          j        |          V  d S r;   r<   r?   s     r#   rC   z1AsyncWebhooks.verify_signature.<locals>.<genexpr>   rD   r%   rE   rF   rX   s                  @r#   r   zAsyncWebhooks.verify_signature   s     >\0F>`  
 /w8KLL'1DEE	(,??
	] #I 	] 	] 	]./QRRX\\	] $)++""Y.../MNNTXXsY.../MNNTXX
 !#
$**,, 	( 	(Du%% (!!$qrr(++++!!$'''' X&& 	-#-fQRRj99NN#]]__N*4We*D*DQw~~g&&&' ';;;;T;;#-H^^%:%:%<%<gnMMTTVV
 

&(( 	
 VVVV:VVVVV 	t./rsss	t 	ts   A# #A>rc   rd   re   ri   r%   r#   r   r   v   sm         "
 
 
 
 
 
6 "Et Et Et Et Et Et Et Etr%   )
__future__r   r=   r    rH   rL   rT   typingr   _typesr   _utilsr   _modelsr   	_resourcer	   r
   _exceptionsr   #types.webhooks.unwrap_webhook_eventr   __all__r   r   ri   r%   r#   <module>rw      sP   # " " " " "                        ( ( ( ( ( ( $ $ $ $ $ $ 9 9 9 9 9 9 9 9 6 6 6 6 6 6 D D D D D D
'] ] ] ] ] ] ] ]@\t \t \t \t \t$ \t \t \t \t \tr%   