3
from twisted.python import defer
7
"""An interface to a set of identities.
9
def setApplication(self, app):
10
"""Set the application for this authorizer.
12
self.application = app
13
def addIdentity(self, identity):
14
"""Create an identity and make a callback when it has been created.
16
raise NotImplementedError()
18
def removeIdentity(self, identityName):
19
raise NotImplementedError()
21
def getIdentityRequest(self, name):
22
"""Get an identity request, make the given callback when it's received.
24
Override this to provide a method for retrieving identities than
25
the hash provided by default. The method should return a Deferred.
27
Note that this is asynchronous specifically to provide support
28
for authenticating users from a database.
30
raise NotImplementedError("%s.getIdentityRequest"%str(self.__class__))
33
class DefaultAuthorizer(Authorizer):
34
"""I am an authorizer which requires no external dependencies.
36
I am implemented as a hash of Identities.
40
"""Create a hash of identities.
44
def addIdentity(self, identity):
45
"""Add an identity to me.
47
if self.identities.has_key(identity.name):
48
raise KeyError("Already have an identity by that name.")
49
self.identities[identity.name] = identity
51
def removeIdentity(self, identityName):
52
del self.identities[identityName]
54
def getIdentityRequest(self, name):
55
"""Get a Deferred callback registration object.
57
I return a deferred (twisted.python.defer.Deferred) which will
58
be called back to when an identity is discovered to be available
59
(or errback for unavailable). It will be returned unarmed, so
60
you must arm it yourself.
63
req = defer.Deferred()
64
if self.identities.has_key(name):
65
req.callback(self.identities[name])
67
req.errback("unauthorized")