<s> """<STR_LIT>""" <EOL> import binascii , random , time , os <EOL> from zope . interface import Interface , Attribute , implements <EOL> from twisted . python . hashlib import md5 <EOL> class ISASLMechanism ( Interface ) : <EOL> name = Attribute ( """<STR_LIT>""" ) <EOL> def getInitialResponse ( ) : <EOL> """<STR_LIT>""" <EOL> def getResponse ( challenge ) : <EOL> """<STR_LIT>""" <EOL> class Anonymous ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( ISASLMechanism ) <EOL> name = '<STR_LIT>' <EOL> def getInitialResponse ( self ) : <EOL> return None <EOL> class Plain ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( ISASLMechanism ) <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , authzid , authcid , password ) : <EOL> self . authzid = authzid or '<STR_LIT>' <EOL> self . authcid = authcid or '<STR_LIT>' <EOL> self . password = password or '<STR_LIT>' <EOL> def getInitialResponse ( self ) : <EOL> return "<STR_LIT>" % ( self . authzid . encode ( '<STR_LIT:utf-8>' ) , <EOL> self . authcid . encode ( '<STR_LIT:utf-8>' ) , <EOL> self . password . encode ( '<STR_LIT:utf-8>' ) ) <EOL> class DigestMD5 ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( ISASLMechanism ) <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , serv_type , host , serv_name , username , password ) : <EOL> self . username = username <EOL> self . password = password <EOL> self . defaultRealm = host <EOL> self . digest_uri = '<STR_LIT>' % ( serv_type , host ) <EOL> if serv_name is not None : <EOL> self . digest_uri += '<STR_LIT>' % serv_name <EOL> def getInitialResponse ( self ) : <EOL> return None <EOL> def getResponse ( self , challenge ) : <EOL> directives = self . _parse ( challenge ) <EOL> if '<STR_LIT>' in directives : <EOL> return '<STR_LIT>' <EOL> try : <EOL> realm = directives [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> realm = self . defaultRealm <EOL> return self . _gen_response ( directives [ '<STR_LIT>' ] , <EOL> realm , <EOL> directives [ '<STR_LIT>' ] ) <EOL> def _parse ( self , challenge ) : <EOL> """<STR_LIT>""" <EOL> s = challenge <EOL> paramDict = { } <EOL> cur = <NUM_LIT:0> <EOL> remainingParams = True <EOL> while remainingParams : <EOL> middle = s . index ( "<STR_LIT:=>" , cur ) <EOL> name = s [ cur : middle ] . lstrip ( ) <EOL> middle += <NUM_LIT:1> <EOL> if s [ middle ] == '<STR_LIT:">' : <EOL> middle += <NUM_LIT:1> <EOL> end = s . index ( '<STR_LIT:">' , middle ) <EOL> value = s [ middle : end ] <EOL> cur = s . find ( '<STR_LIT:U+002C>' , end ) + <NUM_LIT:1> <EOL> if cur == <NUM_LIT:0> : <EOL> remainingParams = False <EOL> else : <EOL> end = s . find ( '<STR_LIT:U+002C>' , middle ) <EOL> if end == - <NUM_LIT:1> : <EOL> value = s [ middle : ] . rstrip ( ) <EOL> remainingParams = False <EOL> else : <EOL> value = s [ middle : end ] . rstrip ( ) <EOL> cur = end + <NUM_LIT:1> <EOL> paramDict [ name ] = value <EOL> for param in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if param in paramDict : <EOL> paramDict [ param ] = paramDict [ param ] . split ( '<STR_LIT:U+002C>' ) <EOL> return paramDict <EOL> def _unparse ( self , directives ) : <EOL> """<STR_LIT>""" <EOL> directive_list = [ ] <EOL> for name , value in directives . iteritems ( ) : <EOL> if name in ( '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> directive = '<STR_LIT>' % ( name , value ) <EOL> else : <EOL> directive = '<STR_LIT>' % ( name , value ) <EOL> directive_list . append ( directive ) <EOL> return '<STR_LIT:U+002C>' . join ( directive_list ) <EOL> def _gen_response ( self , charset , realm , nonce ) : <EOL> """<STR_LIT>""" <EOL> def H ( s ) : <EOL> return md5 ( s ) . digest ( ) <EOL> def HEX ( n ) : <EOL> return binascii . b2a_hex ( n ) <EOL> def KD ( k , s ) : <EOL> return H ( '<STR_LIT>' % ( k , s ) ) <EOL> try : <EOL> username = self . username . encode ( charset ) <EOL> password = self . password . encode ( charset ) <EOL> except UnicodeError : <EOL> raise <EOL> nc = '<STR_LIT>' % <NUM_LIT:1> <EOL> cnonce = self . _gen_nonce ( ) <EOL> qop = '<STR_LIT>' <EOL> a1 = "<STR_LIT>" % ( H ( "<STR_LIT>" % ( username , realm , password ) ) , <EOL> nonce , <EOL> cnonce ) <EOL> a2 = "<STR_LIT>" % self . digest_uri <EOL> response = HEX ( KD ( HEX ( H ( a1 ) ) , <EOL> "<STR_LIT>" % ( nonce , nc , <EOL> cnonce , "<STR_LIT>" , HEX ( H ( a2 ) ) ) ) ) <EOL> directives = { '<STR_LIT:username>' : username , <EOL> '<STR_LIT>' : realm , <EOL> '<STR_LIT>' : nonce , <EOL> '<STR_LIT>' : cnonce , <EOL> '<STR_LIT>' : nc , <EOL> '<STR_LIT>' : qop , <EOL> '<STR_LIT>' : self . digest_uri , <EOL> '<STR_LIT>' : response , <EOL> '<STR_LIT>' : charset } <EOL> return self . _unparse ( directives ) <EOL> def _gen_nonce ( self ) : <EOL> return md5 ( "<STR_LIT>" % ( str ( random . random ( ) ) , str ( time . gmtime ( ) ) , str ( os . getpid ( ) ) ) ) . hexdigest ( ) </s>
