root/trunk/twisted/plugins/cred_memory.py

Revision 22257, 2.3 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 an in-memory user database.
8"""
9
10from zope.interface import implements
11
12from twisted import plugin
13from twisted.cred.strcred import ICheckerFactory
14from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
15from twisted.cred.credentials import IUsernamePassword, IUsernameHashedPassword
16
17
18
19inMemoryCheckerFactoryHelp = """
20A checker that uses an in-memory user database.
21
22This is only of use in one-off test programs or examples which
23don't want to focus too much on how credentials are verified. You
24really don't want to use this for anything else. It is a toy.
25"""
26
27
28
29class InMemoryCheckerFactory(object):
30    """
31    A factory for in-memory credentials checkers.
32
33    This is only of use in one-off test programs or examples which don't
34    want to focus too much on how credentials are verified.
35
36    You really don't want to use this for anything else.  It is, at best, a
37    toy.  If you need a simple credentials checker for a real application,
38    see L{cred_passwd.PasswdCheckerFactory}.
39    """
40    implements(ICheckerFactory, plugin.IPlugin)
41    authType = 'memory'
42    authHelp = inMemoryCheckerFactoryHelp
43    argStringFormat = 'A colon-separated list (name:password:...)'
44    credentialInterfaces = (IUsernamePassword,
45                            IUsernameHashedPassword)
46
47    def generateChecker(self, argstring):
48        """
49        This checker factory expects to get a list of
50        username:password pairs, with each pair also separated by a
51        colon. For example, the string 'alice:f:bob:g' would generate
52        two users, one named 'alice' and one named 'bob'.
53        """
54        checker = InMemoryUsernamePasswordDatabaseDontUse()
55        if argstring:
56            pieces = argstring.split(':')
57            if len(pieces) % 2:
58                from twisted.cred.strcred import InvalidAuthArgumentString
59                raise InvalidAuthArgumentString(
60                    "argstring must be in format U:P:...")
61            for i in range(0, len(pieces), 2):
62                username, password = pieces[i], pieces[i+1]
63                checker.addUser(username, password)
64        return checker
65
66
67
68theInMemoryCheckerFactory = InMemoryCheckerFactory()
Note: See TracBrowser for help on using the browser.