o
    Dfb                     @   s   d Z ddlZddlZddlmZ ddlmZ G dd dZG dd deZG d	d
 d
eZ	dd Z
e
 ZG dd deZG dd deZdS )z(SASL mechanisms for AMQP authentication.    N)BytesIO)_write_tablec                   @   s$   e Zd ZdZedd Zdd ZdS )SASLzThe base class for all amqp SASL authentication mechanisms.

    You should sub-class this if you're implementing your own authentication.
    c                 C      t )z2Return a bytes containing the SASL mechanism name.NotImplementedErrorself r
   B/home/ubuntu/webapp/venv/lib/python3.10/site-packages/amqp/sasl.py	mechanism   s   zSASL.mechanismc                 C   r   )z@Return the first response to a SASL challenge as a bytes object.r   r	   
connectionr
   r
   r   start   s   z
SASL.startN)__name__
__module____qualname____doc__propertyr   r   r
   r
   r
   r   r   
   s
    
r   c                   @   (   e Zd ZdZdZdd ZdZdd ZdS )	PLAINzbPLAIN SASL authentication mechanism.

    See https://tools.ietf.org/html/rfc4616 for details
    s   PLAINc                 C      ||| _ | _d S Nusernamepasswordr	   r   r   r
   r
   r   __init__"      zPLAIN.__init__r   c                 C   s^   | j d u s
| jd u rtS t }|d || j d |d || jd | S )N    zutf-8)r   r   NotImplementedr   writeencodegetvaluer	   r   login_responser
   r
   r   r   *   s   

zPLAIN.startNr   r   r   r   r   r   	__slots__r   r
   r
   r
   r   r          r   c                   @   r   )	AMQPLAINzhAMQPLAIN SASL authentication mechanism.

    This is a non-standard mechanism used by AMQP servers.
    s   AMQPLAINc                 C   r   r   r   r   r
   r
   r   r   =   r   zAMQPLAIN.__init__r   c                 C   sF   | j d u s
| jd u rtS t }t| j | jd|jg  | dd  S )N)s   LOGINs   PASSWORD   )r   r   r    r   r   r!   r#   r$   r
   r
   r   r   E   s   zAMQPLAIN.startNr&   r
   r
   r
   r   r)   5   r(   r)   c                     sT   z
dd l  dd l W n ty   G dd dt} |  Y S w G  fdddt}|S )Nr   c                   @   s*   e Zd ZdZdZ		d	ddZdd ZdS )
z)_get_gssapi_mechanism.<locals>.FakeGSSAPIz7A no-op SASL mechanism for when gssapi isn't available.N   amqpFc                 S   s   |st dd S )Nz?You need to install the `gssapi` module for GSSAPI SASL supportr   r	   client_nameservicerdns	fail_softr
   r
   r   r   Y   s
   z2_get_gssapi_mechanism.<locals>.FakeGSSAPI.__init__c                 S   s   t S r   )r    r   r
   r
   r   r   `      z/_get_gssapi_mechanism.<locals>.FakeGSSAPI.startNr+   FFr   r   r   r   r   r   r   r
   r
   r
   r   
FakeGSSAPIT   s    
r4   c                       s:   e Zd ZdZdZ		dddZdZd	d
 Z fddZdS )z%_get_gssapi_mechanism.<locals>.GSSAPIzsGSSAPI SASL authentication mechanism.

            See https://tools.ietf.org/html/rfc4752 for details
            s   GSSAPINr+   Fc                 S   s4   |rt |ts|d}|| _|| _|| _|| _d S )Nascii)
isinstancebytesr"   r-   r0   r.   r/   r,   r
   r
   r   r   l   s   

z._get_gssapi_mechanism.<locals>.GSSAPI.__init__)r-   r0   r.   r/   c                 S   s^   |j j}| jr|jtjtjfv r| }t|d \}}}n|j j	}t
|ts-|d}|S )Nr   r5   )	transportsockr/   familysocketAF_INETAF_INET6getpeernamegethostbyaddrhostr6   r7   r"   )r	   r   r9   peerhostname_r
   r
   r   get_hostname|   s   

z2_get_gssapi_mechanism.<locals>.GSSAPI.get_hostnamec                    s   z0| j r j | j d}nd }| |} d| j|g jj} j||d}|	d W S   j
jjyB   | jrAt Y S  w )N)name   @)rE   creds)r-   CredentialsNamerD   joinr.   NameTypehostbased_serviceSecurityContextsteprawmiscGSSErrorr0   r    )r	   r   rG   rB   rE   contextgssapir
   r   r      s"   

z+_get_gssapi_mechanism.<locals>.GSSAPI.startr2   )	r   r   r   r   r   r   r'   rD   r   r
   rS   r
   r   GSSAPId   s    
	rU   )rT   gssapi.raw.miscImportErrorr   )r4   rU   r
   rS   r   _get_gssapi_mechanismO   s   5rX   c                   @   s   e Zd ZdZdZdd ZdS )EXTERNALzEXTERNAL SASL mechanism.

    Enables external authentication, i.e. not handled through this protocol.
    Only passes 'EXTERNAL' as authentication mechanism, but no further
    authentication data.
    s   EXTERNALc                 C   s   dS )N    r
   r   r
   r
   r   r      r1   zEXTERNAL.startN)r   r   r   r   r   r   r
   r
   r
   r   rY      s    rY   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )RAWzA generic custom SASL mechanism.

    This mechanism takes a mechanism name and response to send to the server,
    so can be used for simple custom authentication schemes.
    Nc                 C   s:   t |tsJ t |tsJ ||| _| _tdt d S )NznPassing login_method and login_response to Connection is deprecated. Please implement a SASL subclass instead.)r6   r7   r   responsewarningswarnDeprecationWarning)r	   r   r\   r
   r
   r   r      s   zRAW.__init__c                 C   s   | j S r   )r\   r   r
   r
   r   r      s   z	RAW.startr3   r
   r
   r
   r   r[      s
    r[   )r   r;   r]   ior   amqp.serializationr   r   r   r)   rX   rU   rY   r[   r
   r
   r
   r   <module>   s    M