o
    O`                     @   sp   d dl Z d dlZd dlmZmZ d dlmZmZ d dlm	Z	m
Z
mZ dd Zdd Zd	d
 ZG dd deZdS )    N)sha1sha256)PY3string_types)izipurlparseparse_qsc                 C   s<   t | t |kr
dS d}t| |D ]
\}}|||kM }q|S )zCompare two strings while protecting against timing attacks

    :param str string1: the first string
    :param str string2: the second string

    :returns: True if the strings are equal, False if not
    :rtype: :obj:`bool`
    FT)lenr   )string1string2resultc1c2 r   \/var/www/html/asistente-voz-ia/venv/lib/python3.10/site-packages/twilio/request_validator.pycompare
   s   	r   c                 C   s2   | j s|  S | jdd }| j|d}| S )zRemove the port number from a URI

    :param uri: parsed URI that Twilio requested on your server

    :returns: full URI without a port number
    :rtype: str
    :r   netloc)portgeturlr   split_replace)uri
new_netlocnew_urir   r   r   remove_port   s
   r   c                 C   sF   | j r|  S | jdkrdnd}| jd t| }| j|d}| S )zAdd the port number to a URI

    :param uri: parsed URI that Twilio requested on your server

    :returns: full URI with a port number
    :rtype: str
    httpsi  P   r   r   )r   r   schemer   strr   )r   r   r   r   r   r   r   add_port-   s   r!   c                   @   s0   e Zd Zdd ZefddZdd Zdd Zd	S )
RequestValidatorc                 C   s   | d| _d S Nutf-8)encodetoken)selfr&   r   r   r   __init__A   s   zRequestValidator.__init__c           	      C   sd   |}|rt | D ]
\}}||| 7 }q
t| j|dt}t|	 }|r.|
d}| S )a/  Compute the signature for a given request

        :param uri: full URI that Twilio requested on your server
        :param params: post vars that Twilio sent with the request
        :param utf: whether return should be bytestring or unicode (python3)

        :returns: The computed signature
        r$   )sorteditemshmacnewr&   r%   r   base64	b64encodedigestdecodestrip)	r'   r   paramsutfskvmaccomputedr   r   r   compute_signatureD   s   	
z"RequestValidator.compute_signaturec                 C   s   t |d }| S r#   )r   r%   	hexdigestr1   )r'   bodyr8   r   r   r   compute_hashZ   s   zRequestValidator.compute_hashc                 C   s   |du ri }t |}t|}t|}d}d}d}	t|j}
d|
v r4t|tr4t| ||
d d }	i }t| 	|||}t| 	|||}|	oK|pK|S )a]  Validate a request from Twilio

        :param uri: full URI that Twilio requested on your server
        :param params: dictionary of POST variables or string of POST body for JSON requests
        :param signature: expected signature in HTTP X-Twilio-Signature header

        :returns: True if the request passes validation, False if not
        NFT
bodySHA256r   )
r   r!   r   r   query
isinstancer   r   r<   r9   )r'   r   r2   	signature
parsed_uriuri_with_porturi_without_portvalid_signaturevalid_signature_with_portvalid_body_hashr>   r   r   r   validate_   s   	
zRequestValidator.validateN)__name__
__module____qualname__r(   r   r9   r<   rG   r   r   r   r   r"   ?   s
    r"   )r-   r+   hashlibr   r   sixr   r   twilio.compatr   r   r   r   r   r!   objectr"   r   r   r   r   <module>   s    