-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
115 lines (91 loc) · 3.01 KB
/
Copy pathdatabase.py
File metadata and controls
115 lines (91 loc) · 3.01 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# database.py
import sqlite3
DATABASE_NAME = "passwords.db"
def get_db_connection():
return sqlite3.connect(DATABASE_NAME)
def create_table():
connection = get_db_connection()
cursor = connection.cursor()
# Create users table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
master_password_hash TEXT NOT NULL
)
"""
)
# Create passwords table
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS passwords (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
name TEXT NOT NULL,
username TEXT NOT NULL,
encrypted_password BLOB NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (id)
)
"""
)
connection.commit()
connection.close()
def register_user(username, master_password_hash):
connection = get_db_connection()
cursor = connection.cursor()
try:
cursor.execute(
"INSERT INTO users (username, master_password_hash) VALUES (?, ?)",
(username, master_password_hash)
)
connection.commit()
except sqlite3.IntegrityError:
return False # Username already exists
finally:
connection.close()
return True
def authenticate_user(username, master_password_hash):
connection = get_db_connection()
cursor = connection.cursor()
cursor.execute(
"SELECT * FROM users WHERE username = ? AND master_password_hash = ?",
(username, master_password_hash)
)
user = cursor.fetchone()
connection.close()
return user is not None
def get_user_id(username):
connection = get_db_connection()
cursor = connection.cursor()
cursor.execute("SELECT id FROM users WHERE username = ?", (username,))
user = cursor.fetchone()
connection.close()
if user:
return user[0]
return None
def add_password(user_id, name, username, encrypted_password):
connection = get_db_connection()
cursor = connection.cursor()
cursor.execute(
"INSERT INTO passwords (user_id, name, username, encrypted_password) VALUES (?, ?, ?, ?)",
(user_id, name, username, encrypted_password)
)
connection.commit()
connection.close()
def get_passwords(user_id):
connection = get_db_connection()
cursor = connection.cursor()
cursor.execute("SELECT * FROM passwords WHERE user_id = ?", (user_id,))
passwords = cursor.fetchall()
connection.close()
return passwords
def delete_password(password_id):
connection = get_db_connection()
cursor = connection.cursor()
cursor.execute("DELETE FROM passwords WHERE id = ?", (password_id,))
connection.commit()
connection.close()
if __name__ == "__main__":
create_table()
print("Database and tables created successfully.")