From 2283d3867fddb301d024b455ea13991ff529df73 Mon Sep 17 00:00:00 2001 From: boyska Date: Fri, 20 May 2022 18:30:44 +0200 Subject: [PATCH] generate-password can be an external program --- contrib/generate_password.sh | 3 +++ tresetter.py | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100755 contrib/generate_password.sh diff --git a/contrib/generate_password.sh b/contrib/generate_password.sh new file mode 100755 index 0000000..7cdc275 --- /dev/null +++ b/contrib/generate_password.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +shuf -n4 /usr/share/dict/words | tr '\n' . | head -c -1 diff --git a/tresetter.py b/tresetter.py index 3092c5b..d530d17 100644 --- a/tresetter.py +++ b/tresetter.py @@ -4,7 +4,7 @@ import random import logging import json import uuid -from subprocess import Popen, CalledProcessError +from subprocess import Popen, CalledProcessError, check_output from typing import Optional import redis @@ -21,11 +21,15 @@ class Settings(BaseSettings): app_name: str = "tResetter" validate_login_exe: str change_password_exe: str + generate_password: Optional[str] redis_host: str = "localhost" expire_time: int = 60 * 20 root_path: Optional[str] root_prefix: str = "/tresetter" + class Config: + pass + @property def redis_params(self): return {"host": self.redis_host} @@ -125,9 +129,15 @@ def validate(username, password): return p.returncode == 0 -def password_generate(): - symbols = list(string.ascii_lowercase) + list(string.digits) - return "".join(random.choices(symbols, k=10)) +def password_generate() -> str: + if settings.generate_password: + s = check_output([settings.generate_password], encoding='utf8') + assert type(s) is str + return s.strip() + + else: + symbols = list(string.ascii_lowercase) + list(string.digits) + return "".join(random.choices(symbols, k=10)) def change_password(username: str, new_password: str) -> bool: