
    =h                     \    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y)	    N)sha1sha256)urlparseparse_qsc                 r    t        |       t        |      k7  ry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        h/var/www/html/phonemate/phone_mate_backend/venv/lib/python3.12/site-packages/twilio/request_validator.pycomparer      sF     7|s7|#Fgw'B"( ( M    c                     | j                   s| j                         S | j                  j                  d      d   }| j	                  |      }|j                         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      sM     88zz|!!#&q)Jll*l-G>>r   c                     | j                   r| j                         S | j                  dk(  rdnd}| j                  dz   t	        |      z   }| j                  |      }|j                         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#   +   s]     xxzz|**'3RDc!CI-Jll*l-G>>r   c                   *    e Zd Zd Zd Zd Zd Zd Zy)RequestValidatorc                 0    |j                  d      | _        y Nutf-8)encodetoken)selfr*   s     r   __init__zRequestValidator.__init__>   s    \\'*
r   c                    |}|rLt        t        |            D ]5  }| j                  ||      }t        t        |            D ]
  }|||z   z  } 7 t        j                  | j
                  |j                  d      t              }t        j                  |j                               }|j                  d      }|j                         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
<#CK0Ee++A 1 2 hhtzz188G#4d;##CJJL1??7+~~r   c                     	 |j                  |      S # t        $ r, 	 |j                  |      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   sZ    		0$$Z00 	00!))*55! 0":.//0		0s(    	A0AAAAAc                 r    t        |j                  d            j                         }|j                         S r'   )r   r)   	hexdigestr7   )r+   bodyr>   s      r   compute_hashzRequestValidator.compute_hashd   s+    $++g./99;~~r   c                 l   |i }t        |      }t        |      }t        |      }d}t        |j                        }d|v r3t        |t              r#t        | j                  |      |d   d         }i }t        | j                  ||      |      }	t        | j                  ||      |      }
|xr |	xs |
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
        T
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))*5 Z%<%d&7&7&?|ATUVAWXOF """#3V<i
 %,""=&99%
! QO$P7PQr   N)__name__
__module____qualname__r,   r?   r0   rH   rT    r   r   r%   r%   =   s    + .
0 
 Rr   r%   )r3   r1   hashlibr   r   urllib.parser   r   r   r   r#   objectr%   rX   r   r   <module>r\      s0        +$"$LRv LRr   