* Add mysql/phpbb authentication plugin.
authorMatthijs Kooijman <matthijs@stdio.flexvps.nl>
Fri, 6 Jun 2008 21:31:59 +0000 (23:31 +0200)
committerMatthijs Kooijman <matthijs@stdio.flexvps.nl>
Fri, 6 Jun 2008 21:31:59 +0000 (23:31 +0200)
conf/auth/__init__.py [new file with mode: 0644]
conf/auth/mysql_login.py [new file with mode: 0644]

diff --git a/conf/auth/__init__.py b/conf/auth/__init__.py
new file mode 100644 (file)
index 0000000..e4ed3b6
--- /dev/null
@@ -0,0 +1,5 @@
+# -*- coding: iso-8859-1 -*-
+
+from MoinMoin.util import pysupport
+
+modules = pysupport.getPackageModules(__file__)
diff --git a/conf/auth/mysql_login.py b/conf/auth/mysql_login.py
new file mode 100644 (file)
index 0000000..6dae938
--- /dev/null
@@ -0,0 +1,129 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - auth plugin doing a check against MySQL db
+
+    @copyright: 2008 Matthijs Kooijman
+    @license: GNU GPL, see COPYING for details.
+"""
+
+import MySQLdb
+import md5
+from MoinMoin import user
+
+class mysql_login:
+
+    def __init__(self, dbhost=None, dbuser=None, dbpass=None, dbname=None, dbport=None, method='mysql', verbose=False):
+        """
+            Authenticate using credentials from a mysql database
+        """
+        self.verbose = verbose
+        self.dbhost  = dbhost
+        self.dbuser  = dbuser
+        self.dbpass  = dbpass
+        self.dbname  = dbname
+        self.dbport  = dbport
+        self.method  = method
+    
+    def check_login(self, request, username, password):
+        """ Checks the given username password combination. Returns the
+        corresponding emailaddress, or False if authentication failed.
+        """
+        conn = self.connect(request)
+
+        if not conn:
+            return False
+
+        # Get some data
+        cursor = conn.cursor ()
+        cursor.execute ("SELECT user_password,user_email FROM lex_users WHERE username=%s", username)
+
+        # No data? No login.
+        if (cursor.rowcount == 0):
+            conn.close()
+            return False
+       
+        # Check password
+        row = cursor.fetchone()
+        conn.close()
+
+        if (md5.new(password).hexdigest() == row[0]):
+            return row[1]
+        else:
+            return False
+
+    def connect(self, request):
+        # This code was shamelessly stolen from
+        # django.db.backends.mysql.base.cursor
+        kwargs = {
+            'charset': 'utf8',
+            'use_unicode': False,
+        }
+        if self.dbuser:
+            kwargs['user'] = self.dbuser
+        if self.dbname:
+            kwargs['db'] = self.dbname
+        if self.dbpass:
+            kwargs['passwd'] = self.dbpass
+        if self.dbhost.startswith('/'):
+            kwargs['unix_socket'] = self.dbhost
+        elif self.dbhost:
+            kwargs['host'] = self.dbhost
+        if self.dbport:
+            kwargs['port'] = int(self.dbport)
+
+        # End stolen code
+
+        try:
+            conn = MySQLdb.connect (**kwargs)
+        except:
+            import sys
+            import traceback
+            info = sys.exc_info()
+            request.log("mysql_login: authentication failed due to exception connecting to DB, traceback follows...")
+            request.log(''.join(traceback.format_exception(*info)))
+            return False
+
+        return conn
+
+    def __call__(self, request, **kw):
+        try:
+            username = kw.get('name')
+            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
+
+            # Deny empty passwords
+            if not password:
+                return None, False
+
+            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
+
+            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.email = email
+            #u.remember_me = 0 # 0 enforces cookie_lifetime config param
+            u.create_or_update(True)
+            request.log(u.__repr__())
+
+            return u, True # moin_session has to set the cookie
+        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
+
+# vim: set sw=4 expandtab sts=4:vim