Spaces:
Sleeping
Sleeping
File size: 3,161 Bytes
93917f2 |
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 |
import sqlite3
import bcrypt
import tkinter as tk
from tkinter import messagebox
class SecureDatabase:
def __init__(self, db_path: str = "secure_ai_agix.db"):
self.db_path = db_path
self._init_db()
def _init_db(self):
with sqlite3.connect(self.db_path) as conn:
conn.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
username TEXT UNIQUE,
password_hash TEXT
)
"""
)
conn.execute(
"""
CREATE TABLE IF NOT EXISTS interactions (
id INTEGER PRIMARY KEY,
user_id INTEGER,
query TEXT,
response TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
"""
)
def create_user(self, username: str, password: str):
hashed_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
with sqlite3.connect(self.db_path) as conn:
conn.execute("INSERT INTO users (username, password_hash) VALUES (?, ?)", (username, hashed_password))
def authenticate(self, username: str, password: str) -> bool:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT password_hash FROM users WHERE username = ?", (username,))
result = cursor.fetchone()
return result and bcrypt.checkpw(password.encode(), result[0])
class UserAuthApp(tk.Tk):
def __init__(self):
super().__init__()
self.db = SecureDatabase()
self.title("User Authentication")
self.geometry("400x300")
self._init_ui()
def _init_ui(self):
tk.Label(self, text="Username:").pack()
self.username_entry = tk.Entry(self)
self.username_entry.pack()
tk.Label(self, text="Password:").pack()
self.password_entry = tk.Entry(self, show="*")
self.password_entry.pack()
tk.Button(self, text="Register", command=self._register_user).pack(pady=5)
tk.Button(self, text="Login", command=self._authenticate_user).pack(pady=5)
def _register_user(self):
username = self.username_entry.get()
password = self.password_entry.get()
if username and password:
self.db.create_user(username, password)
messagebox.showinfo("Success", "User registered successfully!")
else:
messagebox.showerror("Error", "Username and Password cannot be empty!")
def _authenticate_user(self):
username = self.username_entry.get()
password = self.password_entry.get()
if self.db.authenticate(username, password):
messagebox.showinfo("Success", "Authentication successful!")
else:
messagebox.showerror("Error", "Invalid credentials!")
if __name__ == "__main__":
app = UserAuthApp()
app.mainloop() |