~dustin-spy/twisted/dustin

« back to all changes in this revision

Viewing changes to twisted/cred/identity.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
# System Imports
 
3
import md5
 
4
 
 
5
# Twisted Imports
 
6
from twisted.python import defer
 
7
 
 
8
# Sibling Imports
 
9
from util import respond
 
10
from util import challenge
 
11
 
 
12
class Identity:
 
13
    """An identity, with different methods for verification.
 
14
 
 
15
    An identity represents a user's permissions with a particular
 
16
    application.  It is a username, a password, and a collection of
 
17
    Perspective/Service name pairs, each of which is a perspective
 
18
    that this identity is allowed to access.
 
19
    """
 
20
    hashedPassword = None
 
21
 
 
22
    def __init__(self, name, application):
 
23
        """Create an identity.
 
24
 
 
25
        I must have a name, and a backreference to the Application that the
 
26
        Keys on my keyring make reference to.
 
27
        """
 
28
        self.name = name
 
29
        self.application = application
 
30
        self.keyring = {}
 
31
 
 
32
    def addKeyForPerspective(self, perspective):
 
33
        """Add a key for the given perspective.
 
34
        """
 
35
        perspectiveName = perspective.getPerspectiveName()
 
36
        serviceName = perspective.service.getServiceName()
 
37
        self.addKeyByString(serviceName, perspectiveName)
 
38
 
 
39
    def addKeyByString(self, serviceName, perspectiveName):
 
40
        """Put a key on my keyring.
 
41
 
 
42
        This key will give me a token to access to some service in the
 
43
        future.
 
44
        """
 
45
        self.keyring[(serviceName, perspectiveName)] = 1
 
46
 
 
47
    def requestPerspectiveForService(self, serviceName):
 
48
        """Get the first available perspective for a given service.
 
49
        """
 
50
        keys = self.keyring.keys()
 
51
        keys.sort()
 
52
        for serviceN, perspectiveN in keys:
 
53
            if serviceN == serviceName:
 
54
                return self.requestPerspectiveForKey(serviceName, perspectiveN)
 
55
        return defer.fail("No such perspective.")
 
56
 
 
57
    def requestPerspectiveForKey(self, serviceName, perspectiveName):
 
58
        """Get a perspective request (a Deferred) for the given key.
 
59
 
 
60
        If this identity does not have access to the given (serviceName,
 
61
        perspectiveName) pair, I will raise KeyError.
 
62
        """
 
63
        try:
 
64
            check = self.keyring[(serviceName, perspectiveName)]
 
65
        except KeyError, ke:
 
66
            return defer.fail(failure.Failure())
 
67
        return self.application.getServiceNamed(serviceName).getPerspectiveRequest(perspectiveName)
 
68
 
 
69
    def getAllKeys(self):
 
70
        """Returns a list of all services and perspectives this identity can connect to.
 
71
 
 
72
        This returns a sequence of keys.
 
73
        """
 
74
        return self.keyring.keys()
 
75
 
 
76
    def removeKey(self, serviceName, perspectiveName):
 
77
        """Remove a key from my keyring.
 
78
 
 
79
        If this key is not present, raise KeyError.
 
80
        """
 
81
        del self.keyring[(serviceName, perspectiveName)]
 
82
 
 
83
    def setPassword(self, plaintext):
 
84
        if plaintext is None:
 
85
            self.hashedPassword = None
 
86
        else:
 
87
            self.hashedPassword = md5.new(plaintext).digest()
 
88
 
 
89
    def setAlreadyHashedPassword(self, cyphertext):
 
90
        """(legacy) Set a password for this identity, already md5 hashed.
 
91
        """
 
92
        self.hashedPassword = cyphertext
 
93
 
 
94
    def challenge(self):
 
95
        """I return some random data.
 
96
 
 
97
        This is a method in addition to the module-level function
 
98
        because it is anticipated that we will want to change this
 
99
        to store salted passwords.
 
100
        """
 
101
        return challenge()
 
102
 
 
103
    def verifyPassword(self, challenge, hashedPassword):
 
104
        """Verify a challenge/response password.
 
105
        """
 
106
        md = md5.new()
 
107
        md.update(self.hashedPassword)
 
108
        md.update(challenge)
 
109
        correct = md.digest()
 
110
        result = (hashedPassword == correct)
 
111
        return result
 
112
 
 
113
    def verifyPlainPassword(self, plaintext):
 
114
        """Verify plain text password.
 
115
 
 
116
        This is insecure, but necessary to support legacy protocols such
 
117
        as IRC, POP3, HTTP, etc.
 
118
        """
 
119
 
 
120
        md = md5.new()
 
121
        md.update(plaintext)
 
122
        userPass = md.digest()
 
123
        return (userPass == self.hashedPassword)
 
124
 
 
125
 
 
126
 
 
127
    # TODO: service discovery through listing of self.keyring.