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

Source Code for Module repoze.who.plugins.sql

  1  from zope.interface import implements 
  2   
  3  from repoze.who.interfaces import IAuthenticator 
  4  from repoze.who.interfaces import IMetadataProvider 
  5   
6 -def default_password_compare(cleartext_password, stored_password_hash):
7 try: 8 from hashlib import sha1 9 except ImportError: # Python < 2.5 #pragma NO COVERAGE 10 from sha import new as sha1 #pragma NO COVERAGE 11 12 # the stored password is stored as '{SHA}<SHA hexdigest>'. 13 # or as a cleartext password (no {SHA} prefix) 14 15 if stored_password_hash.startswith('{SHA}'): 16 stored_password_hash = stored_password_hash[5:] 17 digest = sha1(cleartext_password).hexdigest() 18 else: 19 digest = cleartext_password 20 21 if stored_password_hash == digest: 22 return True 23 24 return False
25
26 -def make_psycopg_conn_factory(**kw):
27 # convenience (I always seem to use Postgres) 28 def conn_factory(): #pragma NO COVERAGE 29 import psycopg2 #pragma NO COVERAGE 30 return psycopg2.connect(kw['repoze.who.dsn']) #pragma NO COVERAGE
31 return conn_factory #pragma NO COVERAGE 32
33 -class SQLAuthenticatorPlugin:
34 implements(IAuthenticator) 35
36 - def __init__(self, query, conn_factory, compare_fn):
37 # statement should be pyformat dbapi binding-style, e.g. 38 # "select user_id, password from users where login=%(login)s" 39 self.query = query 40 self.conn_factory = conn_factory 41 self.compare_fn = compare_fn or default_password_compare 42 self.conn = None
43 44 # IAuthenticator
45 - def authenticate(self, environ, identity):
46 if not 'login' in identity: 47 return None 48 if not self.conn: 49 self.conn = self.conn_factory() 50 curs = self.conn.cursor() 51 curs.execute(self.query, identity) 52 result = curs.fetchone() 53 curs.close() 54 if result: 55 user_id, password = result 56 if self.compare_fn(identity['password'], password): 57 return user_id
58
59 -class SQLMetadataProviderPlugin:
60 implements(IMetadataProvider) 61
62 - def __init__(self, name, query, conn_factory, filter):
63 self.name = name 64 self.query = query 65 self.conn_factory = conn_factory 66 self.filter = filter 67 self.conn = None
68 69 # IMetadataProvider
70 - def add_metadata(self, environ, identity):
71 if self.conn is None: 72 self.conn = self.conn_factory() 73 curs = self.conn.cursor() 74 # can't use dots in names in python string formatting :-( 75 identity['__userid'] = identity['repoze.who.userid'] 76 curs.execute(self.query, identity) 77 result = curs.fetchall() 78 if self.filter: 79 result = self.filter(result) 80 curs.close() 81 del identity['__userid'] 82 identity[self.name] = result
83
84 -def make_authenticator_plugin(query=None, conn_factory=None, 85 compare_fn=None, **kw):
86 from repoze.who.utils import resolveDotted 87 if query is None: 88 raise ValueError('query must be specified') 89 if conn_factory is None: 90 raise ValueError('conn_factory must be specified') 91 try: 92 conn_factory = resolveDotted(conn_factory)(**kw) 93 except Exception, why: 94 raise ValueError('conn_factory could not be resolved: %s' % why) 95 if compare_fn is not None: 96 compare_fn = resolveDotted(compare_fn) 97 return SQLAuthenticatorPlugin(query, conn_factory, compare_fn)
98
99 -def make_metadata_plugin(name=None, query=None, conn_factory=None, 100 filter=None, **kw):
101 from repoze.who.utils import resolveDotted 102 if name is None: 103 raise ValueError('name must be specified') 104 if query is None: 105 raise ValueError('query must be specified') 106 if conn_factory is None: 107 raise ValueError('conn_factory must be specified') 108 try: 109 conn_factory = resolveDotted(conn_factory)(**kw) 110 except Exception, why: 111 raise ValueError('conn_factory could not be resolved: %s' % why) 112 if filter is not None: 113 filter = resolveDotted(filter) 114 return SQLMetadataProviderPlugin(name, query, conn_factory, filter)
115