feat: add generate secret token

This commit is contained in:
Project_IO 2024-09-12 11:34:09 +09:00
parent 0b4c55284d
commit 6f8fef42ac
3 changed files with 28 additions and 3 deletions

2
.gitignore vendored
View file

@ -6,6 +6,8 @@ __pycache__/
.env .env
!.env.example !.env.example
secret_token.txt
load.txt load.txt
.DS_Store .DS_Store

View file

@ -1,4 +1,4 @@
import psycopg2, os import psycopg2
import random, string import random, string
from getpass import getpass from getpass import getpass
from util.auth_lib import hash from util.auth_lib import hash
@ -6,8 +6,17 @@ from util.config import conn_param
from service.auth_service import AuthData, AuthService from service.auth_service import AuthData, AuthService
def gen_salt(length = 20): def gen_salt(length = 20):
letters = string.ascii_lowercase + string.digits + string.punctuation letters = string.ascii_lowercase + string.digits + string.punctuation
return ''.join(random.choice(letters) for i in range(length)) 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__(): def __main__():
conn = psycopg2.connect(conn_param) conn = psycopg2.connect(conn_param)
@ -15,10 +24,12 @@ def __main__():
try: try:
f = open("./load.txt", "r") f = open("./load.txt", "r")
_gen_token()
if f.read().split("=")[1] == "false": if f.read().split("=")[1] == "false":
raise ValueError("value not true") raise ValueError("value not true")
print("server already initialized") print("server already initialized")
f.close()
except: except:
cur.execute( cur.execute(
""" """
@ -78,4 +89,6 @@ def __main__():
f = open("load.txt", "w") f = open("load.txt", "w")
f.write("init=true") f.write("init=true")
f.close()
__main__() __main__()

View file

@ -3,6 +3,14 @@ from dotenv import load_dotenv
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" % ( conn_param = "host=%s port=%s dbname=%s user=%s password=%s" % (
os.getenv("DB_URL"), os.getenv("DB_URL"),
os.getenv("DB_PORT"), 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_USERNAME"),
os.getenv("DB_PASSWORD") os.getenv("DB_PASSWORD")
) )
secret = _load_secret()