1
# Copyright (c) 2008 Divmod. See LICENSE for details.
4
An AMP server which requires authentication of its clients before exposing an
10
from twisted.python.log import startLogging, msg
11
from twisted.internet import reactor
12
from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
13
from twisted.cred.portal import Portal
14
from twisted.protocols.amp import IBoxReceiver, Command, Integer, AMP
16
from epsilon.ampauth import CredAMPServerFactory
21
An example of an application-defined command which should be made available
22
to clients after they successfully authenticate.
24
arguments = [("left", Integer()),
27
response = [("sum", Integer())]
33
An example of an application-defined AMP protocol, the responders defined
34
by which should only be available to clients after they have successfully
37
def __init__(self, avatarId):
39
self.avatarId = avatarId
43
def add(self, left, right):
44
msg("Adding %d to %d for %s" % (left, right, self.avatarId))
45
return {'sum': left + right}
49
class AdditionRealm(object):
51
An example of an application-defined realm.
53
def requestAvatar(self, avatarId, mind, *interfaces):
55
Create Adder avatars for any IBoxReceiver request.
57
if IBoxReceiver in interfaces:
58
return (IBoxReceiver, Adder(avatarId), lambda: None)
59
raise NotImplementedError()
65
Start the AMP server and the reactor.
68
checker = InMemoryUsernamePasswordDatabaseDontUse()
69
checker.addUser("testuser", "examplepass")
70
realm = AdditionRealm()
71
factory = CredAMPServerFactory(Portal(realm, [checker]))
72
reactor.listenTCP(7805, factory)
76
if __name__ == '__main__':