Spaces:
Runtime error
Runtime error
File size: 13,271 Bytes
b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c b2f92c4 1c0f80c | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | import os
import secrets
from fastapi import APIRouter, HTTPException, Depends, Form
from fastapi.responses import HTMLResponse
from sqlalchemy.orm import Session
import database as db
router = APIRouter()
# --- Admin Password from Environment ---
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "change_me_in_secrets")
def get_db():
database = db.SessionLocal()
try:
yield database
finally:
database.close()
@router.get("/create-api-key", response_class=HTMLResponse)
async def create_api_key_form():
"""
Admin dashboard for managing API keys.
"""
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>NutriBot Admin Dashboard</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
}
.container {
background: white;
padding: 40px;
border-radius: 12px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
max-width: 600px;
width: 100%;
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 24px;
text-align: center;
}
.subtitle {
color: #666;
margin-bottom: 30px;
font-size: 14px;
text-align: center;
}
.tabs {
display: flex;
margin-bottom: 20px;
border-bottom: 2px solid #eee;
}
.tab {
padding: 10px 20px;
cursor: pointer;
font-weight: 600;
color: #666;
border-bottom: 2px solid transparent;
margin-bottom: -2px;
}
.tab.active {
color: #667eea;
border-bottom-color: #667eea;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 600;
font-size: 14px;
}
input {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 6px;
font-size: 14px;
box-sizing: border-box;
transition: border-color 0.3s;
}
input:focus {
outline: none;
border-color: #667eea;
}
button {
width: 100%;
padding: 14px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 6px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}
.result {
margin-top: 30px;
padding: 20px;
background: #f0f7ff;
border-left: 4px solid #667eea;
border-radius: 6px;
display: none;
}
.result.success { display: block; }
.result.error {
display: block;
background: #fff0f0;
border-left-color: #e74c3c;
}
.api-key {
background: #333;
color: #0f0;
padding: 15px;
border-radius: 6px;
font-family: 'Courier New', monospace;
font-size: 13px;
margin: 15px 0;
word-break: break-all;
cursor: pointer;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #eee;
}
th {
background: #f8f9fa;
font-weight: 600;
color: #555;
}
.badge {
background: #e0e0e0;
padding: 4px 8px;
border-radius: 12px;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<h1>🛡️ Admin Dashboard</h1>
<p class="subtitle">Manage B2B Clients & API Keys</p>
<div class="tabs">
<div class="tab active" onclick="switchTab('create')">Create Key</div>
<div class="tab" onclick="switchTab('list')">View Clients</div>
</div>
<!-- Create Key Tab -->
<div id="create" class="tab-content active">
<form id="apiKeyForm">
<div class="form-group">
<label for="password">Admin Password:</label>
<input type="password" id="password" name="password" required placeholder="Enter admin password">
</div>
<div class="form-group">
<label for="client_name">Client Name:</label>
<input type="text" id="client_name" name="client_name" required placeholder="e.g., HealthTechCo">
</div>
<button type="submit">Generate API Key</button>
</form>
<div id="createResult" class="result"></div>
</div>
<!-- List Clients Tab -->
<div id="list" class="tab-content">
<form id="listClientsForm">
<div class="form-group">
<label for="list_password">Admin Password:</label>
<input type="password" id="list_password" name="password" required placeholder="Enter admin password">
</div>
<button type="submit">Load Clients</button>
</form>
<div id="listResult" style="margin-top: 20px;"></div>
</div>
</div>
<script>
function switchTab(tabName) {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
event.target.classList.add('active');
document.getElementById(tabName).classList.add('active');
// Sync password if typed
const p1 = document.getElementById('password').value;
const p2 = document.getElementById('list_password').value;
if(p1 && !p2) document.getElementById('list_password').value = p1;
if(!p1 && p2) document.getElementById('password').value = p2;
}
// Create Key Handler
document.getElementById('apiKeyForm').addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const resultDiv = document.getElementById('createResult');
try {
const response = await fetch('/admin/create-api-key', {
method: 'POST',
body: formData
});
const data = await response.json();
if (response.ok) {
resultDiv.className = 'result success';
resultDiv.innerHTML = `
<h3 style="margin-top: 0; color: #27ae60;">✅ Created!</h3>
<p><strong>Client:</strong> ${data.client_name} (ID: ${data.client_id})</p>
<div class="api-key" onclick="navigator.clipboard.writeText(this.textContent); alert('Copied!')">
${data.api_key}
</div>
<p style="color: #e74c3c; font-size: 12px;">⚠️ Copy now! Won't be shown again.</p>
`;
e.target.reset();
} else {
throw new Error(data.detail || 'Error');
}
} catch (error) {
resultDiv.className = 'result error';
resultDiv.innerHTML = `<p>❌ ${error.message}</p>`;
}
});
// List Clients Handler
document.getElementById('listClientsForm').addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const resultDiv = document.getElementById('listResult');
resultDiv.innerHTML = '<p style="text-align:center">Loading...</p>';
try {
const response = await fetch('/admin/list-clients', {
method: 'POST',
body: formData
});
const data = await response.json();
if (response.ok) {
if (data.clients.length === 0) {
resultDiv.innerHTML = '<p style="text-align:center; color:#666">No clients found.</p>';
return;
}
let html = `
<p><strong>Total Clients:</strong> ${data.count}</p>
<table>
<thead><tr><th>ID</th><th>Name</th></tr></thead>
<tbody>
`;
data.clients.forEach(client => {
html += `<tr>
<td><span class="badge">${client.id}</span></td>
<td>${client.name}</td>
</tr>`;
});
html += '</tbody></table>';
resultDiv.innerHTML = html;
} else {
throw new Error(data.detail || 'Error');
}
} catch (error) {
resultDiv.innerHTML = `<p style="color: #e74c3c; text-align: center;">❌ ${error.message}</p>`;
}
});
</script>
</body>
</html>
"""
return html_content
@router.post("/create-api-key")
async def create_api_key(
password: str = Form(...),
client_name: str = Form(...),
database: Session = Depends(get_db)
):
"""
Create a new API key for a B2B client.
Protected by admin password.
"""
# Verify admin password
if password != ADMIN_PASSWORD:
raise HTTPException(status_code=401, detail="Invalid admin password")
# Validate client name
if not client_name or not client_name.strip():
raise HTTPException(status_code=400, detail="Client name cannot be empty")
client_name = client_name.strip()
# Check if client already exists
existing_client = db.get_api_client_by_name(database, client_name)
if existing_client:
raise HTTPException(status_code=400, detail=f"Client '{client_name}' already exists")
# Generate secure API key
api_key = f"nbk_live_{secrets.token_hex(32)}"
# Add client to database
new_client = db.add_api_client(database, client_name, api_key)
return {
"status": "success",
"client_name": new_client.client_name,
"client_id": new_client.id,
"api_key": api_key,
"message": "API Key created successfully. This key will not be shown again."
}
@router.post("/list-clients")
async def list_clients(
password: str = Form(...),
database: Session = Depends(get_db)
):
"""
List all registered clients.
Protected by admin password.
"""
if password != ADMIN_PASSWORD:
raise HTTPException(status_code=401, detail="Invalid admin password")
clients = db.get_all_api_clients(database)
return {
"count": len(clients),
"clients": [
{"id": c.id, "name": c.client_name}
for c in clients
]
}
|