~dustin-spy/twisted/dustin

« back to all changes in this revision

Viewing changes to twisted/cred/authorizer.py

  • Committer: glyph
  • Date: 2002-01-27 23:10:25 UTC
  • Revision ID: vcs-imports@canonical.com-20020127231025-6180b0f76c6aba84
Moved twisted.internet.passport into twisted.cred.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
# Twisted Imports
 
3
from twisted.python import defer
 
4
 
 
5
 
 
6
class Authorizer:
 
7
    """An interface to a set of identities.
 
8
    """
 
9
    def setApplication(self, app):
 
10
        """Set the application for this authorizer.
 
11
        """
 
12
        self.application = app
 
13
    def addIdentity(self, identity):
 
14
        """Create an identity and make a callback when it has been created.
 
15
        """
 
16
        raise NotImplementedError()
 
17
 
 
18
    def removeIdentity(self, identityName):
 
19
        raise NotImplementedError()
 
20
 
 
21
    def getIdentityRequest(self, name):
 
22
        """Get an identity request, make the given callback when it's received.
 
23
 
 
24
        Override this to provide a method for retrieving identities than
 
25
        the hash provided by default. The method should return a Deferred.
 
26
 
 
27
        Note that this is asynchronous specifically to provide support
 
28
        for authenticating users from a database.
 
29
        """
 
30
        raise NotImplementedError("%s.getIdentityRequest"%str(self.__class__))
 
31
 
 
32
 
 
33
class DefaultAuthorizer(Authorizer):
 
34
    """I am an authorizer which requires no external dependencies.
 
35
 
 
36
    I am implemented as a hash of Identities.
 
37
    """
 
38
 
 
39
    def __init__(self):
 
40
        """Create a hash of identities.
 
41
        """
 
42
        self.identities = {}
 
43
 
 
44
    def addIdentity(self, identity):
 
45
        """Add an identity to me.
 
46
        """
 
47
        if self.identities.has_key(identity.name):
 
48
            raise KeyError("Already have an identity by that name.")
 
49
        self.identities[identity.name] = identity
 
50
 
 
51
    def removeIdentity(self, identityName):
 
52
        del self.identities[identityName]
 
53
 
 
54
    def getIdentityRequest(self, name):
 
55
        """Get a Deferred callback registration object.
 
56
 
 
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.
 
61
        """
 
62
 
 
63
        req = defer.Deferred()
 
64
        if self.identities.has_key(name):
 
65
            req.callback(self.identities[name])
 
66
        else:
 
67
            req.errback("unauthorized")
 
68
        return req
 
69