o
    tBh[                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZ zd dlZW n	 ey5   Y nw edZG dd deZG dd	 d	eZG d
d deZG dd deZdS )    N)ABCabstractmethod)	b64encode)AnyCallableDictOptionalzgql.transport.appsyncc                
   @   sX   e Zd ZdZdedefddZe	ddee deeee	f  deee	f fd	d
Z
dS )AppSyncAuthenticationzAWS authentication abstract base class

    All AWS authentication class should have a
    :meth:`get_headers <gql.transport.appsync.AppSyncAuthentication.get_headers>`
    method which defines the headers used in the authentication process.urlreturnc                 C   sF   |   }ttj|dd  }|dddd}| d| dS )	z
        :return: a url with base64 encoded headers used to establish
                 a websocket connection to the appsync-realtime-api.
        ),:)
separatorshttps://zwss://appsync-apiappsync-realtime-apiz?header=z&payload=e30=)get_headersr   jsondumpsencodedecodereplace)selfr
   headersencoded_headersurl_base r   q/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/gql/transport/appsync_auth.pyget_auth_url   s   z"AppSyncAuthentication.get_auth_urlNdatar   c                 C   s   t  )N)NotImplementedErrorr   r   r   r   r   r   r   )   s   z!AppSyncAuthentication.get_headersNN)__name__
__module____qualname____doc__strr   r   r   r   r   r   r   r   r   r   r	      s    
r	   c                	   @   X   e Zd ZdZdededdfddZ	ddee d	eeeef  deeef fd
dZ	dS )AppSyncApiKeyAuthenticationz)AWS authentication class using an API keyhostapi_keyr   Nc                 C      | dd| _|| _dS )z
        :param host: the host, something like:
                     XXXXXXXXXXXXXXXXXXXXXXXXXX.appsync-api.REGION.amazonaws.com
        :param api_key: the API key
        r   r   N)r   _hostr+   )r   r*   r+   r   r   r   __init__3      
z$AppSyncApiKeyAuthentication.__init__r   r   c                 C      | j | jdS )N)r*   z	x-api-key)r-   r+   r!   r   r   r   r   <      z'AppSyncApiKeyAuthentication.get_headersr"   
r#   r$   r%   r&   r'   r.   r   r   r   r   r   r   r   r   r)   0   s    

r)   c                	   @   r(   )AppSyncJWTAuthenticationzAWS authentication class using a JWT access token.

    It can be used either for:
     - Amazon Cognito user pools
     - OpenID Connect (OIDC)
    r*   jwtr   Nc                 C   r,   )z
        :param host: the host, something like:
                     XXXXXXXXXXXXXXXXXXXXXXXXXX.appsync-api.REGION.amazonaws.com
        :param jwt: the JWT Access Token
        r   r   N)r   r-   r4   )r   r*   r4   r   r   r   r.   J   r/   z!AppSyncJWTAuthentication.__init__r   r   c                 C   r0   )N)r*   Authorization)r-   r4   r!   r   r   r   r   S   r1   z$AppSyncJWTAuthentication.get_headersr"   r2   r   r   r   r   r3   B   s    

r3   c                   @   s   e Zd ZdZ					ddedee ded deeeeef gdf  d	ed
 ded ddfddZ	dd Z
	ddee deeeef  deeef fddZdS )AppSyncIAMAuthenticationa  AWS authentication class using IAM.

    .. note::
        There is no need for you to use this class directly, you could instead
        intantiate the :class:`gql.transport.appsync.AppSyncWebsocketsTransport`
        without an auth argument.

    During initialization, this class will use botocore to attempt to
    find your IAM credentials, either from environment variables or
    from your AWS credentials file.
    Nr*   region_namesignerzbotocore.auth.BaseSignerrequest_creatorzbotocore.awsrequest.AWSRequestcredentialsz botocore.credentials.Credentialssessionzbotocore.session.Sessionr   c           
      C   s   ddl m} ddlm} ddlm}	 |dd| _|r|n|	 | _|r%|n| j	 | _
d| _|p3|  | _|r9|n|| j
| j| j| _|rJ|| _dS || _dS )	zInitialize itself, saving the found credentials used
        to sign the headers later.

        if no credentials are found, then a NoCredentialsError is raised.
        r   )	SigV4Auth)create_request_object)get_sessionr   r   appsyncN)botocore.authr<   botocore.awsrequestr=   botocore.sessionr>   r   r-   _sessionget_credentials_credentials_service_name_detect_region_name_region_name_signer_request_creator)
r   r*   r7   r8   r9   r:   r;   r<   r=   r>   r   r   r   r.   f   s"   z!AppSyncIAMAuthentication.__init__c                 C   st   ddl m} td| j}|r| d }td|  ntd | j	d| j
 }|du r8td ||S )a_  Try to detect the correct region_name.

        First try to extract the region_name from the host.

        If that does not work, then try to get the region_name from
        the aws configuration (~/.aws/config file) or the AWS_DEFAULT_REGION
        environment variable.

        If no region_name was found, then raise a NoRegionError exception.r   )NoRegionErrorz8appsync-api\.((?![0-9]+$)(?!-)[a-zA-Z0-9-]{,63}(?<!-))\.z!Region name extracted from host: z9Region name not found in host, trying default region nameNz}Region name not found. It was not possible to detect your region either from the host or from your default AWS configuration.)botocore.exceptionsrK   researchr-   groupslogdebugrC   _resolve_region_nameget_default_client_configwarning)r   rK   mr7   r   r   r   rG      s"   

z,AppSyncIAMAuthentication._detect_region_namer   r   c                 C   s   ddl m} |pdddd}| dd| j d	|rd
nd |i |p"dd}z| j| W n |y;   td  w t|j	}| j|d< t
tjrtg }|d | D ]\}}|d| d|  qW|d td| |S )Nr   )NoCredentialsErrorz!application/json, text/javascriptzamz-1.0zapplication/json; charset=UTF-8)acceptzcontent-encodingzcontent-typePOSTr   z/graphql z/connectz{})methodr
   r   contextbodyzWCredentials not found for the IAM auth. Do you have default AWS credentials configured?r*   z

Signed headers:z    z: 
)rL   rV   rJ   r-   rI   add_authrP   rT   dictr   isEnabledForloggingDEBUGappenditemsrQ   join)r   r   r   rV   requestheaders_logkeyvaluer   r   r   r      s@   




z$AppSyncIAMAuthentication.get_headers)NNNNNr"   )r#   r$   r%   r&   r'   r   r   r   r   r.   rG   r   r   r   r   r   r6   Y   sB    	

%'
r6   )r   ra   rM   abcr   r   base64r   typingr   r   r   r   botocoreImportError	getLoggerrP   r	   r)   r3   r6   r   r   r   r   <module>   s     
