o
    ϖi                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	m
Z
mZ er(d dlZG dd deZG dd deZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd de
ZdS )    N)sha256)TYPE_CHECKINGDictOptionaloverload)	NamespaceResourcec                   @   s   e Zd ZU dZeed< dS )WebhookSigningSecretz#
    A webhook signing secret.
    keyN)__name__
__module____qualname____doc__str__annotations__ r   r   E/home/ubuntu/.local/lib/python3.10/site-packages/replicate/webhook.pyr	      s   
 r	   c                   @      e Zd ZdZdS )WebhookValidationErrorz-Base exception for webhook validation errors.Nr   r   r   r   r   r   r   r   r          r   c                   @   r   )MissingWebhookHeaderErrorz;Exception raised when a required webhook header is missing.Nr   r   r   r   r   r      r   r   c                   @   r   )InvalidSecretKeyErrorz7Exception raised when the secret key format is invalid.Nr   r   r   r   r   r   !   r   r   c                   @   r   )MissingWebhookBodyErrorz2Exception raised when the webhook body is missing.Nr   r   r   r   r   r   %   r   r   c                   @   r   )InvalidTimestampErrorzPException raised when the webhook timestamp is invalid or outside the tolerance.Nr   r   r   r   r   r   )   r   r   c                   @   r   )InvalidSignatureErrorz7Exception raised when the webhook signature is invalid.Nr   r   r   r   r   r   -   r   r   c                   @   s   e Zd ZdZedddZG dd deZee		dd	d
de
dee defddZee		ddeeef dede
dee def
ddZe						dd	ed
 deeeef  dee dee
 dee ddfddZdS )Webhooksz7
    Namespace for operations related to webhooks.
    returnWebhooks.Defaultc                 C   s   |  | jS )J
        Namespace for operations related to the default webhook.
        )Default_client)selfr   r   r   default6   s   zWebhooks.defaultc                   @   s,   e Zd ZdZdefddZdefddZdS )r   r   r   c                 C   s    | j dd}tdi | S z
            Get the default webhook signing secret.

            Returns:
                WebhookSigningSecret: The default webhook signing secret.
            GETz/v1/webhooks/default/secretNr   )r!   _requestr	   jsonr"   respr   r   r   secretC   s   zWebhooks.Default.secretc                    s(   | j ddI dH }tdi | S r$   )r!   _async_requestr	   r'   r(   r   r   r   async_secretN   s
   
zWebhooks.Default.async_secretN)r   r   r   r   r	   r*   r,   r   r   r   r   r    >   s    r    Nrequestzhttpx.Requestr*   	tolerancec                 C      d S Nr   )r-   r*   r.   r   r   r   validate[   s   zWebhooks.validateheadersbodyc                 C   r/   r0   r   )r2   r3   r*   r.   r   r   r   r1   c   s   c                 C   s  |st d| rt||grt d| r0| jr0| jd}| jd}| jd}| jd}n|s6tddd	 | D }|d}|d}|d}|sTtd
|sZtd|s`td|sftd|durddl	}t
|	 }	t
|}
t|	|
 }||krtd| d| d| d| }|jd}t|dkrtd|j t|d }t|| t}| }d}| D ]%}|d}t|dk rtd| t|d }t||rd} nq|stddS )a  
        Validate the signature from an incoming webhook request using the provided secret.

        Args:
            request (httpx.Request): The request object.
            headers (Dict[str, str]): The request headers.
            body (str): The request body.
            secret (WebhookSigningSecret): The webhook signing secret.
            tolerance (Optional[int]): Maximum allowed time difference (in seconds) between the current time and the webhook timestamp.

        Returns:
            None: If the request is valid.

        Raises:
            MissingWebhookHeaderError: If required webhook headers are missing.
            InvalidSecretKeyError: If the secret key format is invalid.
            MissingWebhookBodyError: If the webhook body is missing.
            InvalidTimestampError: If the webhook timestamp is invalid or outside the tolerance.
            InvalidSignatureError: If the webhook signature is invalid.
        zMissing webhook signing secretz3Only one of request or headers/body can be providedz
webhook-idzwebhook-timestampzwebhook-signaturezutf-8zMissing webhook headersc                 S   s   i | ]	\}}|  |qS r   )lower).0kvr   r   r   
<dictcomp>   s    z%Webhooks.validate.<locals>.<dictcomp>zMissing webhook idzMissing webhook timestampzMissing webhook signaturezMissing webhook bodyNr   z6Webhook timestamp is outside the allowed tolerance of z seconds._   zInvalid secret key format:    F,zInvalid signature format: TzWebhook signature is invalid)
ValueErroranyr2   getcontentdecoder   itemsr   timeintabsr   r
   splitlenr   base64	b64decodehmacnewencoder   digestr   compare_digest)r-   r2   r3   r*   r.   
webhook_id	timestamp	signaturerD   current_timewebhook_timetime_differencesigned_content	key_partssecret_byteshcomputed_signaturevalidsig	sig_parts	sig_bytesr   r   r   r1   l   sh   





)r   r   r0   )NNNNN)r   r   r   r   propertyr#   r   r    r   staticmethodr	   r   rE   boolr1   r   r   r   r   r   r   r   1   sd    
r   )rI   rK   hashlibr   typingr   r   r   r   replicate.resourcer   r   httpxr	   r>   r   r   r   r   r   r   r   r   r   r   r   <module>   s    