X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=roundup%2Fpassword.py;h=14dbcda0da9338193fbd2b128dbf0881be899778;hb=255fa1ce653b104f8e79b05c6565ceb8d0a0c185;hp=0ba2c5e39f3c7d3b37ffa83c95297e27d2679907;hpb=ef066b9357a8d67c168564b5ff38ac5e84b4e270;p=roundup.git diff --git a/roundup/password.py b/roundup/password.py index 0ba2c5e..14dbcda 100644 --- a/roundup/password.py +++ b/roundup/password.py @@ -15,22 +15,28 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: password.py,v 1.6 2002-09-26 07:39:21 richard Exp $ +# $Id: password.py,v 1.12 2004-03-22 07:45:39 richard Exp $ -__doc__ = """ -Password handling (encoding, decoding). +"""Password handling (encoding, decoding). """ +__docformat__ = 'restructuredtext' -import sha, re, string +import sha, re, string, random try: - import crypt: + import crypt except: crypt = None pass +class PasswordValueError(ValueError): + ''' The password value is not valid ''' + pass + def encodePassword(plaintext, scheme, other=None): '''Encrypt the plaintext password. ''' + if plaintext is None: + plaintext = "" if scheme == 'SHA': s = sha.sha(plaintext).hexdigest() elif scheme == 'crypt' and crypt is not None: @@ -43,9 +49,13 @@ def encodePassword(plaintext, scheme, other=None): elif scheme == 'plaintext': s = plaintext else: - raise ValueError, 'Unknown encryption scheme "%s"'%scheme + raise PasswordValueError, 'unknown encryption scheme %r'%scheme return s +def generatePassword(length=8): + chars = string.letters+string.digits + return ''.join([random.choice(chars) for x in range(length)]) + class Password: '''The class encapsulates a Password property type value in the database. @@ -72,13 +82,15 @@ class Password: default_scheme = 'SHA' # new encryptions use this scheme pwre = re.compile(r'{(\w+)}(.+)') - def __init__(self, plaintext=None, scheme=None): + def __init__(self, plaintext=None, scheme=None, encrypted=None): '''Call setPassword if plaintext is not None.''' if scheme is None: scheme = self.default_scheme if plaintext is not None: self.password = encodePassword(plaintext, self.default_scheme) self.scheme = self.default_scheme + elif encrypted is not None: + self.unpack(encrypted) else: self.password = None self.scheme = self.default_scheme