From 983f147e6487aa252c70cf47249fbce442a7ca05 Mon Sep 17 00:00:00 2001 From: Shamil K Muhammed <noteness@disroot.org> Date: Wed, 19 Apr 2017 13:03:10 +0530 Subject: [PATCH] Add exception handler Catches all `psycopg2` errors for now. And returns False if a error is caught. Not a permanent solution, but works for now. --- diaspora_auth_provider.py | 90 ++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/diaspora_auth_provider.py b/diaspora_auth_provider.py index d22b058..80befdf 100644 --- a/diaspora_auth_provider.py +++ b/diaspora_auth_provider.py @@ -23,24 +23,17 @@ import bcrypt import logging -__VERSION__ = "0.0.1" +__VERSION__ = "0.0.2" logger = logging.getLogger(__name__) class DiasporaAuthProvider: - __version__ = "0.0.1" + __version__ = "0.0.2" def __init__(self, config, account_handler): self.account_handler = account_handler self.config = config - self.connection = psycopg2.connect( - dbname=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 def check_password(self, user_id, password): @@ -49,41 +42,52 @@ class DiasporaAuthProvider: # user_id is @localpart:hs_bare. we only need the localpart. local_part = user_id.split(':', 1)[0][1:] logger.info("Checking if user {} exists.".format(local_part)) - with self.connection.cursor() as cursor: - yield threads.deferToThread( # Don't think this is needed, but w/e - cursor.execute, - "SELECT username, encrypted_password FROM users WHERE username=%s", - (local_part,) - ) - user = yield threads.deferToThread( - cursor.fetchone - ) - # check if the user exists. - if not user: - logger.info("User {} does not exist. Rejecting auth request".format(local_part)) + try: + with psycopg2.connect( + dbname=self.config.db_name, + user=self.config.db_username, + password=self.config.db_password, + host=self.config.db_host, + port=self.config.db_port + ) as connection: + with connection.cursor() as cursor: + yield threads.deferToThread( # Don't think this is needed, but w/e + cursor.execute, + "SELECT username, encrypted_password FROM users WHERE username=%s", + (local_part,) + ) + user = yield threads.deferToThread( + cursor.fetchone + ) + # check if the user exists. + if not user: + logger.info("User {} does not exist. Rejecting auth request".format(local_part)) + defer.returnValue(False) + logger.debug("User {} exists. Checking password".format(local_part)) + # user exists, check if the password is correct. + encrypted_password = user[1] + peppered_pass = "{}{}".format(password, self.config.pepper) + if not (bcrypt.hashpw(peppered_pass, encrypted_password) == encrypted_password): + logger.info("Password given for {} is wrong. Rejecting auth request.".format(local_part)) + defer.returnValue(False) + # Ok, user's password is correct. check if the user exists in the homeserver db. + # and create it if doesn't exist. + if (yield self.account_handler.check_user_exists(user_id)): + logger.info("User {} does exist in synapse db. Authentication complete".format(local_part)) + defer.returnValue(True) + # User not in synapse db. need to create it. + logger.info("User {} does not exist in synapse db. creating it.".format(local_part)) + user_id, access_token = ( + yield self.account_handler.register(localpart=local_part) + ) + 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)) defer.returnValue(False) - logger.debug("User {} exists. Checking password".format(local_part)) - # user exists, check if the password is correct. - encrypted_password = user[1] - peppered_pass = "{}{}".format(password, self.config.pepper) - if not (bcrypt.hashpw(peppered_pass, encrypted_password) == encrypted_password): - logger.info("Password given for {} is wrong. Rejecting auth request.".format(local_part)) - defer.returnValue(False) - # Ok, user's password is correct. check if the user exists in the homeserver db. - # and create it if doesn't exist. - if (yield self.account_handler.check_user_exists(user_id)): - logger.info("User {} does exist in synapse db. Authentication complete".format(local_part)) - defer.returnValue(True) - # User not in synapse db. need to create it. - logger.info("User {} does not exist in synapse db. creating it.".format(local_part)) - user_id, access_token = ( - yield self.account_handler.register(localpart=local_part) - ) - logger.info( - "Registration based on diaspora complete. UserID: {}.".format(user_id) - ) - logger.info("Confirming authentication request.") - defer.returnValue(True) @staticmethod def parse_config(config): -- GitLab