mirror of
https://github.com/jlu5/PyLink.git
synced 2025-10-15 07:57:21 +02: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:
|
try:
|
||||||
from passlib.context import CryptContext
|
from passlib.context import CryptContext
|
||||||
except ImportError:
|
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"],
|
pwd_context = None
|
||||||
all__vary_rounds=0.1,
|
if CryptContext:
|
||||||
sha256_crypt__default_rounds=180000,
|
pwd_context = CryptContext(["sha512_crypt", "sha256_crypt"],
|
||||||
sha512_crypt__default_rounds=90000)
|
all__vary_rounds=0.1,
|
||||||
|
sha256_crypt__default_rounds=180000,
|
||||||
|
sha512_crypt__default_rounds=90000)
|
||||||
|
|
||||||
def checkLogin(user, password):
|
def checkLogin(user, password):
|
||||||
"""Checks whether the given user and password is a valid combination."""
|
"""Checks whether the given user and password is a valid combination."""
|
||||||
@ -48,13 +52,12 @@ def checkLogin(user, password):
|
|||||||
def verifyHash(password, passhash):
|
def verifyHash(password, passhash):
|
||||||
"""Checks whether the password given matches the hash."""
|
"""Checks whether the password given matches the hash."""
|
||||||
if password:
|
if password:
|
||||||
# ... good we have a password inputted
|
if not pwd_context:
|
||||||
# XXX: the greatest thing here is that the hash
|
raise utils.NotAuthorizedError("Cannot log in to an account with a hashed password "
|
||||||
# is just a string either way, not a object with
|
"because passlib is not installed.")
|
||||||
# a method to output the hash
|
|
||||||
return pwd_context.verify(password, passhash)
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
return pwd_context.verify(password, passhash)
|
||||||
|
return False # No password given!
|
||||||
|
|
||||||
@utils.add_cmd
|
@utils.add_cmd
|
||||||
def mkpasswd(irc, source, args):
|
def mkpasswd(irc, source, args):
|
||||||
@ -70,5 +73,9 @@ def mkpasswd(irc, source, args):
|
|||||||
irc.error("Password cannot be empty.")
|
irc.error("Password cannot be empty.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if not pwd_context:
|
||||||
|
irc.error("Password encryption is not available (missing passlib)")
|
||||||
|
return
|
||||||
|
|
||||||
hashed_pass = pwd_context.encrypt(password)
|
hashed_pass = pwd_context.encrypt(password)
|
||||||
irc.reply(hashed_pass, private=True)
|
irc.reply(hashed_pass, private=True)
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
Password hashing utility for PyLink IRC Services.
|
Password hashing utility for PyLink IRC Services.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#import passlib
|
|
||||||
from pylinkirc.coremods.login import pwd_context
|
from pylinkirc.coremods.login import pwd_context
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
@ -13,5 +12,6 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument('password', help='specifies the password to hash')
|
parser.add_argument('password', help='specifies the password to hash')
|
||||||
args = parser.parse_args()
|
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!"
|
assert args.password, "Password cannot be empty!"
|
||||||
print(pwd_context.encrypt(args.password))
|
print(pwd_context.encrypt(args.password))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user