o
    `šjQ  ã                   @   s^   d dl mZmZ d dlZddl mZ d dlmZ e e¡Z	G dd„ deƒZ
G dd	„ d	eƒZdS )
é    )ÚABCÚabstractmethodNé   )ÚSaslMechanism)ÚKafkaConfigurationErrorc                   @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚSaslMechanismOAuthc                 K   sF   d|vrt dƒ‚t|d tƒst dƒ‚|d | _d | _d| _d| _d S )NÚsasl_oauth_token_providerz7sasl_oauth_token_provider required for OAUTHBEARER saslzSsasl_oauth_token_provider must implement kafka.net.sasl.oauth.AbstractTokenProviderF)r   Ú
isinstanceÚAbstractTokenProviderÚtoken_providerÚ_errorÚ_is_doneÚ_is_authenticated©ÚselfÚconfig© r   úR/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/net/sasl/oauth.pyÚ__init__   s   

zSaslMechanismOAuth.__init__c                 C   s0   | j r| j S | j ¡ }|  ¡ }d ||¡ d¡S )Nzn,,auth=Bearer {}{}úutf-8)r   r   ÚtokenÚ_token_extensionsÚformatÚencode)r   r   Ú
extensionsr   r   r   Ú
auth_bytes   s
   
zSaslMechanismOAuth.auth_bytesc                 C   s8   |dkr|  d¡}t d|¡ d| _d S d| _d| _d S )Nó    r   zCSending x01 response to server after receiving SASL OAuth error: %só   T)ÚdecodeÚlogÚdebugr   r   r   )r   r   Úerrorr   r   r   Úreceive   s   


zSaslMechanismOAuth.receivec                 C   ó   | j S ©N)r   ©r   r   r   r   Úis_done(   ó   zSaslMechanismOAuth.is_donec                 C   r#   r$   )r   r%   r   r   r   Úis_authenticated+   r'   z#SaslMechanismOAuth.is_authenticatedc                 C   s2   | j  ¡ }d dd„ | ¡ D ƒ¡}|rd| S dS )zŽ
        Return a string representation of the OPTIONAL key-value pairs that can be sent with an OAUTHBEARER
        initial request.
        úc                 S   s   g | ]
\}}d   ||¡‘qS )z{}={})r   )Ú.0ÚkÚvr   r   r   Ú
<listcomp>5   s    z8SaslMechanismOAuth._token_extensions.<locals>.<listcomp>Ú )r   r   ÚjoinÚitems)r   r   Úmsgr   r   r   r   .   s   
z$SaslMechanismOAuth._token_extensionsc                 C   s   | j stdƒ‚dS )NzNot authenticated yet!zAuthenticated via SASL / OAuth)r(   ÚRuntimeErrorr%   r   r   r   Úauth_details8   s   zSaslMechanismOAuth.auth_detailsN)
Ú__name__Ú
__module__Ú__qualname__r   r   r"   r&   r(   r   r3   r   r   r   r   r      s    
	
r   c                   @   s,   e Zd ZdZdd„ Zedd„ ƒZdd„ ZdS )	r
   a  
    A Token Provider must be used for the SASL OAuthBearer protocol.

    The implementation should ensure token reuse so that multiple
    calls at connect time do not create multiple tokens. The implementation
    should also periodically refresh the token in order to guarantee
    that each call returns an unexpired token. A timeout error should
    be returned after a short period of inactivity so that the
    broker can log debugging info and retry.

    Token Providers MUST implement the token() method
    c                 K   s   d S r$   r   r   r   r   r   r   L   s   zAbstractTokenProvider.__init__c                 C   s   dS )zY
        Returns a (str) ID/Access Token to be sent to the Kafka
        client.
        Nr   r%   r   r   r   r   O   s   zAbstractTokenProvider.tokenc                 C   s   i S )aa  
        This is an OPTIONAL method that may be implemented.

        Returns a map of key-value pairs that can
        be sent with the SASL/OAUTHBEARER initial client request. If
        not implemented, the values are ignored. This feature is only available
        in Kafka >= 2.1.0.

        All returned keys and values should be type str
        r   r%   r   r   r   r   W   s   z AbstractTokenProvider.extensionsN)r4   r5   r6   Ú__doc__r   r   r   r   r   r   r   r   r
   >   s    
r
   )Úabcr   r   Úloggingr   Úkafka.errorsr   Ú	getLoggerr4   r   r   r
   r   r   r   r   Ú<module>   s    
3