o
    •ºé`E  ã                   @   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 )z³Prepare 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   ú./usr/lib/python3/dist-packages/jeepney/auth.pyÚmake_auth_external   s   r   c                  C   s&   ddl m}  td|   d¡ƒ}d| S )zéFormat 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 )ÚAuthenticatorzšProcess data for the SASL authentication conversation

    If enable_fds is True, this includes negotiating support for passing
    file descriptors.
    Fc                 C   s,   || _ tƒ | _dtƒ  | _tj| _d | _d S )Nó    )	Ú
enable_fdsÚ	bytearrayÚbufferr   Ú_to_sendr   r   ÚstateÚerror)r'   r3   r   r   r   r(   ;   s
   
zAuthenticator.__init__c                 C   s   | j tju S r#   )r7   r   r    r)   r   r   r   ÚauthenticatedB   s   zAuthenticator.authenticatedc                 C   s   t | jd ƒS r#   )ÚiterÚdata_to_sendr)   r   r   r   Ú__iter__F   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ó    )r9   r8   r6   )r'   Úto_sendr   r   r   r;   I   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_lineX   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 )zºProcess 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!   rC   r6   r7   Úlen)r'   r&   rB   r   r   r   Úfeedn   s   
ÿÿzAuthenticator.feedN)F)r   r   r   r+   r(   Úpropertyr9   r<   r   Úbytesr;   rC   rF   r   r   r   r   r1   5   s    

r1   )Úbinasciir   Úenumr   r   Útypingr   rH   r   r   rA   r@   r   Ú
ValueErrorr!   r,   r1   Ú
SASLParserr   r   r   r   Ú<module>   s    		S