root/trunk/twisted/plugins/cred_file.py

Revision 22257, 1.8 KB (checked in by therve, 3 years ago)

Merge checker-2570-2

Authors: mesozoic, therve
Reviewers: glyph, exarkun
Fixes #2570

Add the possibility to twistd plugins to use cred checkers, via
twisted.cred.strcred.AuthOptionMixin. This allows twistd plugins to accept
checker via command-line arguments, and also offer a way to create other
pluggable checkers. The only plugin modified for now is the words one.

Line 
1# -*- test-case-name: twisted.test.test_strcred -*-
2#
3# Copyright (c) 2007-2008 Twisted Matrix Laboratories.
4# See LICENSE for details.
5
6"""
7Cred plugin for a file of the format 'username:password'.
8"""
9
10import sys
11
12from zope.interface import implements
13
14from twisted import plugin
15from twisted.cred.checkers import FilePasswordDB
16from twisted.cred.strcred import ICheckerFactory
17from twisted.cred.credentials import IUsernamePassword, IUsernameHashedPassword
18
19
20
21fileCheckerFactoryHelp = """
22This checker expects to receive the location of a file that
23conforms to the FilePasswordDB format. Each line in the file
24should be of the format 'username:password', in plain text.
25"""
26
27invalidFileWarning = 'Warning: not a valid file'
28
29
30
31class 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
60theFileCheckerFactory = FileCheckerFactory()
Note: See TracBrowser for help on using the browser.