mirror of
https://github.com/devproje/balance-application.git
synced 2024-10-20 15:11:21 +00:00
feat: middle save
This commit is contained in:
parent
38c22e4512
commit
4220b7f8e0
5 changed files with 65 additions and 15 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -6,4 +6,6 @@ __pycache__/
|
||||||
.env
|
.env
|
||||||
!.env.example
|
!.env.example
|
||||||
|
|
||||||
|
load.txt
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
40
generate.py
40
generate.py
|
@ -1,21 +1,31 @@
|
||||||
import psycopg2, os
|
import psycopg2, os
|
||||||
|
import random, string
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
|
from util.auth_lib import hash
|
||||||
from util.config import conn_param
|
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))
|
||||||
|
|
||||||
def __main__():
|
def __main__():
|
||||||
conn = psycopg2.connect(conn_param)
|
conn = psycopg2.connect(conn_param)
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
open("./load.txt", "r")
|
f = open("./load.txt", "r")
|
||||||
|
if f.read().split("=")[1] == "false":
|
||||||
|
raise ValueError("value not true")
|
||||||
|
|
||||||
print("server already initialized")
|
print("server already initialized")
|
||||||
except:
|
except:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
create table account(
|
create table if not exists account(
|
||||||
name varchar(25),
|
name varchar(25),
|
||||||
username varchar(25),
|
username varchar(25) not null,
|
||||||
password varchar(50) not null,
|
password varchar(100) not null,
|
||||||
salt varchar(50),
|
salt varchar(50),
|
||||||
primary key(username)
|
primary key(username)
|
||||||
);
|
);
|
||||||
|
@ -24,7 +34,7 @@ def __main__():
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
create table balset(
|
create table if not exists balset(
|
||||||
id serial primary key,
|
id serial primary key,
|
||||||
uid varchar(25) not null,
|
uid varchar(25) not null,
|
||||||
name varchar(50),
|
name varchar(50),
|
||||||
|
@ -42,13 +52,29 @@ def __main__():
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
cur.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
name = input("input your display name: ")
|
name = input("input your display name: ")
|
||||||
username = input("input your username: ")
|
username = input("input your username: ")
|
||||||
password = getpass("input your password: ")
|
password = getpass("input your password: ")
|
||||||
passchk = getpass("type password one more time: ")
|
passchk = getpass("type password one more time: ")
|
||||||
|
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)
|
||||||
|
|
||||||
cur.close()
|
|
||||||
conn.close()
|
|
||||||
f = open("load.txt", "w")
|
f = open("load.txt", "w")
|
||||||
f.write("init=true")
|
f.write("init=true")
|
||||||
|
|
||||||
|
|
1
load.txt
1
load.txt
|
@ -1 +0,0 @@
|
||||||
init=true
|
|
|
@ -8,11 +8,11 @@ class AuthData:
|
||||||
username: str
|
username: str
|
||||||
password: str
|
password: str
|
||||||
salt: str
|
salt: str
|
||||||
|
def __init__(self, name: str, username: str, password: str, salt: str):
|
||||||
class Register:
|
self.name = name
|
||||||
name: str
|
self.username = username
|
||||||
username: str
|
self.password = password
|
||||||
password: str
|
self.salt = salt
|
||||||
|
|
||||||
class Credential(BaseModel):
|
class Credential(BaseModel):
|
||||||
username: str
|
username: str
|
||||||
|
@ -22,9 +22,28 @@ class AuthService:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._conn = psycopg2.connect(conn_param)
|
self._conn = psycopg2.connect(conn_param)
|
||||||
|
|
||||||
|
def create(self, data: AuthData):
|
||||||
|
cur = self._conn.cursor()
|
||||||
|
|
||||||
|
try:
|
||||||
|
if data.username == "" or data.password == "":
|
||||||
|
raise ValueError("username or password must not be null")
|
||||||
|
|
||||||
|
cur.execute(
|
||||||
|
"insert into account (name, username, password, salt) values (%s, %s, %s, %s)",
|
||||||
|
(data.name, data.username, data.password, data.salt)
|
||||||
|
)
|
||||||
|
|
||||||
|
self._conn.commit()
|
||||||
|
except:
|
||||||
|
self._conn.rollback()
|
||||||
|
raise RuntimeError("create account failed")
|
||||||
|
finally:
|
||||||
|
cur.close()
|
||||||
|
self._conn.close()
|
||||||
|
|
||||||
def read(self, username: str):
|
def read(self, username: str):
|
||||||
cur = self._conn.cursor()
|
cur = self._conn.cursor()
|
||||||
|
|
||||||
cur.execute("select * from account where username = %s;", (username))
|
cur.execute("select * from account where username = %s;", (username))
|
||||||
data = cur.fetchone()
|
data = cur.fetchone()
|
||||||
if data == None:
|
if data == None:
|
||||||
|
|
|
@ -2,7 +2,11 @@ import base64
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
|
|
||||||
def hash(password: str, salt: str):
|
def hash(password: str, salt: str):
|
||||||
return sha256("{}:{}".format(password, salt))
|
m = sha256()
|
||||||
|
m.update("{}:{}".format(password, salt).encode())
|
||||||
|
|
||||||
|
return m.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def gen_token(username: str, hashed_password: str):
|
def gen_token(username: str, hashed_password: str):
|
||||||
raw = ("{}:{}".format(username, hashed_password)).encode("utf-8")
|
raw = ("{}:{}".format(username, hashed_password)).encode("utf-8")
|
||||||
|
|
Loading…
Reference in a new issue