root/trunk/twisted/words/tap.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.words.test.test_tap -*-
2# Copyright (c) 2001-2005 Twisted Matrix Laboratories.
3# See LICENSE for details.
4"""
5Shiny new words service maker
6"""
7
8import sys, socket
9
10from twisted.application import strports
11from twisted.application.service import MultiService
12from twisted.python import usage
13from twisted import plugin
14
15from twisted.words import iwords, service
16from twisted.cred import checkers, credentials, portal, strcred
17
18class Options(usage.Options, strcred.AuthOptionMixin):
19    supportedInterfaces = [credentials.IUsernamePassword]
20    optParameters = [
21        ('hostname', None, socket.gethostname(),
22         'Name of this server; purely an informative')]
23
24    interfacePlugins = {}
25    plg = None
26    for plg in plugin.getPlugins(iwords.IProtocolPlugin):
27        assert plg.name not in interfacePlugins
28        interfacePlugins[plg.name] = plg
29        optParameters.append((
30            plg.name + '-port',
31            None, None,
32            'strports description of the port to bind for the  ' + plg.name + ' server'))
33    del plg
34
35    def __init__(self, *a, **kw):
36        usage.Options.__init__(self, *a, **kw)
37        self['groups'] = []
38
39    def opt_group(self, name):
40        """Specify a group which should exist
41        """
42        self['groups'].append(name.decode(sys.stdin.encoding))
43
44    def opt_passwd(self, filename):
45        """
46        Name of a passwd-style file. (This is for
47        backwards-compatibility only; you should use the --auth
48        command instead.)
49        """
50        self.addChecker(checkers.FilePasswordDB(filename))
51
52def makeService(config):
53    credCheckers = config.get('credCheckers', [])
54    wordsRealm = service.InMemoryWordsRealm(config['hostname'])
55    wordsPortal = portal.Portal(wordsRealm, credCheckers)
56
57    msvc = MultiService()
58
59    # XXX Attribute lookup on config is kind of bad - hrm.
60    for plgName in config.interfacePlugins:
61        port = config.get(plgName + '-port')
62        if port is not None:
63            factory = config.interfacePlugins[plgName].getFactory(wordsRealm, wordsPortal)
64            svc = strports.service(port, factory)
65            svc.setServiceParent(msvc)
66
67    # This is bogus.  createGroup is async.  makeService must be
68    # allowed to return a Deferred or some crap.
69    for g in config['groups']:
70        wordsRealm.createGroup(g)
71
72    return msvc
Note: See TracBrowser for help on using the browser.