<s> """<STR_LIT>""" <EOL> import hmac <EOL> from zope . interface import implements , Interface <EOL> from twisted . trial import unittest <EOL> from twisted . cred import portal , checkers , credentials , error <EOL> from twisted . python import components <EOL> from twisted . internet import defer <EOL> from twisted . internet . defer import deferredGenerator as dG , waitForDeferred as wFD <EOL> try : <EOL> from crypt import crypt <EOL> except ImportError : <EOL> crypt = None <EOL> try : <EOL> from twisted . cred . pamauth import callIntoPAM <EOL> except ImportError : <EOL> pamauth = None <EOL> else : <EOL> from twisted . cred import pamauth <EOL> class ITestable ( Interface ) : <EOL> pass <EOL> class TestAvatar : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> self . loggedIn = False <EOL> self . loggedOut = False <EOL> def login ( self ) : <EOL> assert not self . loggedIn <EOL> self . loggedIn = True <EOL> def logout ( self ) : <EOL> self . loggedOut = True <EOL> class Testable ( components . Adapter ) : <EOL> implements ( ITestable ) <EOL> components . registerAdapter ( Testable , TestAvatar , ITestable ) <EOL> class IDerivedCredentials ( credentials . IUsernamePassword ) : <EOL> pass <EOL> class DerivedCredentials ( object ) : <EOL> implements ( IDerivedCredentials , ITestable ) <EOL> def __init__ ( self , username , password ) : <EOL> self . username = username <EOL> self . password = password <EOL> def checkPassword ( self , password ) : <EOL> return password == self . password <EOL> class TestRealm : <EOL> implements ( portal . IRealm ) <EOL> def __init__ ( self ) : <EOL> self . avatars = { } <EOL> def requestAvatar ( self , avatarId , mind , * interfaces ) : <EOL> if avatarId in self . avatars : <EOL> avatar = self . avatars [ avatarId ] <EOL> else : <EOL> avatar = TestAvatar ( avatarId ) <EOL> self . avatars [ avatarId ] = avatar <EOL> avatar . login ( ) <EOL> return ( interfaces [ <NUM_LIT:0> ] , interfaces [ <NUM_LIT:0> ] ( avatar ) , <EOL> avatar . logout ) <EOL> class NewCredTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> r = self . realm = TestRealm ( ) <EOL> p = self . portal = portal . Portal ( r ) <EOL> up = self . checker = checkers . InMemoryUsernamePasswordDatabaseDontUse ( ) <EOL> up . addUser ( "<STR_LIT>" , "<STR_LIT:hello>" ) <EOL> p . registerChecker ( up ) <EOL> def testListCheckers ( self ) : <EOL> expected = [ credentials . IUsernamePassword , credentials . IUsernameHashedPassword ] <EOL> got = self . portal . listCredentialsInterfaces ( ) <EOL> expected . sort ( ) <EOL> got . sort ( ) <EOL> self . assertEqual ( got , expected ) <EOL> def testBasicLogin ( self ) : <EOL> l = [ ] ; f = [ ] <EOL> self . portal . login ( credentials . UsernamePassword ( "<STR_LIT>" , "<STR_LIT:hello>" ) , <EOL> self , ITestable ) . addCallback ( <EOL> l . append ) . addErrback ( f . append ) <EOL> if f : <EOL> raise f [ <NUM_LIT:0> ] <EOL> iface , impl , logout = l [ <NUM_LIT:0> ] <EOL> self . assertEqual ( iface , ITestable ) <EOL> self . failUnless ( iface . providedBy ( impl ) , <EOL> "<STR_LIT>" % ( impl , iface ) ) <EOL> self . failUnless ( impl . original . loggedIn ) <EOL> self . failUnless ( not impl . original . loggedOut ) <EOL> logout ( ) <EOL> self . failUnless ( impl . original . loggedOut ) <EOL> def test_derivedInterface ( self ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> f = [ ] <EOL> self . portal . login ( DerivedCredentials ( "<STR_LIT>" , "<STR_LIT:hello>" ) , self , ITestable <EOL> ) . addCallback ( l . append <EOL> ) . addErrback ( f . append ) <EOL> if f : <EOL> raise f [ <NUM_LIT:0> ] <EOL> iface , impl , logout = l [ <NUM_LIT:0> ] <EOL> self . assertEqual ( iface , ITestable ) <EOL> self . failUnless ( iface . providedBy ( impl ) , <EOL> "<STR_LIT>" % ( impl , iface ) ) <EOL> self . failUnless ( impl . original . loggedIn ) <EOL> self . failUnless ( not impl . original . loggedOut ) <EOL> logout ( ) <EOL> self . failUnless ( impl . original . loggedOut ) <EOL> def testFailedLogin ( self ) : <EOL> l = [ ] <EOL> self . portal . login ( credentials . UsernamePassword ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> self , ITestable ) . addErrback ( <EOL> lambda x : x . trap ( error . UnauthorizedLogin ) ) . addCallback ( l . append ) <EOL> self . failUnless ( l ) <EOL> self . assertEqual ( error . UnauthorizedLogin , l [ <NUM_LIT:0> ] ) <EOL> def testFailedLoginName ( self ) : <EOL> l = [ ] <EOL> self . portal . login ( credentials . UsernamePassword ( "<STR_LIT>" , "<STR_LIT:hello>" ) , <EOL> self , ITestable ) . addErrback ( <EOL> lambda x : x . trap ( error . UnauthorizedLogin ) ) . addCallback ( l . append ) <EOL> self . failUnless ( l ) <EOL> self . assertEqual ( error . UnauthorizedLogin , l [ <NUM_LIT:0> ] ) <EOL> class CramMD5CredentialsTestCase ( unittest . TestCase ) : <EOL> def testIdempotentChallenge ( self ) : <EOL> c = credentials . CramMD5Credentials ( ) <EOL> chal = c . getChallenge ( ) <EOL> self . assertEqual ( chal , c . getChallenge ( ) ) <EOL> def testCheckPassword ( self ) : <EOL> c = credentials . CramMD5Credentials ( ) <EOL> chal = c . getChallenge ( ) <EOL> c . response = hmac . HMAC ( '<STR_LIT>' , chal ) . hexdigest ( ) <EOL> self . failUnless ( c . checkPassword ( '<STR_LIT>' ) ) <EOL> def testWrongPassword ( self ) : <EOL> c = credentials . CramMD5Credentials ( ) <EOL> self . failIf ( c . checkPassword ( '<STR_LIT>' ) ) <EOL> class OnDiskDatabaseTestCase ( unittest . TestCase ) : <EOL> users = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> def testUserLookup ( self ) : <EOL> dbfile = self . mktemp ( ) <EOL> db = checkers . FilePasswordDB ( dbfile ) <EOL> f = file ( dbfile , '<STR_LIT:w>' ) <EOL> for ( u , p ) in self . users : <EOL> f . write ( '<STR_LIT>' % ( u , p ) ) <EOL> f . close ( ) <EOL> for ( u , p ) in self . users : <EOL> self . failUnlessRaises ( KeyError , db . getUser , u . upper ( ) ) <EOL> self . assertEqual ( db . getUser ( u ) , ( u , p ) ) <EOL> def testCaseInSensitivity ( self ) : <EOL> dbfile = self . mktemp ( ) <EOL> db = checkers . FilePasswordDB ( dbfile , caseSensitive = <NUM_LIT:0> ) <EOL> f = file ( dbfile , '<STR_LIT:w>' ) <EOL> for ( u , p ) in self . users : <EOL> f . write ( '<STR_LIT>' % ( u , p ) ) <EOL> f . close ( ) <EOL> for ( u , p ) in self . users : <EOL> self . assertEqual ( db . getUser ( u . upper ( ) ) , ( u , p ) ) <EOL> def testRequestAvatarId ( self ) : <EOL> dbfile = self . mktemp ( ) <EOL> db = checkers . FilePasswordDB ( dbfile , caseSensitive = <NUM_LIT:0> ) <EOL> f = file ( dbfile , '<STR_LIT:w>' ) <EOL> for ( u , p ) in self . users : <EOL> f . write ( '<STR_LIT>' % ( u , p ) ) <EOL> f . close ( ) <EOL> creds = [ credentials . UsernamePassword ( u , p ) for u , p in self . users ] <EOL> d = defer . gatherResults ( <EOL> [ defer . maybeDeferred ( db . requestAvatarId , c ) for c in creds ] ) <EOL> d . addCallback ( self . assertEqual , [ u for u , p in self . users ] ) <EOL> return d <EOL> def testRequestAvatarId_hashed ( self ) : <EOL> dbfile = self . mktemp ( ) <EOL> db = checkers . FilePasswordDB ( dbfile , caseSensitive = <NUM_LIT:0> ) <EOL> f = file ( dbfile , '<STR_LIT:w>' ) <EOL> for ( u , p ) in self . users : <EOL> f . write ( '<STR_LIT>' % ( u , p ) ) <EOL> f . close ( ) <EOL> creds = [ credentials . UsernameHashedPassword ( u , p ) for u , p in self . users ] <EOL> d = defer . gatherResults ( <EOL> [ defer . maybeDeferred ( db . requestAvatarId , c ) for c in creds ] ) <EOL> d . addCallback ( self . assertEqual , [ u for u , p in self . users ] ) <EOL> return d <EOL> class HashedPasswordOnDiskDatabaseTestCase ( unittest . TestCase ) : <EOL> users = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> def hash ( self , u , p , s ) : <EOL> return crypt ( p , s ) <EOL> def setUp ( self ) : <EOL> dbfile = self . mktemp ( ) <EOL> self . db = checkers . FilePasswordDB ( dbfile , hash = self . hash ) <EOL> f = file ( dbfile , '<STR_LIT:w>' ) <EOL> for ( u , p ) in self . users : <EOL> f . write ( '<STR_LIT>' % ( u , crypt ( p , u [ : <NUM_LIT:2> ] ) ) ) <EOL> f . close ( ) <EOL> r = TestRealm ( ) <EOL> self . port = portal . Portal ( r ) <EOL> self . port . registerChecker ( self . db ) <EOL> def testGoodCredentials ( self ) : <EOL> goodCreds = [ credentials . UsernamePassword ( u , p ) for u , p in self . users ] <EOL> d = defer . gatherResults ( [ self . db . requestAvatarId ( c ) for c in goodCreds ] ) <EOL> d . addCallback ( self . assertEqual , [ u for u , p in self . users ] ) <EOL> return d <EOL> def testGoodCredentials_login ( self ) : <EOL> goodCreds = [ credentials . UsernamePassword ( u , p ) for u , p in self . users ] <EOL> d = defer . gatherResults ( [ self . port . login ( c , None , ITestable ) <EOL> for c in goodCreds ] ) <EOL> d . addCallback ( lambda x : [ a . original . name for i , a , l in x ] ) <EOL> d . addCallback ( self . assertEqual , [ u for u , p in self . users ] ) <EOL> return d <EOL> def testBadCredentials ( self ) : <EOL> badCreds = [ credentials . UsernamePassword ( u , '<STR_LIT>' ) <EOL> for u , p in self . users ] <EOL> d = defer . DeferredList ( [ self . port . login ( c , None , ITestable ) <EOL> for c in badCreds ] , consumeErrors = True ) <EOL> d . addCallback ( self . _assertFailures , error . UnauthorizedLogin ) <EOL> return d <EOL> def testHashedCredentials ( self ) : <EOL> hashedCreds = [ credentials . UsernameHashedPassword ( u , crypt ( p , u [ : <NUM_LIT:2> ] ) ) <EOL> for u , p in self . users ] <EOL> d = defer . DeferredList ( [ self . port . login ( c , None , ITestable ) <EOL> for c in hashedCreds ] , consumeErrors = True ) <EOL> d . addCallback ( self . _assertFailures , error . UnhandledCredentials ) <EOL> return d <EOL> def _assertFailures ( self , failures , * expectedFailures ) : <EOL> for flag , failure in failures : <EOL> self . assertEqual ( flag , defer . FAILURE ) <EOL> failure . trap ( * expectedFailures ) <EOL> return None <EOL> if crypt is None : <EOL> skip = "<STR_LIT>" <EOL> class PluggableAuthenticationModulesTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _oldCallIntoPAM = pamauth . callIntoPAM <EOL> pamauth . callIntoPAM = self . callIntoPAM <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> pamauth . callIntoPAM = self . _oldCallIntoPAM <EOL> def callIntoPAM ( self , service , user , conv ) : <EOL> if service != '<STR_LIT>' : <EOL> raise error . UnauthorizedLogin ( '<STR_LIT>' % service ) <EOL> if user != '<STR_LIT>' : <EOL> raise error . UnauthorizedLogin ( '<STR_LIT>' % user ) <EOL> questions = [ <EOL> ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:2> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT:3> , "<STR_LIT>" ) , <EOL> ] <EOL> replies = conv ( questions ) <EOL> if replies != [ <EOL> ( "<STR_LIT:password>" , <NUM_LIT:0> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:0> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> ] : <EOL> raise error . UnauthorizedLogin ( '<STR_LIT>' % repr ( replies ) ) <EOL> return <NUM_LIT:1> <EOL> def _makeConv ( self , d ) : <EOL> def conv ( questions ) : <EOL> return defer . succeed ( [ ( d [ t ] , <NUM_LIT:0> ) for t , q in questions ] ) <EOL> return conv <EOL> def testRequestAvatarId ( self ) : <EOL> db = checkers . PluggableAuthenticationModulesChecker ( ) <EOL> conv = self . _makeConv ( { <NUM_LIT:1> : '<STR_LIT:password>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' } ) <EOL> creds = credentials . PluggableAuthenticationModules ( '<STR_LIT>' , <EOL> conv ) <EOL> d = db . requestAvatarId ( creds ) <EOL> d . addCallback ( self . assertEqual , '<STR_LIT>' ) <EOL> return d <EOL> def testBadCredentials ( self ) : <EOL> db = checkers . PluggableAuthenticationModulesChecker ( ) <EOL> conv = self . _makeConv ( { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' } ) <EOL> creds = credentials . PluggableAuthenticationModules ( '<STR_LIT>' , <EOL> conv ) <EOL> d = db . requestAvatarId ( creds ) <EOL> self . assertFailure ( d , error . UnauthorizedLogin ) <EOL> return d <EOL> def testBadUsername ( self ) : <EOL> db = checkers . PluggableAuthenticationModulesChecker ( ) <EOL> conv = self . _makeConv ( { <NUM_LIT:1> : '<STR_LIT:password>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' } ) <EOL> creds = credentials . PluggableAuthenticationModules ( '<STR_LIT>' , <EOL> conv ) <EOL> d = db . requestAvatarId ( creds ) <EOL> self . assertFailure ( d , error . UnauthorizedLogin ) <EOL> return d <EOL> if not pamauth : <EOL> skip = "<STR_LIT>" <EOL> class CheckersMixin : <EOL> def testPositive ( self ) : <EOL> for chk in self . getCheckers ( ) : <EOL> for ( cred , avatarId ) in self . getGoodCredentials ( ) : <EOL> r = wFD ( chk . requestAvatarId ( cred ) ) <EOL> yield r <EOL> self . assertEqual ( r . getResult ( ) , avatarId ) <EOL> testPositive = dG ( testPositive ) <EOL> def testNegative ( self ) : <EOL> for chk in self . getCheckers ( ) : <EOL> for cred in self . getBadCredentials ( ) : <EOL> r = wFD ( chk . requestAvatarId ( cred ) ) <EOL> yield r <EOL> self . assertRaises ( error . UnauthorizedLogin , r . getResult ) <EOL> testNegative = dG ( testNegative ) <EOL> class HashlessFilePasswordDBMixin : <EOL> credClass = credentials . UsernamePassword <EOL> diskHash = None <EOL> networkHash = staticmethod ( lambda x : x ) <EOL> _validCredentials = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> def getGoodCredentials ( self ) : <EOL> for u , p in self . _validCredentials : <EOL> yield self . credClass ( u , self . networkHash ( p ) ) , u <EOL> def getBadCredentials ( self ) : <EOL> for u , p in [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] : <EOL> yield self . credClass ( u , self . networkHash ( p ) ) <EOL> def getCheckers ( self ) : <EOL> diskHash = self . diskHash or ( lambda x : x ) <EOL> hashCheck = self . diskHash and ( lambda username , password , stored : self . diskHash ( password ) ) <EOL> for cache in True , False : <EOL> fn = self . mktemp ( ) <EOL> fObj = file ( fn , '<STR_LIT:w>' ) <EOL> for u , p in self . _validCredentials : <EOL> fObj . write ( '<STR_LIT>' % ( u , diskHash ( p ) ) ) <EOL> fObj . close ( ) <EOL> yield checkers . FilePasswordDB ( fn , cache = cache , hash = hashCheck ) <EOL> fn = self . mktemp ( ) <EOL> fObj = file ( fn , '<STR_LIT:w>' ) <EOL> for u , p in self . _validCredentials : <EOL> fObj . write ( '<STR_LIT>' % ( diskHash ( p ) , u ) ) <EOL> fObj . close ( ) <EOL> yield checkers . FilePasswordDB ( fn , '<STR_LIT:U+0020>' , <NUM_LIT:3> , <NUM_LIT:0> , cache = cache , hash = hashCheck ) <EOL> fn = self . mktemp ( ) <EOL> fObj = file ( fn , '<STR_LIT:w>' ) <EOL> for u , p in self . _validCredentials : <EOL> fObj . write ( '<STR_LIT>' % ( u . title ( ) , diskHash ( p ) ) ) <EOL> fObj . close ( ) <EOL> yield checkers . FilePasswordDB ( fn , '<STR_LIT:U+002C>' , <NUM_LIT:2> , <NUM_LIT:4> , False , cache = cache , hash = hashCheck ) <EOL> class LocallyHashedFilePasswordDBMixin ( HashlessFilePasswordDBMixin ) : <EOL> diskHash = staticmethod ( lambda x : x . encode ( '<STR_LIT>' ) ) <EOL> class NetworkHashedFilePasswordDBMixin ( HashlessFilePasswordDBMixin ) : <EOL> networkHash = staticmethod ( lambda x : x . encode ( '<STR_LIT>' ) ) <EOL> class credClass ( credentials . UsernameHashedPassword ) : <EOL> def checkPassword ( self , password ) : <EOL> return self . hashed . decode ( '<STR_LIT>' ) == password <EOL> class HashlessFilePasswordDBCheckerTestCase ( HashlessFilePasswordDBMixin , CheckersMixin , unittest . TestCase ) : <EOL> pass <EOL> class LocallyHashedFilePasswordDBCheckerTestCase ( LocallyHashedFilePasswordDBMixin , CheckersMixin , unittest . TestCase ) : <EOL> pass <EOL> class NetworkHashedFilePasswordDBCheckerTestCase ( NetworkHashedFilePasswordDBMixin , CheckersMixin , unittest . TestCase ) : <EOL> pass </s>
