1 # -*- coding: iso-8859-1 -*-
3 MoinMoin - auth plugin doing a check against MySQL db
5 @copyright: 2008 Matthijs Kooijman
6 @license: GNU GPL, see COPYING for details.
11 from MoinMoin import user
15 def __init__(self, dbhost=None, dbuser=None, dbpass=None, dbname=None, dbport=None, method='mysql', verbose=False):
17 Authenticate using credentials from a mysql database
19 self.verbose = verbose
27 def check_login(self, request, username, password):
28 """ Checks the given username password combination. Returns the
29 corresponding emailaddress, or False if authentication failed.
31 conn = self.connect(request)
37 cursor = conn.cursor ()
38 cursor.execute ("SELECT user_password,user_email FROM lex_users WHERE username=%s", username)
41 if (cursor.rowcount == 0):
46 row = cursor.fetchone()
49 if (md5.new(password).hexdigest() == row[0]):
54 def connect(self, request):
55 # This code was shamelessly stolen from
56 # django.db.backends.mysql.base.cursor
62 kwargs['user'] = self.dbuser
64 kwargs['db'] = self.dbname
66 kwargs['passwd'] = self.dbpass
67 if self.dbhost.startswith('/'):
68 kwargs['unix_socket'] = self.dbhost
70 kwargs['host'] = self.dbhost
72 kwargs['port'] = int(self.dbport)
77 conn = MySQLdb.connect (**kwargs)
82 request.log("mysql_login: authentication failed due to exception connecting to DB, traceback follows...")
83 request.log(''.join(traceback.format_exception(*info)))
88 def __call__(self, request, **kw):
90 username = kw.get('name')
91 password = kw.get('password')
92 login = kw.get('login')
93 user_obj = kw.get('user_obj')
95 if self.verbose: request.log("mysql_login: got name=%r login=%r" % (username, login))
101 # Deny empty passwords
105 email = self.check_login(request, username, password)
109 if self.verbose: request.log("mysql_login: authentication failed for %s" % (username))
112 if self.verbose: request.log("mysql_login: authenticated %s (email %s)" % (username, email))
114 u = user.User(request, auth_username=username, auth_method=self.method, auth_attribs=('password', 'email', ))
116 #u.remember_me = 0 # 0 enforces cookie_lifetime config param
117 u.create_or_update(True)
119 return u, True # moin_session has to set the cookie
123 info = sys.exc_info()
124 request.log("mysql_login: authentication failed due to unexpected exception, traceback follows...")
125 request.log(''.join(traceback.format_exception(*info)))
128 # vim: set sw=4 expandtab sts=4:vim