mansi14883md/AI-Keystroke-Security-System
1
1import tkinter as tk2from tkinter import messagebox3 4from backend_utils import build_user_profile_summary, register_user_account, sanitize_username, save_user_sample5from capture_data import SampleCaptureWindow6 7 8class UserRegistrationApp:9 def __init__(self, root: tk.Tk) -> None:10 self.root = root11 self.root.title("Register Typing Profile")12 self.root.geometry("520x320")13 self.root.resizable(False, False)14 self.root.configure(bg="#102235")15 16 self.build_gui()17 18 def build_gui(self) -> None:19 tk.Label(20 self.root,21 texsecu22 t="User Registration",23 font=("Arial", 20, "bold"),24 fg="#f1faee",25 bg="#102235",26 pady=16,27 ).pack()28 29 tk.Label(30 self.root,31 text="Create a typing profile by entering a username and recording samples.",32 font=("Arial", 11),33 fg="#caf0f8",34 bg="#102235",35 ).pack()36 37 form = tk.Frame(self.root, bg="#102235")38 form.pack(pady=24)39 40 tk.Label(form, text="Username", font=("Arial", 12, "bold"), fg="#f1faee", bg="#102235").grid(row=0, column=0, sticky="w", pady=8)41 self.username_entry = tk.Entry(form, font=("Arial", 12), width=24)42 self.username_entry.grid(row=0, column=1, padx=12, pady=8)43 44 tk.Label(form, text="Samples", font=("Arial", 12, "bold"), fg="#f1faee", bg="#102235").grid(row=1, column=0, sticky="w", pady=8)45 self.samples_entry = tk.Entry(form, font=("Arial", 12), width=24)46 self.samples_entry.insert(0, "10")47 self.samples_entry.grid(row=1, column=1, padx=12, pady=8)48 49 tk.Label(form, text="Password", font=("Arial", 12, "bold"), fg="#f1faee", bg="#102235").grid(row=2, column=0, sticky="w", pady=8)50 self.password_entry = tk.Entry(form, font=("Arial", 12), width=24, show="*")51 self.password_entry.grid(row=2, column=1, padx=12, pady=8)52 53 tk.Button(54 self.root,55 text="Register User",56 font=("Arial", 13, "bold"),57 bg="#1f8a70",58 fg="white",59 relief="flat",60 padx=20,61 pady=10,62 command=self.register_user,63 ).pack(pady=12)64 65 self.status_label = tk.Label(66 self.root,67 text="Recommended: record at least 10 samples in your natural style.",68 font=("Arial", 10),69 fg="#98c1d9",70 bg="#102235",71 )72 self.status_label.pack(pady=8)73 74 def register_user(self) -> None:75 username = sanitize_username(self.username_entry.get())76 if not username:77 messagebox.showwarning("Invalid Username", "Please enter a valid username.")78 return79 80 try:81 sample_count = int(self.samples_entry.get().strip())82 except ValueError:83 messagebox.showwarning("Invalid Samples", "Please enter a valid number of samples.")84 return85 86 if sample_count < 5:87 messagebox.showwarning("Too Few Samples", "Please record at least 5 samples.")88 return89 90 password = self.password_entry.get().strip()91 if len(password) < 4:92 messagebox.showwarning("Weak Password", "Please enter a password with at least 4 characters.")93 return94 95 register_user_account(username, password)96 97 saved = 098 while saved < sample_count:99 capture_window = SampleCaptureWindow(saved + 1, sample_count)100 sample = capture_window.run()101 if sample is None:102 break103 save_user_sample(username, sample)104 saved += 1105 106 if saved < 5:107 messagebox.showwarning("Registration Incomplete", "At least 5 samples are needed to create a profile.")108 return109 110 summary = build_user_profile_summary(username)111 if self.status_label.winfo_exists():112 self.status_label.config(text=f"Registered user: {summary['username']} with {summary['sample_count']} samples.")113 messagebox.showinfo(114 "Registration Complete",115 f"User '{summary['username']}' registered successfully with {summary['sample_count']} samples.",116 )117 118 119def main() -> None:120 root = tk.Tk()121 app = UserRegistrationApp(root)122 root.mainloop()123 124 125if __name__ == "__main__":126 main()127 