mansi14883md/AI-Keystroke-Security-System
1
1import os2from flask import Flask, render_template_string, request3from backend_utils import (4 authenticate_registered_user,5 get_dashboard_stats,6 list_registered_users,7 log_auth_attempt,8 verify_user_password,9 TARGET_TEXT10)11 12app = Flask(__name__)13 14HOME_TEMPLATE = """15<!doctype html>16<html>17<head>18 <title>AI Keystroke Dynamics Portal</title>19 <style>20 body { font-family: 'Segoe UI', Arial, sans-serif; background: #050913; color: #effbff; margin: 0; padding: 40px; }21 .wrap { max-width: 800px; margin: 0 auto; }22 h2 { color: #52d8ff; font-weight: 600; }23 .card { background: #102235; border: 1px solid #173b67; border-radius: 12px; padding: 25px; margin-bottom: 25px; box-shadow: 0 4px 15px rgba(0,0,0,0.5); }24 .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }25 label { display: block; font-weight: bold; margin-bottom: 5px; color: #caf0f8; font-size: 14px; }26 input, select { width: 100%; padding: 12px; margin-bottom: 15px; border-radius: 6px; border: 1px solid #264d73; background: #081420; color: #fff; box-sizing: border-box; }27 button { background: #00b4d8; color: white; border: none; padding: 14px; border-radius: 8px; cursor: pointer; font-weight: bold; width: 100%; font-size: 16px; transition: 0.3s; }28 button:hover { background: #0096b6; }29 .success { background: rgba(82, 183, 136, 0.15); border: 1px solid #52b788; color: #52b788; padding: 15px; border-radius: 6px; margin-bottom: 15px; }30 .danger { background: rgba(249, 65, 68, 0.15); border: 1px solid #f94144; color: #f94144; padding: 15px; border-radius: 6px; margin-bottom: 15px; }31 .metrics-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; margin-top: 15px; }32 .metric-box { background: #081420; border: 1px solid #173b67; padding: 15px; border-radius: 6px; text-align: center; }33 .metric-box span { display: block; font-size: 20px; font-weight: bold; color: #52d8ff; }34 </style>35 36 <script>37 // Synchronized baseline dataset vectors for instant profile switching38 const userPatterns = {39 "user1": "0.119071, 0.114572, 0.114255, 0.096018, 0.076100, 0.089939, 0.083782, 0.075981, 0.071754",40 "user2": "0.223320, 0.226164, 0.343563, 0.228134, 0.229460, 0.197329, 0.227038, 0.188159, 0.151757",41 "user3": "0.093423, 0.122090, 0.107540, 0.106249, 0.117023, 0.131446, 0.097817, 0.096623, 0.092870"42 };43 44 function updateVectorValues() {45 const selectedUser = document.getElementById("username_select").value;46 const targetInput = document.getElementById("hold_times_input");47 if (userPatterns[selectedUser]) {48 targetInput.value = userPatterns[selectedUser];49 }50 }51 </script>52</head>53<body>54 <div class="wrap">55 <h2>๐ AI Keystroke Dynamics Biometric Portal</h2>56 <p>Verification Target Phrase: <strong style="color:#52d8ff;">{{ target_text }}</strong></p>57 58 {% if error %}59 <div class="danger">โ ๏ธ <strong>Error:</strong> {{ error }}</div>60 {% endif %}61 62 {% if result %}63 {% if result.prediction == 1 %}64 <div class="success">โ
<strong>Access Granted:</strong> Typing behavioral biometric matches baseline profile!</div>65 {% else %}66 <div class="danger">โ <strong>Access Denied:</strong> Anomaly alert! Typing rhythm indicates an intruder profile.</div>67 {% endif %}68 69 <div class="metrics-grid">70 <div class="metric-box"><span>{{ "%.2f"|format(result.confidence * 100) }}%</span>Confidence Score</div>71 <div class="metric-box"><span>{{ "%.2f"|format(result.profile_similarity * 100) }}%</span>Pattern Match</div>72 <div class="metric-box"><span style="font-size:14px; padding-top:6px;">{{ result.best_model }}</span>Engine Layer</div>73 </div>74 <br>75 {% endif %}76 77 <div class="card">78 <form action="/login" method="post">79 <div class="grid">80 <div>81 <label>Select Profile Target Account ID:</label>82 <select name="username" id="username_select" onchange="updateVectorValues()">83 {% for user in users %}84 <option value="{{ user }}" {% if selected_user == user %}selected{% endif %}>{{ user }}</option>85 {% endfor %}86 </select>87 </div>88 <div>89 <label>Secret Password Token:</label>90 <input type="password" name="password" required placeholder="Type password here...">91 </div>92 </div>93 94 <label>Key Hold Timing Array Vector (Auto-syncs to dropdown value selection):</label>95 <input type="text" name="hold_times" id="hold_times_input" value="0.119071, 0.114572, 0.114255, 0.096018, 0.076100, 0.089939, 0.083782, 0.075981, 0.071754" required>96 97 <button type="submit">Execute Biometric Access Analysis</button>98 </form>99 </div>100 </div>101</body>102</html>103"""104 105@app.route("/")106def index():107 return render_template_string(108 HOME_TEMPLATE, users=list_registered_users(), selected_user="user1", stats=get_dashboard_stats(), target_text=TARGET_TEXT, result=None, error=None109 )110 111@app.route("/login", methods=["POST"])112def login_route():113 username = request.form.get("username", "").strip()114 password = request.form.get("password", "").strip()115 hold_times_text = request.form.get("hold_times", "").strip()116 117 if not verify_user_password(username, password):118 return render_template_string(119 HOME_TEMPLATE, users=list_registered_users(), selected_user=username, stats=get_dashboard_stats(), target_text=TARGET_TEXT, result=None, error="Incorrect password credentials entered."120 )121 122 try:123 hold_times = [float(v.strip()) for v in hold_times_text.split(",") if v.strip()]124 except ValueError:125 return render_template_string(126 HOME_TEMPLATE, users=list_registered_users(), selected_user=username, stats=get_dashboard_stats(), target_text=TARGET_TEXT, result=None, error="Timing entry expects float values only."127 )128 129 result = authenticate_registered_user(username, hold_times)130 return render_template_string(131 HOME_TEMPLATE, users=list_registered_users(), selected_user=username, stats=get_dashboard_stats(), target_text=TARGET_TEXT, result=result, error=None132 )