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
12 from MoinMoin.auth import BaseAuth, ContinueLogin
14 class mysql_login(BaseAuth):
15 logout_possible = True
16 login_inputs = ['username', 'password']
18 def __init__(self, name='mysql', dbhost=None, dbuser=None, dbpass=None, dbname=None, dbport=None, verbose=False):
20 Authenticate using credentials from a mysql database
22 self.verbose = verbose
30 def check_login(self, request, username, password):
31 """ Checks the given username password combination. Returns the
32 corresponding emailaddress, or False if authentication failed.
34 conn = self.connect(request)
40 cursor = conn.cursor ()
41 cursor.execute ("SELECT user_password,user_email FROM lex_users WHERE username=%s", username)
44 if (cursor.rowcount == 0):
49 row = cursor.fetchone()
52 if (md5.new(password).hexdigest() == row[0]):
57 def connect(self, request):
58 # This code was shamelessly stolen from
59 # django.db.backends.mysql.base.cursor
65 kwargs['user'] = self.dbuser
67 kwargs['db'] = self.dbname
69 kwargs['passwd'] = self.dbpass
70 if self.dbhost.startswith('/'):
71 kwargs['unix_socket'] = self.dbhost
73 kwargs['host'] = self.dbhost
75 kwargs['port'] = int(self.dbport)
80 conn = MySQLdb.connect (**kwargs)
85 request.log("mysql_login: authentication failed due to exception connecting to DB, traceback follows...")
86 request.log(''.join(traceback.format_exception(*info)))
91 def login(self, request, user_obj, **kw):
93 username = kw.get('username')
94 password = kw.get('password')
96 if self.verbose: request.log("mysql_login: Trying to log in, username=%r " % (username))
98 # simply continue if something else already logged in
100 if user_obj and user_obj.valid:
101 return ContinueLogin(user_obj)
103 # Deny empty username or passwords
104 if not username or not password:
105 return ContinueLogin(user_obj)
107 email = self.check_login(request, username, password)
111 if self.verbose: request.log("mysql_login: authentication failed for %s" % (username))
112 return ContinueLogin(user_obj)
114 if self.verbose: request.log("mysql_login: authenticated %s (email %s)" % (username, email))
116 u = user.User(request, auth_username=username, auth_method=self.name, auth_attribs=('password', 'email', ))
118 #u.remember_me = 0 # 0 enforces cookie_lifetime config param
119 u.create_or_update(True)
121 return ContinueLogin(u)
125 info = sys.exc_info()
126 request.log("mysql_login: authentication failed due to unexpected exception, traceback follows...")
127 request.log(''.join(traceback.format_exception(*info)))
128 return ContinueLogin(user_obj)
130 # vim: set sw=4 expandtab sts=4:vim