
    Yf"                     &   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e	          Z e
e           G d d	                      Z e
e           G d
 d                      Z e
e           G d d                      ZdS )zF
Protocol agnostic implementations of SASL authentication mechanisms.
    N)md5)	Attribute	Interfaceimplementer)networkStringc                   0    e Zd Z ed          Zd Zd ZdS )ISASLMechanismz#Common name for the SASL Mechanism.c                      dS )z
        Get the initial client response, if defined for this mechanism.

        @return: initial client response string.
        @rtype: C{str}.
        N r       `/var/www/html/env/lib/python3.11/site-packages/twisted/words/protocols/jabber/sasl_mechanisms.pygetInitialResponsez!ISASLMechanism.getInitialResponse         r   c                     dS )z
        Get the response to a server challenge.

        @param challenge: server challenge.
        @type challenge: C{str}.
        @return: client response.
        @rtype: C{str}.
        Nr   )	challenges    r   getResponsezISASLMechanism.getResponse!   r   r   N)__name__
__module____qualname__r   namer   r   r   r   r   r	   r	      sB        9>??D      r   r	   c                   "    e Zd ZdZdZd Zd ZdS )	Anonymouszm
    Implements the ANONYMOUS SASL authentication mechanism.

    This mechanism is defined in RFC 2245.
    	ANONYMOUSc                     d S Nr   selfs    r   r   zAnonymous.getInitialResponse6       tr   c                     d S r   r   r   r   s     r   r   zAnonymous.getResponse9       r   N)r   r   r   __doc__r   r   r   r   r   r   r   r   ,   sC          D      r   r   c                   (    e Zd ZdZdZd Zd Zd ZdS )Plainz
    Implements the PLAIN SASL authentication mechanism.

    The PLAIN SASL authentication mechanism is defined in RFC 2595.
    PLAINc                 <    |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*   s       r   __init__zPlain.__init__H   s)     }"}" Br   c                     | j                             d          dz   | j                            d          z   dz   | j                            d          z   S )Nzutf-8    )r(   encoder)   r*   r   s    r   r   zPlain.getInitialResponseX   s^    L((l!!'**+  m""7++	,	
r   c                     d S r   r   r    s     r   r   zPlain.getResponsea   r!   r   N)r   r   r   r"   r   r+   r   r   r   r   r   r$   r$   >   sR          D' ' ' 
 
 
    r   r$   c                   F    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Z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                 v    || _         || _        || _        | d| | _        || xj        d| z  c_        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_namer4   r*   s         r   r+   zDigestMD5.__init__p   sW    * !  &//// OO9.OOOO ! r   c                     d S r   r   r   s    r   r   zDigestMD5.getInitialResponse   r   r   c                    |                      |          }d|v rdS |d                             d          }	 |d         }n*# t          $ r | j                            |          }Y nw xY w|                     |||d                   S )Ns   rspauthr      charsetascii   realm   nonce)_parsedecodeKeyErrorr5   r.   _genResponse)r   r   
directivescharsetrealms        r   r   zDigestMD5.getResponse   s    [[++
 ##3Z(//88	6x(EE 	6 	6 	6%,,W55EEE	6   %H1EFFFs   A $A('A(c                 L   |}i }d}d}|r|                     d|          }|||                                         }|dz  }|||dz            dk    rG|dz  }|                     d|          }|||         }	|                    d|          dz   }|dk    rd}n\|                    d|          }|dk    r||d	                                         }	d}n|||                                         }	|dz   }|	||<   |d
D ]$}
|
|v r||
                             d          ||
<   %|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params              r   r@   zDigestMD5._parse   ss    	 	$
 WWT3''FSZ=''))DaKF&1*$%--!ggdF++&*ffT3''!+!88&+OffT6**"99fggJ--//E&+OOfSjM0022EAg#IdO/  	$2 ) 	@ 	@E	!!#,U#3#9#9$#?#?	% r   c                     g }|                                 D ]/\  }}|dv r	|dz   |z   }n|dz   |z   }|                    |           0d                    |          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}.
        )   usernamer>      cnoncer?   
   digest-uris   authzidrN   rH   rK   )itemsappendjoin)r   rD   directive_listr   rZ   	directives         r   _unparsezDigestMD5._unparse   s     %++-- 	- 	-KD%    !4K%/		 4K%/	!!),,,,yy(((r   c                    d d }fd}	 |dz   |z   dz   |z             dz   |z   dz   |z   }
d|z   } | |	 | |
                    |dz   |z   dz   |z   dz   dz   dz    | |                    z                       }|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                 D    t          |                                           S r   )r   digest)rT   s    r   Hz'DigestMD5._calculateResponse.<locals>.H   s    q66==??"r   c                 *    t          j        |           S r   )binasciib2a_hex)ns    r   HEXz)DigestMD5._calculateResponse.<locals>.HEX   s    #A&&&r   c                 &     | dz   |z             S )N   :r   )krT   ri   s     r   KDz(DigestMD5._calculateResponse.<locals>.KD   s    1QX\??"r   rp   s   AUTHENTICATE:   authr   )r   cnoncencnoncer4   r*   rF   urirn   rr   a1a2responseri   s                @r   _calculateResponsezDigestMD5._calculateResponse   s    	# 	# 	#	' 	' 	'	# 	# 	# 	# 	# Qx$&-899D@5H4ORXX#3BAAbEE

r!D(61D87BTICCPQPQRTPUPUJJV 
 
 r   c                    	 | j                             |          }| j                            |          }| j                            |          }n# t          $ r  w x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.
        rI   08xrs   r=   )	r]   r>   r?   r^   s   ncrM   r_   s   responser<   )	r4   r.   r*   r6   UnicodeErrorr   
_gen_noncer{   re   )r   rE   rF   rv   r4   r*   r6   ru   rt   qoprz   rD   s               r   rC   zDigestMD5._genResponse	  s    	}++G44H}++G44H//88JJ 	 	 		 a::&&"" **Bx5*
 

 "%!w//

 


 }}Z(((s   AA Ac                     dt          j                     t          j                    t          j                    fz  }t	          |          }t          |                                                              d          S )Nz%f:%f:%dr=   )randomtimeosgetpidr   r   	hexdigestr.   )r   nonceString
nonceBytess      r   r   zDigestMD5._gen_nonce0  sX     FMOOTY[[")++#NN";//
:((**11':::r   N)r   r   r   r"   r   r+   r   r   r@   re   r{   rC   r   r   r   r   r1   r1   f   s          D/ / /:  G G G"* * *X) ) )>  :%) %) %)N; ; ; ; ;r   r1   )r"   rk   r   r   r   hashlibr   zope.interfacer   r   r   twisted.python.compatr   r	   r   r$   r1   r   r   r   <module>r      ss   
  				         < < < < < < < < < < / / / / / /    Y   , ^       " ^$ $ $ $ $ $ $ $N ^L; L; L; L; L; L; L; L; L; L;r   