o
    tBh|&                     @   s   d dl Z d dlZd dlZzd dlmZ d dlmZ W n ey+   d dlmZmZ Y nw d dlZdd Z	dd Z
G d	d
 d
ejjZdS )    N)quote)urlparse)r   r   c                 C   s   t | |dtj S )f
    Copied from https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
    utf-8)hmacnewencodehashlibsha256digest)keymsg r   q/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/aws_requests_auth/aws_auth.pysign   s   r   c                 C   s6   t d|  d|}t ||}t ||}t |d}|S )r   AWS4r   aws4_request)r   r   )r   	dateStamp
regionNameserviceNamekDatekRegionkServicekSigningr   r   r   getSignatureKey   s
   


r   c                   @   sL   e Zd ZdZ	dddZdd Zdd Zd	d
 Zedd Z	edd Z
dS )AWSRequestsAuthz
    Auth class that allows us to connect to AWS services
    via Amazon's signature version 4 signing process

    Adapted from https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
    Nc                 C   s(   || _ || _|| _|| _|| _|| _dS )a  
        Example usage for talking to an AWS Elasticsearch Service:

        AWSRequestsAuth(aws_access_key='YOURKEY',
                        aws_secret_access_key='YOURSECRET',
                        aws_host='search-service-foobar.us-east-1.es.amazonaws.com',
                        aws_region='us-east-1',
                        aws_service='es',
                        aws_token='...')

        The aws_token is optional and is used only if you are using STS
        temporary credentials.
        N)aws_access_keyaws_secret_access_keyaws_host
aws_regionservice	aws_token)selfr   r   r   r   aws_servicer!   r   r   r   __init__*   s   
zAWSRequestsAuth.__init__c                 C   s   |  |}|j| |S )z
        Adds the authorization headers required by Amazon's signature
        version 4 signing process to the request.

        Adapted from https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
        )get_aws_request_headers_handlerheadersupdate)r"   raws_headersr   r   r   __call__E   s   
zAWSRequestsAuth.__call__c                 C   s   | j || j| j| jdS )aN  
        Override get_aws_request_headers_handler() if you have a
        subclass that needs to call get_aws_request_headers() with
        an arbitrary set of AWS credentials. The default implementation
        calls get_aws_request_headers() with self.aws_access_key,
        self.aws_secret_access_key, and self.aws_token
        )r(   r   r   r!   )get_aws_request_headersr   r   r!   )r"   r(   r   r   r   r%   P   s
   z/AWSRequestsAuth.get_aws_request_headers_handlerc              	   C   s  t j  }|d}|d}t|}t|}	d| j d d | d }
|r0|
d| d 7 }
d}|r8|d7 }|jr>|jnt }z|	d	}W n t
tfyU   |}Y nw t| }|jd | d |	 d |
 d | d | }d
}|d | j d | j d d }|d | d | d t|	d	  }t||| j| j}|	d	}t||tj }|d d | d | d d | d d | }|||d}|r||d< |S )a  
        Returns a dictionary containing the necessary headers for Amazon's
        signature version 4 signing process. An example return value might
        look like

            {
                'Authorization': 'AWS4-HMAC-SHA256 Credential=YOURKEY/20160618/us-east-1/es/aws4_request, '
                                 'SignedHeaders=host;x-amz-date, '
                                 'Signature=ca0a856286efce2a4bd96a978ca6c8966057e53184776c0685169d08abd74739',
                'x-amz-date': '20160618T220405Z',
            }
        z%Y%m%dT%H%M%SZz%Y%m%dzhost:
zx-amz-date:zx-amz-security-token:zhost;x-amz-datez;x-amz-security-tokenr   zAWS4-HMAC-SHA256/r    zCredential=z, zSignedHeaders=z
Signature=)Authorizationz
x-amz-datezx-amz-content-sha256zX-Amz-Security-Token)datetimeutcnowstrftimer   get_canonical_pathget_canonical_querystringr   bodybytesr   AttributeErrorUnicodeDecodeErrorr	   r
   	hexdigestmethodr   r    r   r   r   )r"   r(   r   r   r!   tamzdate	datestampcanonical_uricanonical_querystringcanonical_headerssigned_headersr5   payload_hashcanonical_request	algorithmcredential_scopestring_to_signsigning_keystring_to_sign_utf8	signatureauthorization_headerr&   r   r   r   r+   ]   s   




	
z'AWSRequestsAuth.get_aws_request_headersc                 C   s(   t |j}t|jr|jddS dddS )zt
        Create canonical URI--the part of the URI from domain to query
        string (use '/' if no path)
        r-   z/-_.~)safe)r   urlr   path)clsr(   	parsedurlr   r   r   r3      s   
z"AWSRequestsAuth.get_canonical_pathc           	      C   s   d}t |j}dt|jd}|dD ]*}|dd}|d }t|dkr.|d }nd}|rA|r8|d7 }|d||g7 }q|S )af  
        Create the canonical query string. According to AWS, by the
        end of this function our query string values must
        be URL-encoded (space=%20) and the parameters must be sorted
        by name.

        This method assumes that the query params in `r` are *already*
        url encoded.  If they are not url encoded by the time they make
        it to this function, AWS may complain that the signature for your
        request is incorrect.

        It appears elasticsearc-py url encodes query paramaters on its own:
            https://github.com/elastic/elasticsearch-py/blob/5dfd6985e5d32ea353d2b37d01c2521b2089ac2b/elasticsearch/connection/http_requests.py#L64

        If you are using a different client than elasticsearch-py, it
        will be your responsibility to urleconde your query params before
        this method is called.
         &=   r   )r   rL   joinsortedquerysplitlen)	rN   r(   r?   rO   querystring_sortedquery_paramkey_val_splitr   valr   r   r   r4      s   

z)AWSRequestsAuth.get_canonical_querystring)N)__name__
__module____qualname____doc__r$   r*   r%   r+   classmethodr3   r4   r   r   r   r   r   "   s    
`
r   )r   r	   r0   urllibr   r   ImportErrorurllib.parserequestsr   r   authAuthBaser   r   r   r   r   <module>   s    