
    zvh                        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y)    )annotationsN)cast   )HeadersLike)get_required_header)construct_type)SyncAPIResourceAsyncAPIResource)InvalidWebhookSignatureError)UnwrapWebhookEventWebhooksAsyncWebhooksc                  J    e Zd Zdd	 	 	 	 	 	 	 ddZddd	 	 	 	 	 	 	 	 	 ddZy)	r   Nsecretc          	         || j                   j                  }| j                  |||       t        t        t        t        t        j                  |                  S )KValidates that the given payload was sent by OpenAI and parses the payload.payloadheadersr   type_value)_clientwebhook_secretverify_signaturer   r   r   jsonloads)selfr   r   r   s       Q/var/www/html/luna/venv/lib/python3.12/site-packages/openai/resources/webhooks.pyunwrapzWebhooks.unwrap   sT     >\\00FgwvN(jj)
 	
    ,  r   	tolerancec                  || j                   j                  }|t        d      t        |d      }t        |d      }t        |d      }	 t	        |      }t	        t        j                               }	|	|z
  |kD  rt        d      d||	|z   kD  rt        d      dg }
|j                         D ]9  }|j                  d	      r|
j                  |d
d        )|
j                  |       ; |j                  d      rt        j                  |dd       }n|j                         }t        |t              r|j                  d      n|}| d| d| }t        j                   t#        j$                  ||j                         t&        j(                        j+                               j                         t-        fd|
D              st        d      dy# t        $ r t        d      dw xY w)\  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  J   K   | ]  }t        j                  |        y wNhmaccompare_digest.0sigexpected_signatures     r    	<genexpr>z,Webhooks.verify_signature.<locals>.<genexpr>p        VC4&&'93?V    #AThe given webhook signature does not match the expected signaturer   r   
ValueErrorr   intr   timesplit
startswithappendbase64	b64decodeencode
isinstancebytesdecode	b64encoder8   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=   s                  @r    r   zWebhooks.verify_signature,   s     >\\00F>` 
 /w8KL'1DE	(,?
	] #I $))+""Y../MNTXXsY../MNTXX
 !#
$**, 	(Du%!!$qr(+!!$'		( X&#--fQRj9N#]]_N*4We*Dw~~g&' '<q1TF;#--HH^^%:%:%<gnnMTTV

&( 	
 V:VV.S WI  	]./QRX\\	]s   G G*r   str | bytesr   r   r   
str | Nonereturnr   
r   ra   r   r   r   rb   r%   rD   rc   None__name__
__module____qualname__r!   r    r"   r    r   r      su     "

 

 
 

4 "GG G
 G G 
Gr"   c                  J    e Zd Zdd	 	 	 	 	 	 	 ddZddd	 	 	 	 	 	 	 	 	 ddZy)	r   Nr   c          	        || j                   j                  }| j                  |||       t        |t              r|j                  d      n|}t        t        t        t        t        j                  |                  S )r   r   r3   r   )r   r   r   rL   rM   rN   r   r   r   r   r   )r   r   r   r   r^   s        r    r!   zAsyncWebhooks.unwrapw   sm     >\\00FgwvN*4We*Dw~~g&'(jj&
 	
r"   r#   r$   c                  || j                   j                  }|t        d      dt        |d      }t        |d      }t        |d      }	 t	        |      }t	        t        j                               }	|	|z
  |kD  rt        d      d||	|z   kD  rt        d      dg }
|j                         D ]9  }|j                  d	      r|
j                  |d
d        )|
j                  |       ; |j                  d      rt        j                  |dd       }n|j                         }t        |t              r|j                  d      n|}| d| d| }t        j                   t#        j$                  ||j                         t&        j(                        j+                               j                         t-        fd|
D              st        d      y# t        $ r t        d      dw xY w)r'   Nr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   c              3  J   K   | ]  }t        j                  |        y wr6   r7   r:   s     r    r>   z1AsyncWebhooks.verify_signature.<locals>.<genexpr>   r?   r@   rA   rB   rU   s                  @r    r   zAsyncWebhooks.verify_signature   s     >\\00F>` 
 /w8KL'1DE	(,?
	] #I $))+""Y../MNTXXsY../MNTXX
 !#
$**, 	(Du%!!$qr(+!!$'		( X&#--fQRj9N#]]_N*4We*Dw~~g&' '<q1TF;#--HH^^%:%:%<gnnMTTV

&( 	
 V:VV./rss WI  	]./QRX\\	]s   G G*r`   rd   rf   rj   r"   r    r   r   v   s{     "

 

 
 

6 "EtEt Et
 Et Et 
Etr"   )
__future__r   r8   r   rE   rI   rQ   typingr   _typesr   _utilsr   _modelsr   	_resourcer	   r
   _exceptionsr   #types.webhooks.unwrap_webhook_eventr   __all__r   r   rj   r"   r    <module>rx      sX    #         ( $ 9 6 D
'] ]@\t$ \tr"   