From 69066029f1e2a2b87d32fbd5be2a3d76f901cfa0 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 19 Nov 2016 16:29:53 -0800 Subject: [PATCH] Simplify/rewrite the login module --- coremods/login.py | 66 +++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/coremods/login.py b/coremods/login.py index e2a7487..94b69d0 100644 --- a/coremods/login.py +++ b/coremods/login.py @@ -1,57 +1,43 @@ """ -login.py - Implement login method +login.py - Implement core login abstraction """ from pylinkirc import conf, utils, world from pylinkirc.log import log from passlib.apps import custom_app_context as pwd_context -@utils.add_cmd -def login(user, password): - # synonymous to identify() - """ - login to PyLink Services""" - # XXX: First see if the user exists in the config +def checkLogin(user, password): + """Checks whether the given user and password is a valid combination.""" try: - passhash = conf.conf['login']['accounts'][user] - except KeyError: - return False - log.error("Account '%s' not found" % user) - # XXX: if so then see if the user provided username and password - # matches the one in the config. - if verifyhash(password, passhash): - return True - else: + passhash = conf.conf['login']['accounts'][user].get('password') + except KeyError: # Invalid combination return False -@utils.add_cmd -def mkpasswd(irc, source, args): - # synonymous to /mkpasswd so prospective admins - # can give their password without actually - # showing it outright. - """ - hashes a password for use in pylink.yml""" - # TODO: restrict to only certain users? - # XXX: do we allow this to be public or restrict it - # to a certain group of people. - password=None - try: - password = args[0] - except IndexError: - irc.error("Not enough arguments. (Needs 1, password)") - if password == None or password == "None": - # technically we shouldn't end up with this running - irc.error("password can not be empty") - - hashed_pass = pwd_context.encrypt("%s" % password) - if verifyhash(password, hashed_pass): - irc.reply(hashed_pass) + return verifyHash(password, passhash) -def verifyhash(password, passhash): +def verifyHash(password, passhash): + """Checks whether the password given matches the hash.""" if password: # ... good we have a password inputted # XXX: the greatest thing here is that the hash # is just a string either way, not a object with # a method to output the hash - return pwd_context.verify(password, passhash) + return pwd_context.verify(password, passhash) + return False + +@utils.add_cmd +def mkpasswd(irc, source, args): + """ + Hashes a password for use in the configuration file.""" + # TODO: restrict to only certain users? + try: + password = args[0] + except IndexError: + irc.error("Not enough arguments. (Needs 1, password)") + return + if not password: + irc.error("Password cannot be empty.") + + hashed_pass = pwd_context.encrypt(password) + irc.reply(hashed_pass, private=True)