import MySQLdb
import md5
from MoinMoin import user
+from MoinMoin.auth import BaseAuth, ContinueLogin
-class mysql_login:
-
- def __init__(self, dbhost=None, dbuser=None, dbpass=None, dbname=None, dbport=None, method='mysql', verbose=False):
+class mysql_login(BaseAuth):
+ logout_possible = True
+ login_inputs = ['username', 'password']
+
+ def __init__(self, name='mysql', dbhost=None, dbuser=None, dbpass=None, dbname=None, dbport=None, verbose=False):
"""
Authenticate using credentials from a mysql database
"""
self.dbpass = dbpass
self.dbname = dbname
self.dbport = dbport
- self.method = method
+ self.name = name
def check_login(self, request, username, password):
""" Checks the given username password combination. Returns the
return conn
- def __call__(self, request, **kw):
+ def login(self, request, user_obj, **kw):
try:
- username = kw.get('name')
+ username = kw.get('username')
password = kw.get('password')
- login = kw.get('login')
- user_obj = kw.get('user_obj')
- if self.verbose: request.log("mysql_login: got name=%r login=%r" % (username, login))
-
- # Only handle login
- if not login:
- return user_obj, True
+ if self.verbose: request.log("mysql_login: Trying to log in, username=%r " % (username))
+
+ # simply continue if something else already logged in
+ # successfully
+ if user_obj and user_obj.valid:
+ return ContinueLogin(user_obj)
- # Deny empty passwords
- if not password:
- return None, False
+ # Deny empty username or passwords
+ if not username or not password:
+ return ContinueLogin(user_obj)
email = self.check_login(request, username, password)
# Login incorrect
if (not email):
if self.verbose: request.log("mysql_login: authentication failed for %s" % (username))
- return None, True
+ return ContinueLogin(user_obj)
if self.verbose: request.log("mysql_login: authenticated %s (email %s)" % (username, email))
- u = user.User(request, auth_username=username, auth_method=self.method, auth_attribs=('password', 'email', ))
+ u = user.User(request, auth_username=username, auth_method=self.name, auth_attribs=('password', 'email', ))
u.email = email
#u.remember_me = 0 # 0 enforces cookie_lifetime config param
u.create_or_update(True)
- return u, True # moin_session has to set the cookie
+ return ContinueLogin(u)
except:
import sys
import traceback
info = sys.exc_info()
request.log("mysql_login: authentication failed due to unexpected exception, traceback follows...")
request.log(''.join(traceback.format_exception(*info)))
- return None, False
+ return ContinueLogin(user_obj)
# vim: set sw=4 expandtab sts=4:vim