feat(auth): harden session management and security

- Implement server-side session revocation on logout
- Add IP extraction utility and admin setup error handling
- Improve error codes and response consistency for auth endpoints
- Refine landing page redirect logic to handle transient DB outages
- Update deployment documentation and configuration defaults
This commit is contained in:
Team Dark Devil committed 2026-11-19 20:22:01 +06:00
1 parent 935f12a1e9
commit 89e4d68690
81 files changed
+11530 -6916

No files matched your search

+46 -8
View File
@@ -1,12 +1,50 @@
'use client';
import { MainLayout } from '@/components/layout/MainLayout';
import { usePathname } from 'next/navigation';
import { VconShell } from '@/components/layout/VconShell';
import { verifyRequestAdminSession } from '@/core/auth/adminUsers';
import { cookies, headers } from 'next/headers';
import { redirect } from 'next/navigation';
import React from 'react';
export default function VconLayout({ children }: { children: React.ReactNode }) {
const pathname = usePathname();
if (pathname === '/vcon/login' || pathname === '/vcon/setup') return <>{children}</>;
export const dynamic = 'force-dynamic';
return <MainLayout showSidebar={true}>{children}</MainLayout>;
export default async function VconLayout({
children,
}: {
children: React.ReactNode;
}) {
const headerList = await headers();
const pathname = headerList.get('x-datastack-pathname') || '';
if (pathname === '/vcon/login' || pathname === '/vcon/setup') {
return <>{children}</>;
}
const cookieStore = await cookies();
const token = cookieStore.get('datastack_admin_session')?.value;
const auth = await verifyRequestAdminSession(token);
if (!auth.valid) {
if (auth.reason === 'UNAUTHORIZED') {
const loginTarget = pathname
? `/vcon/login?next=${encodeURIComponent(pathname)}`
: '/vcon/login';
redirect(loginTarget);
}
return (
<main className="grid min-h-dvh place-items-center bg-[var(--bg)] p-4 text-[var(--text)]">
<section className="w-full max-w-sm border border-[var(--border)] bg-[var(--surface)] p-5">
<h1 className="text-[13px] font-medium text-[var(--text)]">
Console authentication unavailable
</h1>
<p className="mt-2 text-[12px] text-[var(--text-muted)]">
{auth.reason === 'UNCONFIGURED'
? 'Admin session secret is not configured.'
: 'Database authentication check is temporarily unavailable. Please retry shortly.'}
</p>
</section>
</main>
);
}
return <VconShell>{children}</VconShell>;
}
+32 -4
View File
@@ -1,18 +1,46 @@
import { AdminLogin } from '@/components/sections/AdminLogin';
import { isAdminSetupComplete } from '@/core/auth/adminUsers';
import {
isAdminSetupComplete,
verifyRequestAdminSession,
} from '@/core/auth/adminUsers';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';
export default async function LoginPage() {
const isConfigured = await isAdminSetupComplete().catch(() => false);
if (!isConfigured) redirect('/vcon/setup');
let databaseReady = true;
try {
const isConfigured = await isAdminSetupComplete();
if (!isConfigured) redirect('/vcon/setup');
} catch (error) {
if (
error &&
typeof error === 'object' &&
'digest' in error &&
String((error as { digest?: unknown }).digest).startsWith('NEXT_REDIRECT')
) {
throw error;
}
databaseReady = false;
}
if (databaseReady) {
const cookieStore = await cookies();
const token = cookieStore.get('datastack_admin_session')?.value;
if (token) {
const auth = await verifyRequestAdminSession(token);
if (auth.valid) {
redirect('/vcon/dashboard');
}
}
}
const showDemoHint =
!process.env.DATABASE_URL &&
process.env.NODE_ENV !== 'production' &&
process.env.DATASTACK_ALLOW_DEMO_ADMIN !== 'false';
return <AdminLogin showDemoHint={showDemoHint} />;
return <AdminLogin showDemoHint={showDemoHint} databaseReady={databaseReady} />;
}
+62 -29
View File
@@ -9,6 +9,7 @@ import { Panel } from '@/components/ui/Panel';
import { StatStrip } from '@/components/ui/StatStrip';
import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import { resolvePublicEndpoints } from '@/lib/vconUiHelpers';
import { useEffect, useState } from 'react';
interface SdkClientItem {
@@ -34,23 +35,23 @@ export default function SdkGuidePage() {
const [apiUrl, setApiUrl] = useState<string>('http://localhost:3000');
useEffect(() => {
const isLocalDev = window.location.port === '3000';
setServerUrl(
isLocalDev
? `${window.location.protocol}//${window.location.hostname}:4000`
: window.location.origin
);
setApiUrl(window.location.origin);
const initialPort = getCached<number>('vcon:server-port', 4000);
const initialEndpoints = resolvePublicEndpoints(window.location, initialPort);
setServerUrl(initialEndpoints.serverUrl);
setApiUrl(initialEndpoints.apiUrl);
async function loadClients() {
async function loadSdkContext() {
try {
const res = await fetch('/api/clients', { cache: 'no-store' });
if (res.status === 401) {
const [clientsRes, metricsRes] = await Promise.all([
fetch('/api/clients', { cache: 'no-store' }),
fetch('/api/metrics', { cache: 'no-store' }).catch(() => null),
]);
if (clientsRes.status === 401 || metricsRes?.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (res.ok) {
const data = await res.json();
if (clientsRes.ok) {
const data = await clientsRes.json();
const list = setCached<SdkClientItem[]>('vcon:clients', data.clients || []);
setClients(list);
if (list.length > 0) {
@@ -61,11 +62,21 @@ export default function SdkGuidePage() {
);
}
}
if (metricsRes && metricsRes.ok) {
const metricsData = await metricsRes.json().catch(() => null);
const runtimePort = Number(metricsData?.runtime?.serverPort);
if (Number.isFinite(runtimePort) && runtimePort > 0) {
setCached('vcon:server-port', runtimePort);
const resolved = resolvePublicEndpoints(window.location, runtimePort);
setServerUrl(resolved.serverUrl);
setApiUrl(resolved.apiUrl);
}
}
} catch {
// Retain cached list on transient network error
}
}
void loadClients();
void loadSdkContext();
}, []);
useEffect(() => {
@@ -75,6 +86,7 @@ export default function SdkGuidePage() {
}
let cancelled = false;
setSelectedClientToken('');
setLoadingToken(true);
async function fetchToken() {
@@ -110,8 +122,12 @@ export default function SdkGuidePage() {
const activeClient =
clients.find((c) => c.clientId === selectedClientId) || clients[0];
const resolvedToken = selectedClientToken || activeClient?.token || '';
const sampleToken = resolvedToken || 'YOUR_SIGNED_CLIENT_JWT_TOKEN';
const resolvedToken = loadingToken
? ''
: selectedClientToken || activeClient?.token || '';
const sampleToken = loadingToken
? 'LOADING_CLIENT_JWT_TOKEN...'
: resolvedToken || 'YOUR_SIGNED_CLIENT_JWT_TOKEN';
const configSnippet = `# config.datastack.py — Place in your Python project root
DATASTACK_CONFIG = {
@@ -194,6 +210,7 @@ client.disconnect()`;
size="xs"
variant="secondary"
icon="FileCode"
disabled={loadingToken}
onClick={() => void copySnippet(configSnippet, 'config.datastack.py')}
>
Copy Config
@@ -216,27 +233,42 @@ client.disconnect()`;
{ label: 'Server endpoint', value: serverUrl },
{
label: 'Active client',
value: activeClient?.clientId || 'None selected',
value: activeClient
? activeClient.status === 'revoked'
? `${activeClient.clientId} (revoked)`
: activeClient.clientId
: 'None selected',
dot: activeClient?.status === 'revoked' ? 'off' : activeClient ? 'ok' : undefined,
},
]}
/>
<Card
compact
className="flex w-full items-center justify-between bg-[var(--surface)] px-3 py-2 font-mono text-[11.5px]"
className="flex w-full flex-col gap-1.5 bg-[var(--surface)] px-3 py-2.5"
>
<span className="text-[var(--accent)]">
pip install ./sdk/python/datastack
</span>
<Button
size="xs"
variant="ghost"
icon="Copy"
aria-label="Copy pip install command"
onClick={() =>
void copySnippet('pip install ./sdk/python/datastack', 'Pip command')
}
/>
<div className="flex items-center justify-between gap-2 font-mono text-[11.5px]">
<span className="truncate text-[var(--accent)]">
pip install /path/to/datastack/sdk/python/datastack
</span>
<Button
size="xs"
variant="ghost"
icon="Copy"
aria-label="Copy pip install command"
onClick={() =>
void copySnippet(
'pip install /path/to/datastack/sdk/python/datastack',
'Pip command'
)
}
/>
</div>
<p className="text-[11px] text-[var(--text-faint)]">
Self-hosted package bundled in <code className="font-mono text-[var(--text-muted)]">sdk/python/datastack</code>. From inside the repo run{' '}
<code className="font-mono text-[var(--text-muted)]">pip install ./sdk/python/datastack</code>, or build a portable wheel with{' '}
<code className="font-mono text-[var(--text-muted)]">pip wheel ./sdk/python/datastack -w dist/</code> for external projects.
</p>
</Card>
<Panel
@@ -265,6 +297,7 @@ client.disconnect()`;
variant="ghost"
icon="Copy"
aria-label="Copy config.datastack.py"
disabled={loadingToken}
onClick={() => void copySnippet(configSnippet, 'config.datastack.py')}
/>
</div>
+22 -4
View File
@@ -1,5 +1,8 @@
import { AdminSetup } from '@/components/sections/AdminSetup';
import { isAdminSetupComplete } from '@/core/auth/adminUsers';
import {
isAdminSetupComplete,
requiresPersistentAdminDatabase,
} from '@/core/auth/adminUsers';
import { redirect } from 'next/navigation';
export const runtime = 'nodejs';
@@ -7,15 +10,30 @@ export const dynamic = 'force-dynamic';
export default async function SetupPage() {
let databaseReady = true;
try {
if (await isAdminSetupComplete()) redirect('/vcon/login');
} catch {
let databaseError = '';
let setupComplete = false;
if (requiresPersistentAdminDatabase()) {
databaseReady = false;
databaseError =
'PostgreSQL DATABASE_URL is required in production before creating an administrator account.';
} else {
try {
setupComplete = await isAdminSetupComplete();
} catch {
databaseReady = false;
databaseError = 'Database connection unavailable.';
}
}
if (databaseReady && setupComplete) {
redirect('/vcon/login');
}
return (
<AdminSetup
databaseReady={databaseReady}
databaseError={databaseError}
initialEmail={process.env.DATASTACK_ADMIN_EMAIL?.trim().toLowerCase() ?? ''}
/>
);
+62 -56
View File
@@ -9,6 +9,10 @@ import { Pill } from '@/components/ui/Pill';
import { StatStrip } from '@/components/ui/StatStrip';
import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import {
resolvePublicEndpoints,
summarizeHttpBatchResult,
} from '@/lib/vconUiHelpers';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import { io } from 'socket.io-client';
@@ -22,11 +26,8 @@ interface SimulatorClient {
type TransportMode = 'http' | 'websocket';
type DispatchStatus = 'Ready' | 'Accepted' | 'Rejected' | 'Error';
function getSocketOrigin(): string {
const isLocalDev = window.location.port === '3000';
const protocol = window.location.protocol === 'https:' ? 'https:' : 'http:';
const host = isLocalDev ? `${window.location.hostname}:4000` : window.location.host;
return `${protocol}//${host}`;
function getSocketOrigin(serverPort: number): string {
return resolvePublicEndpoints(window.location, serverPort).serverUrl;
}
export default function SimulatorPage() {
@@ -52,20 +53,26 @@ export default function SimulatorPage() {
2
)
);
const [serverPort, setServerPort] = useState<number>(() =>
getCached<number>('vcon:server-port', 4000)
);
const [sending, setSending] = useState(false);
const [dispatchStatus, setDispatchStatus] = useState<DispatchStatus>('Ready');
const [lastResponse, setLastResponse] = useState<Record<string, unknown> | null>(null);
useEffect(() => {
async function loadClients() {
async function loadSimulatorContext() {
try {
const res = await fetch('/api/clients', { cache: 'no-store' });
if (res.status === 401) {
const [clientsRes, metricsRes] = await Promise.all([
fetch('/api/clients', { cache: 'no-store' }),
fetch('/api/metrics', { cache: 'no-store' }).catch(() => null),
]);
if (clientsRes.status === 401 || metricsRes?.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (res.ok) {
const data = await res.json();
if (clientsRes.ok) {
const data = await clientsRes.json();
const list = setCached<SimulatorClient[]>('vcon:clients', data.clients || []);
setClients(list);
setSelectedClient((prev) => {
@@ -73,11 +80,18 @@ export default function SimulatorPage() {
return list.find((c) => c.status === 'active')?.clientId ?? list[0]?.clientId ?? '';
});
}
if (metricsRes && metricsRes.ok) {
const metricsData = await metricsRes.json().catch(() => null);
const runtimePort = Number(metricsData?.runtime?.serverPort);
if (Number.isFinite(runtimePort) && runtimePort > 0) {
setServerPort(setCached('vcon:server-port', runtimePort));
}
}
} catch {
// Ignore transient failure and retain cached list
}
}
void loadClients();
void loadSimulatorContext();
}, []);
const fetchClientJwt = async (clientId: string): Promise<string> => {
@@ -100,7 +114,7 @@ export default function SimulatorPage() {
items: Array<Record<string, unknown>>
): Promise<{ accepted: number; rejected: number; responses: Record<string, unknown>[] }> => {
const token = await fetchClientJwt(clientId);
const socketUrl = getSocketOrigin();
const socketUrl = getSocketOrigin(serverPort);
return new Promise((resolve, reject) => {
const socket = io(socketUrl, {
@@ -112,7 +126,7 @@ export default function SimulatorPage() {
const timer = window.setTimeout(() => {
socket.disconnect();
reject(new Error('WebSocket timeout (is Port 4000 ingestion server running?)'));
reject(new Error(`WebSocket timeout (is Port ${serverPort} ingestion server running?)`));
}, 5000);
socket.on('connect_error', (connectErr) => {
@@ -271,55 +285,47 @@ export default function SimulatorPage() {
return;
}
let accepted = 0;
let rejected = 0;
const errors: string[] = [];
for (const item of items) {
const res = await fetch('/api/events', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
clientId: selectedClient,
data: item,
}),
});
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
if (res.ok) {
accepted++;
} else {
rejected++;
try {
const errJson = await res.json();
if (errJson?.error && errors.length < 3) {
errors.push(String(errJson.error));
}
} catch {
// ignore non-json error body
}
}
const batchPayload = items.map((item, idx) => ({
eventId: `sim_http_${Date.now()}_${idx}`,
timestamp: item.timestamp,
data: item,
}));
const res = await fetch('/api/events', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
clientId: selectedClient,
batch: batchPayload,
}),
});
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
const data = await res.json().catch(() => ({}));
const summary = summarizeHttpBatchResult(
res.ok,
data && typeof data === 'object' ? (data as Record<string, unknown>) : null,
count,
);
setLastResponse({
transport: 'http',
status: res.status,
batchSent: count,
accepted,
rejected,
...(errors.length > 0 ? { errors } : {}),
accepted: summary.accepted,
rejected: summary.rejected,
duplicates: summary.duplicates,
...(data && typeof data === 'object' ? data : {}),
});
if (accepted === 0) {
setDispatchStatus('Rejected');
error('Batch Rejected', `0/${count} ingested${errors[0] ? ` (${errors[0]})` : ''}`);
} else if (accepted < count) {
setDispatchStatus('Rejected');
warning('Batch Partial', `${accepted}/${count} ingested (${rejected} rejected)`);
setDispatchStatus(summary.status);
if (summary.level === 'error') {
error(summary.title, summary.message);
} else if (summary.level === 'warning') {
warning(summary.title, summary.message);
} else {
setDispatchStatus('Accepted');
success('Batch Streamed', `${accepted}/${count} ingested`);
success(summary.title, summary.message);
}
} catch (err: unknown) {
const message = err instanceof Error ? err.message : 'Batch dispatch error';
@@ -379,7 +385,7 @@ export default function SimulatorPage() {
},
{
label: 'Ingest transport',
value: transport === 'http' ? 'POST /api/events' : 'WS Port 4000',
value: transport === 'http' ? 'POST /api/events' : `WS Port ${serverPort}`,
},
{ label: 'Payload format', value: 'JSON' },
{
@@ -412,7 +418,7 @@ export default function SimulatorPage() {
active={transport === 'websocket'}
onClick={() => setTransport('websocket')}
>
WebSocket Port 4000
WebSocket Port {serverPort}
</Pill>
</div>
</div>
+71 -24
View File
@@ -18,52 +18,96 @@ import { useToast } from '@/components/ui/Toast';
import { getCached, setCached } from '@/lib/vconCache';
import { useCallback, useEffect, useState } from 'react';
interface StorageStatsSummary {
totalMB?: string;
totalFiles?: number;
totalRows?: number;
rotationRule?: string;
rotationMb?: number;
}
interface StoragePartitionFile {
clientId: string;
filename: string;
sizeBytes: number;
linesCount: number;
modifiedAt: string;
}
export default function StoragePage() {
const { success, warning, error } = useToast();
const [stats, setStats] = useState<any>(() =>
getCached<any>('vcon:storage-stats', null)
const [stats, setStats] = useState<StorageStatsSummary | null>(() =>
getCached<StorageStatsSummary | null>('vcon:storage-stats', null)
);
const [files, setFiles] = useState<any[]>(() =>
getCached<any[]>('vcon:storage-files', [])
const [files, setFiles] = useState<StoragePartitionFile[]>(() =>
getCached<StoragePartitionFile[]>('vcon:storage-files', [])
);
const [refreshing, setRefreshing] = useState(false);
const [flushing, setFlushing] = useState(false);
const fetchStorageData = useCallback(async () => {
setRefreshing(true);
try {
const res = await fetch('/api/storage');
if (!res.ok) throw new Error('Unable to load storage partitions');
const data = await res.json();
setStats(setCached('vcon:storage-stats', data.stats));
const res = await fetch('/api/storage', { cache: 'no-store' });
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
const data = await res.json().catch(() => ({}));
if (!res.ok) {
throw new Error(data.error || 'Unable to load storage partitions');
}
setStats(setCached('vcon:storage-stats', data.stats ?? null));
setFiles(setCached('vcon:storage-files', data.files || []));
} catch {
error('Storage load failed', 'Could not load storage partitions.');
} catch (loadErr) {
error(
'Storage load failed',
loadErr instanceof Error
? loadErr.message
: 'Could not load storage partitions.'
);
} finally {
setRefreshing(false);
}
}, [error]);
useEffect(() => {
fetchStorageData();
void fetchStorageData();
}, [fetchStorageData]);
const handleManualFlush = async () => {
setFlushing(true);
try {
const res = await fetch('/api/storage', { method: 'POST' });
const res = await fetch('/api/storage', {
method: 'POST',
cache: 'no-store',
});
if (res.status === 401) {
window.location.assign('/vcon/login');
return;
}
const data = await res.json().catch(() => ({}));
if (res.ok && data.success) {
const count = Number(data.flushedCount) || 0;
success(
'Buffer Flushed',
`Flushed ${count} event${count === 1 ? '' : 's'} to CSV${data.warning ? ` (${data.warning})` : ''}`
);
fetchStorageData();
} else if (data.flushedCount !== undefined && data.flushedCount > 0) {
// Partial flush: local events persisted, remote Port 4000 flush returned error
if (data.warning) {
warning(
'Partial Flush (Web Only)',
`Flushed ${count} event${count === 1 ? '' : 's'}. ${String(data.warning)}`
);
} else {
success(
'Buffer Flushed',
`Flushed ${count} event${count === 1 ? '' : 's'} to CSV.`
);
}
await fetchStorageData();
} else if (data.flushedCount !== undefined && Number(data.flushedCount) > 0) {
warning(
'Partial Flush',
`Flushed ${data.flushedCount} local events. Ingestion server: ${data.error || 'flush failed'}`
);
fetchStorageData();
await fetchStorageData();
} else {
error('Flush Failed', data.error || 'Could not flush buffer');
}
@@ -84,7 +128,9 @@ export default function StoragePage() {
size="xs"
variant="secondary"
icon="RotateCw"
onClick={fetchStorageData}
loading={refreshing}
disabled={refreshing || flushing}
onClick={() => void fetchStorageData()}
>
Refresh
</Button>
@@ -93,7 +139,8 @@ export default function StoragePage() {
variant="primary"
icon="HardDrive"
loading={flushing}
onClick={handleManualFlush}
disabled={flushing}
onClick={() => void handleManualFlush()}
>
Flush Buffer
</Button>
@@ -131,8 +178,8 @@ export default function StoragePage() {
{files.length === 0 ? (
<TableEmpty colSpan={5} message="No CSV files" />
) : (
files.map((file, idx) => (
<TableRow key={idx}>
files.map((file) => (
<TableRow key={`${file.clientId}/${file.filename}`}>
<TableCell className="font-mono text-[var(--accent)]">
{file.clientId}
</TableCell>