
    bf                     d    d dl Z d dlZd dlmZmZ d dlmZmZ d Zd Z	d Z
 G d de          ZdS )	    N)sha1sha256)urlparseparse_qsc                     t          |           t          |          k    rdS d}t          | |          D ]\  }}|||k    z  }|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)lenzip)string1string2resultc1c2s        R/var/www/html/Alfredo/env/lib/python3.11/site-packages/twilio/request_validator.pycomparer      sW     7||s7||##uFgw''  B"(M    c                     | j         s|                                 S | j                            d          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_uris      r   remove_portr      sY     8 zz||!!#&&q)Jll*l--G>>r   c                     | j         r|                                 S | j        dk    rdnd}| j        dz   t	          |          z   }|                     |          }|                                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   s       r   add_portr#   +   sk     x zz||*''33RDc!CII-Jll*l--G>>r   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )RequestValidatorc                 :    |                     d          | _        d S Nutf-8)encodetoken)selfr*   s     r   __init__zRequestValidator.__init__>   s    \\'**


r   c                    |}|r\t          t          |                    D ]?}|                     ||          }t          t          |                    D ]
}|||z   z  }@t          j        | j        |                    d          t                    }t          j	        |
                                          }|                    d          }|                                S )zCompute 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

        :returns: The computed signature
        r(   )sortedset
get_valueshmacnewr*   r)   r   base64	b64encodedigestdecodestrip)	r+   r   paramss
param_namevaluesvaluemaccomputeds	            r   compute_signaturez"RequestValidator.compute_signatureA   s      	,$S[[11 , ,
<<#CKK00 , ,Ee++AA, htz188G#4#4d;;#CJJLL11??7++~~r   c                     	 |                     |          S # t          $ r4 	 |                    |          cY S # t          $ r ||         gcY cY S w xY ww xY w)N)getallAttributeErrorgetlist)r+   
param_dictr:   s      r   r0   zRequestValidator.get_valuesX   s    		0$$Z000 	0 	0 	00!))*55555! 0 0 0":.//////0		0s)    
A9AAAAAc                     t          |                    d                                                    }|                                S r'   )r   r)   	hexdigestr7   )r+   bodyr>   s      r   compute_hashzRequestValidator.compute_hashd   s7    $++g..//99;;~~r   c                    |i }t          |          }t          |          }t          |          }d}t          |j                  }d|v rFt          |t                    r1t          |                     |          |d         d                   }i }t          | 	                    ||          |          }	t          | 	                    ||          |          }
|o|	p|
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
        NT
bodySHA256r   )
r   r#   r   r   query
isinstancer"   r   rH   r?   )r+   r   r8   	signature
parsed_uriuri_with_porturi_without_portvalid_body_hashrK   valid_signaturevalid_signature_with_ports              r   validatezRequestValidator.validatei   s     >Fc]]
 ,,&z22)**5  Z%<%< %d&7&7&?&?|ATUVAWXXOF """#3V<<i
 
 %,""=&999%
 %
! QO$P7PQr   N)__name__
__module____qualname__r,   r?   r0   rH   rT    r   r   r%   r%   =   sd        + + +     .
0 
0 
0     
 R  R  R  R  Rr   r%   )r3   r1   hashlibr   r   urllib.parser   r   r   r   r#   objectr%   rX   r   r   <module>r\      s                      + + + + + + + +  $  "  $LR LR LR LR LRv LR LR LR LR LRr   