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
This commit is contained in:
1 parent
34519c07df
commit
177ed4a844
78 files changed
+6748
-3157
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { CopyButton } from '@/components/ui/CopyButton';
|
||||
import { Textarea } from '@/components/ui/Input';
|
||||
import { Input, Textarea } from '@/components/ui/Input';
|
||||
import { Modal } from '@/components/ui/Modal';
|
||||
import {
|
||||
Table,
|
||||
@@ -43,8 +43,61 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
type: 'rotate' | 'revoke' | 'delete';
|
||||
client: ClientItem;
|
||||
} | null>(null);
|
||||
const [editClient, setEditClient] = useState<ClientItem | null>(null);
|
||||
const [editName, setEditName] = useState('');
|
||||
const [editRateLimit, setEditRateLimit] = useState('100');
|
||||
const [editError, setEditError] = useState('');
|
||||
const [processing, setProcessing] = useState(false);
|
||||
|
||||
const openEditModal = (client: ClientItem) => {
|
||||
setEditClient(client);
|
||||
setEditName(client.name);
|
||||
setEditRateLimit(String(client.rateLimit || 100));
|
||||
setEditError('');
|
||||
};
|
||||
|
||||
const executeEditClient = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
if (!editClient) return;
|
||||
|
||||
const trimmedName = editName.trim();
|
||||
const numericRateLimit = Number(editRateLimit);
|
||||
if (!trimmedName) {
|
||||
setEditError('Display name is required.');
|
||||
return;
|
||||
}
|
||||
if (!Number.isFinite(numericRateLimit) || numericRateLimit < 1 || numericRateLimit > 10000) {
|
||||
setEditError('Rate limit must be between 1 and 10,000 events/s.');
|
||||
return;
|
||||
}
|
||||
|
||||
setEditError('');
|
||||
setProcessing(true);
|
||||
try {
|
||||
const response = await fetch(`/api/clients/${encodeURIComponent(editClient.clientId)}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
name: trimmedName,
|
||||
rateLimit: Math.floor(numericRateLimit),
|
||||
}),
|
||||
});
|
||||
if (response.status === 401) {
|
||||
window.location.assign('/vcon/login');
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!response.ok) throw new Error(data.error || 'Unable to update client');
|
||||
success('Client updated', `${editClient.clientId} settings saved.`);
|
||||
setEditClient(null);
|
||||
await onStatusChange();
|
||||
} catch (updateError) {
|
||||
error('Client update failed', updateError instanceof Error ? updateError.message : 'Unknown error');
|
||||
} finally {
|
||||
setProcessing(false);
|
||||
}
|
||||
};
|
||||
|
||||
const executeRotateKey = async (client: ClientItem) => {
|
||||
setProcessing(true);
|
||||
try {
|
||||
@@ -53,6 +106,10 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ action: 'regenerate_token' }),
|
||||
});
|
||||
if (response.status === 401) {
|
||||
window.location.assign('/vcon/login');
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!response.ok) throw new Error(data.error || 'Unable to rotate credentials');
|
||||
setConfirmAction(null);
|
||||
@@ -74,6 +131,10 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ status }),
|
||||
});
|
||||
if (response.status === 401) {
|
||||
window.location.assign('/vcon/login');
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!response.ok) throw new Error(data.error || 'Unable to update client status');
|
||||
setConfirmAction(null);
|
||||
@@ -92,6 +153,10 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
const response = await fetch(`/api/clients/${encodeURIComponent(client.clientId)}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
if (response.status === 401) {
|
||||
window.location.assign('/vcon/login');
|
||||
return;
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!response.ok) throw new Error(data.error || 'Unable to delete client');
|
||||
setConfirmAction(null);
|
||||
@@ -130,7 +195,7 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Client</TableHead>
|
||||
<TableHead>API key</TableHead>
|
||||
<TableHead>Credentials</TableHead>
|
||||
<TableHead className="text-right">Events</TableHead>
|
||||
<TableHead>Last seen</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
@@ -141,15 +206,18 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
<TableEmpty colSpan={5} message="No matching clients" />
|
||||
) : (
|
||||
clients.map((client) => {
|
||||
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 label = pending ? 'pending' : online ? 'online' : 'offline';
|
||||
const dotClass = online
|
||||
? 'bg-[var(--accent)]'
|
||||
: pending
|
||||
? 'bg-[var(--warning)]'
|
||||
: 'bg-[var(--text-faint)]';
|
||||
const label = revoked ? 'revoked' : pending ? 'pending' : online ? 'online' : 'offline';
|
||||
const dotClass = revoked
|
||||
? 'bg-[var(--danger)]'
|
||||
: online
|
||||
? 'bg-[var(--accent)]'
|
||||
: pending
|
||||
? 'bg-[var(--warning)]'
|
||||
: 'bg-[var(--text-faint)]';
|
||||
|
||||
return (
|
||||
<TableRow key={client.clientId}>
|
||||
@@ -157,13 +225,25 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
<div className="flex min-w-0 items-center gap-2">
|
||||
<span className={`h-1.5 w-1.5 shrink-0 ${dotClass}`} title={label} />
|
||||
<div className="min-w-0 leading-tight">
|
||||
<div className="truncate font-medium text-[var(--text)]">{client.name}</div>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="truncate font-medium text-[var(--text)]">{client.name}</span>
|
||||
{revoked && (
|
||||
<span className="border border-[var(--danger)] bg-[var(--danger-soft)] px-1 py-0 text-[9px] uppercase tracking-wider text-[var(--danger)]">
|
||||
revoked
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="mt-0.5 truncate font-mono text-[10.5px] text-[var(--text-faint)]">{client.clientId}</div>
|
||||
</div>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="font-mono">
|
||||
sec_••••••••
|
||||
<div className="truncate text-[11px] text-[var(--text)]">
|
||||
{client.tokenPreview || 'JWT Active'}
|
||||
</div>
|
||||
<div className="mt-0.5 text-[10px] text-[var(--text-faint)]">
|
||||
{client.rateLimit}/s limit
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-right font-mono tabular-nums">
|
||||
{client.totalEvents.toLocaleString()}
|
||||
@@ -184,6 +264,16 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
aria-label={`View events for ${client.clientId}`}
|
||||
onClick={() => router.push(`/vcon/data?clientId=${encodeURIComponent(client.clientId)}`)}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="icon"
|
||||
size="icon"
|
||||
icon="Pencil"
|
||||
className="h-6 w-6 border border-transparent hover:border-[var(--border)] hover:bg-[var(--surface-2)]"
|
||||
title={`Edit ${client.clientId}`}
|
||||
aria-label={`Edit ${client.clientId}`}
|
||||
onClick={() => openEditModal(client)}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="icon"
|
||||
@@ -224,6 +314,55 @@ export function ClientsTable({ clients, now, onStatusChange }: ClientsTableProps
|
||||
</Table>
|
||||
</TableWrap>
|
||||
|
||||
<Modal
|
||||
isOpen={Boolean(editClient)}
|
||||
onClose={() => !processing && setEditClient(null)}
|
||||
title={`Edit client: ${editClient?.clientId || ''}`}
|
||||
maxWidth="xs"
|
||||
footer={
|
||||
<>
|
||||
<Button type="button" variant="ghost" size="xs" disabled={processing} onClick={() => setEditClient(null)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
form="edit-client-form"
|
||||
variant="primary"
|
||||
size="xs"
|
||||
loading={processing}
|
||||
>
|
||||
Save changes
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<form id="edit-client-form" onSubmit={executeEditClient} className="flex flex-col gap-2.5">
|
||||
<Input
|
||||
label="Display name"
|
||||
compact
|
||||
value={editName}
|
||||
onChange={(event) => setEditName(event.target.value)}
|
||||
placeholder="Service display name"
|
||||
required
|
||||
/>
|
||||
<Input
|
||||
label="Rate limit (events/s)"
|
||||
compact
|
||||
type="number"
|
||||
min="1"
|
||||
max="10000"
|
||||
value={editRateLimit}
|
||||
onChange={(event) => setEditRateLimit(event.target.value)}
|
||||
required
|
||||
/>
|
||||
{editError && (
|
||||
<p role="alert" className="text-[11px] text-[var(--danger)]">
|
||||
{editError}
|
||||
</p>
|
||||
)}
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
isOpen={Boolean(confirmAction)}
|
||||
onClose={() => !processing && setConfirmAction(null)}
|
||||
|
||||
Reference in new issue
Block a user