This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
PassManager/src/database/add.py

56 lines
1.2 KiB
Python
Raw Normal View History

2022-10-14 12:38:03 +00:00
import sqlmodel
from sqlalchemy.exc import IntegrityError
2022-10-14 12:38:03 +00:00
from sqlalchemy.future import Engine
from . import models
2022-10-05 12:04:39 +00:00
2022-09-29 13:02:11 +00:00
2022-10-14 12:38:03 +00:00
def add_account(
engine: Engine,
user_id: int,
name: str,
salt: bytes,
enc_login: bytes,
2022-11-30 12:56:25 +00:00
enc_password: bytes,
2022-10-05 11:44:03 +00:00
) -> bool:
2022-11-30 16:41:57 +00:00
"""Adds account to the database. Returns true on success,
false otherwise"""
2022-10-14 12:38:03 +00:00
account = models.Account(
2022-11-30 12:56:25 +00:00
user_id=user_id,
name=name,
salt=salt,
enc_login=enc_login,
enc_password=enc_password,
2022-10-14 12:38:03 +00:00
)
2022-10-05 11:52:23 +00:00
try:
2022-10-14 12:38:03 +00:00
with sqlmodel.Session(engine) as session:
session.add(account)
session.commit()
except IntegrityError:
2022-10-05 11:52:23 +00:00
return False
else:
return True
2022-09-29 13:02:11 +00:00
2022-11-30 12:56:25 +00:00
def add_master_pass(
engine: Engine,
user_id: int,
salt: bytes,
password_hash: bytes,
) -> bool:
2022-11-30 16:41:57 +00:00
"""Adds master password the database. Returns true on success,
false otherwise"""
2022-11-30 12:56:25 +00:00
master_pass = models.MasterPass(
user_id=user_id,
salt=salt,
password_hash=password_hash,
2022-11-30 12:56:25 +00:00
)
2022-10-05 11:52:23 +00:00
try:
2022-10-14 12:38:03 +00:00
with sqlmodel.Session(engine) as session:
session.add(master_pass)
session.commit()
except IntegrityError:
2022-10-05 11:52:23 +00:00
return False
else:
return True