<s> import sys , hashlib <EOL> class AuthError ( Exception ) : pass <EOL> class WrongPassword ( AuthError ) : pass <EOL> def HTTPauthenticate ( request , response , url , authorizeUserPasswordFunc , authMethod = "<STR_LIT>" , authRealm = None ) : <EOL> def KD ( secret , data ) : <EOL> return hashlib . md5 ( secret + "<STR_LIT::>" + data ) . hexdigest ( ) <EOL> try : <EOL> auth = request . getAuth ( ) <EOL> if not auth : <EOL> raise AuthError ( '<STR_LIT>' ) <EOL> ( scheme , digest ) = auth . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if scheme . lower ( ) == '<STR_LIT>' : <EOL> try : <EOL> ( username , password ) = digest . decode ( '<STR_LIT>' ) . split ( '<STR_LIT::>' ) <EOL> try : <EOL> privileges = authorizeUserPasswordFunc ( "<STR_LIT>" , url , username , password , request ) <EOL> if privileges is not None : <EOL> return username , password , privileges <EOL> except Exception , x : <EOL> print >> sys . stderr , "<STR_LIT>" , x <EOL> raise <EOL> raise WrongPassword ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> raise AuthError ( '<STR_LIT>' ) <EOL> elif scheme . lower ( ) == '<STR_LIT>' : <EOL> digest = [ d . strip ( ) for d in digest . split ( '<STR_LIT:U+002C>' ) ] <EOL> dig = { } <EOL> for digestitem in digest : <EOL> ( name , value ) = digestitem . split ( '<STR_LIT:=>' ) <EOL> if value . startswith ( '<STR_LIT:">' ) and value . endswith ( '<STR_LIT:">' ) : <EOL> value = value [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> dig [ name . lower ( ) ] = value <EOL> username = dig [ '<STR_LIT:username>' ] <EOL> if dig . get ( '<STR_LIT>' ) in ( None , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> hA1 = hashlib . md5 ( username + '<STR_LIT::>' + dig [ '<STR_LIT>' ] + '<STR_LIT::>' + password ) . hexdigest ( ) <EOL> else : <EOL> raise AuthError ( '<STR_LIT>' + dig [ '<STR_LIT>' ] ) <EOL> if dig . get ( '<STR_LIT>' ) in ( None , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> hA2 = hashlib . md5 ( request . getMethod ( ) + '<STR_LIT::>' + dig [ '<STR_LIT>' ] ) . hexdigest ( ) <EOL> else : <EOL> raise AuthError ( '<STR_LIT>' + dig [ '<STR_LIT>' ] ) <EOL> if dig [ '<STR_LIT>' ] . lower ( ) == "<STR_LIT>" : <EOL> computed_request_digest = KD ( hA1 , dig [ '<STR_LIT>' ] + '<STR_LIT::>' + dig [ '<STR_LIT>' ] + '<STR_LIT::>' + dig [ '<STR_LIT>' ] + '<STR_LIT::>' + dig [ '<STR_LIT>' ] + '<STR_LIT::>' + hA2 ) <EOL> else : <EOL> computed_request_digest = KD ( hA1 , dig [ '<STR_LIT>' ] + "<STR_LIT::>" + hA2 ) <EOL> if computed_request_digest != dig [ '<STR_LIT>' ] : <EOL> raise AuthError ( "<STR_LIT>" ) <EOL> try : <EOL> privileges = authorizeUserPasswordFunc ( "<STR_LIT>" , url , username , password , request ) <EOL> if privileges is not None : <EOL> return username , password , privileges <EOL> except Exception , x : <EOL> print >> sys . stderr , "<STR_LIT>" , x <EOL> raise <EOL> raise WrongPassword ( "<STR_LIT>" ) <EOL> else : <EOL> raise AuthError ( '<STR_LIT>' + scheme ) <EOL> except AuthError , x : <EOL> if authMethod == "<STR_LIT>" : <EOL> name = authRealm or request . getWebApp ( ) . getName ( ) [ <NUM_LIT:1> ] or "<STR_LIT>" <EOL> response . setHeader ( "<STR_LIT>" , "<STR_LIT>" + name + "<STR_LIT>" ) <EOL> response . sendError ( <NUM_LIT> ) <EOL> raise <EOL> elif authMethod == "<STR_LIT>" : <EOL> realm = request . getWebApp ( ) . getName ( ) [ <NUM_LIT:1> ] or "<STR_LIT>" <EOL> nonce = hashlib . sha ( ) . hexdigest ( str ( time . time ( ) ) + "<STR_LIT>" ) <EOL> details = "<STR_LIT>" % ( realm , nonce ) <EOL> response . setHeader ( "<STR_LIT>" , details ) <EOL> response . sendError ( <NUM_LIT> ) <EOL> raise <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + authMethod ) </s>
