Ticket #5808: cred-open-5808.patch

File cred-open-5808.patch, 7.9 KB (added by thijs, 4 years ago)
  • twisted/cred/checkers.py

     
    193193
    194194    def _loadCredentials(self):
    195195        try:
    196             f = file(self.filename)
     196            f = open(self.filename)
    197197        except:
    198198            log.err()
    199199            raise error.UnauthorizedLogin()
  • twisted/test/test_newcred.py

     
    177177        ('user3', 'pass3'),
    178178    ]
    179179
    180 
    181     def testUserLookup(self):
     180    def test_userLookup(self):
    182181        dbfile = self.mktemp()
    183182        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()
     183        with open(dbfile, 'w') as f:
     184            for (u, p) in self.users:
     185                f.write('%s:%s\n' % (u, p))
    188186
    189187        for (u, p) in self.users:
    190188            self.failUnlessRaises(KeyError, db.getUser, u.upper())
    191189            self.assertEqual(db.getUser(u), (u, p))
    192190
    193     def testCaseInSensitivity(self):
     191
     192    def test_caseInSensitivity(self):
    194193        dbfile = self.mktemp()
    195194        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()
     195        with open(dbfile, 'w') as f:
     196            for (u, p) in self.users:
     197                f.write('%s:%s\n' % (u, p))
    200198
    201199        for (u, p) in self.users:
    202200            self.assertEqual(db.getUser(u.upper()), (u, p))
    203201
    204     def testRequestAvatarId(self):
     202
     203    def test_requestAvatarId(self):
    205204        dbfile = self.mktemp()
    206205        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()
     206        with open(dbfile, 'w') as f:
     207            for (u, p) in self.users:
     208                f.write('%s:%s\n' % (u, p))
     209
    211210        creds = [credentials.UsernamePassword(u, p) for u, p in self.users]
    212211        d = defer.gatherResults(
    213212            [defer.maybeDeferred(db.requestAvatarId, c) for c in creds])
    214213        d.addCallback(self.assertEqual, [u for u, p in self.users])
    215214        return d
    216215
    217     def testRequestAvatarId_hashed(self):
     216
     217    def test_requestAvatarId_hashed(self):
    218218        dbfile = self.mktemp()
    219219        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        with open(dbfile, 'w') as f:
     221            for (u, p) in self.users:
     222                f.write('%s:%s\n' % (u, p))
     223
    224224        creds = [credentials.UsernameHashedPassword(u, p) for u, p in self.users]
    225225        d = defer.gatherResults(
    226226            [defer.maybeDeferred(db.requestAvatarId, c) for c in creds])
     
    240240    def hash(self, u, p, s):
    241241        return crypt(p, s)
    242242
     243
    243244    def setUp(self):
    244245        dbfile = self.mktemp()
    245246        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()
     247        with open(dbfile, 'w') as f:
     248            for (u, p) in self.users:
     249                f.write('%s:%s\n' % (u, crypt(p, u[:2])))
    250250        r = TestRealm()
    251251        self.port = portal.Portal(r)
    252252        self.port.registerChecker(self.db)
    253253
    254     def testGoodCredentials(self):
     254
     255    def test_goodCredentials(self):
    255256        goodCreds = [credentials.UsernamePassword(u, p) for u, p in self.users]
    256257        d = defer.gatherResults([self.db.requestAvatarId(c) for c in goodCreds])
    257258        d.addCallback(self.assertEqual, [u for u, p in self.users])
    258259        return d
    259260
    260     def testGoodCredentials_login(self):
     261
     262    def test_goodCredentials_login(self):
    261263        goodCreds = [credentials.UsernamePassword(u, p) for u, p in self.users]
    262264        d = defer.gatherResults([self.port.login(c, None, ITestable)
    263265                                 for c in goodCreds])
     
    265267        d.addCallback(self.assertEqual, [u for u, p in self.users])
    266268        return d
    267269
    268     def testBadCredentials(self):
     270
     271    def test_badCredentials(self):
    269272        badCreds = [credentials.UsernamePassword(u, 'wrong password')
    270273                    for u, p in self.users]
    271274        d = defer.DeferredList([self.port.login(c, None, ITestable)
     
    273276        d.addCallback(self._assertFailures, error.UnauthorizedLogin)
    274277        return d
    275278
    276     def testHashedCredentials(self):
     279
     280    def test_hashedCredentials(self):
    277281        hashedCreds = [credentials.UsernameHashedPassword(u, crypt(p, u[:2]))
    278282                       for u, p in self.users]
    279283        d = defer.DeferredList([self.port.login(c, None, ITestable)
     
    281285        d.addCallback(self._assertFailures, error.UnhandledCredentials)
    282286        return d
    283287
     288
    284289    def _assertFailures(self, failures, *expectedFailures):
    285290        for flag, failure in failures:
    286291            self.assertEqual(flag, defer.FAILURE)
     
    290295    if crypt is None:
    291296        skip = "crypt module not available"
    292297
     298
     299
    293300class PluggableAuthenticationModulesTest(unittest.TestCase):
    294301
    295302    def setUp(self):
     
    362369    if not pamauth:
    363370        skip = "Can't run without PyPAM"
    364371
     372
     373
    365374class CheckersMixin:
    366375    def testPositive(self):
    367376        for chk in self.getCheckers():
     
    379388                self.assertRaises(error.UnauthorizedLogin, r.getResult)
    380389    testNegative = dG(testNegative)
    381390
     391
     392
    382393class HashlessFilePasswordDBMixin:
    383394    credClass = credentials.UsernamePassword
    384395    diskHash = None
     
    389400        ('user2', 'password2'),
    390401        ('user3', 'password3')]
    391402
     403
    392404    def getGoodCredentials(self):
    393405        for u, p in self._validCredentials:
    394406            yield self.credClass(u, self.networkHash(p)), u
    395407
     408
    396409    def getBadCredentials(self):
    397410        for u, p in [('user1', 'password3'),
    398411                     ('user2', 'password1'),
    399412                     ('bloof', 'blarf')]:
    400413            yield self.credClass(u, self.networkHash(p))
    401414
     415
    402416    def getCheckers(self):
    403417        diskHash = self.diskHash or (lambda x: x)
    404418        hashCheck = self.diskHash and (lambda username, password, stored: self.diskHash(password))
    405419
    406420        for cache in True, False:
    407421            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()
     422            with open(fn, 'w') as fObj:
     423                for u, p in self._validCredentials:
     424                    fObj.write('%s:%s\n' % (u, diskHash(p)))
    412425            yield checkers.FilePasswordDB(fn, cache=cache, hash=hashCheck)
    413426
    414427            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()
     428            with open(fn, 'w') as fObj:
     429                for u, p in self._validCredentials:
     430                    fObj.write('%s dingle dongle %s\n' % (diskHash(p), u))
    419431            yield checkers.FilePasswordDB(fn, ' ', 3, 0, cache=cache, hash=hashCheck)
    420432
    421433            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()
     434            with open(fn, 'w') as fObj:
     435                for u, p in self._validCredentials:
     436                    fObj.write('zip,zap,%s,zup,%s\n' % (u.title(), diskHash(p)))
    426437            yield checkers.FilePasswordDB(fn, ',', 2, 4, False, cache=cache, hash=hashCheck)
    427438
     439
     440
    428441class LocallyHashedFilePasswordDBMixin(HashlessFilePasswordDBMixin):
    429442    diskHash = staticmethod(lambda x: x.encode('hex'))
    430443