Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / plugins / cred_file.py
1 # -*- test-case-name: twisted.test.test_strcred -*-
2 #
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
5
6 """
7 Cred plugin for a file of the format 'username:password'.
8 """
9
10 import sys
11
12 from zope.interface import implements
13
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
18
19
20
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.
25 """
26
27 invalidFileWarning = 'Warning: not a valid file'
28
29
30
31 class FileCheckerFactory(object):
32     """
33     A factory for instances of L{FilePasswordDB}.
34     """
35     implements(ICheckerFactory, plugin.IPlugin)
36     authType = 'file'
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)
41
42     errorOutput = sys.stderr
43
44     def generateChecker(self, argstring):
45         """
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).
50         """
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)
57
58
59
60 theFileCheckerFactory = FileCheckerFactory()