o
    NDi)                     @   sR  d dl Z d dlZzd dlZdZW n ey   dZY nw z
d dlmZ dZW n ey1   dZY nw zd dlmZ W n eyE   dZY nw ee	Z
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G dd deZG dd deZe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G dd deZdS )    NTF)
SASLClientProxyExecutec                   @   s   e Zd ZdZdd ZdS )AuthProviderz
    An abstract class that defines the interface that will be used for
    creating :class:`~.Authenticator` instances when opening new
    connections to Cassandra.

    .. versionadded:: 2.0.0
    c                 C      t  )z
        Implementations of this class should return a new instance
        of :class:`~.Authenticator` or one of its subclasses.
        NotImplementedErrorselfhost r   G/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/auth.pynew_authenticator2   s   zAuthProvider.new_authenticatorN)__name__
__module____qualname____doc__r   r   r   r   r   r   )   s    r   c                   @   s.   e Zd ZdZdZ	 dd Zdd Zdd ZdS )	Authenticatora8  
    An abstract class that handles SASL authentication with Cassandra servers.

    Each time a new connection is created and the server requires authentication,
    a new instance of this class will be created by the corresponding
    :class:`~.AuthProvider` to handler that authentication. The lifecycle of the
    new :class:`~.Authenticator` will the be:

    1) The :meth:`~.initial_response()` method will be called. The return
    value will be sent to the server to initiate the handshake.

    2) The server will respond to each client response by either issuing a
    challenge or indicating that the authentication is complete (successful or not).
    If a new challenge is issued, :meth:`~.evaluate_challenge()`
    will be called to produce a response that will be sent to the
    server. This challenge/response negotiation will continue until the server
    responds that authentication is successful (or an :exc:`~.AuthenticationFailed`
    is raised).

    3) When the server indicates that authentication is successful,
    :meth:`~.on_authentication_success` will be called a token string that
    that the server may optionally have sent.

    The exact nature of the negotiation between the client and server is specific
    to the authentication mechanism configured server-side.

    .. versionadded:: 2.0.0
    Nc                 C      dS )z
        Returns an message to send to the server to initiate the SASL handshake.
        :const:`None` may be returned to send an empty message.
        Nr   r	   r   r   r   initial_response[   s   zAuthenticator.initial_responsec                 C   r   )z
        Called when the server sends a challenge message.  Generally, this method
        should return :const:`None` when authentication is complete from a
        client perspective.  Otherwise, a string should be returned.
        r   r	   	challenger   r   r   evaluate_challengeb   s   z Authenticator.evaluate_challengec                 C   r   )z
        Called when the server indicates that authentication was successful.
        Depending on the authentication mechanism, `token` may be :const:`None`
        or a string.
        Nr   )r	   tokenr   r   r   on_authentication_successj   s   z'Authenticator.on_authentication_success)r   r   r   r   server_authenticator_classr   r   r   r   r   r   r   r   :   s    r   c                   @       e Zd ZdZdd Zdd ZdS )PlainTextAuthProvidera  
    An :class:`~.AuthProvider` that works with Cassandra's PasswordAuthenticator.

    Example usage::

        from cassandra.cluster import Cluster
        from cassandra.auth import PlainTextAuthProvider

        auth_provider = PlainTextAuthProvider(
                username='cassandra', password='cassandra')
        cluster = Cluster(auth_provider=auth_provider)

    .. versionadded:: 2.0.0
    c                 C      || _ || _d S Nusernamepasswordr	   r!   r"   r   r   r   __init__      
zPlainTextAuthProvider.__init__c                 C   s   t | j| jS r   )PlainTextAuthenticatorr!   r"   r   r   r   r   r      s   z'PlainTextAuthProvider.new_authenticatorNr   r   r   r   r$   r   r   r   r   r   r   s   s    r   c                   @   r   )%TransitionalModePlainTextAuthProvidera@  
    An :class:`~.AuthProvider` that works with DSE TransitionalModePlainTextAuthenticator.

    Example usage::

        from cassandra.cluster import Cluster
        from cassandra.auth import TransitionalModePlainTextAuthProvider

        auth_provider = TransitionalModePlainTextAuthProvider()
        cluster = Cluster(auth_provider=auth_provider)

    .. warning:: TransitionalModePlainTextAuthProvider will be removed in cassandra-driver
                 4.0. The transitional mode will be handled internally without the need
                 of any auth provider.
    c                 C   s   t d d S )NzTransitionalModePlainTextAuthProvider will be removed in cassandra-driver 4.0. The transitional mode will be handled internally without the need of any auth provider.)logwarningr   r   r   r   r$      s   z.TransitionalModePlainTextAuthProvider.__init__c                 C   s   t  S r   )&TransitionalModePlainTextAuthenticatorr   r   r   r   r      s   z7TransitionalModePlainTextAuthProvider.new_authenticatorNr'   r   r   r   r   r(      s    r(   c                   @   r   )SaslAuthProvidera  
    An :class:`~.AuthProvider` supporting general SASL auth mechanisms

    Suitable for GSSAPI or other SASL mechanisms

    Example usage::

        from cassandra.cluster import Cluster
        from cassandra.auth import SaslAuthProvider

        sasl_kwargs = {'service': 'something',
                       'mechanism': 'GSSAPI',
                       'qops': 'auth'.split(',')}
        auth_provider = SaslAuthProvider(**sasl_kwargs)
        cluster = Cluster(auth_provider=auth_provider)

    .. versionadded:: 2.1.4
    c                 K   s*   t d u rtdd|v rtd|| _d S )N+The puresasl library has not been installedr
   zTkwargs should not contain 'host' since it is passed dynamically to new_authenticator)r   ImportError
ValueErrorsasl_kwargs)r	   r0   r   r   r   r$      s
   
zSaslAuthProvider.__init__c                 C   s   t |fi | jS r   )SaslAuthenticatorr0   r   r   r   r   r      s   z"SaslAuthProvider.new_authenticatorNr'   r   r   r   r   r,      s    r,   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )r1   z
    A pass-through :class:`~.Authenticator` using the third party package
    'pure-sasl' for authentication

    .. versionadded:: 2.1.4
    GSSAPIc                 K   s*   t d u rtdt |||fi || _d S )Nr-   )r   r.   sasl)r	   r
   service	mechanismr0   r   r   r   r$      s   zSaslAuthenticator.__init__c                 C   s
   | j  S r   r3   processr   r   r   r   r      s   
z"SaslAuthenticator.initial_responsec                 C   s   | j |S r   r6   r   r   r   r   r      s   z$SaslAuthenticator.evaluate_challengeN)r2   )r   r   r   r   r$   r   r   r   r   r   r   r1      s
    
r1   c                   @   s"   e Zd ZdZd
ddZdd Zd	S )DSEGSSAPIAuthProviderz
    Auth provider for GSS API authentication. Works with legacy `KerberosAuthenticator`
    or `DseAuthenticator` if `kerberos` scheme is enabled.
    dseauthTc                 K   s4   t stdtstd|| _|| _|| _|| _dS )a  
        :param service: name of the service
        :param qops: iterable of "Quality of Protection" allowed; see ``puresasl.QOP``
        :param resolve_host_name: boolean flag indicating whether the authenticator should reverse-lookup an FQDN when
            creating a new authenticator. Default is ``True``, which will resolve, or return the numeric address if there is no PTR
            record. Setting ``False`` creates the authenticator with the numeric address known by Cassandra
        :param properties: additional keyword properties to pass for the ``puresasl.mechanisms.GSSAPIMechanism`` class.
            Presently, 'principal' (user) is the only one referenced in the ``pure-sasl`` implementation
        r-   z+The kerberos library has not been installedN)_have_puresaslr.   _have_kerberosr4   qopsresolve_host_name
properties)r	   r4   r>   r?   r@   r   r   r   r$      s   

zDSEGSSAPIAuthProvider.__init__c                 C   s.   | j rt|dfdd }t|| j| j| jS )Nr   )r?   socketgetnameinfoGSSAPIAuthenticatorr4   r>   r@   r   r   r   r   r      s   z'DSEGSSAPIAuthProvider.new_authenticatorN)r9   r:   Tr'   r   r   r   r   r8      s    
r8   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )BaseDSEAuthenticatorc                 C      t d)Nzget_mechanism not implementedr   r   r   r   r   get_mechanism      z"BaseDSEAuthenticator.get_mechanismc                 C   rE   )Nz%get_initial_challenge not implementedr   r   r   r   r   get_initial_challenge   rG   z*BaseDSEAuthenticator.get_initial_challengec                 C   s    | j dkr	|  S | |  S )Nz0com.datastax.bdp.cassandra.auth.DseAuthenticator)r   rF   r   rH   r   r   r   r   r     s   
z%BaseDSEAuthenticator.initial_responseN)r   r   r   rF   rH   r   r   r   r   r   rD      s    rD   c                   @   ,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r&   c                 C   r   r   r    r#   r   r   r   r$   
  r%   zPlainTextAuthenticator.__init__c                 C   r   )Ns   PLAINr   r   r   r   r   rF        z$PlainTextAuthenticator.get_mechanismc                 C   r   )N   PLAIN-STARTr   r   r   r   r   rH     rJ   z,PlainTextAuthenticator.get_initial_challengec                 C   s(   |dkrd| j | jf }| S td)NrK   z %s %sz6Did not receive a valid challenge response from server)r!   r"   encode	Exception)r	   r   datar   r   r   r     s   z)PlainTextAuthenticator.evaluate_challengeNr   r   r   r$   rF   rH   r   r   r   r   r   r&     s
    r&   c                       s    e Zd ZdZ fddZ  ZS )r+   zc
     Authenticator that accounts for DSE authentication is configured with transitional mode.
    c                    s   t t| dd d S )N )superr+   r$   r   	__class__r   r   r$      s   z/TransitionalModePlainTextAuthenticator.__init__)r   r   r   r   r$   __classcell__r   r   rR   r   r+     s    r+   c                   @   rI   )
rC   c                 C   s&   |pi }t ||dfd|i|| _d S )Nr2   r>   )r   r3   )r	   r
   r4   r>   r@   r   r   r   r$   %  s   zGSSAPIAuthenticator.__init__c                 C   r   )Ns   GSSAPIr   r   r   r   r   rF   )  rJ   z!GSSAPIAuthenticator.get_mechanismc                 C   r   Ns   GSSAPI-STARTr   r   r   r   r   rH   ,  rJ   z)GSSAPIAuthenticator.get_initial_challengec                 C   s   |dkr	| j  S | j |S rU   r6   r   r   r   r   r   /  s   
z&GSSAPIAuthenticator.evaluate_challengeNrO   r   r   r   r   rC   $  s
    rC   )rA   loggingkerberosr=   r.   puresasl.clientr   r<   	getLoggerr   r)   _proxy_execute_keyobjectr   r   r   r(   r,   r1   DSEPlainTextAuthProviderr8   rD   r&   r+   rC   r   r   r   r   <module>   sB   
9	