2016-03-16 12:45:37 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import argparse
|
2018-10-20 03:16:55 +03:00
|
|
|
import getpass
|
2016-07-06 06:17:54 +03:00
|
|
|
import sys
|
2018-11-07 20:57:28 +03:00
|
|
|
import unicodedata
|
2016-07-06 06:17:54 +03:00
|
|
|
|
2016-03-16 12:45:37 +03:00
|
|
|
import bcrypt
|
2016-07-06 06:17:54 +03:00
|
|
|
import yaml
|
|
|
|
|
2018-11-07 20:57:28 +03:00
|
|
|
bcrypt_rounds = 12
|
2016-07-06 06:17:54 +03:00
|
|
|
password_pepper = ""
|
2016-03-16 12:45:37 +03:00
|
|
|
|
2018-11-07 20:57:28 +03:00
|
|
|
|
2016-03-16 12:45:37 +03:00
|
|
|
def prompt_for_pass():
|
|
|
|
password = getpass.getpass("Password: ")
|
|
|
|
|
|
|
|
if not password:
|
|
|
|
raise Exception("Password cannot be blank.")
|
|
|
|
|
|
|
|
confirm_password = getpass.getpass("Confirm password: ")
|
|
|
|
|
|
|
|
if password != confirm_password:
|
|
|
|
raise Exception("Passwords do not match.")
|
|
|
|
|
|
|
|
return password
|
|
|
|
|
2018-11-07 20:57:28 +03:00
|
|
|
|
2016-03-16 12:45:37 +03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(
|
2018-11-07 20:57:28 +03:00
|
|
|
description=(
|
|
|
|
"Calculate the hash of a new password, so that passwords can be reset"
|
|
|
|
)
|
|
|
|
)
|
2016-03-16 12:45:37 +03:00
|
|
|
parser.add_argument(
|
2018-11-07 20:57:28 +03:00
|
|
|
"-p",
|
|
|
|
"--password",
|
2016-03-16 12:45:37 +03:00
|
|
|
default=None,
|
|
|
|
help="New password for user. Will prompt if omitted.",
|
|
|
|
)
|
2016-07-06 06:17:54 +03:00
|
|
|
parser.add_argument(
|
2018-11-07 20:57:28 +03:00
|
|
|
"-c",
|
|
|
|
"--config",
|
2021-05-14 13:46:35 +03:00
|
|
|
type=argparse.FileType("r"),
|
2018-11-07 20:57:28 +03:00
|
|
|
help=(
|
|
|
|
"Path to server config file. "
|
|
|
|
"Used to read in bcrypt_rounds and password_pepper."
|
|
|
|
),
|
2016-07-06 06:17:54 +03:00
|
|
|
)
|
2016-03-16 12:45:37 +03:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
2016-07-06 06:17:54 +03:00
|
|
|
if "config" in args and args.config:
|
|
|
|
config = yaml.safe_load(args.config)
|
|
|
|
bcrypt_rounds = config.get("bcrypt_rounds", bcrypt_rounds)
|
2020-01-22 10:32:52 +03:00
|
|
|
password_config = config.get("password_config", None) or {}
|
2016-07-06 06:17:54 +03:00
|
|
|
password_pepper = password_config.get("pepper", password_pepper)
|
2016-03-16 12:45:37 +03:00
|
|
|
password = args.password
|
|
|
|
|
|
|
|
if not password:
|
|
|
|
password = prompt_for_pass()
|
|
|
|
|
2018-11-07 20:57:28 +03:00
|
|
|
# On Python 2, make sure we decode it to Unicode before we normalise it
|
|
|
|
if isinstance(password, bytes):
|
|
|
|
try:
|
|
|
|
password = password.decode(sys.stdin.encoding)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
print(
|
|
|
|
"ERROR! Your password is not decodable using your terminal encoding (%s)."
|
|
|
|
% (sys.stdin.encoding,)
|
|
|
|
)
|
|
|
|
|
|
|
|
pw = unicodedata.normalize("NFKC", password)
|
|
|
|
|
|
|
|
hashed = bcrypt.hashpw(
|
2021-05-14 13:46:35 +03:00
|
|
|
pw.encode("utf8") + password_pepper.encode("utf8"),
|
2018-11-07 20:57:28 +03:00
|
|
|
bcrypt.gensalt(bcrypt_rounds),
|
2021-05-14 13:46:35 +03:00
|
|
|
).decode("ascii")
|
2018-11-07 20:57:28 +03:00
|
|
|
|
|
|
|
print(hashed)
|