-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_functions.py
More file actions
34 lines (29 loc) · 1.03 KB
/
db_functions.py
File metadata and controls
34 lines (29 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from db_setup import User, Password, Base
import util
import uuid
from typing import Tuple
from datetime import datetime
import exceptions
# Connects to the database
engine = create_engine(util.path_to_db())
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
def create_user(username: str, plaintext_password: str) -> str:
if session.query(User).filter(User.username == username).first() is not None:
raise exceptions.UsernameTakenException()
uid = str(uuid.uuid4())
hashed_password = util.encrypt_new_password(plaintext_password)
session.add(User(
uid=uid,
username=username,
hashed_password=hashed_password
))
session.commit()
return uid
def create_password(uid: str, raw_password: str, password_name: str, password: str):
if session.query(Password).filter(Password.uid == uid).filter(Password.name == password_name).first() is not None:
raise exceptions.AlreadyAPasswordWithThatNameException()
hashed_password =