Skip to content
Snippets Groups Projects
Unverified Commit e42cc814 authored by Shamil K Muhammed's avatar Shamil K Muhammed
Browse files

Support MySQL database

A new engine variable is added to support switching of the
database engine. Currently supports postgresql and mysql databases.

This should be enough as mysql and postgres are the only 2 database
engines supported by diaspora*.

Closes #1
parent c68c47e9
Branches
Tags
No related merge requests found
...@@ -18,22 +18,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -18,22 +18,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from twisted.internet import defer, threads from twisted.internet import defer, threads
import psycopg2
import bcrypt import bcrypt
import logging import logging
__VERSION__ = "0.0.3" __VERSION__ = "0.0.4"
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class DiasporaAuthProvider: class DiasporaAuthProvider:
__version__ = "0.0.3" __version__ = "0.0.4"
def __init__(self, config, account_handler): def __init__(self, config, account_handler):
self.account_handler = account_handler self.account_handler = account_handler
self.config = config self.config = config
if self.config.engine == "mysql":
import pymysql
self.module = pymysql
elif self.config.engine == 'postgres':
import psycopg2
self.module = psycopg2
self.connection = self.module.connect(
database=self.config.db_name,
user=self.config.db_username,
password=self.config.db_password,
host=self.config.db_host,
port=self.config.db_port
)
@defer.inlineCallbacks @defer.inlineCallbacks
def check_password(self, user_id, password): def check_password(self, user_id, password):
...@@ -43,50 +55,43 @@ class DiasporaAuthProvider: ...@@ -43,50 +55,43 @@ class DiasporaAuthProvider:
local_part = user_id.split(':', 1)[0][1:] local_part = user_id.split(':', 1)[0][1:]
logger.info("Checking if user {} exists.".format(local_part)) logger.info("Checking if user {} exists.".format(local_part))
try: try:
with psycopg2.connect( with self.connection.cursor() as cursor:
dbname=self.config.db_name, yield threads.deferToThread( # Don't think this is needed, but w/e
user=self.config.db_username, cursor.execute,
password=self.config.db_password, "SELECT username, encrypted_password FROM users WHERE username=%s",
host=self.config.db_host, (local_part,)
port=self.config.db_port )
) as connection: user = yield threads.deferToThread(
with connection.cursor() as cursor: cursor.fetchone
yield threads.deferToThread( # Don't think this is needed, but w/e )
cursor.execute, # check if the user exists.
"SELECT username, encrypted_password FROM users WHERE username=%s", if not user:
(local_part,) logger.info("User {} does not exist. Rejecting auth request".format(local_part))
) defer.returnValue(False)
user = yield threads.deferToThread( logger.debug("User {} exists. Checking password".format(local_part))
cursor.fetchone # user exists, check if the password is correct.
) encrypted_password = user[1]
# check if the user exists. peppered_pass = "{}{}".format(password, self.config.pepper)
if not user: if not (bcrypt.hashpw(peppered_pass.encode('utf8'), encrypted_password.encode('utf8'))
logger.info("User {} does not exist. Rejecting auth request".format(local_part)) == encrypted_password.encode('utf8')):
defer.returnValue(False) logger.info("Password given for {} is wrong. Rejecting auth request.".format(local_part))
logger.debug("User {} exists. Checking password".format(local_part)) defer.returnValue(False)
# user exists, check if the password is correct. # Ok, user's password is correct. check if the user exists in the homeserver db.
encrypted_password = user[1] # and create it if doesn't exist.
peppered_pass = "{}{}".format(password, self.config.pepper) if (yield self.account_handler.check_user_exists(user_id)):
if not (bcrypt.hashpw(peppered_pass.encode('utf8'), encrypted_password.encode('utf8')) \ logger.info("User {} does exist in synapse db. Authentication complete".format(local_part))
== encrypted_password.encode('utf8')): defer.returnValue(True)
logger.info("Password given for {} is wrong. Rejecting auth request.".format(local_part)) # User not in synapse db. need to create it.
defer.returnValue(False) logger.info("User {} does not exist in synapse db. creating it.".format(local_part))
# Ok, user's password is correct. check if the user exists in the homeserver db. user_id, access_token = (
# and create it if doesn't exist. yield self.account_handler.register(localpart=local_part)
if (yield self.account_handler.check_user_exists(user_id)): )
logger.info("User {} does exist in synapse db. Authentication complete".format(local_part)) logger.info(
defer.returnValue(True) "Registration based on diaspora complete. UserID: {}.".format(user_id)
# User not in synapse db. need to create it. )
logger.info("User {} does not exist in synapse db. creating it.".format(local_part)) logger.info("Confirming authentication request.")
user_id, access_token = ( defer.returnValue(True)
yield self.account_handler.register(localpart=local_part) except self.module.Error as e:
)
logger.info(
"Registration based on diaspora complete. UserID: {}.".format(user_id)
)
logger.info("Confirming authentication request.")
defer.returnValue(True)
except psycopg2.Error as e:
logger.warning("Error during diaspora authentication: {}".format(e)) logger.warning("Error during diaspora authentication: {}".format(e))
defer.returnValue(False) defer.returnValue(False)
...@@ -95,6 +100,7 @@ class DiasporaAuthProvider: ...@@ -95,6 +100,7 @@ class DiasporaAuthProvider:
class _Conf: class _Conf:
pass pass
Conf = _Conf() Conf = _Conf()
Conf.engine = config['database']['engine']
Conf.db_name = "diaspora_production" if not config['database']['name'] else config['database']['name'] Conf.db_name = "diaspora_production" if not config['database']['name'] else config['database']['name']
Conf.db_host = config['database']['host'] Conf.db_host = config['database']['host']
Conf.db_port = config['database']['port'] Conf.db_port = config['database']['port']
......
...@@ -46,7 +46,7 @@ setup( ...@@ -46,7 +46,7 @@ setup(
version=exec_file("diaspora_auth_provider.py", "__VERSION__"), version=exec_file("diaspora_auth_provider.py", "__VERSION__"),
py_modules=["diaspora_auth_provider"], py_modules=["diaspora_auth_provider"],
description="An LDAP3 auth provider for Synapse", description="A Diaspora* auth provider for Synapse",
install_requires=[ install_requires=[
"Twisted>=15.1.0", "Twisted>=15.1.0",
"psycopg2", "psycopg2",
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment