Ticket #5808: cred-open-5808-2.patch
| File cred-open-5808-2.patch, 11.3 KB (added by thijs, 10 months ago) |
|---|
-
twisted/cred/checkers.py
8 8 9 9 from twisted.internet import defer 10 10 from twisted.python import failure, log 11 from twisted.python.filepath import FilePath 11 12 from twisted.cred import error, credentials 12 13 13 14 … … 97 98 98 99 99 100 class FilePasswordDB: 100 """A file-based, text-based username/password database. 101 """ 102 A file-based, text-based username/password database. 101 103 102 104 Records in the datafile for this class are delimited by a particular 103 105 string. The username appears in a fixed field of the columns delimited … … 193 195 194 196 def _loadCredentials(self): 195 197 try: 196 f = file(self.filename)198 f = FilePath(self.filename).open() 197 199 except: 198 200 log.err() 199 201 raise error.UnauthorizedLogin() -
twisted/test/test_newcred.py
12 12 from twisted.trial import unittest 13 13 from twisted.cred import portal, checkers, credentials, error 14 14 from twisted.python import components 15 from twisted.python.filepath import FilePath 15 16 from twisted.internet import defer 16 from twisted.internet.defer import deferredGenerator as dG, waitForDeferred as wFD 17 from twisted.internet.defer import (deferredGenerator as dG, 18 waitForDeferred as wFD) 17 19 18 20 try: 19 21 from crypt import crypt … … 170 172 c = credentials.CramMD5Credentials() 171 173 self.failIf(c.checkPassword('secret')) 172 174 175 176 173 177 class OnDiskDatabaseTestCase(unittest.TestCase): 174 178 users = [ 175 179 ('user1', 'pass1'), … … 177 181 ('user3', 'pass3'), 178 182 ] 179 183 184 def setUp(self): 185 self.dbfile = FilePath(self.mktemp()) 180 186 181 def testUserLookup(self): 182 dbfile = self.mktemp() 183 db = checkers.FilePasswordDB(dbfile) 184 f = file(dbfile, 'w') 185 for (u, p) in self.users: 186 f.write('%s:%s\n' % (u, p)) 187 f.close() 187 def test_userLookup(self): 188 db = checkers.FilePasswordDB(self.dbfile.path) 189 with self.dbfile.open('w') as f: 190 for (u, p) in self.users: 191 f.write('%s:%s\n' % (u, p)) 188 192 189 193 for (u, p) in self.users: 190 194 self.failUnlessRaises(KeyError, db.getUser, u.upper()) 191 195 self.assertEqual(db.getUser(u), (u, p)) 192 196 193 def testCaseInSensitivity(self): 194 dbfile = self.mktemp() 195 db = checkers.FilePasswordDB(dbfile, caseSensitive=0) 196 f = file(dbfile, 'w') 197 for (u, p) in self.users: 198 f.write('%s:%s\n' % (u, p)) 199 f.close() 197 198 def test_caseInSensitivity(self): 199 db = checkers.FilePasswordDB(self.dbfile.path, caseSensitive=0) 200 with self.dbfile.open('w') as f: 201 for (u, p) in self.users: 202 f.write('%s:%s\n' % (u, p)) 200 203 201 204 for (u, p) in self.users: 202 205 self.assertEqual(db.getUser(u.upper()), (u, p)) 203 206 204 def testRequestAvatarId(self): 205 dbfile = self.mktemp()206 db = checkers.FilePasswordDB( dbfile, caseSensitive=0)207 f = file(dbfile, 'w')208 for (u, p) in self.users:209 f.write('%s:%s\n' % (u, p))210 f.close() 207 208 def test_requestAvatarId(self): 209 db = checkers.FilePasswordDB(self.dbfile.path, caseSensitive=0) 210 with self.dbfile.open('w') as f: 211 for (u, p) in self.users: 212 f.write('%s:%s\n' % (u, p)) 213 211 214 creds = [credentials.UsernamePassword(u, p) for u, p in self.users] 212 215 d = defer.gatherResults( 213 216 [defer.maybeDeferred(db.requestAvatarId, c) for c in creds]) 214 217 d.addCallback(self.assertEqual, [u for u, p in self.users]) 215 218 return d 216 219 217 def testRequestAvatarId_hashed(self): 218 dbfile = self.mktemp()219 db = checkers.FilePasswordDB( dbfile, caseSensitive=0)220 f = file(dbfile, 'w')221 for (u, p) in self.users:222 f.write('%s:%s\n' % (u, p))223 f.close() 220 221 def test_requestAvatarId_hashed(self): 222 db = checkers.FilePasswordDB(self.dbfile.path, caseSensitive=0) 223 with self.dbfile.open('w') as f: 224 for (u, p) in self.users: 225 f.write('%s:%s\n' % (u, p)) 226 224 227 creds = [credentials.UsernameHashedPassword(u, p) for u, p in self.users] 225 228 d = defer.gatherResults( 226 229 [defer.maybeDeferred(db.requestAvatarId, c) for c in creds]) … … 240 243 def hash(self, u, p, s): 241 244 return crypt(p, s) 242 245 246 243 247 def setUp(self): 244 dbfile = self.mktemp() 245 self.db = checkers.FilePasswordDB(dbfile, hash=self.hash) 246 f = file(dbfile, 'w') 247 for (u, p) in self.users: 248 f.write('%s:%s\n' % (u, crypt(p, u[:2]))) 249 f.close() 248 dbfile = FilePath(self.mktemp()) 249 self.db = checkers.FilePasswordDB(dbfile.path, hash=self.hash) 250 with dbfile.open('w') as f: 251 for (u, p) in self.users: 252 f.write('%s:%s\n' % (u, crypt(p, u[:2]))) 250 253 r = TestRealm() 251 254 self.port = portal.Portal(r) 252 255 self.port.registerChecker(self.db) 253 256 254 def testGoodCredentials(self): 257 258 def test_goodCredentials(self): 255 259 goodCreds = [credentials.UsernamePassword(u, p) for u, p in self.users] 256 260 d = defer.gatherResults([self.db.requestAvatarId(c) for c in goodCreds]) 257 261 d.addCallback(self.assertEqual, [u for u, p in self.users]) 258 262 return d 259 263 260 def testGoodCredentials_login(self): 264 265 def test_goodCredentials_login(self): 261 266 goodCreds = [credentials.UsernamePassword(u, p) for u, p in self.users] 262 267 d = defer.gatherResults([self.port.login(c, None, ITestable) 263 268 for c in goodCreds]) … … 265 270 d.addCallback(self.assertEqual, [u for u, p in self.users]) 266 271 return d 267 272 268 def testBadCredentials(self): 273 274 def test_badCredentials(self): 269 275 badCreds = [credentials.UsernamePassword(u, 'wrong password') 270 276 for u, p in self.users] 271 277 d = defer.DeferredList([self.port.login(c, None, ITestable) … … 273 279 d.addCallback(self._assertFailures, error.UnauthorizedLogin) 274 280 return d 275 281 276 def testHashedCredentials(self): 282 283 def test_hashedCredentials(self): 277 284 hashedCreds = [credentials.UsernameHashedPassword(u, crypt(p, u[:2])) 278 285 for u, p in self.users] 279 286 d = defer.DeferredList([self.port.login(c, None, ITestable) … … 281 288 d.addCallback(self._assertFailures, error.UnhandledCredentials) 282 289 return d 283 290 291 284 292 def _assertFailures(self, failures, *expectedFailures): 285 293 for flag, failure in failures: 286 294 self.assertEqual(flag, defer.FAILURE) … … 290 298 if crypt is None: 291 299 skip = "crypt module not available" 292 300 301 302 293 303 class PluggableAuthenticationModulesTest(unittest.TestCase): 294 304 295 305 def setUp(self): … … 362 372 if not pamauth: 363 373 skip = "Can't run without PyPAM" 364 374 375 376 365 377 class CheckersMixin: 366 378 def testPositive(self): 367 379 for chk in self.getCheckers(): … … 379 391 self.assertRaises(error.UnauthorizedLogin, r.getResult) 380 392 testNegative = dG(testNegative) 381 393 394 395 382 396 class HashlessFilePasswordDBMixin: 383 397 credClass = credentials.UsernamePassword 384 398 diskHash = None … … 389 403 ('user2', 'password2'), 390 404 ('user3', 'password3')] 391 405 406 392 407 def getGoodCredentials(self): 393 408 for u, p in self._validCredentials: 394 409 yield self.credClass(u, self.networkHash(p)), u 395 410 411 396 412 def getBadCredentials(self): 397 413 for u, p in [('user1', 'password3'), 398 414 ('user2', 'password1'), 399 415 ('bloof', 'blarf')]: 400 416 yield self.credClass(u, self.networkHash(p)) 401 417 418 402 419 def getCheckers(self): 403 420 diskHash = self.diskHash or (lambda x: x) 404 hashCheck = self.diskHash and (lambda username, password, stored: self.diskHash(password)) 421 hashCheck = self.diskHash and (lambda username, password, 422 stored: self.diskHash(password)) 405 423 406 424 for cache in True, False: 407 fn = self.mktemp() 408 fObj = file(fn, 'w') 409 for u, p in self._validCredentials: 410 fObj.write('%s:%s\n' % (u, diskHash(p))) 411 fObj.close() 412 yield checkers.FilePasswordDB(fn, cache=cache, hash=hashCheck) 413 414 fn = self.mktemp() 415 fObj = file(fn, 'w') 416 for u, p in self._validCredentials: 417 fObj.write('%s dingle dongle %s\n' % (diskHash(p), u)) 418 fObj.close() 419 yield checkers.FilePasswordDB(fn, ' ', 3, 0, cache=cache, hash=hashCheck) 420 421 fn = self.mktemp() 422 fObj = file(fn, 'w') 423 for u, p in self._validCredentials: 424 fObj.write('zip,zap,%s,zup,%s\n' % (u.title(), diskHash(p))) 425 fObj.close() 426 yield checkers.FilePasswordDB(fn, ',', 2, 4, False, cache=cache, hash=hashCheck) 425 fn = FilePath(self.mktemp()) 426 with fn.open('w') as fObj: 427 for u, p in self._validCredentials: 428 fObj.write('%s:%s\n' % (u, diskHash(p))) 429 yield checkers.FilePasswordDB(fn.path, cache=cache, hash=hashCheck) 430 431 fn = FilePath(self.mktemp()) 432 with fn.open('w') as fObj: 433 for u, p in self._validCredentials: 434 fObj.write('%s dingle dongle %s\n' % (diskHash(p), u)) 435 yield checkers.FilePasswordDB(fn.path, ' ', 3, 0, cache=cache, 436 hash=hashCheck) 437 438 fn = FilePath(self.mktemp()) 439 with fn.open('w') as fObj: 440 for u, p in self._validCredentials: 441 fObj.write('zip,zap,%s,zup,%s\n' % (u.title(), 442 diskHash(p))) 443 yield checkers.FilePasswordDB(fn.path, ',', 2, 4, False, 444 cache=cache, hash=hashCheck) 445 446 427 447 428 448 class LocallyHashedFilePasswordDBMixin(HashlessFilePasswordDBMixin): 429 449 diskHash = staticmethod(lambda x: x.encode('hex')) 430 450 451 452 431 453 class NetworkHashedFilePasswordDBMixin(HashlessFilePasswordDBMixin): 432 454 networkHash = staticmethod(lambda x: x.encode('hex')) 433 455 class credClass(credentials.UsernameHashedPassword): 434 456 def checkPassword(self, password): 435 457 return self.hashed.decode('hex') == password 436 458 437 class HashlessFilePasswordDBCheckerTestCase(HashlessFilePasswordDBMixin, CheckersMixin, unittest.TestCase): 459 460 461 class HashlessFilePasswordDBCheckerTestCase(HashlessFilePasswordDBMixin, 462 CheckersMixin, unittest.TestCase): 438 463 pass 439 464 440 class LocallyHashedFilePasswordDBCheckerTestCase(LocallyHashedFilePasswordDBMixin, CheckersMixin, unittest.TestCase): 465 466 467 class LocallyHashedFilePasswordDBCheckerTestCase(LocallyHashedFilePasswordDBMixin, 468 CheckersMixin, unittest.TestCase): 441 469 pass 442 470 443 class NetworkHashedFilePasswordDBCheckerTestCase(NetworkHashedFilePasswordDBMixin, CheckersMixin, unittest.TestCase): 471 472 473 class NetworkHashedFilePasswordDBCheckerTestCase(NetworkHashedFilePasswordDBMixin, 474 CheckersMixin, unittest.TestCase): 444 475 pass 445 476
