
    hrA                    ^   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJrJrJr  SSKJr  SSKJrJrJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSKJ r    " S S\5      r! " S S\!5      r" " S S\!5      r# " S S\!5      r$ " S S\!5      r%\" \#\"5       " S S5      5       r& " S S5      r'\" \#5       " S S5      5       r(\" \#5       " S S5      5       r)\" \$5       " S  S!5      5       r*\" \%5       " S" S#5      5       r+ " S$ S%\!5      r,\" \,5       " S& S'5      5       r-g)(z
This module defines L{ICredentials}, an interface for objects that represent
authentication credentials to provide, and also includes a number of useful
implementations of that interface.
    )annotationsN)hexlify)md5)	Attribute	Interfaceimplementer)error)calcHA1calcHA2calcResponse)Deferred)nativeStringnetworkString)deprecatedModuleAttribute)secureRandom)Versionc                      \ rS rSrSrSrg)ICredentials   z
I check credentials.

Implementors I{must} specify the sub-interfaces of ICredentials
to which it conforms, using L{zope.interface.implementer}.
 N__name__
__module____qualname____firstlineno____doc____static_attributes__r       _/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/cred/credentials.pyr   r      s    r   r   c                      \ rS rSrSrS rSrg)IUsernameDigestHash(   z
This credential is used when a CredentialChecker has access to the hash
of the username:realm:password as in an Apache .htdigest file.
c                    g)a  
@param digestHash: The hashed username:realm:password to check against.

@return: C{True} if the credentials represented by this object match
    the given hash, C{False} if they do not, or a L{Deferred} which
    will be called back with one of these values.
Nr   )
digestHashs    r   	checkHashIUsernameDigestHash.checkHash.       r   r   N)r   r   r   r   r   r%   r   r   r   r   r!   r!   (   s    
r   r!   c                  :    \ rS rSr% Sr\" S5      rS\S'   S rSr	g)	IUsernameHashedPassword8   av  
I encapsulate a username and a hashed password.

This credential is used when a hashed password is received from the
party requesting authentication.  CredentialCheckers which check this
kind of credential must store the passwords in plaintext (or as
password-equivalent hashes) form so that they can be hashed in a manner
appropriate for the particular credentials class.
zA
        The username associated with these credentials.
        bytesusernamec                    g)a|  
Validate these credentials against the correct password.

@type password: L{bytes}
@param password: The correct, plaintext password against which to
check.

@rtype: C{bool} or L{Deferred}
@return: C{True} if the credentials represented by this object match the
    given password, C{False} if they do not, or a L{Deferred} which will
    be called back with one of these values.
Nr   passwords    r   checkPassword%IUsernameHashedPassword.checkPasswordI   r'   r   r   N)
r   r   r   r   r   r   r,   __annotations__r0   r   r   r   r   r)   r)   8   s$      	He r   r)   c                  8    \ rS rSr% SrS\S'   S\S'   S	S jrSrg)
IUsernamePasswordX   a  
I encapsulate a username and a plaintext password.

This encapsulates the case where the password received over the network
has been hashed with the identity function (That is, not at all).  The
CredentialsChecker may store the password in whatever format it desires,
it need only transform the stored password in a similar way before
performing the comparison.

@type username: L{bytes}
@ivar username: The username associated with these credentials.

@type password: L{bytes}
@ivar password: The password associated with these credentials.
r+   r,   r/   c                    g)aH  
Validate these credentials against the correct password.

@param password: The correct, plaintext password against which to
    check.

@return: C{True} if the credentials represented by this object match
    the given password, C{False} if they do not, or a L{Deferred} which
    will be called back with one of these values.
Nr   r.   s    r   r0   IUsernamePassword.checkPasswordl   r'   r   r   N)r/   r+   returnzbool | Deferred[bool])r   r   r   r   r   r2   r0   r   r   r   r   r4   r4   X   s      OO
r   r4   c                      \ rS rSrSrSrg)
IAnonymousy   zg
I am an explicitly anonymous request for access.

@see: L{twisted.cred.checkers.AllowAnonymousAccess}
r   Nr   r   r   r   r:   r:   y   s    r   r:   c                  *    \ rS rSrSrS rS rS rSrg)DigestedCredentials   z7
Yet Another Simple HTTP Digest authentication scheme.
c                4    Xl         X l        X0l        X@l        g N)r,   methodrealmfields)selfr,   rA   rB   rC   s        r   __init__DigestedCredentials.__init__   s     
r   c           
     0   U R                   R                  S5      nU R                   R                  S5      nU R                   R                  S5      nU R                   R                  S5      nU R                   R                  S5      nU R                   R                  SS5      R                  5       nU R                   R                  SS	5      n[        [	        XpR
                  U R                  XU5      [        XpR                  X8S
5      UUUUU5      n	X:H  $ )z
Verify that the credentials represented by this object agree with the
given plaintext C{password} by hashing C{password} in the same way the
response hash represented by this object was generated and comparing
the results.
responseurinoncecnoncenc	algorithm   md5qop   authN)	rC   getlowerr   r
   r,   rB   r   rA   )
rD   r/   rH   rI   rJ   rK   rL   algorO   expecteds
             r   r0   !DigestedCredentials.checkPassword   s     ;;??:.kkooe$(*[[__T"{{{F399;kkooeW-D--XfMD++s6
 ##r   c                   U R                   R                  S5      nU R                   R                  S5      nU R                   R                  S5      nU R                   R                  S5      nU R                   R                  S5      nU R                   R                  SS5      R                  5       nU R                   R                  SS	5      n[        [	        US
S
S
XEUS9[        XpR                  X8S
5      UUUUU5      n	X:H  $ )a  
Verify that the credentials represented by this object agree with the
credentials represented by the I{H(A1)} given in C{digestHash}.

@param digestHash: A precomputed H(A1) value based on the username,
    realm, and password associate with this credentials object.
rH   rI   rJ   rK   rL   rM   rN   rO   rP   N)preHA1)rC   rQ   rR   r   r
   r   rA   )
rD   r$   rH   rI   rJ   rK   rL   rS   rO   rT   s
             r   r%   DigestedCredentials.checkHash   s     ;;??:.kkooe$(*[[__T"{{{F399;kkooeW-D$dE*MD++s6
 ##r   )rC   rA   rB   r,   N)	r   r   r   r   r   rE   r0   r%   r   r   r   r   r=   r=      s    $6$r   r=   c                  n    \ rS rSrSr\R                  " S5      rSrSr	S r
S rS rS	 rS
 rS rS rSrg)DigestCredentialFactory   a>  
Support for RFC2617 HTTP Digest Authentication

@cvar CHALLENGE_LIFETIME_SECS: The number of seconds for which an
    opaque should be valid.

@type privateKey: L{bytes}
@ivar privateKey: A random string used for generating the secure opaque.

@type algorithm: L{bytes}
@param algorithm: Case insensitive string specifying the hash algorithm to
    use.  Must be either C{'md5'} or C{'sha'}.  C{'md5-sess'} is B{not}
    supported.

@type authenticationRealm: L{bytes}
@param authenticationRealm: case sensitive string that specifies the realm
    portion of the challenge
s    ([^= ]+)=(?:"([^"]*)"|([^,]+)),?i  s   digestc                <    Xl         X l        [        S5      U l        g )N   )rM   authenticationRealmr   
privateKey)rD   rM   r^   s      r   rE    DigestCredentialFactory.__init__   s    "#6 &r*r   c                |    U R                  5       nU R                  X!5      nUUSU R                  U R                  S.$ )z
Generate the challenge for use in the WWW-Authenticate header.

@param address: The client address to which this challenge is being
    sent.

@return: The L{dict} that can be used to generate a WWW-Authenticate
    header.
rP   )rJ   opaquerO   rM   rB   )_generateNonce_generateOpaquerM   r^   )rD   addresscos       r   getChallenge$DigestCredentialFactory.getChallenge   sG     !  , --
 	
r   c                *    [        [        S5      5      $ )zr
Create a random value suitable for use as the nonce parameter of a
WWW-Authenticate challenge.

@rtype: L{bytes}
r]   )r   r   rD   s    r   rc   &DigestCredentialFactory._generateNonce  s     |B'((r   c                ,    [         R                   " 5       $ )zq
Parameterize the time based seed used in C{_generateOpaque}
so we can deterministically unittest it's behavior.
)timerk   s    r   _getTime DigestCredentialFactory._getTime  s    
 yy{r   c                   S[        U R                  5       5      4-  nU(       d  SnO&[        U[        5      (       a  UR	                  S5      nSR                  XU45      n[        [        X@R                  -   5      R                  5       5      n[        R                  " U5      nSR                  XVR                  SS5      45      $ )zt
Generate an opaque to be returned to the client.  This is a unique
string that can be returned to us and verified.
s   %dr   ascii   ,   -   
)intro   
isinstancestrencodejoinr   r   r_   digestbase64	b64encodereplace)rD   rJ   clientipnowkeyr{   ekeys          r   rd   'DigestCredentialFactory._generateOpaque  s     s4==?+--H#&&w/Hii#./S??23::<=$yy&,,uc":;<<r   c                p   UR                  S5      n[        U5      S:w  a  [        R                  " S5      eU(       d  SnO&[	        U[
        5      (       a  UR                  S5      n[        R                  " US   5      nUR                  S5      n[        U5      S:w  a  [        R                  " S5      eUS	   U:w  a  [        R                  " S
5      eUS   U:w  a  [        R                  " S5      e [        US   5      n[        U R                  5       5      U-
  [        R                  :  a  [        R                  " S5      e[        [        XPR                   -   5      R#                  5       5      nXS	   :w  a  [        R                  " S5      eg! [         a    [        R                  " S5      ef = f)aW  
Given the opaque and nonce from the request, as well as the client IP
that made the request, verify that the opaque was generated by us.
And that it's not too old.

@param opaque: The opaque value from the Digest response
@param nonce: The nonce value from the Digest response
@param clientip: The remote IP address of the client making the request
    or L{None} if the request was submitted over a channel where this
    does not make sense.

@return: C{True} if the opaque was successfully verified.

@raise error.LoginFailed: if C{opaque} could not be parsed or
    contained the wrong values.
rt      z&Invalid response, invalid opaque valuer   rr      rs      r   z2Invalid response, incompatible opaque/nonce valuesz3Invalid response, incompatible opaque/client valuesz,Invalid response, invalid opaque/time valuesz3Invalid response, incompatible opaque/nonce too oldT)splitlenr	   LoginFailedrw   rx   ry   r|   	b64decoderv   
ValueErrorro   rZ   CHALLENGE_LIFETIME_SECSr   r   r_   r{   )	rD   rb   rJ   r   opaquePartsr   keyPartswhenr{   s	            r   _verifyOpaque%DigestCredentialFactory._verifyOpaque%  s   $ ll4({q ##$LMMH#&&w/H {1~.99T?x=A##$LMMA;%##D  A;("##E 	Tx{#D
  4'%==> ##E 
 S??23::<=^###$LMM!  	T##$RSS	Ts   5F !F5c                b   SR                  UR                  5       5      nU R                  R                  U5      n0 nU H:  u  pgnU=(       d    UR	                  5       n	X[        UR	                  5       5      '   M<     UR                  S5      n
U
(       d  [        R                  " S5      eSU;  a  [        R                  " S5      eSU;  a  [        R                  " S5      eU R                  UR                  S5      UR                  S5      U5      (       a  [        XU R                  U5      $ g)	a  
Decode the given response and attempt to generate a
L{DigestedCredentials} from it.

@type response: L{bytes}
@param response: A string of comma separated key=value pairs

@type method: L{bytes}
@param method: The action requested to which this response is addressed
    (GET, POST, INVITE, OPTIONS, etc).

@type host: L{bytes}
@param host: The address the request was sent from.

@raise error.LoginFailed: If the response does not contain a username,
    a nonce, an opaque, or if the opaque is invalid.

@return: L{DigestedCredentials}
    r,   z$Invalid response, no username given.rb   z"Invalid response, no opaque given.rJ   z!Invalid response, no nonce given.N)rz   
splitlines_parsepartsfindallstripr   rQ   r	   r   r   r=   r^   )rD   rH   rA   hostpartsauthr   barequotedvaluer,   s              r   decodeDigestCredentialFactory.decodee  s   ( 99X0023  ((2!&Cv^t**,E.3ciik*+ "' 88J'##$JKK4##$HII$##$GHH dhhx0$((72CTJJ&x9Q9QSWXX Kr   )rM   r^   r_   N)r   r   r   r   r   recompiler   r   schemerE   rh   rc   ro   rd   r   r   r   r   r   r   rZ   rZ      sN    & **		K &F+

*)=&>@'Yr   rZ   c                  F    \ rS rSrSrSrSrSrSS jrS r	S r
S rS	 rS
rg)CramMD5Credentialsi  aS  
An encapsulation of some CramMD5 hashed credentials.

@ivar challenge: The challenge to be sent to the client.
@type challenge: L{bytes}

@ivar response: The hashed response from the client.
@type response: L{bytes}

@ivar username: The username from the response from the client.
@type username: L{bytes} or L{None} if not yet provided.
Nr   c                    Xl         g r@   )r   )rD   r   s     r   rE   CramMD5Credentials.__init__  s    	r   c                    U R                   (       a  U R                   $ [        R                  " S5      n[        R                  " 5       n[	        SXU R
                  (       a  [        U R
                  5      OS 4-  5      U l         U R                   $ )Niz
<%d.%d@%s>)	challengerandom	randrangern   r   r   r   )rD   rts      r   rh   CramMD5Credentials.getChallenge  sf    >>>>! Z(IIK&ATYY,tyy"9DQQ
 ~~r   c                B    UR                  S S5      u  U l        U l        g )Nr   )r   r,   rH   )rD   rH   s     r   setResponseCramMD5Credentials.setResponse  s    '/~~dA'>$t}r   c                    g)NFr   rk   s    r   moreChallenges!CramMD5Credentials.moreChallenges  s    r   c                    [        [        R                  " XR                  [        S9R                  5       5      nX R                  :H  $ )N)	digestmod)r   hmacHMACr   r   r{   rH   )rD   r/   verifys      r   r0    CramMD5Credentials.checkPassword  s1    8^^sKRRTU&&r   )r   r   rH   r,   r@   )r   r   r   r   r   r,   r   rH   rE   rh   r   r   r0   r   r   r   r   r   r     s1     HIH ?'r   r   c                  H    \ rS rSr\" \" SSSS5      SSS 5        S rS	 rS
rg)UsernameHashedPasswordi  Twisted   r   r   z6Use twisted.cred.credentials.UsernamePassword instead.ztwisted.cred.credentialsc                    Xl         X l        g r@   )r,   hashed)rD   r,   r   s      r   rE   UsernameHashedPassword.__init__  s     r   c                     U R                   U:H  $ r@   )r   rD   r/   s     r   r0   $UsernameHashedPassword.checkPassword  s    {{h&&r   )r   r,   N)	r   r   r   r   r   r   rE   r0   r   r   r   r   r   r     s+    	2q!$@" 	'r   r   c                  B    \ rS rSr% SrS\S'   S\S'   S
S jrSS jrSrg	)UsernamePasswordi  zY
A trivial implementation of L{IUsernamePassword}, containing a username and
a password.
r+   r,   r/   c                    Xl         X l        g r@   )r,   r/   )rD   r,   r/   s      r   rE   UsernamePassword.__init__  s      r   c                     U R                   U:H  $ r@   r.   r   s     r   r0   UsernamePassword.checkPassword  s    }}((r   )r/   r,   N)r,   r+   r/   r+   r8   None)r/   r+   r8   bool)	r   r   r   r   r   r2   rE   r0   r   r   r   r   r   r     s    
 OO!)r   r   c                      \ rS rSrSrg)	Anonymousi  r   N)r   r   r   r   r   r   r   r   r   r     s    r   r   c                      \ rS rSrSrSrg)ISSHPrivateKeyi  a2  
L{ISSHPrivateKey} credentials encapsulate an SSH public key to be checked
against a user's private key.

@ivar username: The username associated with these credentials.
@type username: L{bytes}

@ivar algName: The algorithm name for the blob.
@type algName: L{bytes}

@ivar blob: The public key blob as sent by the client.
@type blob: L{bytes}

@ivar sigData: The data the signature was made from.
@type sigData: L{bytes}

@ivar signature: The signed data.  This is checked to verify that the user
    owns the private key.
@type signature: L{bytes} or L{None}
r   Nr   r   r   r   r   r     s    r   r   c                      \ rS rSrS rSrg)SSHPrivateKeyi  c                @    Xl         X l        X0l        X@l        XPl        g r@   )r,   algNameblobsigData	signature)rD   r,   r   r   r   r   s         r   rE   SSHPrivateKey.__init__  s     	"r   )r   r   r   r   r,   N)r   r   r   r   rE   r   r   r   r   r   r     s    #r   r   ).r   
__future__r   r|   r   r   r   rn   binasciir   hashlibr   zope.interfacer   r   r   twisted.credr	   twisted.cred._digestr
   r   r   twisted.internet.deferr   twisted.python.compatr   r   twisted.python.deprecater   twisted.python.randbytesr   twisted.python.versionsr   r   r!   r)   r4   r:   r=   rZ   r   r   r   r   r   r   r   r   r   <module>r      s[  

 #    	    < <  ? ? + = > 1 +9 ,  l @ B  $&9:@$ @$ ;@$FGY GYT $%-' -' &-'` $%' ' &'  ) )  )" Z	 	 	\ . ^# # #r   