krz/yoshi-cli
A password manager for the command line.
clone: git clone https://gitbay.org/krz/yoshi-cli.git
v0.2.2: yoshi/database.py · raw
1"""
2This module provides a basic interface for connecting to and interacting with a SQLite database.
3It includes functions for creating connections, executing queries, and retrieving results.
4"""
5
6import sqlite3
7import sys
8import os
9
10VAULT_DECRYPTED = "vault.sqlite"
11VAULT_ENCRYPTED = "vault.sqlite.aes"
12
13
14def create_table() -> None:
15 """Create the accounts table within the vault database."""
16 db_connection = sqlite3.connect(VAULT_DECRYPTED)
17 cursor = db_connection.cursor()
18 cursor.execute(
19 """ CREATE TABLE IF NOT EXISTS accounts (uuid text, application text,
20 username text, password text, url text) """
21 )
22 db_connection.commit()
23 db_connection.close()
24
25
26def check_table() -> bool:
27 """Check if the 'accounts' table exists within the vault database."""
28 check = False
29 db_connection = sqlite3.connect(VAULT_DECRYPTED)
30 cursor = db_connection.cursor()
31 cursor.execute(
32 """ SELECT count(name) FROM sqlite_master WHERE type='table'
33 AND name='accounts' """
34 )
35 if cursor.fetchone()[0] != 1:
36 user_choice = input(
37 "Password vault does not exist. Would you like to create it now? (y/n): "
38 )
39 if user_choice.lower() == "y":
40 create_table()
41 check = True
42 else:
43 sys.exit("Program aborted upon user request.")
44 else:
45 check = True
46 db_connection.commit()
47 db_connection.close()
48 return check
49
50
51def add_account(
52 uuid: str, application: str, username: str, password: str, url: str
53) -> None:
54 """Add a new account within the vault database."""
55 db_connection = sqlite3.connect(VAULT_DECRYPTED)
56 cursor = db_connection.cursor()
57 cursor.execute(
58 """ INSERT INTO accounts VALUES (:uuid,:application,:username,
59 :password,:url) """,
60 {
61 "uuid": uuid,
62 "application": application,
63 "username": username,
64 "password": password,
65 "url": url,
66 },
67 )
68 db_connection.commit()
69 db_connection.close()
70
71
72def delete_account(uuid: str) -> None:
73 """Delete an account within the vault database by its unique ID."""
74 db_connection = sqlite3.connect(VAULT_DECRYPTED)
75 cursor = db_connection.cursor()
76 cursor.execute(""" DELETE FROM accounts WHERE uuid = :uuid """, {"uuid": uuid})
77 db_connection.commit()
78 db_connection.close()
79
80
81def find_account(uuid: str) -> list:
82 """Find an account within the vault database by its unique ID."""
83 db_connection = sqlite3.connect(VAULT_DECRYPTED)
84 cursor = db_connection.cursor()
85 cursor.execute(""" SELECT * FROM accounts WHERE uuid = :uuid """, {"uuid": uuid})
86 account = cursor.fetchall()
87 db_connection.close()
88 return account
89
90
91def find_accounts() -> list:
92 """Return all accounts stored within the vault database."""
93 db_connection = sqlite3.connect(VAULT_DECRYPTED)
94 cursor = db_connection.cursor()
95 cursor.execute(""" SELECT * FROM accounts """)
96 accounts = cursor.fetchall()
97 db_connection.close()
98 return accounts
99
100
101def update_account(field_name: str, new_value: str, uuid: str) -> None:
102 """Update an account within the vault database by its unique ID."""
103 queries = {
104 "application": "UPDATE accounts SET application = :new_value WHERE uuid = :uuid",
105 "username": "UPDATE accounts SET username = :new_value WHERE uuid = :uuid",
106 "password": "UPDATE accounts SET password = :new_value WHERE uuid = :uuid",
107 "url": "UPDATE accounts SET url = :new_value WHERE uuid = :uuid",
108 }
109 db_connection = sqlite3.connect(VAULT_DECRYPTED)
110 cursor = db_connection.cursor()
111 cursor.execute(queries[field_name], {"new_value": new_value, "uuid": uuid})
112 db_connection.commit()
113 db_connection.close()
114
115
116def purge_table() -> None:
117 """Purge the 'accounts' table within the vault database."""
118 db_connection = sqlite3.connect(VAULT_DECRYPTED)
119 cursor = db_connection.cursor()
120 cursor.execute(""" DROP TABLE accounts """)
121 db_connection.commit()
122 db_connection.close()
123
124
125def purge_database() -> None:
126 """Purge the entire vault database."""
127 os.remove(VAULT_DECRYPTED)