comparison roundup/password.py @ 270:a4241ddd22d7

Added the Password property type. See "pydoc roundup.password" for implementation details. Have updated some of the documentation too.
author Richard Jones <richard@users.sourceforge.net>
date Tue, 09 Oct 2001 07:25:59 +0000
parents
children 1cc866cec608
comparison
equal deleted inserted replaced
269:82cfd78f3c4e 270:a4241ddd22d7
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 # $Id: password.py,v 1.1 2001-10-09 07:25:59 richard Exp $
19
20 import sha, re
21
22 def encodePassword(plaintext, scheme):
23 '''Encrypt the plaintext password.
24 '''
25 if scheme == 'SHA':
26 s = sha.sha(plaintext).hexdigest()
27 elif scheme == 'plaintext':
28 pass
29 else:
30 raise ValueError, 'Unknown encryption scheme "%s"'%scheme
31 return s
32
33 class Password:
34 '''The class encapsulates a Password property type value in the database.
35
36 The encoding of the password is one if None, 'SHA' or 'plaintext'. The
37 encodePassword function is used to actually encode the password from
38 plaintext. The None encoding is used in legacy databases where no
39 encoding scheme is identified.
40
41 The scheme is stored with the encoded data in the database:
42 {scheme}data
43
44 Example usage:
45 >>> p = Password('sekrit')
46 >>> p == 'sekrit'
47 1
48 >>> p != 'not sekrit'
49 1
50 >>> 'sekrit' == p
51 1
52 >>> 'not sekrit' != p
53 1
54 '''
55
56 default_scheme = 'SHA' # new encryptions use this scheme
57 pwre = re.compile(r'{(\w+)}(.+)')
58
59 def __init__(self, plaintext=None):
60 '''Call setPassword if plaintext is not None.'''
61 if plaintext is not None:
62 self.password = encodePassword(plaintext, self.default_scheme)
63 self.scheme = self.default_scheme
64 else:
65 self.password = None
66 self.scheme = self.default_scheme
67
68 def unpack(self, encrypted):
69 '''Set the password info from the scheme:<encryted info> string
70 (the inverse of __str__)
71 '''
72 m = self.pwre.match(encrypted)
73 if m:
74 self.scheme = m.group(1)
75 self.password = m.group(2)
76 else:
77 # currently plaintext - encrypt
78 self.password = encodePassword(plaintext, self.default_scheme)
79 self.scheme = self.default_scheme
80
81 def setPassword(self, plaintext):
82 '''Sets encrypts plaintext.'''
83 self.password = encodePassword(plaintext, self.scheme)
84
85 def __cmp__(self, plaintext):
86 '''Compare this password against the plaintext.'''
87 if self.password is None:
88 raise ValueError, 'Password not set'
89 return cmp(self.password, encodePassword(plaintext, self.scheme))
90
91 def __str__(self):
92 '''Stringify the encrypted password for database storage.'''
93 if self.password is None:
94 raise ValueError, 'Password not set'
95 return '{%s}%s'%(self.scheme, self.password)
96
97 def test():
98 p = Password('sekrit')
99 assert p == 'sekrit'
100 assert p != 'not sekrit'
101 assert 'sekrit' == p
102 assert 'not sekrit' != p
103
104 if __name__ == '__main__':
105 test()
106
107 #
108 # $Log: not supported by cvs2svn $
109 #
110 #
111 # vim: set filetype=python ts=4 sw=4 et si

Roundup Issue Tracker: http://roundup-tracker.org/