1 # -*- test-case-name: twisted.test.test_strcred -*-
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
7 Cred plugin for a file of the format 'username:password'.
12 from zope.interface import implements
14 from twisted import plugin
15 from twisted.cred.checkers import FilePasswordDB
16 from twisted.cred.strcred import ICheckerFactory
17 from twisted.cred.credentials import IUsernamePassword, IUsernameHashedPassword
21 fileCheckerFactoryHelp = """
22 This checker expects to receive the location of a file that
23 conforms to the FilePasswordDB format. Each line in the file
24 should be of the format 'username:password', in plain text.
27 invalidFileWarning = 'Warning: not a valid file'
31 class FileCheckerFactory(object):
33 A factory for instances of L{FilePasswordDB}.
35 implements(ICheckerFactory, plugin.IPlugin)
37 authHelp = fileCheckerFactoryHelp
38 argStringFormat = 'Location of a FilePasswordDB-formatted file.'
39 # Explicitly defined here because FilePasswordDB doesn't do it for us
40 credentialInterfaces = (IUsernamePassword, IUsernameHashedPassword)
42 errorOutput = sys.stderr
44 def generateChecker(self, argstring):
46 This checker factory expects to get the location of a file.
47 The file should conform to the format required by
48 L{FilePasswordDB} (using defaults for all
49 initialization parameters).
51 from twisted.python.filepath import FilePath
52 if not argstring.strip():
53 raise ValueError, '%r requires a filename' % self.authType
54 elif not FilePath(argstring).isfile():
55 self.errorOutput.write('%s: %s\n' % (invalidFileWarning, argstring))
56 return FilePasswordDB(argstring)
60 theFileCheckerFactory = FileCheckerFactory()