From 177ed4a844118117603de65cdf8653fba2a768a0 Mon Sep 17 00:00:00 2001 From: Team Dark Devil Date: Wed, 18 Nov 2026 05:18:55 +0600 Subject: [PATCH] refactor(auth): standardize session management and security - Centralize secret retrieval using `getAdminSessionSecret` - Replace manual session parsing with `verifyRequestAdminSession` utility - Tighten API validation and improve security headers handling - Enforce environment variable security for production deployments --- .env.example | 27 +- app/(landing)/page.tsx | 6 +- app/api/auth/login/route.ts | 181 +-- app/api/auth/profile/route.ts | 89 +- app/api/auth/setup/route.ts | 21 +- app/api/clients/[id]/route.ts | 50 +- app/api/clients/route.ts | 47 +- app/api/data/route.ts | 44 +- app/api/events/route.ts | 105 +- app/api/export/route.ts | 54 +- app/api/health/route.ts | 14 +- app/api/settings/route.ts | 180 ++- app/api/storage/route.ts | 70 +- app/error.tsx | 50 + app/layout.tsx | 31 +- app/not-found.tsx | 11 +- app/vcon/clients/page.tsx | 21 +- app/vcon/error.tsx | 50 + app/vcon/login/page.tsx | 11 +- app/vcon/sdk/page.tsx | 117 +- app/vcon/simulator/page.tsx | 353 +++++- bun.lock | 442 ++++--- components/layout/Footer.tsx | 6 +- components/layout/Navbar.tsx | 20 +- components/pwa/PwaLifecycle.tsx | 175 +++ components/sections/AdminLogin.tsx | 4 +- components/sections/ClientsFilters.tsx | 1 + components/sections/ClientsTable.tsx | 159 ++- components/sections/DashboardOverview.tsx | 112 +- components/sections/DataBrowser.tsx | 136 ++- components/sections/ExportWorkspace.tsx | 55 +- components/sections/Hero.tsx | 10 +- components/sections/LandingAbout.tsx | 6 +- components/sections/SettingsWorkspace.tsx | 209 +++- components/sections/clients/types.ts | 8 +- components/ui/Button.tsx | 70 +- core/Database/database.ts | 25 + core/Security/security.ts | 49 +- core/Storage/csvStorage.ts | 445 ++++++- core/adminSettings/settingsManager.ts | 80 +- core/apiEngine/apiService.ts | 83 +- core/auth/adminSession.ts | 30 +- core/auth/adminUsers.ts | 442 ++++--- core/auth/jwt.ts | 43 +- core/data/buffer.ts | 408 ++++++- core/sdk/sdkManager.ts | 362 +++++- docker/docker-compose.yml | 8 +- middleware.ts | 49 +- nginx/default.conf | 21 +- package.json | 1 + prisma/schema.prisma | 78 +- public/apple-touch-icon.png | Bin 0 -> 632 bytes public/icon-192.png | Bin 0 -> 718 bytes public/icon-512.png | Bin 0 -> 3695 bytes public/icon-maskable-512.png | Bin 0 -> 2802 bytes public/icon.svg | 7 + public/manifest.json | 22 +- public/sw.js | 52 +- sdk-python/README.md | 20 +- sdk-python/datastack_sdk/__init__.py | 30 +- sdk-python/datastack_sdk/client.py | 1194 +------------------ sdk-python/datastack_sdk/config.py | 322 +---- sdk-python/datastack_sdk/exceptions.py | 73 +- sdk-python/datastack_sdk/heartbeat.py | 187 +-- sdk-python/datastack_sdk/spool.py | 165 +-- sdk/python/datastack/README.md | 1 + sdk/python/datastack/datastack/__init__.py | 10 +- sdk/python/datastack/datastack/client.py | 116 +- sdk/python/datastack/datastack/heartbeat.py | 5 + sdk/python/datastack/datastack/spool.py | 45 +- server/index.js | 812 +++++++++++-- server/socket/handlers.js | 55 +- server/workers/csvWriter.js | 147 ++- tests/phase01-auth.test.ts | 340 ++++++ tests/phase02-sync.test.ts | 375 ++++++ tests/phase03-storage-buffer.test.ts | 511 ++++++++ tests/phase05_sdk_test.py | 266 +++++ tests/phase06-pwa-landing.test.ts | 81 ++ 78 files changed, 6748 insertions(+), 3157 deletions(-) create mode 100644 app/error.tsx create mode 100644 app/vcon/error.tsx create mode 100644 components/pwa/PwaLifecycle.tsx create mode 100644 public/apple-touch-icon.png create mode 100644 public/icon-192.png create mode 100644 public/icon-512.png create mode 100644 public/icon-maskable-512.png create mode 100644 public/icon.svg create mode 100644 tests/phase01-auth.test.ts create mode 100644 tests/phase02-sync.test.ts create mode 100644 tests/phase03-storage-buffer.test.ts create mode 100644 tests/phase05_sdk_test.py create mode 100644 tests/phase06-pwa-landing.test.ts diff --git a/.env.example b/.env.example index 7e70749..8f114b4 100644 --- a/.env.example +++ b/.env.example @@ -1,16 +1,23 @@ # DataStack Environment Variables Configuration +# NOTE: In production (NODE_ENV=production), replace all placeholder values below with strong 32+ char secrets. -# Administrator Account (defaults to admin@datastack.dev / admin12345678 if omitted) -DATASTACK_ADMIN_EMAIL=admin@datastack.dev -DATASTACK_ADMIN_PASSWORD=admin12345678 +# Security & Session Secrets (REQUIRED in production, min 32 characters) +DATASTACK_ADMIN_SESSION_SECRET=replace-with-32-plus-char-admin-session-secret +DATASTACK_JWT_SECRET=replace-with-32-plus-char-client-jwt-secret +DATASTACK_ADMIN_SETUP_KEY=replace-with-32-plus-char-admin-setup-key +DATASTACK_INTERNAL_SECRET=replace-with-32-plus-char-internal-service-secret -# Security & Session Secrets (min 32 characters) -DATASTACK_ADMIN_SESSION_SECRET=datastack-admin-session-default-secret-32-chars -DATASTACK_JWT_SECRET=datastack-jwt-default-secret-key-32-chars-long -DATASTACK_ADMIN_SETUP_KEY=datastack-default-setup-key-32-chars +# Administrator Account (development/in-memory bootstrap; production uses /vcon/setup) +DATASTACK_ADMIN_EMAIL=admin@example.com +# DATASTACK_ADMIN_PASSWORD=replace-with-strong-password +# DATASTACK_ALLOW_DEMO_ADMIN=false -# PostgreSQL Database (optional - in-memory fallback active when not configured) +# PostgreSQL Database (recommended for production persistence) # DATABASE_URL=postgresql://datastack:password@localhost:5432/datastack_db -# Ingestion Engine Port (defaults to 4000) -PORT_SOCKET=4000 \ No newline at end of file +# Shared Redis Buffer (optional; enables cross-process buffering & distributed flush locks) +# REDIS_URL=redis://127.0.0.1:6379 + +# Service Networking +PORT_SOCKET=4000 +INGESTION_INTERNAL_URL=http://127.0.0.1:4000 \ No newline at end of file diff --git a/app/(landing)/page.tsx b/app/(landing)/page.tsx index 5be914c..7545dd7 100644 --- a/app/(landing)/page.tsx +++ b/app/(landing)/page.tsx @@ -9,10 +9,8 @@ import { redirect } from 'next/navigation'; export const dynamic = 'force-dynamic'; export default async function LandingPage() { - if (process.env.DATABASE_URL) { - const isConfigured = await isAdminSetupComplete().catch(() => false); - if (!isConfigured) redirect('/vcon/setup'); - } + const isConfigured = await isAdminSetupComplete().catch(() => false); + if (!isConfigured) redirect('/vcon/setup'); return ( diff --git a/app/api/auth/login/route.ts b/app/api/auth/login/route.ts index 96d7f02..afd12a6 100644 --- a/app/api/auth/login/route.ts +++ b/app/api/auth/login/route.ts @@ -1,11 +1,12 @@ import { createAdminSessionToken, + getAdminSessionSecret, getSessionCookieOptions, } from "@/core/auth/adminSession"; import { + authenticateAdminCredentials, clearAdminLoginAttempts, consumeAdminLoginAttempt, - verifyAdminCredentials, } from "@/core/auth/adminUsers"; import { NextRequest, NextResponse } from "next/server"; @@ -13,10 +14,19 @@ const SESSION_COOKIE = "datastack_admin_session"; const SESSION_SECONDS = 8 * 60 * 60; const REMEMBERED_SESSION_SECONDS = 30 * 24 * 60 * 60; +function extractClientIp(request: NextRequest) { + const forwardedFor = request.headers.get("x-forwarded-for"); + if (forwardedFor) { + const first = forwardedFor.split(",")[0]?.trim(); + if (first) return first; + } + const realIp = request.headers.get("x-real-ip")?.trim(); + if (realIp) return realIp; + return "unknown"; +} + export async function POST(request: NextRequest) { - const sessionSecret = - process.env.DATASTACK_ADMIN_SESSION_SECRET || - "datastack-admin-session-default-secret-32-chars"; + const sessionSecret = getAdminSessionSecret(); if (!sessionSecret || sessionSecret.length < 32) { return NextResponse.json( @@ -27,86 +37,99 @@ export async function POST(request: NextRequest) { ); } + let body: Record; try { - const body = await request.json(); - const email = - typeof body.email === "string" ? body.email.trim().toLowerCase() : ""; - const password = typeof body.password === "string" ? body.password : ""; - if (!email || password.length === 0 || password.length > 1024) { - return NextResponse.json( - { error: "Invalid email or password." }, - { status: 401 }, - ); - } - - let throttleDecision; - try { - throttleDecision = await consumeAdminLoginAttempt(email, sessionSecret); - } catch { - return NextResponse.json( - { error: "Admin authentication is temporarily unavailable." }, - { status: 503 }, - ); - } - if (!throttleDecision.allowed) { - return NextResponse.json( - { error: "Too many sign-in attempts. Try again later." }, - { - status: 429, - headers: { - "Retry-After": String(throttleDecision.retryAfterSeconds), - }, - }, - ); - } - - let authenticated = false; - try { - authenticated = await verifyAdminCredentials(email, password); - } catch { - return NextResponse.json( - { error: "Admin authentication is temporarily unavailable." }, - { status: 503 }, - ); - } - - if (!authenticated) { - return NextResponse.json( - { error: "Invalid email or password." }, - { status: 401 }, - ); - } - - try { - await clearAdminLoginAttempts(email, sessionSecret); - } catch { - return NextResponse.json( - { error: "Admin authentication is temporarily unavailable." }, - { status: 503 }, - ); - } - - const remember = body.remember === true; - const maxAge = remember ? REMEMBERED_SESSION_SECONDS : SESSION_SECONDS; - const expiresAt = Date.now() + maxAge * 1000; - const token = await createAdminSessionToken( - email, - sessionSecret, - expiresAt, - ); - const response = NextResponse.json({ success: true }); - - response.cookies.set( - SESSION_COOKIE, - token, - getSessionCookieOptions(request, maxAge), - ); - - return response; + const parsed = await request.json(); + body = + parsed && typeof parsed === "object" && !Array.isArray(parsed) + ? (parsed as Record) + : {}; } catch { return NextResponse.json( { error: "Invalid login request." }, { status: 400 }, ); } + + const email = + typeof body.email === "string" ? body.email.trim().toLowerCase() : ""; + const password = typeof body.password === "string" ? body.password : ""; + if (!email || password.length === 0 || password.length > 1024) { + return NextResponse.json( + { error: "Invalid email or password." }, + { status: 401 }, + ); + } + + const clientIp = extractClientIp(request); + + let throttleDecision; + try { + throttleDecision = await consumeAdminLoginAttempt( + email, + sessionSecret, + clientIp, + ); + } catch { + return NextResponse.json( + { error: "Admin authentication is temporarily unavailable." }, + { status: 503 }, + ); + } + if (!throttleDecision.allowed) { + return NextResponse.json( + { error: "Too many sign-in attempts. Try again later." }, + { + status: 429, + headers: { + "Retry-After": String(throttleDecision.retryAfterSeconds), + }, + }, + ); + } + + let authResult: { authenticated: boolean; tokenVersion: number }; + try { + authResult = await authenticateAdminCredentials(email, password); + } catch { + return NextResponse.json( + { error: "Admin authentication is temporarily unavailable." }, + { status: 503 }, + ); + } + + if (!authResult.authenticated) { + return NextResponse.json( + { error: "Invalid email or password." }, + { status: 401 }, + ); + } + + try { + await clearAdminLoginAttempts(email, sessionSecret, clientIp); + } catch { + return NextResponse.json( + { error: "Admin authentication is temporarily unavailable." }, + { status: 503 }, + ); + } + + const remember = body.remember === true; + const maxAge = remember ? REMEMBERED_SESSION_SECONDS : SESSION_SECONDS; + const expiresAt = Date.now() + maxAge * 1000; + const token = await createAdminSessionToken( + email, + sessionSecret, + expiresAt, + authResult.tokenVersion, + ); + const response = NextResponse.json({ success: true }); + + response.cookies.set( + SESSION_COOKIE, + token, + getSessionCookieOptions(request, maxAge), + ); + + return response; } diff --git a/app/api/auth/profile/route.ts b/app/api/auth/profile/route.ts index 8baee38..9cdee9c 100644 --- a/app/api/auth/profile/route.ts +++ b/app/api/auth/profile/route.ts @@ -1,11 +1,11 @@ import { createAdminSessionToken, - getAdminSessionPayload, getSessionCookieOptions, } from "@/core/auth/adminSession"; import { getAdminAccountInfo, updateAdminAccount, + verifyRequestAdminSession, } from "@/core/auth/adminUsers"; import { NextRequest, NextResponse } from "next/server"; @@ -17,51 +17,69 @@ function validEmail(email: string) { } export async function GET(request: NextRequest) { - const sessionSecret = - process.env.DATASTACK_ADMIN_SESSION_SECRET || - "datastack-admin-session-default-secret-32-chars"; - const token = request.cookies.get(SESSION_COOKIE)?.value || ""; - const session = await getAdminSessionPayload(token, sessionSecret); + const token = request.cookies.get(SESSION_COOKIE)?.value; + const auth = await verifyRequestAdminSession(token); - if (!session) { - return NextResponse.json( - { error: "Authentication required." }, - { status: 401 }, - ); + if (!auth.valid) { + const status = auth.reason === "UNAUTHORIZED" ? 401 : 503; + const message = + auth.reason === "UNAUTHORIZED" + ? "Authentication required." + : "Admin authentication is temporarily unavailable."; + return NextResponse.json({ error: message }, { status }); } - const account = await getAdminAccountInfo(session.email); - return NextResponse.json({ - profile: { - email: account.email, - role: "Owner", - storageMode: account.storageMode, - createdAt: account.createdAt, - sessionExpiresAt: new Date(session.expiresAt).toISOString(), - }, - }); + try { + const account = await getAdminAccountInfo(auth.session.email); + return NextResponse.json({ + profile: { + email: account.email, + role: "Owner", + storageMode: account.storageMode, + createdAt: account.createdAt, + sessionExpiresAt: new Date(auth.session.expiresAt).toISOString(), + }, + }); + } catch { + return NextResponse.json( + { error: "Unable to load admin profile." }, + { status: 503 }, + ); + } } export async function PUT(request: NextRequest) { - const sessionSecret = - process.env.DATASTACK_ADMIN_SESSION_SECRET || - "datastack-admin-session-default-secret-32-chars"; - const token = request.cookies.get(SESSION_COOKIE)?.value || ""; - const session = await getAdminSessionPayload(token, sessionSecret); + const token = request.cookies.get(SESSION_COOKIE)?.value; + const auth = await verifyRequestAdminSession(token); - if (!session) { + if (!auth.valid) { + const status = auth.reason === "UNAUTHORIZED" ? 401 : 503; + const message = + auth.reason === "UNAUTHORIZED" + ? "Authentication required." + : "Admin authentication is temporarily unavailable."; + return NextResponse.json({ error: message }, { status }); + } + + let body: Record; + try { + const parsed = await request.json(); + body = + parsed && typeof parsed === "object" && !Array.isArray(parsed) + ? (parsed as Record) + : {}; + } catch { return NextResponse.json( - { error: "Authentication required." }, - { status: 401 }, + { error: "Invalid profile payload." }, + { status: 400 }, ); } try { - const body = await request.json(); const nextEmail = typeof body.email === "string" ? body.email.trim().toLowerCase() - : session.email; + : auth.session.email; const currentPassword = typeof body.currentPassword === "string" ? body.currentPassword : ""; const newPassword = @@ -88,7 +106,7 @@ export async function PUT(request: NextRequest) { ); } - const account = await getAdminAccountInfo(session.email); + const account = await getAdminAccountInfo(auth.session.email); const result = await updateAdminAccount({ currentEmail: account.email, currentPassword, @@ -103,13 +121,14 @@ export async function PUT(request: NextRequest) { // Preserve remaining session duration (e.g., 30-day "Remember me" session) const remainingSeconds = Math.max( SESSION_SECONDS, - Math.ceil((session.expiresAt - Date.now()) / 1000), + Math.ceil((auth.session.expiresAt - Date.now()) / 1000), ); const expiresAt = Date.now() + remainingSeconds * 1000; const nextToken = await createAdminSessionToken( result.email, - sessionSecret, + auth.sessionSecret, expiresAt, + result.tokenVersion, ); const response = NextResponse.json({ @@ -133,7 +152,7 @@ export async function PUT(request: NextRequest) { } catch { return NextResponse.json( { error: "Unable to update profile." }, - { status: 400 }, + { status: 500 }, ); } } diff --git a/app/api/auth/setup/route.ts b/app/api/auth/setup/route.ts index 2881166..331a20b 100644 --- a/app/api/auth/setup/route.ts +++ b/app/api/auth/setup/route.ts @@ -1,5 +1,6 @@ import { createAdminSessionToken, + getAdminSessionSecret, getSessionCookieOptions, } from "@/core/auth/adminSession"; import { @@ -20,9 +21,7 @@ function validEmail(email: string) { } export async function POST(request: NextRequest) { - const sessionSecret = - process.env.DATASTACK_ADMIN_SESSION_SECRET || - "datastack-admin-session-default-secret-32-chars"; + const sessionSecret = getAdminSessionSecret(); if (!sessionSecret || sessionSecret.length < 32) { return NextResponse.json( { error: "Admin session configuration is unavailable." }, @@ -30,8 +29,21 @@ export async function POST(request: NextRequest) { ); } + let body: Record; + try { + const parsed = await request.json(); + body = + parsed && typeof parsed === "object" && !Array.isArray(parsed) + ? (parsed as Record) + : {}; + } catch { + return NextResponse.json( + { error: "Invalid setup request payload." }, + { status: 400 }, + ); + } + try { - const body = await request.json(); const email = typeof body.email === "string" ? body.email.trim().toLowerCase() : ""; const password = typeof body.password === "string" ? body.password : ""; @@ -74,6 +86,7 @@ export async function POST(request: NextRequest) { email, sessionSecret, Date.now() + SESSION_SECONDS * 1000, + 1, ); const response = NextResponse.json({ success: true }); response.cookies.set( diff --git a/app/api/clients/[id]/route.ts b/app/api/clients/[id]/route.ts index ce37e00..8523234 100644 --- a/app/api/clients/[id]/route.ts +++ b/app/api/clients/[id]/route.ts @@ -1,23 +1,54 @@ +import { verifyRequestAdminSession } from "@/core/auth/adminUsers"; import { SDKManager } from "@/core/sdk/sdkManager"; import { NextRequest, NextResponse } from "next/server"; +const SESSION_COOKIE = "datastack_admin_session"; + export async function GET( - _req: NextRequest, + req: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { + const auth = await verifyRequestAdminSession( + req.cookies.get(SESSION_COOKIE)?.value, + ); + if (!auth.valid) { + return NextResponse.json( + { error: "Authentication required." }, + { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, + ); + } + const { id } = await params; const client = await SDKManager.getClientAsync(id); if (!client) { return NextResponse.json({ error: "Client not found" }, { status: 404 }); } const { secret, token, ...clientSummary } = client; - return NextResponse.json({ client: clientSummary }); + void secret; + const includeToken = req.nextUrl.searchParams.get("includeToken") === "true"; + return NextResponse.json({ + client: { + ...clientSummary, + tokenPreview: token ? `${token.slice(0, 12)}...` : "", + ...(includeToken ? { token } : {}), + }, + }); } export async function PATCH( req: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { + const auth = await verifyRequestAdminSession( + req.cookies.get(SESSION_COOKIE)?.value, + ); + if (!auth.valid) { + return NextResponse.json( + { error: "Authentication required." }, + { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, + ); + } + const { id } = await params; try { const body = await req.json(); @@ -46,11 +77,22 @@ export async function PATCH( } export async function DELETE( - _req: NextRequest, + req: NextRequest, { params }: { params: Promise<{ id: string }> }, ) { + const auth = await verifyRequestAdminSession( + req.cookies.get(SESSION_COOKIE)?.value, + ); + if (!auth.valid) { + return NextResponse.json( + { error: "Authentication required." }, + { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, + ); + } + const { id } = await params; - const deleted = await SDKManager.deleteClientAsync(id); + const purgeFiles = req.nextUrl.searchParams.get("purgeFiles") === "true"; + const deleted = await SDKManager.deleteClientAsync(id, { purgeFiles }); if (!deleted) { return NextResponse.json({ error: "Client not found" }, { status: 404 }); } diff --git a/app/api/clients/route.ts b/app/api/clients/route.ts index 2b7250f..000932f 100644 --- a/app/api/clients/route.ts +++ b/app/api/clients/route.ts @@ -1,12 +1,28 @@ +import { verifyRequestAdminSession } from "@/core/auth/adminUsers"; import { SDKManager } from "@/core/sdk/sdkManager"; import { NextRequest, NextResponse } from "next/server"; -export async function GET() { +const SESSION_COOKIE = "datastack_admin_session"; + +export async function GET(req: NextRequest) { + const auth = await verifyRequestAdminSession( + req.cookies.get(SESSION_COOKIE)?.value, + ); + if (!auth.valid) { + return NextResponse.json( + { error: "Authentication required." }, + { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, + ); + } + try { const allClients = await SDKManager.listClientsAsync(); - const clients = allClients.map(({ secret, ...client }) => { + const clients = allClients.map(({ secret, token, ...client }) => { void secret; - return client; + return { + ...client, + tokenPreview: token ? `${token.slice(0, 12)}...` : "", + }; }); return NextResponse.json({ clients, count: clients.length }); } catch (err: unknown) { @@ -17,8 +33,31 @@ export async function GET() { } export async function POST(req: NextRequest) { + const auth = await verifyRequestAdminSession( + req.cookies.get(SESSION_COOKIE)?.value, + ); + if (!auth.valid) { + return NextResponse.json( + { error: "Authentication required." }, + { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, + ); + } + + let body: Record; + try { + const parsed = await req.json(); + body = + parsed && typeof parsed === "object" && !Array.isArray(parsed) + ? (parsed as Record) + : {}; + } catch { + return NextResponse.json( + { error: "Invalid request payload." }, + { status: 400 }, + ); + } + try { - const body = await req.json(); const { clientId, name, rateLimit } = body; if (!clientId || !name) { diff --git a/app/api/data/route.ts b/app/api/data/route.ts index bdb6f32..934c437 100644 --- a/app/api/data/route.ts +++ b/app/api/data/route.ts @@ -1,4 +1,6 @@ +import { SettingsManager } from "@/core/adminSettings/settingsManager"; import { DataBufferEngine } from "@/core/data/buffer"; +import { getInternalIngestionHeaders } from "@/core/Security/security"; import { CSVStorageManager } from "@/core/Storage/csvStorage"; import { parse } from "csv-parse"; import { NextRequest, NextResponse } from "next/server"; @@ -31,18 +33,25 @@ export async function GET(request: NextRequest) { let total = 0; const addIfMatching = (event: DataEvent) => { - const eventDate = event.timestamp.slice(0, 10); + const eventDate = CSVStorageManager.extractSafePartitionDate(event.timestamp); + const dedupeKey = `${event.clientId}:${event.eventId}`; + const matchesSearch = + !search || + event.payload.toLowerCase().includes(search) || + event.eventId.toLowerCase().includes(search) || + event.clientId.toLowerCase().includes(search); + if ( (clientId !== "all" && event.clientId !== clientId) || (from && eventDate < from) || (to && eventDate > to) || - (search && !event.payload.toLowerCase().includes(search)) || - eventIds.has(event.eventId) + !matchesSearch || + eventIds.has(dedupeKey) ) { return; } - eventIds.add(event.eventId); + eventIds.add(dedupeKey); total += 1; let low = 0; @@ -109,7 +118,16 @@ export async function GET(request: NextRequest) { } } - for (const event of DataBufferEngine.getRecentEvents(200)) { + const settings = await SettingsManager.getSettingsAsync(); + const recentWindowLimit = Math.min( + 2000, + Math.max(200, settings.batchFlushThreshold || 500), + ); + + for (const event of await DataBufferEngine.getRecentEventsAsync( + recentWindowLimit, + clientId, + )) { addIfMatching({ ...event, status: "buffered" }); } @@ -117,10 +135,18 @@ export async function GET(request: NextRequest) { process.env.INGESTION_INTERNAL_URL || "http://127.0.0.1:4000" ).replace(/\/$/, ""); try { - const res = await fetch(`${ingestionBaseUrl}/internal/recent?limit=200`, { - cache: "no-store", - signal: AbortSignal.timeout(1000), - }); + const qs = new URLSearchParams({ limit: String(recentWindowLimit) }); + if (clientId && clientId !== "all") { + qs.set("clientId", clientId); + } + const res = await fetch( + `${ingestionBaseUrl}/internal/recent?${qs.toString()}`, + { + headers: getInternalIngestionHeaders(), + cache: "no-store", + signal: AbortSignal.timeout(1000), + }, + ); if (res.ok) { const data = await res.json(); if (Array.isArray(data.events)) { diff --git a/app/api/events/route.ts b/app/api/events/route.ts index a93d99f..bf607f1 100644 --- a/app/api/events/route.ts +++ b/app/api/events/route.ts @@ -1,9 +1,12 @@ import { SettingsManager } from '@/core/adminSettings/settingsManager'; -import { verifyAdminSessionToken } from '@/core/auth/adminSession'; +import { verifyRequestAdminSession } from '@/core/auth/adminUsers'; import { AuthService } from '@/core/auth/jwt'; import { DataBufferEngine } from '@/core/data/buffer'; import { SDKManager } from '@/core/sdk/sdkManager'; -import { SecurityEngine } from '@/core/Security/security'; +import { + SecurityEngine, + getInternalIngestionHeaders, +} from '@/core/Security/security'; import { CSVStorageManager, type DataEvent } from '@/core/Storage/csvStorage'; import { parse } from 'csv-parse'; import { NextRequest, NextResponse } from 'next/server'; @@ -15,14 +18,15 @@ async function getRecentStoredEvents( limit: number, clientId?: string, ): Promise { - const files = CSVStorageManager.listAllFiles() + const allFiles = await CSVStorageManager.listAllFilesAsync(); + const files = allFiles .filter((f) => !clientId || clientId === 'all' || f.clientId === clientId) .slice(0, 5); const collected: DataEvent[] = []; for (const file of files) { - const fileEvents: DataEvent[] = []; + const fileTail: DataEvent[] = []; const parser = createReadStream(file.filePath).pipe( parse({ columns: true, skip_empty_lines: true, bom: true }), ); @@ -32,7 +36,10 @@ async function getRecentStoredEvents( Record >) { if (!record.eventId) continue; - fileEvents.push({ + if (fileTail.length >= limit) { + fileTail.shift(); + } + fileTail.push({ clientId: record.clientId || file.clientId, eventId: record.eventId, timestamp: record.timestamp || `${file.date}T00:00:00.000Z`, @@ -43,8 +50,8 @@ async function getRecentStoredEvents( parser.destroy(); } - if (fileEvents.length > 0) { - collected.push(...fileEvents.slice(-limit)); + if (fileTail.length > 0) { + collected.push(...fileTail); } } @@ -55,9 +62,10 @@ export async function GET(req: NextRequest) { try { const { searchParams } = new URL(req.url); const clientId = searchParams.get('clientId') || undefined; - const limit = Math.min(200, Math.max(1, Number(searchParams.get('limit')) || 50)); + const limit = Math.min(2000, Math.max(1, Number(searchParams.get('limit')) || 50)); - const bufferedEvents = DataBufferEngine.getRecentEvents(limit, clientId); + await SettingsManager.getSettingsAsync(); + const bufferedEvents = await DataBufferEngine.getRecentEventsAsync(limit, clientId); let socketBufferedEvents: DataEvent[] = []; const ingestionBaseUrl = ( process.env.INGESTION_INTERNAL_URL || 'http://127.0.0.1:4000' @@ -67,6 +75,7 @@ export async function GET(req: NextRequest) { const qs = new URLSearchParams({ limit: String(limit) }); if (clientId) qs.set('clientId', clientId); const res = await fetch(`${ingestionBaseUrl}/internal/recent?${qs.toString()}`, { + headers: getInternalIngestionHeaders(), cache: 'no-store', signal: AbortSignal.timeout(1000), }); @@ -86,8 +95,10 @@ export async function GET(req: NextRequest) { const merged: DataEvent[] = []; for (const evt of [...bufferedEvents, ...socketBufferedEvents, ...storedEvents]) { - if (!evt.eventId || seenIds.has(evt.eventId)) continue; - seenIds.add(evt.eventId); + if (!evt.eventId) continue; + const dedupeKey = `${evt.clientId}:${evt.eventId}`; + if (seenIds.has(dedupeKey)) continue; + seenIds.add(dedupeKey); merged.push(evt); } @@ -107,9 +118,6 @@ export async function POST(req: NextRequest) { // Verify either Client Bearer JWT or Admin Session Cookie const authHeader = req.headers.get('authorization'); - const sessionSecret = - process.env.DATASTACK_ADMIN_SESSION_SECRET || - 'datastack-admin-session-default-secret-32-chars'; const sessionCookie = req.cookies.get(SESSION_COOKIE)?.value; let tokenClientId: string | null = null; @@ -128,20 +136,30 @@ export async function POST(req: NextRequest) { tokenClientId = verified.payload.clientId; tokenRateLimit = verified.payload.rateLimit; } else { - const isAdmin = Boolean( - sessionSecret && - sessionCookie && - (await verifyAdminSessionToken(sessionCookie, sessionSecret)), - ); - if (!isAdmin) { + const auth = await verifyRequestAdminSession(sessionCookie); + if (!auth.valid) { return NextResponse.json( - { error: 'Authentication required. Provide a valid Client Bearer JWT or Admin Session.' }, - { status: 401 }, + { + error: + auth.reason === 'UNAUTHORIZED' + ? 'Authentication required. Provide a valid Client Bearer JWT or Admin Session.' + : 'Admin authentication is temporarily unavailable.', + }, + { status: auth.reason === 'UNAUTHORIZED' ? 401 : 503 }, ); } } - const body = await req.json(); + let body: unknown; + try { + body = await req.json(); + } catch { + return NextResponse.json( + { error: 'Invalid JSON request payload.', code: 'INVALID_JSON' }, + { status: 400 }, + ); + } + const bodyObj = body && typeof body === 'object' && !Array.isArray(body) ? (body as Record) @@ -154,7 +172,27 @@ export async function POST(req: NextRequest) { .toLowerCase() .replace(/[^a-z0-9_-]/g, '_'); + if (await SDKManager.isClientDeletedAsync(clientId)) { + return NextResponse.json( + { + error: `CLIENT_REVOKED: Client '${clientId}' has been deleted by administrator.`, + code: 'CLIENT_REVOKED', + }, + { status: 403 }, + ); + } + const registeredClient = await SDKManager.getClientAsync(clientId); + if (rawBearerToken && !registeredClient) { + return NextResponse.json( + { + error: `CLIENT_REVOKED: Client '${clientId}' registration not found or has been deleted.`, + code: 'CLIENT_REVOKED', + }, + { status: 403 }, + ); + } + if (registeredClient) { if (registeredClient.status === 'revoked') { return NextResponse.json( @@ -196,6 +234,7 @@ export async function POST(req: NextRequest) { : null; if (batchItems !== null) { + const envelopeKeys = new Set(['eventId', 'timestamp', 'data', 'clientId']); let processed = 0; let rejected = 0; let duplicates = 0; @@ -205,6 +244,8 @@ export async function POST(req: NextRequest) { const oversizedEventIds: string[] = []; const errors: string[] = []; + const batchEvents: DataEvent[] = []; + for (const item of batchItems) { const safeItem = item && typeof item === 'object' && !Array.isArray(item) @@ -221,8 +262,10 @@ export async function POST(req: NextRequest) { ? safeItem.timestamp.trim() : new Date().toISOString(); - const itemData = - safeItem.data !== undefined ? safeItem.data : safeItem; + const isEnvelope = + safeItem.data !== undefined && + Object.keys(safeItem).every((k) => envelopeKeys.has(k)); + const itemData = isEnvelope ? safeItem.data : safeItem; const val = SecurityEngine.validateEvent( clientId, @@ -251,18 +294,20 @@ export async function POST(req: NextRequest) { continue; } - const event: DataEvent = { + batchEvents.push({ clientId, eventId: itemEventId, timestamp: itemTimestamp, payload: typeof itemData === 'string' ? itemData : JSON.stringify(itemData), - }; - - DataBufferEngine.push(event); + }); processed += 1; } + if (batchEvents.length > 0) { + await DataBufferEngine.pushBatchAsync(batchEvents); + } + if (processed > 0) { SDKManager.incrementEventCount(clientId, processed); } @@ -347,7 +392,7 @@ export async function POST(req: NextRequest) { payload: typeof data === 'string' ? data : JSON.stringify(data), }; - DataBufferEngine.push(event); + await DataBufferEngine.pushAsync(event); SDKManager.incrementEventCount(clientId, 1); return NextResponse.json({ success: true, eventId, event }); diff --git a/app/api/export/route.ts b/app/api/export/route.ts index 7d31b8e..27eff34 100644 --- a/app/api/export/route.ts +++ b/app/api/export/route.ts @@ -1,4 +1,6 @@ +import { SettingsManager } from "@/core/adminSettings/settingsManager"; import { DataBufferEngine } from "@/core/data/buffer"; +import { getInternalIngestionHeaders } from "@/core/Security/security"; import { CSVStorageManager, type StorageFileMeta, @@ -6,6 +8,7 @@ import { import { parse } from "csv-parse"; import { NextRequest, NextResponse } from "next/server"; import { createReadStream } from "node:fs"; +import path from "node:path"; type ExportFormat = "csv" | "json"; type CsvRecord = Record; @@ -108,19 +111,26 @@ function streamFile(file: StorageFileMeta) { export async function GET(request: NextRequest) { try { - await DataBufferEngine.flush(); - const ingestionBaseUrl = - process.env.INGESTION_INTERNAL_URL || "http://127.0.0.1:4000"; - try { - await fetch(`${ingestionBaseUrl}/internal/flush`, { - method: "POST", - signal: AbortSignal.timeout(1500), - }); - } catch { - // Port 4000 may not be running in isolated single-process dev mode + const { searchParams } = new URL(request.url); + const shouldFlush = searchParams.get("flush") !== "false"; + + await SettingsManager.getSettingsAsync(); + if (shouldFlush) { + await DataBufferEngine.flush(); + const ingestionBaseUrl = ( + process.env.INGESTION_INTERNAL_URL || "http://127.0.0.1:4000" + ).replace(/\/$/, ""); + try { + await fetch(`${ingestionBaseUrl}/internal/flush`, { + method: "POST", + headers: getInternalIngestionHeaders(), + signal: AbortSignal.timeout(1500), + }); + } catch { + // Port 4000 may not be running in isolated single-process dev mode + } } - const { searchParams } = new URL(request.url); const clientId = searchParams.get("clientId") || "all"; const filename = searchParams.get("filename"); const from = searchParams.get("from") || ""; @@ -129,17 +139,33 @@ export async function GET(request: NextRequest) { searchParams.get("format") === "json" ? "json" : "csv"; const includeHeader = searchParams.get("includeHeader") !== "false"; - let files = CSVStorageManager.listAllFiles(); + let files = await CSVStorageManager.listAllFilesAsync(); if (clientId !== "all") files = files.filter((file) => file.clientId === clientId); if (filename) { - const file = files.find((candidate) => candidate.filename === filename); - if (!file) + const safeFilename = path.basename(filename); + const matches = files.filter( + (candidate) => candidate.filename === safeFilename, + ); + if (matches.length === 0) { return NextResponse.json( { error: "CSV file not found" }, { status: 404 }, ); + } + if (matches.length > 1) { + return NextResponse.json( + { + error: + "Ambiguous filename matches multiple clients. Specify clientId.", + code: "AMBIGUOUS_FILENAME", + matchingClients: matches.map((m) => m.clientId), + }, + { status: 400 }, + ); + } + const file = matches[0]; return new NextResponse(streamFile(file), { status: 200, headers: { diff --git a/app/api/health/route.ts b/app/api/health/route.ts index 8654d60..4359f46 100644 --- a/app/api/health/route.ts +++ b/app/api/health/route.ts @@ -23,7 +23,19 @@ export async function GET(req: NextRequest) { ); } - const clientId = verified.payload.clientId; + const clientId = String(verified.payload.clientId || '') + .trim() + .toLowerCase(); + if (SDKManager.isClientDeleted(clientId)) { + return NextResponse.json( + { + status: 'error', + code: 'CLIENT_REVOKED', + error: `CLIENT_REVOKED: Client '${clientId}' has been deleted by administrator.`, + }, + { status: 403 }, + ); + } const registeredClient = await SDKManager.getClientAsync(clientId); if (registeredClient) { if (registeredClient.status === 'revoked') { diff --git a/app/api/settings/route.ts b/app/api/settings/route.ts index 4cb6bbe..5d8c3b3 100644 --- a/app/api/settings/route.ts +++ b/app/api/settings/route.ts @@ -1,8 +1,55 @@ -import { SettingsManager } from "@/core/adminSettings/settingsManager"; +import { + SettingsManager, + type AdminSettings, +} from "@/core/adminSettings/settingsManager"; +import { verifyRequestAdminSession } from "@/core/auth/adminUsers"; import { NextRequest, NextResponse } from "next/server"; -export async function GET() { +const SESSION_COOKIE = "datastack_admin_session"; + +const ALLOWED_SETTING_KEYS = new Set([ + "maxEventsPerSec", + "maxPayloadSizeKb", + "batchFlushThreshold", + "batchFlushIntervalMs", + "fileRotationMb", + "enableIdempotency", + "autoPruneDays", + "jwtSecret", +]); + +function parseBoundedInt( + raw: unknown, + field: string, + min: number, + max: number, +): number { + if (typeof raw !== "number" || !Number.isInteger(raw)) { + throw new Error(`${field} must be an integer between ${min} and ${max}.`); + } + if (raw < min || raw > max) { + throw new Error(`${field} must be between ${min} and ${max}.`); + } + return raw; +} + +export async function GET(req: NextRequest) { try { + const auth = await verifyRequestAdminSession( + req.cookies.get(SESSION_COOKIE)?.value, + ); + if (!auth.valid) { + return NextResponse.json( + { + error: + auth.reason === "UNAUTHORIZED" + ? "Authentication required." + : "Admin authentication is temporarily unavailable.", + }, + { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, + ); + } + const settings = await SettingsManager.getPublicSettingsAsync(); return NextResponse.json({ settings }); } catch (err: unknown) { @@ -13,8 +60,133 @@ export async function GET() { export async function POST(req: NextRequest) { try { - const body = await req.json(); - await SettingsManager.updateSettingsAsync(body); + const auth = await verifyRequestAdminSession( + req.cookies.get(SESSION_COOKIE)?.value, + ); + if (!auth.valid) { + return NextResponse.json( + { + error: + auth.reason === "UNAUTHORIZED" + ? "Authentication required." + : "Admin authentication is temporarily unavailable.", + }, + { status: auth.reason === "UNAUTHORIZED" ? 401 : 503 }, + ); + } + + let rawBody: unknown; + try { + rawBody = await req.json(); + } catch { + return NextResponse.json( + { error: "Invalid JSON request payload.", code: "INVALID_JSON" }, + { status: 400 }, + ); + } + + if (!rawBody || typeof rawBody !== "object" || Array.isArray(rawBody)) { + return NextResponse.json( + { + error: "Settings payload must be a JSON object.", + code: "VALIDATION_ERROR", + }, + { status: 400 }, + ); + } + + const body = rawBody as Record; + const unknownKeys = Object.keys(body).filter( + (key) => !ALLOWED_SETTING_KEYS.has(key), + ); + if (unknownKeys.length > 0) { + return NextResponse.json( + { + error: `Unsupported settings field(s): ${unknownKeys.join(", ")}`, + code: "VALIDATION_ERROR", + }, + { status: 400 }, + ); + } + + const validated: Partial = {}; + try { + if (body.maxEventsPerSec !== undefined) { + validated.maxEventsPerSec = parseBoundedInt( + body.maxEventsPerSec, + "maxEventsPerSec", + 1, + 100000, + ); + } + if (body.maxPayloadSizeKb !== undefined) { + validated.maxPayloadSizeKb = parseBoundedInt( + body.maxPayloadSizeKb, + "maxPayloadSizeKb", + 1, + 10240, + ); + } + if (body.batchFlushThreshold !== undefined) { + validated.batchFlushThreshold = parseBoundedInt( + body.batchFlushThreshold, + "batchFlushThreshold", + 1, + 50000, + ); + } + if (body.batchFlushIntervalMs !== undefined) { + validated.batchFlushIntervalMs = parseBoundedInt( + body.batchFlushIntervalMs, + "batchFlushIntervalMs", + 100, + 60000, + ); + } + if (body.fileRotationMb !== undefined) { + validated.fileRotationMb = parseBoundedInt( + body.fileRotationMb, + "fileRotationMb", + 1, + 2048, + ); + } + if (body.autoPruneDays !== undefined) { + validated.autoPruneDays = parseBoundedInt( + body.autoPruneDays, + "autoPruneDays", + 0, + 3650, + ); + } + if (body.enableIdempotency !== undefined) { + if (typeof body.enableIdempotency !== "boolean") { + throw new Error("enableIdempotency must be a boolean."); + } + validated.enableIdempotency = body.enableIdempotency; + } + if (body.jwtSecret !== undefined) { + if (typeof body.jwtSecret !== "string" || body.jwtSecret.trim().length < 32) { + throw new Error( + "jwtSecret must be a string with at least 32 characters.", + ); + } + validated.jwtSecret = body.jwtSecret.trim(); + } + } catch (validationErr: unknown) { + return NextResponse.json( + { + error: + validationErr instanceof Error + ? validationErr.message + : "Invalid settings value.", + code: "VALIDATION_ERROR", + }, + { status: 400 }, + ); + } + + await SettingsManager.updateSettingsAsync(validated); const settings = await SettingsManager.getPublicSettingsAsync(); return NextResponse.json({ settings, diff --git a/app/api/storage/route.ts b/app/api/storage/route.ts index 9f098ea..9739161 100644 --- a/app/api/storage/route.ts +++ b/app/api/storage/route.ts @@ -1,4 +1,6 @@ import { NextRequest, NextResponse } from 'next/server'; +import { SettingsManager } from '@/core/adminSettings/settingsManager'; +import { getInternalIngestionHeaders } from '@/core/Security/security'; import { CSVStorageManager } from '@/core/Storage/csvStorage'; import { DataBufferEngine } from '@/core/data/buffer'; @@ -7,8 +9,11 @@ export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); const clientId = searchParams.get('clientId') || undefined; - const stats = CSVStorageManager.getStorageStats(); - let files = CSVStorageManager.listAllFiles(); + const [stats, allFiles] = await Promise.all([ + CSVStorageManager.getStorageStatsAsync(), + CSVStorageManager.listAllFilesAsync(), + ]); + let files = allFiles; if (clientId && clientId !== 'all') { files = files.filter((f) => f.clientId === clientId); @@ -23,12 +28,17 @@ export async function GET(req: NextRequest) { export async function POST(_req: NextRequest) { try { - // 1. Flush local Next.js DataBufferEngine + await SettingsManager.getSettingsAsync(); + + // 1. Flush local / shared Next.js DataBufferEngine const localRes = await DataBufferEngine.flush(); // 2. Also flush Port 4000 Socket.IO ingestion buffer let remoteCount = 0; let remoteFiles: string[] = []; + let ingestionFlushed = false; + let warning: string | undefined; + const explicitIngestionUrl = Boolean(process.env.INGESTION_INTERNAL_URL?.trim()); const ingestionBaseUrl = ( process.env.INGESTION_INTERNAL_URL || 'http://127.0.0.1:4000' ).replace(/\/$/, ''); @@ -36,17 +46,53 @@ export async function POST(_req: NextRequest) { try { const ingestRes = await fetch(`${ingestionBaseUrl}/internal/flush`, { method: 'POST', + headers: getInternalIngestionHeaders(), cache: 'no-store', + signal: AbortSignal.timeout(2500), }); - if (ingestRes.ok) { - const ingestData = await ingestRes.json(); - remoteCount = Number(ingestData.flushedCount) || 0; - if (Array.isArray(ingestData.filesUpdated)) { - remoteFiles = ingestData.filesUpdated; - } + if (!ingestRes.ok) { + const errPayload = await ingestRes.json().catch(() => ({})); + const remoteErr = + typeof errPayload?.error === 'string' + ? errPayload.error + : `Ingestion flush returned HTTP ${ingestRes.status}`; + return NextResponse.json( + { + success: false, + error: remoteErr, + code: 'INGESTION_FLUSH_FAILED', + flushedCount: localRes.count, + filesUpdated: localRes.files, + ingestionFlushed: false, + ingestionStatus: ingestRes.status, + }, + { status: 502 }, + ); } - } catch { - // Port 4000 server may be offline in standalone Next.js dev mode + + ingestionFlushed = true; + const ingestData = await ingestRes.json(); + remoteCount = Number(ingestData.flushedCount) || 0; + if (Array.isArray(ingestData.filesUpdated)) { + remoteFiles = ingestData.filesUpdated; + } + } catch (err: unknown) { + const errMsg = + err instanceof Error ? err.message : 'Ingestion server unreachable'; + if (explicitIngestionUrl) { + return NextResponse.json( + { + success: false, + error: `Ingestion server flush failed: ${errMsg}`, + code: 'INGESTION_UNREACHABLE', + flushedCount: localRes.count, + filesUpdated: localRes.files, + ingestionFlushed: false, + }, + { status: 502 }, + ); + } + warning = `Port 4000 ingestion server is unreachable (${errMsg}); flushed Next.js buffer only.`; } const combinedFiles = Array.from( @@ -57,6 +103,8 @@ export async function POST(_req: NextRequest) { success: true, flushedCount: localRes.count + remoteCount, filesUpdated: combinedFiles, + ingestionFlushed, + ...(warning ? { warning } : {}), }); } catch (err: unknown) { const message = err instanceof Error ? err.message : 'Manual flush failed'; diff --git a/app/error.tsx b/app/error.tsx new file mode 100644 index 0000000..0273cb2 --- /dev/null +++ b/app/error.tsx @@ -0,0 +1,50 @@ +'use client'; + +import { Button } from '@/components/ui/Button'; +import { AlertTriangle, RotateCcw } from 'lucide-react'; +import Link from 'next/link'; +import { useEffect } from 'react'; + +export default function RootErrorBoundary({ + error, + reset, +}: { + error: Error & { digest?: string }; + reset: () => void; +}) { + useEffect(() => { + console.error('Application error boundary:', error); + }, [error]); + + return ( +
+
+
+
+

+ An unexpected rendering error occurred. You can retry the view or return to the overview page. +

+ {error?.message && ( +
+ {error.message} +
+ )} +
+ + + + +
+
+
+ ); +} diff --git a/app/layout.tsx b/app/layout.tsx index 37377a6..b0c4e73 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -1,11 +1,31 @@ +import { PwaLifecycle } from '@/components/pwa/PwaLifecycle'; import { ToastProvider } from '@/components/ui/Toast'; -import type { Metadata } from 'next'; +import type { Metadata, Viewport } from 'next'; import './globals.css'; +export const viewport: Viewport = { + themeColor: '#0a0a0b', + width: 'device-width', + initialScale: 1, +}; + export const metadata: Metadata = { title: 'DataStack - Realtime Data Collection Platform', description: 'Self-hosted universal realtime data collection engine with Socket.IO ingestion, Redis buffer, JWT client auth, CSV batch rotation, and /vcon/ admin panel.', manifest: '/manifest.json', + appleWebApp: { + capable: true, + statusBarStyle: 'black-translucent', + title: 'DataStack', + }, + icons: { + icon: [ + { url: '/icon.svg', type: 'image/svg+xml' }, + { url: '/icon-192.png', sizes: '192x192', type: 'image/png' }, + { url: '/icon-512.png', sizes: '512x512', type: 'image/png' }, + ], + apple: [{ url: '/apple-touch-icon.png', sizes: '180x180', type: 'image/png' }], + }, openGraph: { title: 'DataStack - Realtime Data Collection Platform', description: 'Self-hosted universal realtime data collection engine with Socket.IO ingestion, Redis buffer, JWT client auth, CSV batch rotation, and /vcon/ admin panel.', @@ -21,12 +41,11 @@ export const metadata: Metadata = { export default function RootLayout({ children }: { children: React.ReactNode }) { return ( - - - - - {children} + + {children} + + ); diff --git a/app/not-found.tsx b/app/not-found.tsx index 01fe544..3ce7075 100644 --- a/app/not-found.tsx +++ b/app/not-found.tsx @@ -1,5 +1,5 @@ import { MainLayout } from '@/components/layout/MainLayout'; -import { Button } from '@/components/ui/Button'; +import { buttonClassNames } from '@/components/ui/Button'; import Link from 'next/link'; export default function NotFound() { @@ -11,10 +11,11 @@ export default function NotFound() { The requested page or route does not exist on DataStack.

- - + + Return Home + + + Open Console
diff --git a/app/vcon/clients/page.tsx b/app/vcon/clients/page.tsx index bac8ae2..da7b501 100644 --- a/app/vcon/clients/page.tsx +++ b/app/vcon/clients/page.tsx @@ -23,7 +23,11 @@ export default function ClientsPage() { const [now, setNow] = useState(() => Date.now()); const fetchClients = useCallback(async () => { - const response = await fetch('/api/clients'); + const response = await fetch('/api/clients', { cache: 'no-store' }); + if (response.status === 401) { + window.location.assign('/vcon/login'); + return; + } if (!response.ok) throw new Error('Unable to load clients'); const data = await response.json(); setClients(setCached('vcon:clients', data.clients || [])); @@ -49,16 +53,19 @@ export default function ClientsPage() { !query || client.clientId.toLowerCase().includes(query) || client.name.toLowerCase().includes(query); + const revoked = client.status === 'revoked'; const online = client.status === 'active' && Boolean(client.lastActive) && now - new Date(client.lastActive!).getTime() < 60_000; const pending = client.status === 'active' && !client.lastActive; + const offline = client.status === 'active' && !online && !pending; const matchesStatus = statusFilter === 'all' || (statusFilter === 'online' && online) || - (statusFilter === 'offline' && !online && !pending) || - (statusFilter === 'pending' && pending); + (statusFilter === 'offline' && offline) || + (statusFilter === 'pending' && pending) || + (statusFilter === 'revoked' && revoked); return matchesSearch && matchesStatus; }); }, [clients, now, search, statusFilter]); @@ -72,7 +79,10 @@ export default function ClientsPage() { const pendingCount = clients.filter( (client) => client.status === 'active' && !client.lastActive ).length; - const offlineCount = clients.length - onlineCount - pendingCount; + const revokedCount = clients.filter( + (client) => client.status === 'revoked' + ).length; + const offlineCount = clients.length - onlineCount - pendingCount - revokedCount; return ( @@ -109,6 +121,7 @@ export default function ClientsPage() { online: onlineCount, offline: offlineCount, pending: pendingCount, + revoked: revokedCount, }} /> void; +}) { + useEffect(() => { + console.error('Admin console error boundary:', error); + }, [error]); + + return ( +
+
+
+
+

+ This console view encountered an unexpected failure. Retry rendering or navigate back to the dashboard. +

+ {error?.message && ( +
+ {error.message} +
+ )} +
+ + + + +
+
+
+ ); +} diff --git a/app/vcon/login/page.tsx b/app/vcon/login/page.tsx index 30578fd..3d14267 100644 --- a/app/vcon/login/page.tsx +++ b/app/vcon/login/page.tsx @@ -6,12 +6,13 @@ export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; export default async function LoginPage() { - if (process.env.DATABASE_URL) { - const isConfigured = await isAdminSetupComplete().catch(() => false); - if (!isConfigured) redirect('/vcon/setup'); - } + const isConfigured = await isAdminSetupComplete().catch(() => false); + if (!isConfigured) redirect('/vcon/setup'); - const showDemoHint = !process.env.DATABASE_URL && process.env.NODE_ENV !== 'production'; + const showDemoHint = + !process.env.DATABASE_URL && + process.env.NODE_ENV !== 'production' && + process.env.DATASTACK_ALLOW_DEMO_ADMIN !== 'false'; return ; } diff --git a/app/vcon/sdk/page.tsx b/app/vcon/sdk/page.tsx index dce2a0c..f580043 100644 --- a/app/vcon/sdk/page.tsx +++ b/app/vcon/sdk/page.tsx @@ -11,15 +11,25 @@ import { useToast } from '@/components/ui/Toast'; import { getCached, setCached } from '@/lib/vconCache'; import { useEffect, useState } from 'react'; +interface SdkClientItem { + clientId: string; + name: string; + status?: 'active' | 'revoked'; + tokenPreview?: string; + token?: string; +} + export default function SdkGuidePage() { - const { success } = useToast(); - const [clients, setClients] = useState(() => - getCached('vcon:clients', []) + const { success, error } = useToast(); + const [clients, setClients] = useState(() => + getCached('vcon:clients', []) ); const [selectedClientId, setSelectedClientId] = useState(() => { - const cached = getCached('vcon:clients', []); - return cached[0]?.clientId ?? ''; + const cached = getCached('vcon:clients', []); + return cached.find((c) => c.status !== 'revoked')?.clientId ?? cached[0]?.clientId ?? ''; }); + const [selectedClientToken, setSelectedClientToken] = useState(''); + const [loadingToken, setLoadingToken] = useState(false); const [serverUrl, setServerUrl] = useState('http://localhost:4000'); const [apiUrl, setApiUrl] = useState('http://localhost:3000'); @@ -34,33 +44,81 @@ export default function SdkGuidePage() { async function loadClients() { try { - const res = await fetch('/api/clients'); + const res = await fetch('/api/clients', { cache: 'no-store' }); + if (res.status === 401) { + window.location.assign('/vcon/login'); + return; + } if (res.ok) { const data = await res.json(); - const list = setCached('vcon:clients', data.clients || []); + const list = setCached('vcon:clients', data.clients || []); setClients(list); if (list.length > 0) { setSelectedClientId((prev) => - list.some((c: any) => c.clientId === prev) ? prev : list[0].clientId + list.some((c) => c.clientId === prev) + ? prev + : list.find((c) => c.status !== 'revoked')?.clientId ?? list[0].clientId ); } } } catch { - // ignore + // Retain cached list on transient network error } } - loadClients(); + void loadClients(); }, []); + useEffect(() => { + if (!selectedClientId) { + setSelectedClientToken(''); + return; + } + + let cancelled = false; + setLoadingToken(true); + + async function fetchToken() { + try { + const res = await fetch( + `/api/clients/${encodeURIComponent(selectedClientId)}?includeToken=true`, + { cache: 'no-store' } + ); + if (res.status === 401) { + window.location.assign('/vcon/login'); + return; + } + if (res.ok) { + const data = await res.json(); + if (!cancelled) { + setSelectedClientToken(String(data?.client?.token || '')); + } + } else if (!cancelled) { + setSelectedClientToken(''); + } + } catch { + if (!cancelled) setSelectedClientToken(''); + } finally { + if (!cancelled) setLoadingToken(false); + } + } + + void fetchToken(); + return () => { + cancelled = true; + }; + }, [selectedClientId]); + const activeClient = clients.find((c) => c.clientId === selectedClientId) || clients[0]; - const sampleToken = activeClient?.token || 'YOUR_SIGNED_CLIENT_JWT_TOKEN'; + const resolvedToken = selectedClientToken || activeClient?.token || ''; + const sampleToken = resolvedToken || 'YOUR_SIGNED_CLIENT_JWT_TOKEN'; const configSnippet = `# config.datastack.py — Place in your Python project root DATASTACK_CONFIG = { "SERVER_URL": "${serverUrl}", "API_URL": "${apiUrl}", "CLIENT_TOKEN": "${sampleToken}", + "CLIENT_ID": "${activeClient?.clientId || ''}", "AUTO_CONNECT": True, "ENABLE_HTTP_FALLBACK": True, "HEARTBEAT_INTERVAL_SEC": 5.0, @@ -99,9 +157,21 @@ print("SDK Status:", client.status()) client.disconnect()`; - const copySnippet = (text: string, label: string) => { - navigator.clipboard.writeText(text); - success('Copied', `${label} copied`); + const copySnippet = async (text: string, label: string) => { + try { + if (!navigator?.clipboard?.writeText) { + throw new Error('Clipboard API unavailable in this browser context'); + } + await navigator.clipboard.writeText(text); + success('Copied', `${label} copied`); + } catch (copyErr) { + error( + 'Copy failed', + copyErr instanceof Error + ? copyErr.message + : 'Unable to copy to clipboard. Copy manually from the code block.' + ); + } }; return ( @@ -109,12 +179,13 @@ client.disconnect()`; title="Python SDK" actions={ <> - {activeClient?.token && ( + {resolvedToken && ( @@ -123,7 +194,7 @@ client.disconnect()`; size="xs" variant="secondary" icon="FileCode" - onClick={() => copySnippet(configSnippet, 'config.datastack.py')} + onClick={() => void copySnippet(configSnippet, 'config.datastack.py')} > Copy Config @@ -131,7 +202,7 @@ client.disconnect()`; size="xs" variant="secondary" icon="Copy" - onClick={() => copySnippet(pythonSnippet, 'Python SDK snippet')} + onClick={() => void copySnippet(pythonSnippet, 'Python SDK snippet')} > Copy Code @@ -161,8 +232,9 @@ client.disconnect()`; size="xs" variant="ghost" icon="Copy" + aria-label="Copy pip install command" onClick={() => - copySnippet('pip install ./sdk/python/datastack', 'Pip command') + void copySnippet('pip install ./sdk/python/datastack', 'Pip command') } /> @@ -176,6 +248,7 @@ client.disconnect()`;
setSelectedClient(e.target.value)} > - {clients.map((c) => ( - - ))} + {clients.length === 0 ? ( + + ) : ( + clients.map((c) => ( + + )) + )}