U
    k7g                     @   s   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ edd	d
ddddgZG dd de
ZdS )    )
namedtuple)RequestSession)TwilioRestException)urlparse)
HttpClient)Response)ClientValidationJwtValidationPayloadmethodpathquery_stringall_headerssigned_headersbodyc                   @   s@   e Zd ZddgZdddZddd	Zd
d Zdd Zdd ZdS )ValidationClientauthorizationhostTc                 C   s,   || _ || _|| _|| _|r"t nd| _dS )a+  
        Build a ValidationClient which signs requests with private_key and allows Twilio to
        validate request has not been tampered with.

        :param str account_sid: A Twilio Account Sid starting with 'AC'
        :param str api_key_sid: A Twilio API Key Sid starting with 'SK'
        :param str credential_sid: A Credential Sid starting with 'CR',
                                   corresponds to public key Twilio will use to verify the JWT.
        :param str private_key: The private key used to sign the Client Validation JWT.
        N)account_sidcredential_sidapi_key_sidprivate_keyr   session)selfr   r   r   r   pool_connections r   A/tmp/pip-unpacked-wheel-qtbjxrb6/twilio/http/validation_client.py__init__   s
    zValidationClient.__init__NFc	                 C   s   | j p
t }	t| |||||d}
|	|
}d|jkrRd|jkrR| ||jd< | |}t| j	| j
| j| j|}| |jd< |	j|||d}tt|j|jS )a  
        Make a signed HTTP Request

        :param str method: The HTTP method to use
        :param str url: The URL to request
        :param dict params: Query parameters to append to the URL
        :param dict data: Parameters to go in the body of the HTTP request
        :param dict headers: HTTP Headers to send with the request
        :param tuple auth: Basic Auth arguments
        :param float timeout: Socket/Read timeout for the request
        :param boolean allow_redirects: Whether or not to allow redirects
        See the requests documentation for explanation of all these parameters

        :return: An http response
        :rtype: A :class:`Response <twilio.rest.http.response.Response>` object
        )paramsdataheadersauthHostr   zTwilio-Client-Validation)allow_redirectstimeout)r   r   r   upperprepare_requestr    	_get_host_build_validation_payloadr	   r   r   r   r   Zto_jwtsendr   intstatus_codetext)r   r   urlr   r   r    r!   r$   r#   r   requestprepared_requestZvalidation_payloadZjwtresponser   r   r   r.   -   s<         

zValidationClient.requestc                 C   s:   t |j}|j}|jpd}t|j|||jtj|j	p4ddS )z
        Extract relevant information from request to build a ClientValidationJWT
        :param PreparedRequest request: request we will extract information from.
        :return: ValidationPayload
         )r   r   r   r   r   r   )
r   r-   r   queryr
   r   r    r   !_ValidationClient__SIGNED_HEADERSr   )r   r.   parsedr   r   r   r   r   r(   f   s    

z*ValidationClient._build_validation_payloadc                 C   s   t |j}t|jS )z Pull the Host out of the request)r   r-   strnetloc)r   r.   r4   r   r   r   r'   y   s    
zValidationClient._get_hostc                 C   s2   | dd}|jdk s |jdkr.t|jdddS )z
        Validate that a request to the new SSL certificate is successful
        :return: null on success, raise TwilioRestException if the request fails
        GETzhttps://tls-test.twilio.com:443   i,  z"Failed to validate SSL certificateN)r.   r+   r   )r   clientr0   r   r   r   validate_ssl_certificate~   s    z)ValidationClient.validate_ssl_certificate)T)NNNNNF)	__name__
__module____qualname__r3   r   r.   r(   r'   r:   r   r   r   r   r      s    
      
9r   N)collectionsr   requestsr   r   Ztwilio.base.exceptionsr   urllib.parser   Ztwilio.httpr   Ztwilio.http.responser   Ztwilio.jwt.validationr	   r
   r   r   r   r   r   <module>   s   