mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-27 04:59:24 +01:00
core: make passlib an optional dependency
This commit is contained in:
parent
354a3022a4
commit
fd12a5d919
@ -8,12 +8,16 @@ from pylinkirc.log import log
|
||||
try:
|
||||
from passlib.context import CryptContext
|
||||
except ImportError:
|
||||
raise ImportError("PyLink requires passlib to function; please install it and try again.")
|
||||
CryptContext = None
|
||||
log.warning("Hashed passwords are disabled because passlib is not installed. Please install "
|
||||
"it (pip3 install passlib) and restart for this feature to work.")
|
||||
|
||||
pwd_context = CryptContext(["sha512_crypt", "sha256_crypt"],
|
||||
all__vary_rounds=0.1,
|
||||
sha256_crypt__default_rounds=180000,
|
||||
sha512_crypt__default_rounds=90000)
|
||||
pwd_context = None
|
||||
if CryptContext:
|
||||
pwd_context = CryptContext(["sha512_crypt", "sha256_crypt"],
|
||||
all__vary_rounds=0.1,
|
||||
sha256_crypt__default_rounds=180000,
|
||||
sha512_crypt__default_rounds=90000)
|
||||
|
||||
def checkLogin(user, password):
|
||||
"""Checks whether the given user and password is a valid combination."""
|
||||
@ -48,13 +52,12 @@ def checkLogin(user, password):
|
||||
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
|
||||
if not pwd_context:
|
||||
raise utils.NotAuthorizedError("Cannot log in to an account with a hashed password "
|
||||
"because passlib is not installed.")
|
||||
|
||||
return pwd_context.verify(password, passhash)
|
||||
return False # No password given!
|
||||
|
||||
@utils.add_cmd
|
||||
def mkpasswd(irc, source, args):
|
||||
@ -70,5 +73,9 @@ def mkpasswd(irc, source, args):
|
||||
irc.error("Password cannot be empty.")
|
||||
return
|
||||
|
||||
if not pwd_context:
|
||||
irc.error("Password encryption is not available (missing passlib)")
|
||||
return
|
||||
|
||||
hashed_pass = pwd_context.encrypt(password)
|
||||
irc.reply(hashed_pass, private=True)
|
||||
|
@ -3,7 +3,6 @@
|
||||
Password hashing utility for PyLink IRC Services.
|
||||
"""
|
||||
|
||||
#import passlib
|
||||
from pylinkirc.coremods.login import pwd_context
|
||||
|
||||
if __name__ == '__main__':
|
||||
@ -13,5 +12,6 @@ if __name__ == '__main__':
|
||||
parser.add_argument('password', help='specifies the password to hash')
|
||||
args = parser.parse_args()
|
||||
|
||||
assert pwd_context, 'Cannot hash passwords because passlib is missing! Install it via "pip3 install passlib".'
|
||||
assert args.password, "Password cannot be empty!"
|
||||
print(pwd_context.encrypt(args.password))
|
||||
|
Loading…
Reference in New Issue
Block a user