
    _j                        S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJr  SSK	J
r
  SSKJr  SSKJr  SSKJrJr  SS	KJr  SS
KJr  SS/r " S S\5      r " S S\5      rg)    )annotationsN)cast   )HeadersLike)get_required_header)construct_type)SyncAPIResourceAsyncAPIResource)InvalidWebhookSignatureError)UnwrapWebhookEventWebhooksAsyncWebhooksc                  Z    \ rS rSrSS.       S	S jjrSSS.         S
S jjrSrg)r      Nsecretc          	         Uc  U R                   R                  nU R                  XUS9  [        [        [        [        [        R                  " U5      S95      $ )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       W/home/rurouni/.local/lib/python3.13/site-packages/openai/resources/webhooks/webhooks.pyunwrapWebhooks.unwrap   sR     >\\00FgvN(jj)
 	
    ,  r   	tolerancec                 ^ Uc  U R                   R                  nUc  [        S5      e[        US5      n[        US5      n[        US5      n [	        U5      n[	        [        R                  " 5       5      n	X-
  U:  a  [        S5      SeXU-   :  a  [        S5      Se/ n
UR                  5        H@  nUR                  S	5      (       a  U
R                  US
S 5        M/  U
R                  U5        MB     UR                  S5      (       a  [        R                  " USS 5      nOUR                  5       n[        U[        5      (       a  UR                  S5      OUnU SU SU 3n[        R                   " ["        R$                  " XR                  5       [&        R(                  5      R+                  5       5      R                  5       m[-        U4S jU
 5       5      (       d  [        S5      Seg! [         a    [        S5      Sef = f),  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,r   whsec_   utf-8.c              3  R   >#    U  H  n[         R                  " TU5      v   M     g 7fNhmaccompare_digest.0sigexpected_signatures     r!   	<genexpr>,Webhooks.verify_signature.<locals>.<genexpr>p   "     V:C4&&'93??:   $'AThe given webhook signature does not match the expected signaturer   r   
ValueErrorr   intr   timesplit
startswithappendbase64	b64decodeencode
isinstancebytesdecode	b64encoder9   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   Webhooks.verify_signature,   s     >\\00F>` 
 /w8KL'1DE	(,?
	] #I $))+"Y../MNTXXY../MNTXX
 !#
$**,Du%%!!$qr(+!!$'	 - X&&#--fQRj9N#]]_N*4We*D*Dw~~g&' '<q1TF;#--HH^%:%:%<gnnMTTV

&( 	
 V:VVV.S WI  	]./QRX\\	]s   G1 1H r   str | bytesr   r   r   
str | Nonereturnr   
r   re   r   r   r   rf   r'   rF   rg   None__name__
__module____qualname____firstlineno__r"   r   __static_attributes__rc   r$   r!   r   r      s{     "

 

 
 

4 "GG G
 G G 
G Gr$   c                  Z    \ rS rSrSS.       S	S jjrSSS.         S
S jjrSrg)r   v   Nr   c          	        Uc  U R                   R                  nU R                  XUS9  [        U[        5      (       a  UR                  S5      OUn[        [        [        [        [        R                  " U5      S95      $ )r   r   r4   r   )r   r   r   rN   rO   rP   r   r   r   r   r   )r    r   r   r   r`   s        r!   r"   AsyncWebhooks.unwrapw   sn     >\\00FgvN*4We*D*Dw~~g&'(jj&
 	
r$   r%   r&   c                 ^ Uc  U R                   R                  nUc  [        S5      Se[        US5      n[        US5      n[        US5      n [	        U5      n[	        [        R                  " 5       5      n	X-
  U:  a  [        S5      SeXU-   :  a  [        S5      Se/ n
UR                  5        H@  nUR                  S	5      (       a  U
R                  US
S 5        M/  U
R                  U5        MB     UR                  S5      (       a  [        R                  " USS 5      nOUR                  5       n[        U[        5      (       a  UR                  S5      OUnU SU SU 3n[        R                   " ["        R$                  " XR                  5       [&        R(                  5      R+                  5       5      R                  5       m[-        U4S jU
 5       5      (       d  [        S5      eg! [         a    [        S5      Sef = f)r)   Nr*   r+   r,   r-   r.   r/   r0   r1   r   r2   r3   r4   r5   c              3  R   >#    U  H  n[         R                  " TU5      v   M     g 7fr7   r8   r;   s     r!   r?   1AsyncWebhooks.verify_signature.<locals>.<genexpr>   rA   rB   rC   rD   rW   s                  @r!   r   AsyncWebhooks.verify_signature   s     >\\00F>` 
 /w8KL'1DE	(,?
	] #I $))+"Y../MNTXXY../MNTXX
 !#
$**,Du%%!!$qr(+!!$'	 - X&&#--fQRj9N#]]_N*4We*D*Dw~~g&' '<q1TF;#--HH^%:%:%<gnnMTTV

&( 	
 V:VVV./rss WI  	]./QRX\\	]s   G1 1Hrc   rd   rh   rj   rc   r$   r!   r   r   v   s     "

 

 
 

6 "EtEt Et
 Et Et 
Et Etr$   )
__future__r   r9   r   rG   rK   rS   typingr   _typesr   _utilsr   _modelsr   	_resourcer	   r
   _exceptionsr   #types.webhooks.unwrap_webhook_eventr   __all__r   r   rc   r$   r!   <module>r      sX    #       ! ) % : 7 E
'] ]@\t$ \tr$   