2024-09-12 02:34:09 +00:00
|
|
|
import psycopg2
|
2024-09-10 08:08:06 +00:00
|
|
|
import random, string
|
2024-09-10 07:36:03 +00:00
|
|
|
from getpass import getpass
|
2024-09-10 08:08:06 +00:00
|
|
|
from util.auth_lib import hash
|
2024-09-10 04:14:09 +00:00
|
|
|
from util.config import conn_param
|
2024-09-10 08:08:06 +00:00
|
|
|
from service.auth_service import AuthData, AuthService
|
|
|
|
|
|
|
|
def gen_salt(length = 20):
|
2024-09-12 02:34:09 +00:00
|
|
|
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()
|
2024-09-10 04:14:09 +00:00
|
|
|
|
|
|
|
def __main__():
|
|
|
|
conn = psycopg2.connect(conn_param)
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
2024-09-10 07:36:03 +00:00
|
|
|
try:
|
2024-09-10 08:08:06 +00:00
|
|
|
f = open("./load.txt", "r")
|
2024-09-12 02:34:09 +00:00
|
|
|
_gen_token()
|
2024-09-10 08:08:06 +00:00
|
|
|
if f.read().split("=")[1] == "false":
|
|
|
|
raise ValueError("value not true")
|
|
|
|
|
2024-09-10 07:36:03 +00:00
|
|
|
print("server already initialized")
|
2024-09-12 02:34:09 +00:00
|
|
|
f.close()
|
2024-09-10 07:36:03 +00:00
|
|
|
except:
|
|
|
|
cur.execute(
|
|
|
|
"""
|
2024-09-10 08:08:06 +00:00
|
|
|
create table if not exists account(
|
2024-09-10 07:36:03 +00:00
|
|
|
name varchar(25),
|
2024-09-10 08:08:06 +00:00
|
|
|
username varchar(25) not null,
|
|
|
|
password varchar(100) not null,
|
2024-09-10 07:36:03 +00:00
|
|
|
salt varchar(50),
|
|
|
|
primary key(username)
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
)
|
2024-09-10 05:20:11 +00:00
|
|
|
|
2024-09-10 07:36:03 +00:00
|
|
|
cur.execute(
|
|
|
|
"""
|
2024-09-10 08:08:06 +00:00
|
|
|
create table if not exists balset(
|
2024-09-10 07:36:03 +00:00
|
|
|
id serial primary key,
|
|
|
|
uid varchar(25) not null,
|
|
|
|
name varchar(50),
|
|
|
|
date bigint,
|
|
|
|
price bigint,
|
|
|
|
buy boolean,
|
|
|
|
memo varchar(300),
|
|
|
|
constraint FK_Account_ID
|
|
|
|
foreign key (uid)
|
|
|
|
references account(username)
|
|
|
|
on delete CASCADE
|
|
|
|
);
|
|
|
|
"""
|
|
|
|
)
|
2024-09-10 04:14:09 +00:00
|
|
|
|
2024-09-10 07:36:03 +00:00
|
|
|
conn.commit()
|
|
|
|
|
2024-09-10 08:08:06 +00:00
|
|
|
cur.close()
|
|
|
|
conn.close()
|
|
|
|
|
2024-09-10 07:36:03 +00:00
|
|
|
name = input("input your display name: ")
|
|
|
|
username = input("input your username: ")
|
|
|
|
password = getpass("input your password: ")
|
|
|
|
passchk = getpass("type password one more time: ")
|
2024-09-10 08:08:06 +00:00
|
|
|
salt = gen_salt()
|
|
|
|
|
|
|
|
if password != passchk:
|
|
|
|
return
|
|
|
|
|
|
|
|
hashed_password = hash(password, salt)
|
|
|
|
packed = AuthData(
|
|
|
|
name=name,
|
|
|
|
username=username,
|
|
|
|
password=hashed_password,
|
|
|
|
salt=salt
|
|
|
|
)
|
|
|
|
|
|
|
|
service = AuthService()
|
|
|
|
service.create(data=packed)
|
2024-09-10 07:36:03 +00:00
|
|
|
|
|
|
|
f = open("load.txt", "w")
|
|
|
|
f.write("init=true")
|
2024-09-10 04:14:09 +00:00
|
|
|
|
2024-09-12 02:34:09 +00:00
|
|
|
f.close()
|
|
|
|
|
2024-09-10 04:14:09 +00:00
|
|
|
__main__()
|