From 6f8fef42ac26acfa7ad304ac7ce81e39f3a94456 Mon Sep 17 00:00:00 2001 From: Project_IO Date: Thu, 12 Sep 2024 11:34:09 +0900 Subject: [PATCH] feat: add generate secret token --- .gitignore | 2 ++ generate.py | 19 ++++++++++++++++--- util/config.py | 10 ++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index beb137b..6a82ba1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ __pycache__/ .env !.env.example +secret_token.txt + load.txt .DS_Store diff --git a/generate.py b/generate.py index 185be4d..b35ccfd 100644 --- a/generate.py +++ b/generate.py @@ -1,4 +1,4 @@ -import psycopg2, os +import psycopg2 import random, string from getpass import getpass from util.auth_lib import hash @@ -6,8 +6,17 @@ from util.config import conn_param from service.auth_service import AuthData, AuthService def gen_salt(length = 20): - letters = string.ascii_lowercase + string.digits + string.punctuation - return ''.join(random.choice(letters) for i in range(length)) + letters = string.ascii_lowercase + string.digits + string.punctuation + return "".join(random.choice(letters) for i in range(length)) + +def _gen_token(): + deps = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation + token = "".join(random.choice(deps) for i in range(20)) + + sec = open("./secret_token.txt", "w") + sec.write(token) + + sec.close() def __main__(): conn = psycopg2.connect(conn_param) @@ -15,10 +24,12 @@ def __main__(): try: f = open("./load.txt", "r") + _gen_token() if f.read().split("=")[1] == "false": raise ValueError("value not true") print("server already initialized") + f.close() except: cur.execute( """ @@ -78,4 +89,6 @@ def __main__(): f = open("load.txt", "w") f.write("init=true") + f.close() + __main__() diff --git a/util/config.py b/util/config.py index c00e94f..19f0e5f 100644 --- a/util/config.py +++ b/util/config.py @@ -3,6 +3,14 @@ from dotenv import load_dotenv load_dotenv() +def _load_secret(): + try: + tok = open("./secret_token.txt", "r").read() + except: + return "" + + return tok + conn_param = "host=%s port=%s dbname=%s user=%s password=%s" % ( os.getenv("DB_URL"), os.getenv("DB_PORT"), @@ -10,3 +18,5 @@ conn_param = "host=%s port=%s dbname=%s user=%s password=%s" % ( os.getenv("DB_USERNAME"), os.getenv("DB_PASSWORD") ) + +secret = _load_secret()