o
    >h"                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ G dd de	Ze
eG dd	 d	Ze
eG d
d dZe
eG dd dZdS )zF
Protocol agnostic implementations of SASL authentication mechanisms.
    N)md5)	Attribute	Interfaceimplementer)networkStringc                   @   s$   e Zd ZedZdd Zdd ZdS )ISASLMechanismz#Common name for the SASL Mechanism.c                   C      dS )z
        Get the initial client response, if defined for this mechanism.

        @return: initial client response string.
        @rtype: C{str}.
        N r	   r	   r	   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/words/protocols/jabber/sasl_mechanisms.pygetInitialResponse       z!ISASLMechanism.getInitialResponsec                 C   r   )z
        Get the response to a server challenge.

        @param challenge: server challenge.
        @type challenge: C{str}.
        @return: client response.
        @rtype: C{str}.
        Nr	   )	challenger	   r	   r
   getResponse!   r   zISASLMechanism.getResponseN)__name__
__module____qualname__r   namer   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S )	Anonymouszm
    Implements the ANONYMOUS SASL authentication mechanism.

    This mechanism is defined in RFC 2245.
    	ANONYMOUSc                 C      d S Nr	   selfr	   r	   r
   r   6      zAnonymous.getInitialResponsec                 C   r   r   r	   r   r   r	   r	   r
   r   9      zAnonymous.getResponseN)r   r   r   __doc__r   r   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 )
Plainz
    Implements the PLAIN SASL authentication mechanism.

    The PLAIN SASL authentication mechanism is defined in RFC 2595.
    PLAINc                 C   s"   |pd| _ |pd| _|pd| _dS )a  
        @param authzid: The authorization identity.
        @type authzid: L{unicode}

        @param authcid: The authentication identity.
        @type authcid: L{unicode}

        @param password: The plain-text password.
        @type password: L{unicode}
         N)authzidauthcidpassword)r   r    r!   r"   r	   r	   r
   __init__H   s   

zPlain.__init__c                 C   s,   | j dd | jd d | jd S )Nzutf-8    )r    encoder!   r"   r   r	   r	   r
   r   X   s   


zPlain.getInitialResponsec                 C   r   r   r	   r   r	   r	   r
   r   a   r   zPlain.getResponseN)r   r   r   r   r   r#   r   r   r	   r	   r	   r
   r   >   s    	r   c                   @   sT   e Zd Zd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d ZdS )	DigestMD5z
    Implements the DIGEST-MD5 SASL authentication mechanism.

    The DIGEST-MD5 SASL authentication mechanism is defined in RFC 2831.
    z
DIGEST-MD5c                 C   sF   || _ || _|| _| d| | _|dur!|  jd| 7  _dS dS )a  
        @param serv_type: An indication of what kind of server authentication
            is being attempted against.  For example, C{u"xmpp"}.
        @type serv_type: C{unicode}

        @param host: The authentication hostname.  Also known as the realm.
            This is used as a scope to help select the right credentials.
        @type host: C{unicode}

        @param serv_name: An additional identifier for the server.
        @type serv_name: C{unicode}

        @param username: The authentication username to use to respond to a
            challenge.
        @type username: C{unicode}

        @param password: The authentication password to use to respond to a
            challenge.
        @type password: C{unicode}
        /N)usernamer"   defaultRealm
digest_uri)r   	serv_typehost	serv_namer(   r"   r	   r	   r
   r#   p   s   zDigestMD5.__init__c                 C   r   r   r	   r   r	   r	   r
   r      r   zDigestMD5.getInitialResponsec                 C   sb   |  |}d|v rdS |d d}z|d }W n ty'   | j|}Y nw | |||d S )Ns   rspauth       charsetascii   realm   nonce)_parsedecodeKeyErrorr)   r%   _genResponse)r   r   
directivescharsetrealmr	   r	   r
   r      s   
zDigestMD5.getResponsec                 C   s   |}i }d}d}|rl| d|}|||  }|d7 }|||d  dkrE|d7 }| d|}||| }	|d|d }|dkrDd}n!|d|}|dkrZ||d	  }	d}n|||  }	|d }|	||< |s
d
D ]}
|
|v r}||
 d||
< qn|S )z
        Parses the server challenge.

        Splits the challenge into a dictionary of directives with values.

        @return: challenge directives and their values.
        @rtype: C{dict} of C{str} to C{str}.
        r   T   =      "   ,FN)   qop   cipher)indexlstripfindrstripsplit)r   r   s	paramDictcurremainingParamsmiddler   endvalueparamr	   r	   r
   r3      s:   	zDigestMD5._parsec                 C   sL   g }|  D ]\}}|dv r|d | }n|d | }|| qd|S )af  
        Create message string from directives.

        @param directives: dictionary of directives (names to their values).
                           For certain directives, extra quotes are added, as
                           needed.
        @type directives: C{dict} of C{str} to C{str}
        @return: message string.
        @rtype: C{str}.
        )   usernamer1      cnoncer2   
   digest-uris   authzidr@   r:   r=   )itemsappendjoin)r   r7   directive_listr   rL   	directiver	   r	   r
   _unparse   s   	
zDigestMD5._unparsec                    s   dd  dd } fdd}	 |d | d | d | d | }
d| }||	| |
|d | d | d d	 d | | }|S )
z
        Calculates response with given encoded parameters.

        @return: The I{response} field of a response to a Digest-MD5 challenge
            of the given parameters.
        @rtype: L{bytes}
        c                 S   s   t |  S r   )r   digest)rF   r	   r	   r
   H   s   z'DigestMD5._calculateResponse.<locals>.Hc                 S   s
   t | S r   )binasciib2a_hex)nr	   r	   r
   HEX   s   
z)DigestMD5._calculateResponse.<locals>.HEXc                    s    | d | S )N   :r	   )krF   rX   r	   r
   KD   s   z(DigestMD5._calculateResponse.<locals>.KDr]   s   AUTHENTICATE:   authr	   )r   cnoncencnoncer(   r"   r9   urir\   r`   a1a2responser	   r_   r
   _calculateResponse   s   	(
*zDigestMD5._calculateResponsec                 C   s   z| j |}| j|}| j|}W n ty    w tdd}|  }d}	| |||||||}
||||||	||
|dd	}| |S )z
        Generate response-value.

        Creates a response to a challenge according to section 2.1.2.1 of
        RFC 2831 using the C{charset}, C{realm} and C{nonce} directives
        from the challenge.
        r;   08xra   r0   )	rN   r1   r2   rO   s   ncr?   rP   s   responser/   )	r(   r%   r"   r*   UnicodeErrorr   
_gen_nonceri   rV   )r   r8   r9   rd   r(   r"   r*   rc   rb   qoprh   r7   r	   r	   r
   r6   	  s0   
zDigestMD5._genResponsec                 C   s4   dt   t t f }t|}t| dS )Nz%f:%f:%dr0   )randomtimeosgetpidr   r   	hexdigestr%   )r   nonceString
nonceBytesr	   r	   r
   rl   0  s   zDigestMD5._gen_nonceN)r   r   r   r   r   r#   r   r   r3   rV   ri   r6   rl   r	   r	   r	   r
   r&   f   s    ,'r&   )r   rY   rp   rn   ro   hashlibr   zope.interfacer   r   r   twisted.python.compatr   r   r   r   r&   r	   r	   r	   r
   <module>   s   '