Changeset 2120

Show
Ignore:
Timestamp:
03/24/07 17:23:07 (2 years ago)
Author:
mgood
Message:

AccountManagerPlugin:

add a SessionStore class for storing user passwords as a session attribute

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • accountmanagerplugin/0.10/acct_mgr/htfile.py

    r2063 r2120  
    11# -*- coding: utf8 -*- 
    22# 
    3 # Copyright (C) 2005 Matthew Good <trac@matt-good.net> 
     3# Copyright (C) 2005,2006,2007 Matthew Good <trac@matt-good.net> 
    44# 
    55# "THE BEER-WARE LICENSE" (Revision 42): 
     
    1212from __future__ import generators 
    1313 
    14 from binascii import hexlify 
    1514import errno 
    16 import md5, sha 
    1715import os.path 
    1816import fileinput 
    19 from md5crypt import md5crypt 
    2017 
    2118from trac.core import * 
     
    2320 
    2421from api import IPasswordStore 
    25  
    26 # check for the availability of the "crypt" module for checking passwords on 
    27 # Unix-like platforms 
    28 # MD5 is still used when adding/updating passwords 
    29 try: 
    30     from crypt import crypt 
    31 except ImportError: 
    32     crypt = None 
    33  
    34 # os.urandom was added in Python 2.4 
    35 # try to fall back on reading from /dev/urandom on older Python versions 
    36 try: 
    37     from os import urandom 
    38 except ImportError: 
    39     from random import randrange 
    40     def urandom(n): 
    41         return ''.join([chr(randrange(256)) for _ in xrange(n)]) 
    42  
     22from pwhash import htpasswd, htdigest 
    4323 
    4424class _RelativePathOption(Option): 
     
    132112 
    133113 
    134 def salt(): 
    135     s = '' 
    136     v = long(hexlify(urandom(4)), 16) 
    137     itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' 
    138     for i in range(8): 
    139         s += itoa64[v & 0x3f]; v >>= 6 
    140     return s 
    141  
    142  
    143114class HtPasswdStore(AbstractPasswordFileStore): 
    144115    """Manages user accounts stored in Apache's htpasswd format. 
     
    162133 
    163134    def userline(self, user, password): 
    164         if crypt is None: 
    165             return self.prefix(user) + md5crypt(password, salt(), '$apr1$') 
    166         else: 
    167             return self.prefix(user) + crypt(password, salt()) 
     135        return self.prefix(user) + htpasswd(password) 
    168136 
    169137    def _check_userline(self, password, prefix, suffix): 
    170         if suffix.startswith('$apr1$'): 
    171             return suffix == md5crypt(password, suffix[6:].split('$')[0], 
    172                                       '$apr1$') 
    173         elif suffix.startswith('{SHA}'): 
    174             return (suffix[5:] == 
    175                     sha.new(password).digest().encode('base64')[:-1]) 
    176         elif crypt is None: 
    177             # crypt passwords are only supported on Unix-like systems 
    178             raise NotImplementedError('The "crypt" module is unavailable ' 
    179                                       'on this platform.  Only MD5 ' 
    180                                       'passwords (starting with "$apr1$") ' 
    181                                       'are supported in the htpasswd file.') 
    182         else: 
    183             return suffix == crypt(password, suffix) 
     138        return suffix == htpasswd(password, suffix) 
    184139 
    185140    def _get_users(self, filename): 
     
    216171 
    217172    def userline(self, user, password): 
    218         p = self.prefix(user) 
    219         return p + md5.new(p + password).hexdigest() 
     173        return self.prefix(user) + htdigest(user, self.realm, password) 
    220174 
    221175    def _check_userline(self, password, prefix, suffix): 
    222         return suffix == md5.new(prefix + password).hexdigest(
     176        return suffix == htdigest(user, self.realm, password
    223177 
    224178    def _get_users(self, filename): 
  • accountmanagerplugin/0.10/acct_mgr/tests/__init__.py

    r1128 r2120  
    1414 
    1515def suite(): 
    16     from acct_mgr.tests import htfile 
     16    from acct_mgr.tests import htfile, db 
    1717    suite = unittest.TestSuite() 
    1818    suite.addTest(htfile.suite()) 
     19    suite.addTest(db.suite()) 
    1920    return suite 
    2021 
  • accountmanagerplugin/0.10/setup.py

    r1549 r2120  
    3232            'acct_mgr.api = acct_mgr.api', 
    3333            'acct_mgr.admin = acct_mgr.admin', 
     34            'acct_mgr.db = acct_mgr.db', 
     35            'acct_mgr.pwhash = acct_mgr.pwhash', 
    3436        ] 
    3537    }, 
  • accountmanagerplugin/trunk/acct_mgr/htfile.py

    r2068 r2120  
    11# -*- coding: utf8 -*- 
    22# 
    3 # Copyright (C) 2005 Matthew Good <trac@matt-good.net> 
     3# Copyright (C) 2005,2006,2007 Matthew Good <trac@matt-good.net> 
    44# 
    55# "THE BEER-WARE LICENSE" (Revision 42): 
     
    1010# Author: Matthew Good <trac@matt-good.net> 
    1111 
    12 from binascii import hexlify 
    1312import errno 
    14 import md5, sha 
    1513import os.path 
    1614import fileinput 
    17 from md5crypt import md5crypt 
    1815 
    1916from trac.core import * 
     
    2118 
    2219from api import IPasswordStore 
     20from pwhash import htpasswd, htdigest 
    2321from util import EnvRelativePathOption 
    24  
    25 # check for the availability of the "crypt" module for checking passwords on 
    26 # Unix-like platforms 
    27 # MD5 is still used when adding/updating passwords 
    28 try: 
    29     from crypt import crypt 
    30 except ImportError: 
    31     crypt = None 
    32  
    33 # os.urandom was added in Python 2.4 
    34 # try to fall back on reading from /dev/urandom on older Python versions 
    35 try: 
    36     from os import urandom 
    37 except ImportError: 
    38     from random import randrange 
    39     def urandom(n): 
    40         return ''.join([chr(randrange(256)) for _ in xrange(n)]) 
    4122 
    4223 
     
    122103 
    123104 
    124 def salt(): 
    125     s = '' 
    126     v = long(hexlify(urandom(4)), 16) 
    127     itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' 
    128     for i in range(8): 
    129         s += itoa64[v & 0x3f]; v >>= 6 
    130     return s 
    131  
    132  
    133105class HtPasswdStore(AbstractPasswordFileStore): 
    134106    """Manages user accounts stored in Apache's htpasswd format. 
     
    152124 
    153125    def userline(self, user, password): 
    154         if crypt is None: 
    155             return self.prefix(user) + md5crypt(password, salt(), '$apr1$') 
    156         else: 
    157             return self.prefix(user) + crypt(password, salt()) 
     126        return self.prefix(user) + htpasswd(password) 
    158127 
    159128    def _check_userline(self, password, prefix, suffix): 
    160         if suffix.startswith('$apr1$'): 
    161             return suffix == md5crypt(password, suffix[6:].split('$')[0], 
    162                                       '$apr1$') 
    163         elif suffix.startswith('{SHA}'): 
    164             return (suffix[5:] == 
    165                     sha.new(password).digest().encode('base64')[:-1]) 
    166         elif crypt is None: 
    167             # crypt passwords are only supported on Unix-like systems 
    168             raise NotImplementedError('The "crypt" module is unavailable ' 
    169                                       'on this platform.  Only MD5 ' 
    170                                       'passwords (starting with "$apr1$") ' 
    171                                       'are supported in the htpasswd file.') 
    172         else: 
    173             return suffix == crypt(password, suffix) 
     129        return suffix == htpasswd(password, suffix) 
    174130 
    175131    def _get_users(self, filename): 
     
    206162 
    207163    def userline(self, user, password): 
    208         p = self.prefix(user) 
    209         return p + md5.new(p + password).hexdigest() 
     164        return self.prefix(user) + htdigest(user, self.realm, password) 
    210165 
    211166    def _check_userline(self, password, prefix, suffix): 
    212         return suffix == md5.new(prefix + password).hexdigest(
     167        return suffix == htdigest(user, self.realm, password
    213168 
    214169    def _get_users(self, filename): 
  • accountmanagerplugin/trunk/acct_mgr/tests/__init__.py

    r1128 r2120  
    1414 
    1515def suite(): 
    16     from acct_mgr.tests import htfile 
     16    from acct_mgr.tests import htfile, db 
    1717    suite = unittest.TestSuite() 
    1818    suite.addTest(htfile.suite()) 
     19    suite.addTest(db.suite()) 
    1920    return suite 
    2021 
  • accountmanagerplugin/trunk/setup.py

    r2068 r2120  
    3030            'acct_mgr.admin = acct_mgr.admin', 
    3131            'acct_mgr.api = acct_mgr.api', 
     32            'acct_mgr.db = acct_mgr.db', 
    3233            'acct_mgr.htfile = acct_mgr.htfile', 
    3334            'acct_mgr.http = acct_mgr.http', 
     35            'acct_mgr.pwhash = acct_mgr.pwhash', 
    3436            'acct_mgr.svnserve = acct_mgr.svnserve', 
    3537            'acct_mgr.web_ui = acct_mgr.web_ui',