Package repoze :: Package who :: Package plugins :: Module htpasswd
[hide private]

Source Code for Module repoze.who.plugins.htpasswd

 1  from zope.interface import implements 
 2   
 3  from repoze.who.interfaces import IAuthenticator 
 4  from repoze.who.utils import resolveDotted 
 5   
6 -class HTPasswdPlugin(object):
7 8 implements(IAuthenticator) 9
10 - def __init__(self, filename, check):
11 self.filename = filename 12 self.check = check
13 14 # IAuthenticatorPlugin
15 - def authenticate(self, environ, identity):
16 try: 17 login = identity['login'] 18 password = identity['password'] 19 except KeyError: 20 return None 21 22 if hasattr(self.filename, 'seek'): 23 # assumed to have a readline 24 self.filename.seek(0) 25 f = self.filename 26 else: 27 try: 28 f = open(self.filename, 'r') 29 except IOError: 30 environ['repoze.who.logger'].warn('could not open htpasswd ' 31 'file %s' % self.filename) 32 return None 33 34 for line in f: 35 try: 36 username, hashed = line.rstrip().split(':', 1) 37 except ValueError: 38 continue 39 if username == login: 40 if self.check(password, hashed): 41 return username 42 return None
43
44 - def __repr__(self):
45 return '<%s %s>' % (self.__class__.__name__, 46 id(self)) #pragma NO COVERAGE
47
48 -def crypt_check(password, hashed):
49 from crypt import crypt 50 salt = hashed[:2] 51 return hashed == crypt(password, salt)
52
53 -def plain_check(password, hashed):
54 return hashed == password
55
56 -def make_plugin(filename=None, check_fn=None):
57 if filename is None: 58 raise ValueError('filename must be specified') 59 if check_fn is None: 60 raise ValueError('check_fn must be specified') 61 check = resolveDotted(check_fn) 62 return HTPasswdPlugin(filename, check)
63