2016-11-20 01:29:39 +01:00
|
|
|
"""
|
2016-11-20 01:29:53 +01:00
|
|
|
login.py - Implement core login abstraction
|
2016-11-20 01:29:39 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
from pylinkirc import conf, utils, world
|
|
|
|
from pylinkirc.log import log
|
|
|
|
from passlib.apps import custom_app_context as pwd_context
|
|
|
|
|
2016-11-20 01:29:53 +01:00
|
|
|
def checkLogin(user, password):
|
|
|
|
"""Checks whether the given user and password is a valid combination."""
|
2016-11-20 01:29:39 +01:00
|
|
|
try:
|
2016-11-20 01:29:53 +01:00
|
|
|
passhash = conf.conf['login']['accounts'][user].get('password')
|
|
|
|
except KeyError: # Invalid combination
|
2016-11-20 01:29:39 +01:00
|
|
|
return False
|
|
|
|
|
2016-11-20 01:29:53 +01:00
|
|
|
return 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 False
|
|
|
|
|
|
|
|
|
2016-11-20 01:29:39 +01:00
|
|
|
@utils.add_cmd
|
|
|
|
def mkpasswd(irc, source, args):
|
|
|
|
"""<password>
|
2016-11-20 01:29:53 +01:00
|
|
|
Hashes a password for use in the configuration file."""
|
2016-11-20 01:29:39 +01:00
|
|
|
# TODO: restrict to only certain users?
|
|
|
|
try:
|
|
|
|
password = args[0]
|
|
|
|
except IndexError:
|
|
|
|
irc.error("Not enough arguments. (Needs 1, password)")
|
2016-11-20 01:29:53 +01:00
|
|
|
return
|
|
|
|
if not password:
|
|
|
|
irc.error("Password cannot be empty.")
|
2016-11-20 01:29:39 +01:00
|
|
|
|
2016-11-20 01:29:53 +01:00
|
|
|
hashed_pass = pwd_context.encrypt(password)
|
|
|
|
irc.reply(hashed_pass, private=True)
|