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
13 from MoinMoin import log
14 logging = log.getLogger(__name__)
16 class phpbb_login(BaseAuth):
17 logout_possible = True
18 login_inputs = ['username', 'password']
20 def __init__(self, name='phpbb', dbhost=None, dbuser=None, dbpass=None, dbname=None, dbport=None, hint=None):
22 Authenticate using credentials from a phpbb database
24 The name parameter should be unique among all authentication methods.
26 The hint parameter is a snippet of HTML that is displayed below the login form.
36 def check_login(self, request, username, password):
37 """ Checks the given username password combination. Returns the
38 corresponding emailaddress, or False if authentication failed.
40 conn = self.connect(request)
46 cursor = conn.cursor ()
47 cursor.execute ("SELECT user_password,user_email FROM lex_users WHERE username=%s", username)
50 if (cursor.rowcount == 0):
55 row = cursor.fetchone()
58 if (md5.new(password).hexdigest() == row[0]):
63 def connect(self, request):
64 # This code was shamelessly stolen from
65 # django.db.backends.mysql.base.cursor
71 kwargs['user'] = self.dbuser
73 kwargs['db'] = self.dbname
75 kwargs['passwd'] = self.dbpass
76 if self.dbhost.startswith('/'):
77 kwargs['unix_socket'] = self.dbhost
79 kwargs['host'] = self.dbhost
81 kwargs['port'] = int(self.dbport)
86 conn = MySQLdb.connect (**kwargs)
91 logging.error("phpbb_login: authentication failed due to exception connecting to DB, traceback follows...")
92 logging.error(''.join(traceback.format_exception(*info)))
97 def login(self, request, user_obj, **kw):
99 username = kw.get('username')
100 password = kw.get('password')
102 logging.debug("phpbb_login: Trying to log in, username=%r " % (username))
104 # simply continue if something else already logged in
106 if user_obj and user_obj.valid:
107 return ContinueLogin(user_obj)
109 # Deny empty username or passwords
110 if not username or not password:
111 return ContinueLogin(user_obj)
113 email = self.check_login(request, username, password)
117 logging.debug("phpbb_login: authentication failed for %s" % (username))
118 return ContinueLogin(user_obj)
120 logging.debug("phpbb_login: authenticated %s (email %s)" % (username, email))
122 u = user.User(request, auth_username=username, auth_method=self.name, auth_attribs=('name', 'password', 'email'))
124 #u.remember_me = 0 # 0 enforces cookie_lifetime config param
125 u.create_or_update(True)
127 return ContinueLogin(u)
131 info = sys.exc_info()
132 logging.error("phpbb_login: authentication failed due to unexpected exception, traceback follows...")
133 logging.error(''.join(traceback.format_exception(*info)))
134 return ContinueLogin(user_obj)
136 def login_hint(self, request):
137 """ Return a snippet of HTML that is displayed with the login form. """
140 # vim: set sw=4 expandtab sts=4:vim