o
    Uh!                     @   s   d dl mZ d dlmZ d dlZd dlmZ defddZdefdd	Z	d
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ZeZdS )    )hexlify)EnumN)Optionalreturnc                  C   s   t tt d} d|  S )zPrepare an AUTH command line with the current effective user ID.

    This is the preferred authentication method for typical D-Bus connections
    over a Unix domain socket.
    asciis   AUTH EXTERNAL %b
)r   strosgeteuidencode)hex_uid r   `/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/jeepney/auth.pymake_auth_external   s   r   c                  C   s&   ddl m}  td|  d}d| S )zFormat an AUTH command line for the ANONYMOUS mechanism

    Jeepney's higher-level wrappers don't currently use this mechanism,
    but third-party code may choose to.

    See <https://tools.ietf.org/html/rfc4505> for details.
       )__version__z
Jeepney %sr   s   AUTH ANONYMOUS %s
) r   r   r
   )r   tracer   r   r   make_auth_anonymous   s   r   s   BEGIN
s   NEGOTIATE_UNIX_FD
c                   @   s    e Zd ZdZdZdZdZdZdS )ClientStater               N)__name__
__module____qualname__WaitingForDataWaitingForOkWaitingForRejectWaitingForAgreeUnixFDSuccessr   r   r   r   r      s    r   c                   @   s"   e Zd ZdZdddZdd ZdS )	AuthenticationErrorz%Raised when DBus authentication failsAuthentication failedc                 C   s   || _ || _d S Nmsgdata)selfr&   r%   r   r   r   __init__(   s   
zAuthenticationError.__init__c                 C   s   | j  d| jS )Nz. Bus sent: r$   r'   r   r   r   __str__,   s   zAuthenticationError.__str__N)r"   )r   r   r   __doc__r(   r*   r   r   r   r   r!   &   s    
r!   c                       s    e Zd ZdZ fddZ  ZS )FDNegotiationErrorzBRaised when file descriptor support is requested but not availablec                    s   t  j|dd d S )Nz%File descriptor support not available)r%   )superr(   )r'   r&   	__class__r   r   r(   1   s   zFDNegotiationError.__init__)r   r   r   r+   r(   __classcell__r   r   r.   r   r,   /   s    r,   c                   @   sV   e Zd ZdZdddZedd Zdd	 Zd
ee	 fddZ
dd Zde	fddZdS )Authenticatora  Process data for the SASL authentication conversation

    If enable_fds is True, this includes negotiating support for passing
    file descriptors. If inc_null_byte is True, sends the ' ' byte
    at the beginning of the negotiations, which was the past behavior,
    but which prevents sending the SCM_CREDS ancillary data over the socket,
    breaking authentication on *BSD; the caller should rather send that
    null byte and ancillary data and pass inc_null_byte=False to prevent
    it being done here.
    FTc                 C   s:   || _ t | _|rdt  | _nt | _tj| _d | _d S )N    )	
enable_fds	bytearraybufferr   _to_sendr   r   stateerror)r'   r3   inc_null_byter   r   r   r(   @   s   
zAuthenticator.__init__c                 C   s   | j tju S r#   )r7   r   r    r)   r   r   r   authenticatedJ   s   zAuthenticator.authenticatedc                 C   s   t | jd S r#   )iterdata_to_sendr)   r   r   r   __iter__N   s   zAuthenticator.__iter__r   c                 C   s"   | j s| jrdS d| j| _}|S )a  Get a line of data to send to the server

        The data returned should be sent before waiting to receive data.
        Returns empty bytes if waiting for more data from the server, and None
        if authentication is finished (success or error).

        Iterating over the Authenticator object will also yield these lines;
        :meth:`feed` should be called with received data inside the loop.
        N    )r:   r8   r6   )r'   to_sendr   r   r   r<   Q   s   
zAuthenticator.data_to_sendc                 C   sn   | j tju r|dr| jrttjfS ttjfS n| j tju r0|dr)ttjfS || _	t
||| _	t|)Ns   OK s   AGREE_UNIX_FD)r7   r   r   
startswithr3   NEGOTIATE_UNIX_FDr   BEGINr    r8   r,   r!   )r'   liner   r   r   process_line`   s   




zAuthenticator.process_liner&   c                 C   sr   |  j |7  _ d| j v r*| j dd\}| _ | j rt| j d| |\| _| _dS t| j dkr7t| j ddS )zProcess received data

        Raises AuthenticationError if the incoming data is not as expected for
        successful authentication. The connection should then be abandoned.
        s   
r   zUnexpected data receivedi    z*Too much data received without line endingN)r5   splitr!   rD   r6   r7   len)r'   r&   rC   r   r   r   feedv   s   
zAuthenticator.feedN)FT)r   r   r   r+   r(   propertyr:   r=   r   bytesr<   rD   rG   r   r   r   r   r1   5   s    



r1   )binasciir   enumr   r   typingr   rI   r   r   rB   rA   r   
ValueErrorr!   r,   r1   
SASLParserr   r   r   r   <module>   s    		[