Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0cccceccd9 | ||
|
|
40b553016c | ||
|
|
4894810fd4 | ||
|
|
ac21315255 | ||
|
|
ec1b227631 | ||
|
|
8f4e08b6f1 | ||
|
|
0c4e3a2227 | ||
|
|
1f41ea0166 | ||
|
|
5efdd2c07d | ||
|
|
6ae87a2cdb | ||
|
|
604e718cdc | ||
|
|
0bbc656263 | ||
|
|
a98734171c | ||
|
|
a61057cd81 | ||
|
|
55d08e77ed | ||
|
|
1f084b817c | ||
|
|
80050cd2a8 | ||
|
|
75dda1e7d6 | ||
|
|
76169920d6 |
@@ -209,6 +209,7 @@ the `/file/{cid}` reverse-lookup path.)
|
||||
|
||||
```bash
|
||||
GET /api/metadata/hash-ids
|
||||
GET /api/files/tuples
|
||||
GET /api/metadata/list
|
||||
POST /api/metadata/search
|
||||
POST /api/metadata/batch
|
||||
@@ -287,6 +288,10 @@ return a deprecation pointer at the `/api/watchers/*` routes
|
||||
|
||||
```bash
|
||||
POST /api/admin/migrate-dual-roots # reunify stranded midhash-rooted entries
|
||||
POST /api/admin/sweep-literature-echoes[?apply=true]
|
||||
# strip query echoes off literature cards: anilistid on site cards,
|
||||
# workcid/chapterNumber/volumeNumber/chapterStart/chapterEnd on any card.
|
||||
# Dry-run unless apply=true; returns {dryRun, planned, fixed, byKey}
|
||||
GET/PUT/DELETE /webdav/... # mounted on /files
|
||||
```
|
||||
|
||||
|
||||
Vendored
+1
-1
@@ -21,7 +21,7 @@
|
||||
min-height: 100vh;
|
||||
}
|
||||
</style>
|
||||
<script type="module" crossorigin src="/assets/index-Cbd1uH5A.js"></script>
|
||||
<script type="module" crossorigin src="/assets/index-BW9EQGCP.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="/assets/index-DtR_2V4s.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
Generated
+10
@@ -8,6 +8,7 @@
|
||||
"name": "@meta-core/dashboard",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@noble/secp256k1": "^3.1.0",
|
||||
"iso-639-3": "^3.0.1",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
@@ -744,6 +745,15 @@
|
||||
"@jridgewell/sourcemap-codec": "^1.4.14"
|
||||
}
|
||||
},
|
||||
"node_modules/@noble/secp256k1": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/@noble/secp256k1/-/secp256k1-3.1.0.tgz",
|
||||
"integrity": "sha512-+F7iS7tUMaNGXcc9X3PjmjvuQnXEuSjCRNzVVA2xAcKXgCaP0dHYz4SFyt4FKNHef7sOP//xihowcySSS7PK9g==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://paulmillr.com/funding/"
|
||||
}
|
||||
},
|
||||
"node_modules/@remix-run/router": {
|
||||
"version": "1.23.2",
|
||||
"resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.23.2.tgz",
|
||||
|
||||
@@ -9,10 +9,11 @@
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@noble/secp256k1": "^3.1.0",
|
||||
"iso-639-3": "^3.0.1",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-router-dom": "^6.20.0",
|
||||
"iso-639-3": "^3.0.1"
|
||||
"react-router-dom": "^6.20.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.2.38",
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
// Client-side proof of possession for account operations.
|
||||
//
|
||||
// meta-core stores every private key in plaintext, so it can sign as anybody —
|
||||
// which means a signature it produced proves nothing about who asked. The only
|
||||
// signature that carries authority is one made where the key actually is, and
|
||||
// for a browser-driven admin console that is here. meta-core enforces the other
|
||||
// half by refusing to sign anything in the challenge domain
|
||||
// (identity.ChallengeDomain), so this path cannot be short-circuited.
|
||||
//
|
||||
// The secret key entered on this page is used for exactly one signature and is
|
||||
// never sent anywhere: `/api/identity/challenge` receives only the uid, and the
|
||||
// request that follows carries only the challenge and its signature.
|
||||
//
|
||||
// Same scheme, same curve and same encodings as meta-watch's sign-in
|
||||
// (packages/meta-watch/ui/js/auth.js) — deliberately, so one implementation on
|
||||
// the server verifies both.
|
||||
|
||||
import * as secp from '@noble/secp256k1';
|
||||
|
||||
export type IdentityAction = 'reveal' | 'delete';
|
||||
|
||||
function hexToBytes(hex: string): Uint8Array | null {
|
||||
let h = hex.trim();
|
||||
if (h.startsWith('0x') || h.startsWith('0X')) h = h.slice(2);
|
||||
if (h.length !== 64 || /[^0-9a-fA-F]/.test(h)) return null;
|
||||
const out = new Uint8Array(32);
|
||||
for (let i = 0; i < 32; i++) out[i] = parseInt(h.slice(i * 2, i * 2 + 2), 16);
|
||||
return out;
|
||||
}
|
||||
|
||||
function bytesToBase64(bytes: Uint8Array): string {
|
||||
let s = '';
|
||||
for (const b of bytes) s += String.fromCharCode(b);
|
||||
return btoa(s);
|
||||
}
|
||||
|
||||
const B58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
||||
|
||||
/// base58btc, the multibase "z" alphabet. Spelled out rather than pulled in:
|
||||
/// the alternative is a second dependency for thirty lines of arithmetic.
|
||||
function base58Encode(bytes: Uint8Array): string {
|
||||
const digits: number[] = [0];
|
||||
for (const byte of bytes) {
|
||||
let carry = byte;
|
||||
for (let i = 0; i < digits.length; i++) {
|
||||
carry += digits[i] << 8;
|
||||
digits[i] = carry % 58;
|
||||
carry = (carry / 58) | 0;
|
||||
}
|
||||
while (carry) {
|
||||
digits.push(carry % 58);
|
||||
carry = (carry / 58) | 0;
|
||||
}
|
||||
}
|
||||
// Leading zero bytes encode as leading '1's rather than being dropped.
|
||||
let out = '';
|
||||
for (let i = 0; i < bytes.length && bytes[i] === 0; i++) out += B58[0];
|
||||
for (let i = digits.length - 1; i >= 0; i--) out += B58[digits[i]];
|
||||
return out;
|
||||
}
|
||||
|
||||
/// The uid a secret key belongs to: "z" + base58btc(33-byte compressed pubkey).
|
||||
/// Derived locally so a mistyped or wrong-account key is caught before any
|
||||
/// request goes out. Mirrors `identityFromPrivKey` in meta-core's identity.go.
|
||||
export function uidFromSecretKey(secretKeyHex: string): string | null {
|
||||
const raw = hexToBytes(secretKeyHex);
|
||||
if (!raw) return null;
|
||||
try {
|
||||
return 'z' + base58Encode(secp.getPublicKey(raw, true));
|
||||
} catch {
|
||||
return null; // not a valid scalar
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this page can sign at all.
|
||||
///
|
||||
/// noble's signAsync takes its sha256 from crypto.subtle, which browsers expose
|
||||
/// only in a secure context. The dashboard behind Caddy is https and fine; the
|
||||
/// debug-direct port (18083) is plain http, where every signature would fail
|
||||
/// with a cryptic error unless we say so first.
|
||||
export function canSign(): boolean {
|
||||
return Boolean(globalThis.crypto?.subtle);
|
||||
}
|
||||
|
||||
export const INSECURE_CONTEXT_MESSAGE =
|
||||
'This page cannot sign over a plain-http connection — browsers only expose the ' +
|
||||
'needed crypto on https (or localhost). Reach the dashboard through Caddy ' +
|
||||
'(https://metacore-dev.localhost:8083) rather than the debug-direct port.';
|
||||
|
||||
/// Ask meta-core for a challenge, sign it locally, and hand back the proof.
|
||||
///
|
||||
/// Throws with a message fit to show the user. The caller passes the proof
|
||||
/// straight into the reveal/delete request body.
|
||||
export async function proveOwnership(
|
||||
uid: string,
|
||||
action: IdentityAction,
|
||||
secretKeyHex: string
|
||||
): Promise<{ challenge: string; signature: string }> {
|
||||
if (!canSign()) throw new Error(INSECURE_CONTEXT_MESSAGE);
|
||||
|
||||
const raw = hexToBytes(secretKeyHex);
|
||||
if (!raw) {
|
||||
throw new Error('That is not a valid secret key: expected 64 hex characters.');
|
||||
}
|
||||
const derived = uidFromSecretKey(secretKeyHex);
|
||||
if (!derived) throw new Error('That is not a valid secp256k1 secret key.');
|
||||
if (derived !== uid) {
|
||||
// Caught here rather than by the server, so the message can say which
|
||||
// account the key actually is — the useful half of the answer.
|
||||
throw new Error(`That key belongs to a different account (${derived}), not this one.`);
|
||||
}
|
||||
|
||||
const cRes = await fetch('/api/identity/challenge', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ uid, action }),
|
||||
});
|
||||
const cBody = await cRes.json().catch(() => ({}));
|
||||
if (!cRes.ok || !cBody.challenge) {
|
||||
throw new Error(cBody.message ?? `Could not start verification (HTTP ${cRes.status}).`);
|
||||
}
|
||||
|
||||
// signAsync prehashes with sha256 and returns a 64-byte compact (r‖s)
|
||||
// signature — noble v3 dropped DER. meta-core's identity.Verify accepts both.
|
||||
const sig = await secp.signAsync(new TextEncoder().encode(cBody.challenge), raw);
|
||||
return { challenge: cBody.challenge, signature: bytesToBase64(sig) };
|
||||
}
|
||||
+614
-130
@@ -1,21 +1,74 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
// Status returned by GET /api/identity. Private key is never present here —
|
||||
// it is shown exactly once at generation time (see GeneratedKeyPanel).
|
||||
interface IdentityStatus {
|
||||
hasIdentity: boolean;
|
||||
uid?: string;
|
||||
curve?: string;
|
||||
createdAt?: number;
|
||||
}
|
||||
import {
|
||||
INSECURE_CONTEXT_MESSAGE,
|
||||
canSign,
|
||||
proveOwnership,
|
||||
uidFromSecretKey,
|
||||
type IdentityAction,
|
||||
} from '../lib/sign';
|
||||
|
||||
interface GeneratedKey {
|
||||
// Admin console for meta-core's account keystore.
|
||||
//
|
||||
// meta-core stores one secp256k1 keypair per file under
|
||||
// {META_CORE_PATH}/identity/accounts/<uid>.json and has NO notion of a current
|
||||
// user — which account is "you" is decided by whichever client signs in
|
||||
// (meta-watch). So this page lists accounts; it never speaks of an active one.
|
||||
//
|
||||
// The private key is returned by exactly two calls: POST /api/identity/generate
|
||||
// (once, at creation) and POST /api/identity/reveal (on demand, for an account
|
||||
// that already exists). Both land in the same exclusive SecretKeyPanel.
|
||||
//
|
||||
// Reveal and remove both require the account's own secret key, entered here and
|
||||
// used to sign a one-shot challenge locally — the key itself is never sent. So
|
||||
// this page can enumerate and create, but it holds no authority over an
|
||||
// existing account that its operator does not already have the key for. There
|
||||
// is deliberately no admin override: on this node the private key *is* the
|
||||
// account.
|
||||
|
||||
interface Account {
|
||||
uid: string;
|
||||
curve: string;
|
||||
createdAt: number;
|
||||
privateKeyHex: string;
|
||||
}
|
||||
|
||||
// What one account holds in the User Data Layer. Absent from
|
||||
// /api/udl/users/stats means zero, not unknown.
|
||||
interface Stats {
|
||||
records: number;
|
||||
cids: number;
|
||||
keys: number;
|
||||
}
|
||||
|
||||
// A secret on screen. Carries no createdAt: /api/identity/reveal answers with
|
||||
// {uid, curve, privateKeyHex} only.
|
||||
interface SecretKeyView {
|
||||
uid: string;
|
||||
privateKeyHex: string;
|
||||
kind: 'generated' | 'revealed';
|
||||
}
|
||||
|
||||
// Which row has an open confirmation, and for what. One discriminated value
|
||||
// rather than a pair of ids, so at most one confirm panel can ever be open.
|
||||
interface Pending {
|
||||
uid: string;
|
||||
kind: 'remove' | 'reveal';
|
||||
}
|
||||
|
||||
// Both confirmable operations map to a server-side action name, and the
|
||||
// challenge is bound to it: a challenge minted to reveal a key cannot delete
|
||||
// the account. Keeping the mapping in one place stops the two drifting.
|
||||
const ACTION_FOR: Record<Pending['kind'], IdentityAction> = {
|
||||
reveal: 'reveal',
|
||||
remove: 'delete',
|
||||
};
|
||||
|
||||
// meta-watch's sentinel cell for profile records: display name and avatar are
|
||||
// about the *user*, not a content file, so they live at (uid, "self", ...).
|
||||
// meta-core stores them opaquely and knows nothing of the convention.
|
||||
const PROFILE_CID = 'self';
|
||||
const PROFILE_NAME_KEY = 'profile:name';
|
||||
|
||||
const cardStyle: React.CSSProperties = {
|
||||
background: '#16213e',
|
||||
borderRadius: '8px',
|
||||
@@ -59,6 +112,21 @@ const monoStyle: React.CSSProperties = {
|
||||
borderRadius: '4px',
|
||||
};
|
||||
|
||||
const dangerPanelStyle: React.CSSProperties = {
|
||||
background: '#1f0a0a',
|
||||
border: '1px solid #7f1d1d',
|
||||
padding: '0.75rem 1rem',
|
||||
borderRadius: '4px',
|
||||
marginTop: '0.75rem',
|
||||
};
|
||||
|
||||
const errorTextStyle: React.CSSProperties = {
|
||||
color: '#f87171',
|
||||
background: '#1f0a0a',
|
||||
padding: '0.5rem 0.75rem',
|
||||
borderRadius: '4px',
|
||||
};
|
||||
|
||||
function formatCreatedAt(unixSec?: number): string {
|
||||
if (!unixSec) return '';
|
||||
try {
|
||||
@@ -79,25 +147,165 @@ async function errorMessage(res: Response, fallback: string): Promise<string> {
|
||||
return `${fallback} (HTTP ${res.status})`;
|
||||
}
|
||||
|
||||
function GeneratedKeyPanel({ data, onDismiss }: { data: GeneratedKey; onDismiss: () => void }) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const copy = async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(data.privateKeyHex);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
} catch {
|
||||
// Clipboard API blocked (non-HTTPS context, etc.) — fall back to selection.
|
||||
const sel = window.getSelection();
|
||||
// Copy text, falling back to selecting the element that displays it.
|
||||
//
|
||||
// The fallback is load-bearing, not politeness: the dashboard is served over
|
||||
// plain HTTP, where navigator.clipboard is undefined — without it the copy
|
||||
// buttons do nothing at all on a real deployment.
|
||||
async function copyToClipboard(text: string, fallbackElementId: string): Promise<void> {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} catch {
|
||||
const sel = window.getSelection();
|
||||
const el = document.getElementById(fallbackElementId);
|
||||
if (sel && el) {
|
||||
const range = document.createRange();
|
||||
const el = document.getElementById('mm-privkey-box');
|
||||
if (sel && el) {
|
||||
range.selectNodeContents(el);
|
||||
sel.removeAllRanges();
|
||||
sel.addRange(range);
|
||||
}
|
||||
range.selectNodeContents(el);
|
||||
sel.removeAllRanges();
|
||||
sel.addRange(range);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchAccounts(): Promise<Account[]> {
|
||||
const res = await fetch('/api/identity/accounts');
|
||||
if (!res.ok) throw new Error(await errorMessage(res, 'Failed to load accounts'));
|
||||
const data = (await res.json()) as { accounts?: Account[] };
|
||||
// Backend order is CreatedAt ascending then uid (identity.List) — stable, so
|
||||
// render it as given rather than re-sorting here.
|
||||
return data.accounts ?? [];
|
||||
}
|
||||
|
||||
// Resolve an account's display name from the User Data Layer, or null.
|
||||
//
|
||||
// Never throws and never reports an error upward: a name is decoration on top
|
||||
// of the uid, and the account list must render identically whether Redis is up
|
||||
// (identity itself is file-backed, the UDL is not). The name is also *unsigned*
|
||||
// — meta-core stores the plaintext beside the signed record and verifies
|
||||
// nothing — so the uid stays on screen as the real identity, and the length cap
|
||||
// bounds what a hostile writer can do to the layout.
|
||||
async function fetchDisplayName(uid: string): Promise<string | null> {
|
||||
const url =
|
||||
`/api/udl/record?uid=${encodeURIComponent(uid)}` +
|
||||
`&cid=${encodeURIComponent(PROFILE_CID)}` +
|
||||
`&key=${encodeURIComponent(PROFILE_NAME_KEY)}`;
|
||||
try {
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) return null; // 503 when storage is down, 500, …
|
||||
const data = (await res.json()) as { exists?: boolean; value?: unknown };
|
||||
// Key off `value`, not `exists`: clearing a name writes a tombstone, which
|
||||
// leaves the cell existing with no plaintext. Private-tier cells look the
|
||||
// same, which is exactly right — there is nothing to show for either.
|
||||
if (typeof data.value !== 'string') return null;
|
||||
const name = data.value.trim();
|
||||
return name ? name.slice(0, 64) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// How much each account holds. One call for the whole page, not one per row:
|
||||
// meta-core answers it from a keyspace SCAN (there is no per-user cid index),
|
||||
// so per-row calls would walk the keyspace once per account.
|
||||
//
|
||||
// Never throws, for the same reason fetchDisplayName does not: the counts are
|
||||
// context on top of the uid, and the list must render identically when Redis is
|
||||
// down — accounts are file-backed, their data is not.
|
||||
//
|
||||
// Returns null on failure rather than an empty map. The difference is
|
||||
// load-bearing: an empty map renders as "no stored data" against every account,
|
||||
// which inside a delete confirmation is a false statement about what is at
|
||||
// stake. Unknown has to look like unknown.
|
||||
async function fetchStats(): Promise<Record<string, Stats> | null> {
|
||||
try {
|
||||
const res = await fetch('/api/udl/users/stats');
|
||||
if (!res.ok) return null;
|
||||
const data = (await res.json()) as { users?: Record<string, Stats> };
|
||||
return data.users ?? {};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// The secret-key box shared by the reveal and remove confirmations.
|
||||
//
|
||||
// Separate from the surrounding panel so both operations ask the same way, and
|
||||
// so the key lives in this component's state rather than the page's — it is
|
||||
// discarded with the panel instead of lingering in a parent that outlives it.
|
||||
function SecretKeyConfirm({
|
||||
label,
|
||||
actionLabel,
|
||||
busy,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}: {
|
||||
label: string;
|
||||
actionLabel: string;
|
||||
busy: boolean;
|
||||
onConfirm: (secretKeyHex: string) => void;
|
||||
onCancel: () => void;
|
||||
}) {
|
||||
const [secret, setSecret] = useState('');
|
||||
const signable = canSign();
|
||||
// Derived locally, purely to tell the operator they have the wrong key before
|
||||
// they hit the button. The server would refuse either way.
|
||||
const derived = secret.trim() ? uidFromSecretKey(secret) : null;
|
||||
const mismatch = Boolean(secret.trim() && derived && derived !== label);
|
||||
|
||||
return (
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
onConfirm(secret.trim());
|
||||
}}
|
||||
>
|
||||
<p style={{ color: '#fecaca', margin: '0 0 0.5rem' }}>
|
||||
Paste this account's secret key to prove it is yours. It is used to sign a one-time
|
||||
challenge <em>in this browser</em> and is never sent to the server.
|
||||
</p>
|
||||
{!signable && <p style={errorTextStyle}>{INSECURE_CONTEXT_MESSAGE}</p>}
|
||||
<input
|
||||
style={inputStyle}
|
||||
type="password"
|
||||
autoComplete="off"
|
||||
placeholder="64 hex characters"
|
||||
value={secret}
|
||||
onChange={(e) => setSecret(e.target.value)}
|
||||
disabled={busy || !signable}
|
||||
autoFocus
|
||||
/>
|
||||
{mismatch && (
|
||||
<p style={{ ...errorTextStyle, marginTop: 0 }}>
|
||||
That key belongs to a different account ({derived}).
|
||||
</p>
|
||||
)}
|
||||
<button
|
||||
type="submit"
|
||||
style={dangerButtonStyle}
|
||||
disabled={busy || !signable || !secret.trim() || mismatch}
|
||||
>
|
||||
{busy ? 'Verifying…' : actionLabel}
|
||||
</button>
|
||||
<button type="button" style={buttonStyle} onClick={onCancel} disabled={busy}>
|
||||
Cancel
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
// The one place a private key is ever displayed. Rendered exclusively (the
|
||||
// account list is hidden while it is open) so the "save it now" contract of a
|
||||
// freshly generated key is not competing with anything, and so the hardcoded
|
||||
// element id below stays unique.
|
||||
function SecretKeyPanel({ data, onDismiss }: { data: SecretKeyView; onDismiss: () => void }) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
const copy = async () => {
|
||||
await copyToClipboard(data.privateKeyHex, 'mm-privkey-box');
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={{ ...cardStyle, border: '2px solid #dc2626' }}>
|
||||
<div
|
||||
@@ -109,9 +317,19 @@ function GeneratedKeyPanel({ data, onDismiss }: { data: GeneratedKey; onDismiss:
|
||||
marginBottom: '1rem',
|
||||
}}
|
||||
>
|
||||
<strong>This private key is shown ONCE.</strong> Save it now in a password manager.
|
||||
It will never be displayed again — there is no recovery path. Losing it means losing access
|
||||
to every record signed under this identity.
|
||||
{data.kind === 'generated' ? (
|
||||
<>
|
||||
<strong>This private key is shown ONCE.</strong> Save it now in a password manager.
|
||||
You can ask for it again later with <em>Reveal private key</em>, but only for as long
|
||||
as this account exists on this node — delete it and the key is gone for good.
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<strong>This is the live private key for an existing account.</strong> Anyone holding
|
||||
it can sign as this uid forever, on any machine. There is no revocation and no
|
||||
rotation. Do not paste it into chat, tickets or screenshots.
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<p style={{ color: '#9ca3af', margin: '0 0 0.5rem' }}>uid (public):</p>
|
||||
<div style={monoStyle}>{data.uid}</div>
|
||||
@@ -124,36 +342,40 @@ function GeneratedKeyPanel({ data, onDismiss }: { data: GeneratedKey; onDismiss:
|
||||
{copied ? 'Copied' : 'Copy private key'}
|
||||
</button>
|
||||
<button style={dangerButtonStyle} onClick={onDismiss}>
|
||||
I have saved it
|
||||
{data.kind === 'generated' ? 'I have saved it' : 'Done — hide it'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function NoIdentityPanel({
|
||||
// Create / import. Always available, not just on an empty keystore: this is an
|
||||
// admin console, so adding an account to a populated node has to be possible.
|
||||
function NewAccountPanel({
|
||||
onGenerated,
|
||||
onImported,
|
||||
}: {
|
||||
onGenerated: (g: GeneratedKey) => void;
|
||||
onGenerated: (s: SecretKeyView) => void;
|
||||
onImported: () => void;
|
||||
}) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [importing, setImporting] = useState(false);
|
||||
const [importHex, setImportHex] = useState('');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [notice, setNotice] = useState<string | null>(null);
|
||||
|
||||
const generate = async () => {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
setNotice(null);
|
||||
try {
|
||||
const res = await fetch('/api/identity/generate', { method: 'POST' });
|
||||
if (!res.ok) {
|
||||
setError(await errorMessage(res, 'Generate failed'));
|
||||
return;
|
||||
}
|
||||
const data = (await res.json()) as GeneratedKey;
|
||||
onGenerated(data);
|
||||
const data = (await res.json()) as { uid: string; privateKeyHex: string };
|
||||
onGenerated({ uid: data.uid, privateKeyHex: data.privateKeyHex, kind: 'generated' });
|
||||
} catch (e) {
|
||||
setError(`Generate failed: ${e instanceof Error ? e.message : String(e)}`);
|
||||
} finally {
|
||||
@@ -165,6 +387,7 @@ function NoIdentityPanel({
|
||||
e.preventDefault();
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
setNotice(null);
|
||||
try {
|
||||
const res = await fetch('/api/identity/import', {
|
||||
method: 'POST',
|
||||
@@ -175,6 +398,15 @@ function NoIdentityPanel({
|
||||
setError(await errorMessage(res, 'Import failed'));
|
||||
return;
|
||||
}
|
||||
// Import is idempotent — presenting the key IS the proof of ownership, so
|
||||
// a key this node already holds is a success, not a conflict. `created`
|
||||
// only decides the wording.
|
||||
const data = (await res.json()) as { created?: boolean };
|
||||
setNotice(
|
||||
data.created
|
||||
? 'Imported — a new account was added to this keystore.'
|
||||
: 'That key was already in this keystore; nothing changed.'
|
||||
);
|
||||
setImportHex('');
|
||||
setImporting(false);
|
||||
onImported();
|
||||
@@ -187,15 +419,16 @@ function NoIdentityPanel({
|
||||
|
||||
return (
|
||||
<div style={cardStyle}>
|
||||
<h3 style={{ marginTop: 0 }}>No identity yet</h3>
|
||||
<p style={{ color: '#9ca3af' }}>
|
||||
The User Data Layer needs a signing key for your ratings, watch progress, and "liked" flags
|
||||
to be attributed to you across devices. Nothing happens automatically — generate one below,
|
||||
or import a key you already have.
|
||||
<h3 style={{ marginTop: 0 }}>Add an account</h3>
|
||||
<p style={{ color: '#9ca3af', marginTop: 0 }}>
|
||||
Generating mints a fresh secp256k1 keypair here. Importing adds a key created elsewhere —
|
||||
it is also how an existing account is moved onto this node, so importing a key the
|
||||
keystore already holds succeeds and changes nothing.
|
||||
</p>
|
||||
{error && (
|
||||
<p style={{ color: '#f87171', background: '#1f0a0a', padding: '0.5rem 0.75rem', borderRadius: '4px' }}>
|
||||
{error}
|
||||
{error && <p style={errorTextStyle}>{error}</p>}
|
||||
{notice && (
|
||||
<p style={{ color: '#9ca3af', background: '#0a0a18', padding: '0.5rem 0.75rem', borderRadius: '4px' }}>
|
||||
{notice}
|
||||
</p>
|
||||
)}
|
||||
<div>
|
||||
@@ -235,73 +468,156 @@ function NoIdentityPanel({
|
||||
);
|
||||
}
|
||||
|
||||
function HasIdentityPanel({ status, onReplace }: { status: IdentityStatus; onReplace: () => void }) {
|
||||
const [confirming, setConfirming] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
function AccountCard({
|
||||
account,
|
||||
name,
|
||||
stats,
|
||||
busy,
|
||||
disabled,
|
||||
pending,
|
||||
error,
|
||||
onAsk,
|
||||
onCancel,
|
||||
onReveal,
|
||||
onRemove,
|
||||
}: {
|
||||
account: Account;
|
||||
name?: string;
|
||||
// undefined means "counted, and it holds nothing"; null means "could not be
|
||||
// counted". The two must not read the same on a confirmation screen.
|
||||
stats?: Stats | null;
|
||||
busy: boolean;
|
||||
disabled: boolean;
|
||||
pending: Pending | null;
|
||||
error: string | null;
|
||||
onAsk: (p: Pending) => void;
|
||||
onCancel: () => void;
|
||||
onReveal: (uid: string, secretKeyHex: string) => void;
|
||||
onRemove: (uid: string, secretKeyHex: string) => void;
|
||||
}) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const uidBoxId = `mm-uid-${account.uid}`;
|
||||
const label = name ?? account.uid;
|
||||
const counted = stats !== null;
|
||||
const records = stats?.records ?? 0;
|
||||
const titles = stats?.cids ?? 0;
|
||||
|
||||
const remove = async () => {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch('/api/identity?confirm=true', { method: 'DELETE' });
|
||||
if (!res.ok && res.status !== 204) {
|
||||
setError(await errorMessage(res, 'Delete failed'));
|
||||
return;
|
||||
}
|
||||
onReplace();
|
||||
} catch (e) {
|
||||
setError(`Delete failed: ${e instanceof Error ? e.message : String(e)}`);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
setConfirming(false);
|
||||
}
|
||||
const copyUid = async () => {
|
||||
await copyToClipboard(account.uid, uidBoxId);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={cardStyle}>
|
||||
<h3 style={{ marginTop: 0 }}>Active identity</h3>
|
||||
<p style={{ color: '#9ca3af', margin: '0 0 0.5rem' }}>uid:</p>
|
||||
<div style={monoStyle}>{status.uid}</div>
|
||||
<p style={{ color: '#888', marginTop: '1rem' }}>
|
||||
Curve: {status.curve} · Created: {formatCreatedAt(status.createdAt)}
|
||||
</p>
|
||||
<p style={{ color: '#9ca3af', fontSize: '0.85rem' }}>
|
||||
The private key lives at <code>/meta-core/identity/identity.json</code> on this volume and
|
||||
is never returned through the API after generation. Other services on this stack (e.g.
|
||||
meta-watch) sign records by calling <code>POST /api/identity/sign</code>.
|
||||
</p>
|
||||
{error && (
|
||||
<p style={{ color: '#f87171', background: '#1f0a0a', padding: '0.5rem 0.75rem', borderRadius: '4px' }}>
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
{!confirming ? (
|
||||
<button style={dangerButtonStyle} onClick={() => setConfirming(true)} disabled={busy}>
|
||||
Replace identity
|
||||
</button>
|
||||
) : (
|
||||
<div
|
||||
style={{
|
||||
background: '#1f0a0a',
|
||||
border: '1px solid #7f1d1d',
|
||||
padding: '0.75rem 1rem',
|
||||
borderRadius: '4px',
|
||||
marginTop: '0.5rem',
|
||||
}}
|
||||
>
|
||||
<p style={{ color: '#fecaca', marginTop: 0 }}>
|
||||
Replacing the identity deletes the current keypair from disk. Records signed by the
|
||||
current uid will keep verifying for anyone who has them cached, but this node will no
|
||||
longer be able to write under that uid. <strong>Make sure you have backed up the key
|
||||
first.</strong>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', gap: '1rem' }}>
|
||||
<div style={{ minWidth: 0, flex: 1 }}>
|
||||
<h3 style={{ margin: '0 0 0.5rem', color: name ? '#fff' : '#888' }}>
|
||||
{name ?? 'Unnamed account'}
|
||||
</h3>
|
||||
<div id={uidBoxId} style={monoStyle}>
|
||||
{account.uid}
|
||||
</div>
|
||||
<p style={{ color: '#888', margin: '0.75rem 0 0', fontSize: '0.85rem' }}>
|
||||
Curve: {account.curve} · Created: {formatCreatedAt(account.createdAt)}
|
||||
</p>
|
||||
<button style={dangerButtonStyle} onClick={remove} disabled={busy}>
|
||||
{busy ? 'Deleting…' : 'Yes, delete identity'}
|
||||
<p style={{ color: '#888', margin: '0.25rem 0 0', fontSize: '0.85rem' }}>
|
||||
{/* An account that has never been used legitimately holds nothing, so
|
||||
zero is a real answer — but only once the count actually ran. */}
|
||||
{!counted
|
||||
? 'Stored data: could not be counted (storage unavailable)'
|
||||
: records === 0
|
||||
? 'No stored data'
|
||||
: `${records.toLocaleString()} stored ${records === 1 ? 'record' : 'records'}` +
|
||||
` across ${titles.toLocaleString()} ${titles === 1 ? 'title' : 'titles'}`}
|
||||
</p>
|
||||
</div>
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem', flexShrink: 0 }}>
|
||||
<button style={{ ...buttonStyle, marginRight: 0 }} onClick={copyUid} disabled={disabled}>
|
||||
{copied ? 'Copied' : 'Copy uid'}
|
||||
</button>
|
||||
<button style={buttonStyle} onClick={() => setConfirming(false)} disabled={busy}>
|
||||
Cancel
|
||||
<button
|
||||
style={{ ...dangerButtonStyle, marginRight: 0 }}
|
||||
onClick={() => onAsk({ uid: account.uid, kind: 'reveal' })}
|
||||
disabled={disabled}
|
||||
>
|
||||
Reveal private key
|
||||
</button>
|
||||
<button
|
||||
style={{ ...dangerButtonStyle, marginRight: 0 }}
|
||||
onClick={() => onAsk({ uid: account.uid, kind: 'remove' })}
|
||||
disabled={disabled}
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && <p style={{ ...errorTextStyle, marginBottom: 0 }}>{error}</p>}
|
||||
|
||||
{pending?.uid === account.uid && pending.kind === 'reveal' && (
|
||||
<div style={dangerPanelStyle}>
|
||||
<p style={{ color: '#fecaca', marginTop: 0 }}>
|
||||
Show the private key for <strong>{label}</strong>?
|
||||
</p>
|
||||
<p style={{ color: '#fecaca', marginTop: 0 }}>
|
||||
The key <em>is</em> the account. Anyone who copies it can sign records as this uid on
|
||||
any machine, forever — there is no way to revoke or rotate it afterwards. Because
|
||||
proving ownership already requires the key, this only confirms that the copy this node
|
||||
holds matches yours.
|
||||
</p>
|
||||
<SecretKeyConfirm
|
||||
label={account.uid}
|
||||
actionLabel="Show the private key"
|
||||
busy={busy}
|
||||
onConfirm={(secret) => onReveal(account.uid, secret)}
|
||||
onCancel={onCancel}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{pending?.uid === account.uid && pending.kind === 'remove' && (
|
||||
<div style={dangerPanelStyle}>
|
||||
<p style={{ color: '#fecaca', marginTop: 0 }}>
|
||||
Remove <strong>{label}</strong> from this node?
|
||||
</p>
|
||||
<ul style={{ color: '#fecaca', paddingLeft: '1.2rem', margin: '0 0 0.75rem' }}>
|
||||
<li>
|
||||
<strong>The key is gone for good.</strong> The uid <em>is</em> the public key, and
|
||||
the private half exists only in this node's keystore. If nobody saved it, nothing —
|
||||
not you, not meta-core — can ever sign as this uid again. Reveal and copy it first
|
||||
if there is any doubt.
|
||||
</li>
|
||||
<li>
|
||||
<strong>Nothing is retracted.</strong> Records already signed under this uid keep
|
||||
verifying on every peer that replicated them. Deleting a key is not a recall.
|
||||
</li>
|
||||
<li>
|
||||
<strong>Its data is destroyed, not orphaned.</strong>{' '}
|
||||
{!counted
|
||||
? 'This account\u2019s records could not be counted because storage is unavailable — ' +
|
||||
'so meta-core will refuse this delete rather than remove the key and strand them.'
|
||||
: records === 0
|
||||
? 'This account holds no User Data Layer records, so there is nothing to purge.'
|
||||
: `All ${records.toLocaleString()} User Data Layer ${
|
||||
records === 1 ? 'record' : 'records'
|
||||
} — likes, ratings, watch progress across ${titles.toLocaleString()} ${
|
||||
titles === 1 ? 'title' : 'titles'
|
||||
} — are permanently deleted from Redis, along with every index that names this uid.`}{' '}
|
||||
This cannot be undone and there is no backup.
|
||||
</li>
|
||||
<li>
|
||||
<strong>This signs nobody out elsewhere.</strong> Any device that holds this key
|
||||
still holds it, and can import it back here at any time.
|
||||
</li>
|
||||
</ul>
|
||||
<SecretKeyConfirm
|
||||
label={account.uid}
|
||||
actionLabel="Remove this account and all its data"
|
||||
busy={busy}
|
||||
onConfirm={(secret) => onRemove(account.uid, secret)}
|
||||
onCancel={onCancel}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -309,67 +625,235 @@ function HasIdentityPanel({ status, onReplace }: { status: IdentityStatus; onRep
|
||||
}
|
||||
|
||||
export default function Identity() {
|
||||
const [status, setStatus] = useState<IdentityStatus | null>(null);
|
||||
const [generated, setGenerated] = useState<GeneratedKey | null>(null);
|
||||
const [accounts, setAccounts] = useState<Account[] | null>(null);
|
||||
const [names, setNames] = useState<Record<string, string>>({});
|
||||
const [stats, setStats] = useState<Record<string, Stats> | null>({});
|
||||
const [notice, setNotice] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [fetchError, setFetchError] = useState<string | null>(null);
|
||||
const [secret, setSecret] = useState<SecretKeyView | null>(null);
|
||||
const [pending, setPending] = useState<Pending | null>(null);
|
||||
const [busyUid, setBusyUid] = useState<string | null>(null);
|
||||
const [rowError, setRowError] = useState<{ uid: string; message: string } | null>(null);
|
||||
|
||||
// Bumped per refresh so a slow response from a superseded load cannot
|
||||
// overwrite a newer one. The page does not poll (unlike the rest of the
|
||||
// dashboard) — a key list has no business changing under the cursor.
|
||||
const reqGen = useRef(0);
|
||||
|
||||
const refresh = async () => {
|
||||
const gen = ++reqGen.current;
|
||||
setLoading(true);
|
||||
setFetchError(null);
|
||||
let list: Account[];
|
||||
try {
|
||||
const res = await fetch('/api/identity');
|
||||
if (!res.ok) {
|
||||
setFetchError(await errorMessage(res, 'Failed to load identity status'));
|
||||
return;
|
||||
}
|
||||
setStatus((await res.json()) as IdentityStatus);
|
||||
list = await fetchAccounts();
|
||||
} catch (e) {
|
||||
setFetchError(`Failed to load identity status: ${e instanceof Error ? e.message : String(e)}`);
|
||||
} finally {
|
||||
if (gen !== reqGen.current) return;
|
||||
setFetchError(e instanceof Error ? e.message : String(e));
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
if (gen !== reqGen.current) return;
|
||||
// Render the list before resolving names, so a down Redis delays nothing.
|
||||
setAccounts(list);
|
||||
setLoading(false);
|
||||
|
||||
// Names and counts both come after the list is on screen, and neither can
|
||||
// fail the page: a down Redis costs decoration, not the account list.
|
||||
const [settled, counts] = await Promise.all([
|
||||
Promise.allSettled(list.map((a) => fetchDisplayName(a.uid))),
|
||||
fetchStats(),
|
||||
]);
|
||||
if (gen !== reqGen.current) return;
|
||||
const next: Record<string, string> = {};
|
||||
settled.forEach((r, i) => {
|
||||
if (r.status === 'fulfilled' && r.value) next[list[i].uid] = r.value;
|
||||
});
|
||||
// Replace rather than merge, so a removed account's name and counts go with
|
||||
// it instead of lingering against a uid that no longer exists.
|
||||
setNames(next);
|
||||
setStats(counts);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
refresh();
|
||||
}, []);
|
||||
|
||||
const reveal = async (uid: string, secretKeyHex: string) => {
|
||||
setBusyUid(uid);
|
||||
setRowError(null);
|
||||
try {
|
||||
const proof = await proveOwnership(uid, ACTION_FOR.reveal, secretKeyHex);
|
||||
// POST with a body rather than ?uid=, so neither the target nor the
|
||||
// signature reaches the proxy's access log. The handler accepts the uid
|
||||
// either way; the proof must be in the body.
|
||||
const res = await fetch('/api/identity/reveal', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ confirm: true, uid, ...proof }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
setRowError({ uid, message: await errorMessage(res, 'Reveal failed') });
|
||||
return;
|
||||
}
|
||||
const data = (await res.json()) as { uid: string; privateKeyHex: string };
|
||||
setPending(null);
|
||||
setSecret({ uid: data.uid, privateKeyHex: data.privateKeyHex, kind: 'revealed' });
|
||||
} catch (e) {
|
||||
setRowError({ uid, message: `Reveal failed: ${e instanceof Error ? e.message : String(e)}` });
|
||||
} finally {
|
||||
setBusyUid(null);
|
||||
}
|
||||
};
|
||||
|
||||
const remove = async (uid: string, secretKeyHex: string) => {
|
||||
setBusyUid(uid);
|
||||
setRowError(null);
|
||||
setNotice(null);
|
||||
try {
|
||||
const proof = await proveOwnership(uid, ACTION_FOR.remove, secretKeyHex);
|
||||
// The uid is mandatory: the signature authorises one specific account, so
|
||||
// the backend refuses an unqualified delete rather than guess a target.
|
||||
const res = await fetch('/api/identity', {
|
||||
method: 'DELETE',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ confirm: true, uid, ...proof }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
setRowError({ uid, message: await errorMessage(res, 'Remove failed') });
|
||||
return;
|
||||
}
|
||||
// Report what the purge actually removed. "Account removed" on its own
|
||||
// would not distinguish a clean delete from one that stranded the data.
|
||||
const data = (await res.json().catch(() => ({}))) as { purged?: Stats };
|
||||
const purged = data.purged;
|
||||
setNotice(
|
||||
purged && purged.records > 0
|
||||
? `Account removed. Purged ${purged.records.toLocaleString()} User Data Layer ` +
|
||||
`${purged.records === 1 ? 'record' : 'records'} across ` +
|
||||
`${purged.cids.toLocaleString()} ${purged.cids === 1 ? 'title' : 'titles'}.`
|
||||
: 'Account removed. It held no User Data Layer records.'
|
||||
);
|
||||
setPending(null);
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
setRowError({ uid, message: `Remove failed: ${e instanceof Error ? e.message : String(e)}` });
|
||||
} finally {
|
||||
setBusyUid(null);
|
||||
}
|
||||
};
|
||||
|
||||
const dismissSecret = () => {
|
||||
setSecret(null);
|
||||
refresh();
|
||||
};
|
||||
|
||||
const ask = (p: Pending) => {
|
||||
setRowError(null);
|
||||
setNotice(null);
|
||||
setPending(p);
|
||||
};
|
||||
|
||||
const showList = !loading && !fetchError && !secret;
|
||||
|
||||
return (
|
||||
// App.tsx's content area is `overflow: hidden`, so every page owns its own
|
||||
// scrolling (Files.tsx does the same). Without this the account list is
|
||||
// simply clipped at the viewport with no scrollbar — invisible on a node
|
||||
// with two accounts, and a dead end on one with fifteen.
|
||||
//
|
||||
// The scroller is the outer element and the padding lives inside it, so the
|
||||
// scrollbar sits at the edge of the content area rather than floating in
|
||||
// 2rem of whitespace, and `margin: 0 auto` still centres the column.
|
||||
<div style={{ height: '100%', overflowY: 'auto', minHeight: 0 }}>
|
||||
<div style={{ padding: '2rem', maxWidth: '900px', margin: '0 auto' }}>
|
||||
<h1>Identity</h1>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
|
||||
<h1 style={{ marginBottom: 0 }}>Identity</h1>
|
||||
<button style={{ ...buttonStyle, marginRight: 0 }} onClick={refresh} disabled={loading}>
|
||||
{loading ? 'Loading…' : 'Refresh'}
|
||||
</button>
|
||||
</div>
|
||||
<p style={{ color: '#9ca3af' }}>
|
||||
Signing key for the User Data Layer (per-user ratings, progress, "liked" flags). One
|
||||
identity per node. The same key on another device makes that device count as "you".
|
||||
Every signing key in this node's keystore. meta-core has no "current user" — it stores
|
||||
accounts, and which one is <em>you</em> is decided by whichever client signs in (e.g.
|
||||
meta-watch). Keys live one JSON file per account under{' '}
|
||||
<code>{'{META_CORE_PATH}'}/identity/accounts/<uid>.json</code>, and are used to sign
|
||||
User Data Layer records (ratings, watch progress, "liked" flags).
|
||||
</p>
|
||||
|
||||
<div style={{ ...dangerPanelStyle, marginTop: 0, marginBottom: '1rem' }}>
|
||||
<p style={{ color: '#fecaca', margin: 0 }}>
|
||||
<strong>The key is the only authority.</strong> Revealing or removing an account requires
|
||||
that account's secret key, entered here and used to sign a one-time challenge in your
|
||||
browser — there is no admin override, and holding this page is not one. Listing and
|
||||
creating accounts are still open to anything that can reach this API, so the perimeter in
|
||||
front of this port is what keeps the roster private.
|
||||
</p>
|
||||
<p style={{ color: '#fecaca', margin: '0.5rem 0 0' }}>
|
||||
What this does <em>not</em> defend against: anyone with filesystem access to{' '}
|
||||
<code>{'{META_CORE_PATH}'}/identity/accounts/</code> reads every private key directly.
|
||||
The keys are stored in plaintext, matching the trust model of the rest of{' '}
|
||||
<code>/meta-core</code>.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{notice && (
|
||||
<div style={{ ...cardStyle, border: '1px solid #0f3460' }}>
|
||||
<p style={{ color: '#9ca3af', margin: 0 }}>{notice}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{loading && <div style={cardStyle}>Loading…</div>}
|
||||
|
||||
{fetchError && (
|
||||
<div style={{ ...cardStyle, border: '1px solid #7f1d1d' }}>
|
||||
<p style={{ color: '#f87171', margin: 0 }}>{fetchError}</p>
|
||||
<p style={{ color: '#f87171', margin: '0 0 1rem' }}>{fetchError}</p>
|
||||
<button style={buttonStyle} onClick={refresh}>
|
||||
Retry
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!loading && !fetchError && generated && (
|
||||
<GeneratedKeyPanel
|
||||
data={generated}
|
||||
onDismiss={() => {
|
||||
setGenerated(null);
|
||||
refresh();
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{!loading && !fetchError && secret && <SecretKeyPanel data={secret} onDismiss={dismissSecret} />}
|
||||
|
||||
{!loading && !fetchError && !generated && status && !status.hasIdentity && (
|
||||
<NoIdentityPanel onGenerated={(g) => setGenerated(g)} onImported={refresh} />
|
||||
)}
|
||||
{showList && (
|
||||
<>
|
||||
<NewAccountPanel onGenerated={(s) => setSecret(s)} onImported={refresh} />
|
||||
|
||||
{!loading && !fetchError && !generated && status && status.hasIdentity && (
|
||||
<HasIdentityPanel status={status} onReplace={refresh} />
|
||||
<h2 style={{ fontSize: '1.1rem', color: '#9ca3af', margin: '1.5rem 0 1rem' }}>
|
||||
Accounts ({accounts?.length ?? 0})
|
||||
</h2>
|
||||
|
||||
{accounts && accounts.length === 0 && (
|
||||
<div style={cardStyle}>
|
||||
<p style={{ color: '#9ca3af', margin: 0 }}>
|
||||
No accounts in this keystore. Accounts are normally created by a client when
|
||||
someone signs up; use <em>Add an account</em> above if you are bootstrapping this
|
||||
node.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{accounts?.map((a) => (
|
||||
<AccountCard
|
||||
key={a.uid}
|
||||
account={a}
|
||||
name={names[a.uid]}
|
||||
stats={stats === null ? null : stats[a.uid]}
|
||||
busy={busyUid === a.uid}
|
||||
disabled={busyUid !== null}
|
||||
pending={pending}
|
||||
error={rowError?.uid === a.uid ? rowError.message : null}
|
||||
onAsk={ask}
|
||||
onCancel={() => setPending(null)}
|
||||
onReveal={reveal}
|
||||
onRemove={remove}
|
||||
/>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -301,6 +301,7 @@ CID-addressed (public, auth-bypassed at the perimeter)
|
||||
|
||||
Editor / KV / schema / snapshot
|
||||
GET /api/metadata/hash-ids
|
||||
GET /api/files/tuples
|
||||
GET /api/metadata/list
|
||||
POST /api/metadata/search
|
||||
POST /api/metadata/batch
|
||||
@@ -339,6 +340,9 @@ Watchers
|
||||
|
||||
Admin
|
||||
POST /api/admin/migrate-dual-roots
|
||||
POST /api/admin/sweep-literature-echoes (dry-run unless ?apply=true;
|
||||
strips query echoes off domain=literature fileType=card records —
|
||||
see storage/literature_echo_sweep.go)
|
||||
|
||||
WebDAV
|
||||
/webdav/... (GET, PUT, DELETE, MKCOL, COPY, MOVE, PROPFIND)
|
||||
|
||||
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// handleMigrateDualRoots triggers the one-shot sweep that re-unifies
|
||||
@@ -28,3 +29,73 @@ func (s *Server) handleMigrateDualRoots(w http.ResponseWriter, r *http.Request)
|
||||
"fixed": fixed,
|
||||
})
|
||||
}
|
||||
|
||||
// handleMigrateDomainScreen triggers the one-shot sweep that rewrites the
|
||||
// retired `domain=film|tv` values to `screen` and backfills `workForm` from
|
||||
// `contentKind` (METADATA_KEYS.md §14.17). See
|
||||
// storage.MigrateDomainScreen for why the vocabulary change is paid in the
|
||||
// data rather than with a read-side alias.
|
||||
//
|
||||
// Returns {fixed: N} — records changed, not records examined. Safe to call
|
||||
// repeatedly: a record already on the current vocabulary is skipped.
|
||||
func (s *Server) handleMigrateDomainScreen(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.storage.IsConnected() {
|
||||
writeError(w, http.StatusServiceUnavailable, "storage not connected")
|
||||
return
|
||||
}
|
||||
|
||||
fixed, err := s.storage.MigrateDomainScreenWithTimeout()
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"success": true,
|
||||
"fixed": fixed,
|
||||
})
|
||||
}
|
||||
|
||||
// handleSweepLiteratureEchoes removes query echoes stored on literature cards:
|
||||
// `anilistid` on a site card, and `workcid` / position keys on any card
|
||||
// (meta-read docs/reading-model.md §7). See storage.SweepLiteratureEchoes for
|
||||
// why chapters and anchor cards' ids are deliberately left alone.
|
||||
//
|
||||
// Dry-run unless `?apply=true`: unlike the migrations above this one deletes
|
||||
// keys, so a bare call only reports what it would delete. An unparseable
|
||||
// `apply` is a 400 rather than a silent dry run, so a typo cannot pass for a
|
||||
// sweep that ran.
|
||||
//
|
||||
// Returns {success, dryRun, planned, fixed, byKey} — planned/fixed count
|
||||
// records, byKey counts fields per key. Safe to call repeatedly: a swept
|
||||
// record plans nothing on the next pass.
|
||||
func (s *Server) handleSweepLiteratureEchoes(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.storage.IsConnected() {
|
||||
writeError(w, http.StatusServiceUnavailable, "storage not connected")
|
||||
return
|
||||
}
|
||||
|
||||
apply := false
|
||||
if v := r.URL.Query().Get("apply"); v != "" {
|
||||
parsed, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "apply must be true or false")
|
||||
return
|
||||
}
|
||||
apply = parsed
|
||||
}
|
||||
|
||||
res, err := s.storage.SweepLiteratureEchoesWithTimeout(apply)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"success": true,
|
||||
"dryRun": !apply,
|
||||
"planned": res.Planned,
|
||||
"fixed": res.Fixed,
|
||||
"byKey": res.ByKey,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
|
||||
"github.com/metazla/meta-core/internal/storage"
|
||||
)
|
||||
|
||||
// newSweepTestServer is a Server over miniredis with one literature card
|
||||
// carrying a copied `workcid`.
|
||||
func newSweepTestServer(t *testing.T) *Server {
|
||||
t.Helper()
|
||||
mr := miniredis.RunT(t)
|
||||
c := storage.NewClient("")
|
||||
if err := c.Connect("redis://" + mr.Addr()); err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = c.Close() })
|
||||
for k, v := range map[string]string{
|
||||
"domain": "literature",
|
||||
"fileType": "card",
|
||||
"workcid": "bagdsaaaua5qw42lmnfzxi3lbnztwcorrga2tonzy",
|
||||
} {
|
||||
if err := c.SetProperty("card-1", k, v); err != nil {
|
||||
t.Fatalf("seed %s: %v", k, err)
|
||||
}
|
||||
}
|
||||
return &Server{storage: c}
|
||||
}
|
||||
|
||||
type sweepResponse struct {
|
||||
Success bool `json:"success"`
|
||||
DryRun bool `json:"dryRun"`
|
||||
Planned int `json:"planned"`
|
||||
Fixed int `json:"fixed"`
|
||||
ByKey map[string]int `json:"byKey"`
|
||||
}
|
||||
|
||||
func postSweep(t *testing.T, s *Server, query string) (*httptest.ResponseRecorder, sweepResponse) {
|
||||
t.Helper()
|
||||
rr := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/admin/sweep-literature-echoes"+query, nil)
|
||||
s.handleSweepLiteratureEchoes(rr, req)
|
||||
var body sweepResponse
|
||||
if rr.Code == http.StatusOK {
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("decode %s: %v", rr.Body.String(), err)
|
||||
}
|
||||
}
|
||||
return rr, body
|
||||
}
|
||||
|
||||
// A bare call must never delete: the default is the dry run.
|
||||
func TestSweepLiteratureEchoes_DefaultsToDryRun(t *testing.T) {
|
||||
s := newSweepTestServer(t)
|
||||
|
||||
rr, body := postSweep(t, s, "")
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
if !body.Success || !body.DryRun || body.Planned != 1 || body.Fixed != 0 || body.ByKey["workcid"] != 1 {
|
||||
t.Errorf("dry run = %+v", body)
|
||||
}
|
||||
if v, _ := s.storage.GetProperty("card-1", "workcid"); v == "" {
|
||||
t.Errorf("dry run deleted workcid")
|
||||
}
|
||||
|
||||
rr, body = postSweep(t, s, "?apply=true")
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
if body.DryRun || body.Planned != 1 || body.Fixed != 1 || body.ByKey["workcid"] != 1 {
|
||||
t.Errorf("apply = %+v", body)
|
||||
}
|
||||
if v, _ := s.storage.GetProperty("card-1", "workcid"); v != "" {
|
||||
t.Errorf("apply left workcid = %q", v)
|
||||
}
|
||||
}
|
||||
|
||||
// A typo must not pass for a sweep that ran.
|
||||
func TestSweepLiteratureEchoes_RejectsUnparseableApply(t *testing.T) {
|
||||
s := newSweepTestServer(t)
|
||||
|
||||
rr, _ := postSweep(t, s, "?apply=yes-please")
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", rr.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/metazla/meta-core/internal/watcher"
|
||||
)
|
||||
|
||||
// registerFileRequest is the body of POST /api/files/register.
|
||||
type registerFileRequest struct {
|
||||
// Path relative to FILES_PATH, e.g. "plugin/subtitles/ab12….eng.srt".
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
type registerFileResponse struct {
|
||||
Root string `json:"root"`
|
||||
CID string `json:"cid"`
|
||||
Created bool `json:"created"`
|
||||
Path string `json:"path"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
// pluginRelPath normalises a FILES_PATH-relative path and refuses anything
|
||||
// outside plugin/. Plugin output is the only tree a service may register this
|
||||
// way: /files/watch is the user's library and belongs to the watcher.
|
||||
func pluginRelPath(raw string) (string, bool) {
|
||||
p := strings.TrimPrefix(filepath.ToSlash(strings.TrimSpace(raw)), "/")
|
||||
if p == "" {
|
||||
return "", false
|
||||
}
|
||||
for _, seg := range strings.Split(p, "/") {
|
||||
if seg == ".." {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
clean := path.Clean(p)
|
||||
if !strings.HasPrefix(clean, "plugin/") || clean == "plugin/" {
|
||||
return "", false
|
||||
}
|
||||
return clean, true
|
||||
}
|
||||
|
||||
// handleRegisterFile handles POST /api/files/register.
|
||||
//
|
||||
// Does for ONE file what the watcher does for every file it scans: midhash256
|
||||
// it, mint a root carrying filePath/sizeByte/mtimeNano, register the
|
||||
// cid:<midhash> alias, and append a file:events entry so meta-share's
|
||||
// ipfs_seed chunks, seeds and provides it.
|
||||
//
|
||||
// It exists for cores that run with ENABLE_FILE_WATCHER=false (a meta-watch
|
||||
// client box). Without it, bytes written under /files/plugin over WebDAV are
|
||||
// never resolvable by CID and never reach the swarm. Idempotent: the same
|
||||
// content at the same path returns the existing root with created=false.
|
||||
func (s *Server) handleRegisterFile(w http.ResponseWriter, r *http.Request) {
|
||||
if s.storage == nil || !s.storage.IsConnected() {
|
||||
writeError(w, http.StatusServiceUnavailable, "storage not connected")
|
||||
return
|
||||
}
|
||||
|
||||
var req registerFileRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
rel, ok := pluginRelPath(req.Path)
|
||||
if !ok {
|
||||
writeError(w, http.StatusBadRequest, "path must be a file under plugin/")
|
||||
return
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(s.config.FilesPath, filepath.FromSlash(rel))
|
||||
absFiles, _ := filepath.Abs(filepath.Join(s.config.FilesPath, "plugin"))
|
||||
absFull, _ := filepath.Abs(fullPath)
|
||||
if !strings.HasPrefix(absFull, absFiles+string(filepath.Separator)) {
|
||||
writeError(w, http.StatusBadRequest, "path must be a file under plugin/")
|
||||
return
|
||||
}
|
||||
|
||||
info, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, "file not found")
|
||||
return
|
||||
}
|
||||
if info.IsDir() {
|
||||
writeError(w, http.StatusBadRequest, "path is a directory, not a file")
|
||||
return
|
||||
}
|
||||
|
||||
midhash, err := watcher.ComputeMidHash256(fullPath, info.Size())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "failed to hash file")
|
||||
return
|
||||
}
|
||||
|
||||
root, created, err := s.storage.RegisterFileTuple(fullPath, info.Size(), info.ModTime().UnixNano(), midhash)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if created {
|
||||
ev := watcher.FileEvent{
|
||||
Type: watcher.EventTypeAdd,
|
||||
Path: rel,
|
||||
Size: info.Size(),
|
||||
MidHash256: midhash,
|
||||
Timestamp: watcher.NowMS(),
|
||||
}
|
||||
if err := watcher.PublishEvent(s.storage, ev); err != nil {
|
||||
// The record is written and resolvable; only seeding is late.
|
||||
writeError(w, http.StatusInternalServerError, "registered, but file event not published: "+err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
status := http.StatusOK
|
||||
if created {
|
||||
status = http.StatusCreated
|
||||
}
|
||||
writeJSON(w, status, registerFileResponse{
|
||||
Root: root,
|
||||
CID: midhash,
|
||||
Created: created,
|
||||
Path: rel,
|
||||
Size: info.Size(),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/metazla/meta-core/internal/config"
|
||||
"github.com/metazla/meta-core/internal/storage"
|
||||
)
|
||||
|
||||
// newRegisterTestServer wires handleRegisterFile to an in-memory Redis and a
|
||||
// temp FILES_PATH. Built directly, not through NewServer, so no watcher runs —
|
||||
// which is exactly the configuration this route exists for.
|
||||
func newRegisterTestServer(t *testing.T) (*Server, string, *miniredis.Miniredis) {
|
||||
t.Helper()
|
||||
mr := miniredis.RunT(t)
|
||||
c := storage.NewClient("")
|
||||
if err := c.Connect("redis://" + mr.Addr()); err != nil {
|
||||
t.Fatalf("connect to miniredis: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = c.Close() })
|
||||
files := t.TempDir()
|
||||
return &Server{storage: c, config: &config.Config{FilesPath: files}}, files, mr
|
||||
}
|
||||
|
||||
func writeFixture(t *testing.T, files, rel, body string) string {
|
||||
t.Helper()
|
||||
full := filepath.Join(files, filepath.FromSlash(rel))
|
||||
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(full, []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return full
|
||||
}
|
||||
|
||||
func postRegister(t *testing.T, s *Server, path string) (int, registerFileResponse) {
|
||||
t.Helper()
|
||||
body, _ := json.Marshal(registerFileRequest{Path: path})
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/files/register", strings.NewReader(string(body)))
|
||||
rr := httptest.NewRecorder()
|
||||
s.handleRegisterFile(rr, req)
|
||||
var resp registerFileResponse
|
||||
_ = json.Unmarshal(rr.Body.Bytes(), &resp)
|
||||
return rr.Code, resp
|
||||
}
|
||||
|
||||
func streamLen(t *testing.T, mr *miniredis.Miniredis) int {
|
||||
t.Helper()
|
||||
entries, err := mr.Stream("file:events")
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return len(entries)
|
||||
}
|
||||
|
||||
// The whole point of the route: with the watcher off, a file written under
|
||||
// plugin/ becomes a record resolvable by its midhash, and meta-share hears
|
||||
// about it on file:events.
|
||||
func TestRegisterFile_MintsAliasAndPublishesEvent(t *testing.T) {
|
||||
s, files, mr := newRegisterTestServer(t)
|
||||
rel := "plugin/subtitles/ab12cd34.eng.srt"
|
||||
full := writeFixture(t, files, rel, "1\n00:00:01,000 --> 00:00:02,000\nHello\n")
|
||||
|
||||
code, resp := postRegister(t, s, rel)
|
||||
if code != http.StatusCreated || !resp.Created {
|
||||
t.Fatalf("want 201 created, got %d %+v", code, resp)
|
||||
}
|
||||
if !strings.HasPrefix(resp.CID, "b") || resp.Root == "" {
|
||||
t.Fatalf("want a multibase cid and a root, got %+v", resp)
|
||||
}
|
||||
|
||||
root, err := s.storage.GetByCID(resp.CID)
|
||||
if err != nil || root != resp.Root {
|
||||
t.Fatalf("cid alias must resolve to the root: got %q err=%v, want %q", root, err, resp.Root)
|
||||
}
|
||||
if fp, _ := s.storage.GetProperty(resp.Root, "filePath"); fp != full {
|
||||
t.Fatalf("filePath = %q, want %q", fp, full)
|
||||
}
|
||||
|
||||
entries, err := mr.Stream("file:events")
|
||||
if err != nil || len(entries) != 1 {
|
||||
t.Fatalf("want exactly one file:events entry, got %d err=%v", len(entries), err)
|
||||
}
|
||||
vals := strings.Join(entries[0].Values, "|")
|
||||
if !strings.Contains(vals, "midhash256|"+resp.CID) || !strings.Contains(vals, "path|"+rel) {
|
||||
t.Fatalf("event must carry the midhash and the relative path, got %q", vals)
|
||||
}
|
||||
}
|
||||
|
||||
// Re-uploading the same subtitle is a no-op: same root, no second event (which
|
||||
// would make meta-share re-chunk and re-provide the file).
|
||||
func TestRegisterFile_IsIdempotent(t *testing.T) {
|
||||
s, files, mr := newRegisterTestServer(t)
|
||||
rel := "plugin/subtitles/same.eng.srt"
|
||||
writeFixture(t, files, rel, "same bytes")
|
||||
|
||||
_, first := postRegister(t, s, rel)
|
||||
code, second := postRegister(t, s, rel)
|
||||
if code != http.StatusOK || second.Created {
|
||||
t.Fatalf("second register must be 200 created=false, got %d %+v", code, second)
|
||||
}
|
||||
if second.Root != first.Root || second.CID != first.CID {
|
||||
t.Fatalf("second register must return the same record: %+v vs %+v", second, first)
|
||||
}
|
||||
if n := streamLen(t, mr); n != 1 {
|
||||
t.Fatalf("an idempotent re-register must not publish again, stream has %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
// Only plugin output may be registered here. /files/watch is the user's
|
||||
// library and belongs to the watcher; traversal must not escape either.
|
||||
func TestRegisterFile_RejectsPathsOutsidePlugin(t *testing.T) {
|
||||
s, files, _ := newRegisterTestServer(t)
|
||||
writeFixture(t, files, "watch/movie.mkv", "x")
|
||||
for _, p := range []string{
|
||||
"", "plugin", "plugin/", "watch/movie.mkv", "/watch/movie.mkv",
|
||||
"plugin/../watch/movie.mkv", "../etc/passwd", "plugin/../../etc/passwd",
|
||||
} {
|
||||
if code, _ := postRegister(t, s, p); code != http.StatusBadRequest {
|
||||
t.Errorf("path %q: want 400, got %d", p, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterFile_MissingFileAndDirectory(t *testing.T) {
|
||||
s, files, _ := newRegisterTestServer(t)
|
||||
if code, _ := postRegister(t, s, "plugin/subtitles/nope.srt"); code != http.StatusNotFound {
|
||||
t.Errorf("missing file: want 404, got %d", code)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(files, "plugin", "subtitles", "dir"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code, _ := postRegister(t, s, "plugin/subtitles/dir"); code != http.StatusBadRequest {
|
||||
t.Errorf("directory: want 400, got %d", code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package api
|
||||
|
||||
import "net/http"
|
||||
|
||||
// fileTupleJSON is one file-backed root on the wire.
|
||||
type fileTupleJSON struct {
|
||||
HashID string `json:"hashId"`
|
||||
FilePath string `json:"filePath"`
|
||||
SizeByte int64 `json:"sizeByte"`
|
||||
MtimeNano int64 `json:"mtimeNano"`
|
||||
}
|
||||
|
||||
// FileTuplesResponse is the body of GET /api/files/tuples.
|
||||
//
|
||||
// Files is a pointer so the summary form can omit it while the full form
|
||||
// always carries it — an empty library is `"files": []`, never an absent key,
|
||||
// which a client could mistake for a meta-core that lacks the rows.
|
||||
type FileTuplesResponse struct {
|
||||
Files *[]fileTupleJSON `json:"files,omitempty"`
|
||||
Count int `json:"count"`
|
||||
TotalSize int64 `json:"totalSize"`
|
||||
}
|
||||
|
||||
// handleGetFileTuples handles GET /api/files/tuples.
|
||||
//
|
||||
// Lists the FILE-BACKED roots — records carrying filePath, sizeByte and
|
||||
// mtimeNano, i.e. what the watcher or /api/files/register minted — with their
|
||||
// sizes, plus the count and total. Gateway / cid-rooted records carry no file
|
||||
// and are not included; neither is a locator record that only merged a cache
|
||||
// file's metadata (it has no mtimeNano).
|
||||
//
|
||||
// It exists so a client never walks every hashId to answer "how many files,
|
||||
// how big": GetTuplesForAllFiles reads the three fields for every root in
|
||||
// chunked MGETs server-side (~250 round-trips for 82k roots, no SCAN), where
|
||||
// the client-side equivalent was one HTTP request per record.
|
||||
//
|
||||
// ?summary=1 omits the rows and returns only {count, totalSize}.
|
||||
func (s *Server) handleGetFileTuples(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.storage.IsConnected() {
|
||||
writeError(w, http.StatusServiceUnavailable, "storage not connected")
|
||||
return
|
||||
}
|
||||
|
||||
tuples, err := s.storage.GetTuplesForAllFiles()
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
summary := r.URL.Query().Get("summary")
|
||||
withRows := summary == "" || summary == "0" || summary == "false"
|
||||
|
||||
resp := FileTuplesResponse{Count: len(tuples)}
|
||||
var rows []fileTupleJSON
|
||||
if withRows {
|
||||
rows = make([]fileTupleJSON, 0, len(tuples))
|
||||
}
|
||||
for _, t := range tuples {
|
||||
resp.TotalSize += t.Size
|
||||
if withRows {
|
||||
rows = append(rows, fileTupleJSON{
|
||||
HashID: t.HashID,
|
||||
FilePath: t.FilePath,
|
||||
SizeByte: t.Size,
|
||||
MtimeNano: t.MtimeNano,
|
||||
})
|
||||
}
|
||||
}
|
||||
if withRows {
|
||||
resp.Files = &rows
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/metazla/meta-core/internal/storage"
|
||||
)
|
||||
|
||||
func newFileTuplesTestServer(t *testing.T) (*Server, *storage.Client) {
|
||||
t.Helper()
|
||||
mr := miniredis.RunT(t)
|
||||
c := storage.NewClient("")
|
||||
if err := c.Connect("redis://" + mr.Addr()); err != nil {
|
||||
t.Fatalf("connect to miniredis: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = c.Close() })
|
||||
return &Server{storage: c}, c
|
||||
}
|
||||
|
||||
func getFileTuples(t *testing.T, s *Server, query string) (int, FileTuplesResponse) {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/files/tuples"+query, nil)
|
||||
rr := httptest.NewRecorder()
|
||||
s.handleGetFileTuples(rr, req)
|
||||
var resp FileTuplesResponse
|
||||
_ = json.Unmarshal(rr.Body.Bytes(), &resp)
|
||||
return rr.Code, resp
|
||||
}
|
||||
|
||||
func mustMint(t *testing.T, c *storage.Client, path string, size, mtime int64) string {
|
||||
t.Helper()
|
||||
id, err := c.Mint(path, size, mtime)
|
||||
if err != nil {
|
||||
t.Fatalf("mint %s: %v", path, err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// Only records that are actually files count: a gateway record and a record
|
||||
// that merely carries a path + size (no mtimeNano) are both left out.
|
||||
func TestFileTuples_OnlyFileBackedRoots(t *testing.T) {
|
||||
s, c := newFileTuplesTestServer(t)
|
||||
a := mustMint(t, c, "/files/watch/a.mkv", 1000, 1)
|
||||
b := mustMint(t, c, "/files/watch/b.mkv", 2500, 2)
|
||||
if err := c.SetMetadataFlat("bagcsgatewayrecord", map[string]string{"title": "Some Release"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.SetMetadataFlat("01partialroot", map[string]string{"filePath": "/files/cache/x.mkv", "sizeByte": "999"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
code, resp := getFileTuples(t, s, "")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("want 200, got %d", code)
|
||||
}
|
||||
if resp.Count != 2 || resp.TotalSize != 3500 {
|
||||
t.Fatalf("want count 2 / totalSize 3500, got %d / %d", resp.Count, resp.TotalSize)
|
||||
}
|
||||
if resp.Files == nil || len(*resp.Files) != 2 {
|
||||
t.Fatalf("want 2 rows, got %+v", resp.Files)
|
||||
}
|
||||
got := []string{(*resp.Files)[0].HashID, (*resp.Files)[1].HashID}
|
||||
want := []string{a, b}
|
||||
sort.Strings(got)
|
||||
sort.Strings(want)
|
||||
if got[0] != want[0] || got[1] != want[1] {
|
||||
t.Fatalf("want roots %v, got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileTuples_SummaryOmitsRows(t *testing.T) {
|
||||
s, c := newFileTuplesTestServer(t)
|
||||
mustMint(t, c, "/files/watch/a.mkv", 1000, 1)
|
||||
mustMint(t, c, "/files/watch/b.mkv", 2500, 2)
|
||||
|
||||
code, resp := getFileTuples(t, s, "?summary=1")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("want 200, got %d", code)
|
||||
}
|
||||
if resp.Files != nil {
|
||||
t.Fatalf("summary must omit rows, got %d", len(*resp.Files))
|
||||
}
|
||||
if resp.Count != 2 || resp.TotalSize != 3500 {
|
||||
t.Fatalf("want count 2 / totalSize 3500, got %d / %d", resp.Count, resp.TotalSize)
|
||||
}
|
||||
}
|
||||
|
||||
// An empty library still carries "files": [] — clients treat an absent key as
|
||||
// "this meta-core cannot list files", so it must not disappear when empty.
|
||||
func TestFileTuples_EmptyLibraryKeepsFilesKey(t *testing.T) {
|
||||
s, _ := newFileTuplesTestServer(t)
|
||||
|
||||
code, resp := getFileTuples(t, s, "")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("want 200, got %d", code)
|
||||
}
|
||||
if resp.Files == nil || len(*resp.Files) != 0 || resp.Count != 0 {
|
||||
t.Fatalf("want an empty files array and count 0, got %+v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileTuples_StorageNotConnected(t *testing.T) {
|
||||
s := &Server{storage: storage.NewClient("")}
|
||||
|
||||
code, _ := getFileTuples(t, s, "")
|
||||
if code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("want 503, got %d", code)
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,14 @@ package api
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/metazla/meta-core/internal/identity"
|
||||
"github.com/metazla/meta-core/internal/storage"
|
||||
)
|
||||
|
||||
// identityStatusResponse is the shape of GET /api/identity. The private key
|
||||
@@ -50,11 +54,68 @@ type identityImportResponse struct {
|
||||
UID string `json:"uid"`
|
||||
Curve string `json:"curve"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
// Created distinguishes "this key was new here" (201) from "this key was
|
||||
// already in the keystore" (200). Import is the sign-in primitive for
|
||||
// clients: presenting the secret key is the proof of ownership, so a
|
||||
// re-import must succeed rather than conflict. Clients use the flag only
|
||||
// to word the confirmation ("new profile" vs "welcome back").
|
||||
Created bool `json:"created"`
|
||||
}
|
||||
|
||||
// identityChallengeRequest asks for a one-shot challenge to sign.
|
||||
type identityChallengeRequest struct {
|
||||
UID string `json:"uid"`
|
||||
Action string `json:"action"`
|
||||
}
|
||||
|
||||
type identityChallengeResponse struct {
|
||||
Challenge string `json:"challenge"`
|
||||
ExpiresAt int64 `json:"expiresAt"`
|
||||
}
|
||||
|
||||
// identityProof is the proof-of-possession carried by reveal and delete: the
|
||||
// challenge text as issued, and a signature over it by the account's key.
|
||||
//
|
||||
// The signature is produced by whoever holds the key — the browser, locally.
|
||||
// It is NOT obtainable from POST /api/identity/sign, which refuses any payload
|
||||
// in the challenge domain; that refusal is what makes this proof mean
|
||||
// something on a node that stores every private key in plaintext.
|
||||
type identityProof struct {
|
||||
Challenge string `json:"challenge"`
|
||||
Signature string `json:"signature"`
|
||||
}
|
||||
|
||||
// identityRevealRequest asks for an existing account's private key back.
|
||||
//
|
||||
// Gated on proof-of-possession, which makes it a "does this node hold the same
|
||||
// key I do" check rather than the recovery path it once was. That is the
|
||||
// deliberate consequence of the rule this file enforces: the private key, and
|
||||
// nothing else, is authority over the account. An operator with root on the box
|
||||
// can still read the file; an operator with only the API cannot.
|
||||
type identityRevealRequest struct {
|
||||
Confirm bool `json:"confirm"`
|
||||
UID string `json:"uid"`
|
||||
identityProof
|
||||
}
|
||||
|
||||
type identityRevealResponse struct {
|
||||
UID string `json:"uid"`
|
||||
Curve string `json:"curve"`
|
||||
PrivateKeyHex string `json:"privateKeyHex"`
|
||||
}
|
||||
|
||||
type identityDeleteRequest struct {
|
||||
Confirm bool `json:"confirm"`
|
||||
UID string `json:"uid"`
|
||||
identityProof
|
||||
}
|
||||
|
||||
// identityDeleteResponse reports what the delete actually removed. The purge
|
||||
// counts are the point: "account removed" alone leaves the caller unable to
|
||||
// tell a clean removal from one that silently stranded a decade of ratings.
|
||||
type identityDeleteResponse struct {
|
||||
UID string `json:"uid"`
|
||||
Purged storage.UDLUserStats `json:"purged"`
|
||||
}
|
||||
|
||||
type identitySignRequest struct {
|
||||
@@ -68,6 +129,41 @@ type identitySignResponse struct {
|
||||
SigB64 string `json:"sigB64"`
|
||||
}
|
||||
|
||||
// identitySignBatchRequest signs many payloads under one account in one call.
|
||||
//
|
||||
// Exists because a User Data Layer write signs exactly one record, and the bulk
|
||||
// verbs above it ("mark this whole series seen") produce one record per episode:
|
||||
// a 300-episode show is 300 sequential sign round trips through
|
||||
// meta-watch -> meta-search -> here, which dominates the operation. The private
|
||||
// key still never leaves this process; only the transport is batched.
|
||||
type identitySignBatchRequest struct {
|
||||
// PayloadsB64 are the canonical byte strings to sign, in order. The
|
||||
// response's signatures line up index-for-index.
|
||||
PayloadsB64 []string `json:"payloadsB64"`
|
||||
// UID selects which account signs — same optional single-identity
|
||||
// back-compat as identitySignRequest.
|
||||
UID string `json:"uid,omitempty"`
|
||||
}
|
||||
|
||||
type identitySignBatchResponse struct {
|
||||
SigsB64 []string `json:"sigsB64"`
|
||||
}
|
||||
|
||||
// maxSignBatch bounds one batch. A signature is cheap but not free, and the
|
||||
// request holds the account lock for its duration; a caller with more than this
|
||||
// should page. Sized well above the longest real series (Supernatural, 327).
|
||||
const maxSignBatch = 1000
|
||||
|
||||
// reservedPayloadMessage explains the one refusal the signing endpoints make.
|
||||
//
|
||||
// This is the load-bearing half of proof-of-possession on a node that holds
|
||||
// every private key: without it, a caller wanting to delete someone else's
|
||||
// account just asks meta-core to sign the challenge for them, and the gate on
|
||||
// reveal/delete becomes a formality. See identity.ChallengeDomain.
|
||||
const reservedPayloadMessage = "payload is in the reserved account-authorisation domain (" +
|
||||
identity.ChallengeDomain + "…); meta-core will not sign one. " +
|
||||
"Signing a challenge is what proves you hold the key, so it must be done where the key is."
|
||||
|
||||
type identityAEADKeyResponse struct {
|
||||
AEADKeyB64 string `json:"aeadKeyB64"`
|
||||
}
|
||||
@@ -172,9 +268,17 @@ func (s *Server) handleIdentityImport(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
dir := s.config.IdentityAccountsDir()
|
||||
if identity.ExistsByUID(dir, id.UID) {
|
||||
writeErrorSlug(w, http.StatusConflict, "identity_exists",
|
||||
"an account with this key already exists", false)
|
||||
// Idempotent: importing a key that is already in the keystore is how a
|
||||
// second device signs in to an existing account. Answer 200 with the
|
||||
// stored account (preserving its original createdAt) instead of 409 —
|
||||
// the caller proved ownership by presenting the key.
|
||||
if existing, err := identity.LoadByUID(dir, id.UID); err == nil && existing != nil {
|
||||
writeJSON(w, http.StatusOK, identityImportResponse{
|
||||
UID: existing.UID,
|
||||
Curve: existing.Curve,
|
||||
CreatedAt: existing.CreatedAt,
|
||||
Created: false,
|
||||
})
|
||||
return
|
||||
}
|
||||
if err := identity.SaveAccount(dir, id); err != nil {
|
||||
@@ -185,49 +289,176 @@ func (s *Server) handleIdentityImport(w http.ResponseWriter, r *http.Request) {
|
||||
UID: id.UID,
|
||||
Curve: id.Curve,
|
||||
CreatedAt: id.CreatedAt,
|
||||
Created: true,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) handleIdentityDelete(w http.ResponseWriter, r *http.Request) {
|
||||
var body identityDeleteRequest
|
||||
// Tolerate empty body — confirm flag in a query param is also acceptable
|
||||
// for shells where sending a JSON body with DELETE is awkward.
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
if !body.Confirm && r.URL.Query().Get("confirm") != "true" {
|
||||
writeErrorSlug(w, http.StatusBadRequest, "confirm_required",
|
||||
"identity deletion requires {\"confirm\": true} or ?confirm=true", false)
|
||||
// handleIdentityChallenge issues the one-shot text a client signs to prove it
|
||||
// holds an account's private key.
|
||||
//
|
||||
// Unauthenticated by design: a challenge is useless without the key, and
|
||||
// GET /api/identity/accounts already publishes every uid, so refusing to mint
|
||||
// one for an unknown caller would protect nothing. Requiring the account to
|
||||
// exist is for a clear error, not secrecy.
|
||||
func (s *Server) handleIdentityChallenge(w http.ResponseWriter, r *http.Request) {
|
||||
var body identityChallengeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
dir := s.config.IdentityAccountsDir()
|
||||
uid := strings.TrimSpace(body.UID)
|
||||
if uid == "" {
|
||||
writeErrorSlug(w, http.StatusBadRequest, "uid_required", "uid is required", false)
|
||||
return
|
||||
}
|
||||
action := identity.Action(strings.TrimSpace(body.Action))
|
||||
if !identity.ValidAction(action) {
|
||||
writeErrorSlug(w, http.StatusBadRequest, "bad_action",
|
||||
`action must be "reveal" or "delete"`, false)
|
||||
return
|
||||
}
|
||||
if !identity.ExistsByUID(s.config.IdentityAccountsDir(), uid) {
|
||||
writeErrorSlug(w, http.StatusNotFound, "no_identity", "no identity for the given uid", false)
|
||||
return
|
||||
}
|
||||
text, expires, err := s.challenges.Issue(uid, action)
|
||||
if err != nil {
|
||||
if errors.Is(err, identity.ErrChallengeFull) {
|
||||
writeErrorSlug(w, http.StatusServiceUnavailable, "challenges_full", err.Error(), true)
|
||||
return
|
||||
}
|
||||
writeErrorSlug(w, http.StatusBadRequest, "bad_uid", err.Error(), false)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, identityChallengeResponse{
|
||||
Challenge: text,
|
||||
ExpiresAt: expires.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
// requireProof resolves the target account and verifies the caller's
|
||||
// proof-of-possession for `action`. status == 0 means the proof held.
|
||||
//
|
||||
// The uid is mandatory here, unlike elsewhere in this file: the old "sole
|
||||
// account" fallback would have this pick a target the signature does not name,
|
||||
// and a proof that authorises an unnamed account authorises the wrong one as
|
||||
// soon as a second profile exists.
|
||||
func (s *Server) requireProof(uid string, proof identityProof, action identity.Action) (
|
||||
id *identity.Identity, status int, slug, msg string,
|
||||
) {
|
||||
uid = strings.TrimSpace(uid)
|
||||
if uid == "" {
|
||||
return nil, http.StatusBadRequest, "uid_required",
|
||||
"uid is required: the signature authorises one specific account"
|
||||
}
|
||||
if proof.Challenge == "" || proof.Signature == "" {
|
||||
return nil, http.StatusUnauthorized, "signature_required",
|
||||
"this operation requires proof you hold the account's private key: " +
|
||||
"POST /api/identity/challenge, sign the returned text, and resend it as {challenge, signature}"
|
||||
}
|
||||
sig, err := base64.StdEncoding.DecodeString(proof.Signature)
|
||||
if err != nil {
|
||||
return nil, http.StatusBadRequest, "bad_signature", "signature is not valid base64"
|
||||
}
|
||||
// Resolve before redeeming so a request naming a missing account does not
|
||||
// burn a challenge it could not have used anyway.
|
||||
id, status, slug, msg = s.resolveAccount(uid)
|
||||
if status != 0 {
|
||||
return nil, status, slug, msg
|
||||
}
|
||||
// Redeem first, verify second: the challenge is single-use either way, so a
|
||||
// wrong signature costs the attacker a fresh round trip per guess rather
|
||||
// than letting them grind one challenge offline against the same nonce.
|
||||
if err := s.challenges.Redeem(proof.Challenge, uid, action); err != nil {
|
||||
return nil, http.StatusUnauthorized, "bad_challenge", err.Error()
|
||||
}
|
||||
if err := identity.Verify(uid, []byte(proof.Challenge), sig); err != nil {
|
||||
return nil, http.StatusUnauthorized, "bad_signature", err.Error()
|
||||
}
|
||||
return id, 0, "", ""
|
||||
}
|
||||
|
||||
// handleIdentityReveal returns an existing account's private key to a caller
|
||||
// that proves it already holds that key.
|
||||
//
|
||||
// Deliberately POST (never a bare GET) so it cannot be triggered by a link or
|
||||
// an image tag. The proof is what actually guards it: before this gate existed,
|
||||
// anything that could reach the port could read every private key on the node.
|
||||
func (s *Server) handleIdentityReveal(w http.ResponseWriter, r *http.Request) {
|
||||
var body identityRevealRequest
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
uid := strings.TrimSpace(body.UID)
|
||||
if uid == "" {
|
||||
uid = strings.TrimSpace(r.URL.Query().Get("uid"))
|
||||
}
|
||||
if uid == "" {
|
||||
// Back-compat: with no uid, delete the sole account. Ambiguous (and so
|
||||
// refused) once more than one account exists.
|
||||
list, err := identity.List(dir)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
switch len(list) {
|
||||
case 0:
|
||||
w.WriteHeader(http.StatusNoContent) // idempotent
|
||||
return
|
||||
case 1:
|
||||
uid = list[0].UID
|
||||
default:
|
||||
writeErrorSlug(w, http.StatusBadRequest, "uid_required",
|
||||
"multiple accounts exist; specify uid to delete", false)
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := identity.DeleteByUID(dir, uid); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
id, status, slug, msg := s.requireProof(uid, body.identityProof, identity.ActionReveal)
|
||||
if status != 0 {
|
||||
writeErrorSlug(w, status, slug, msg, false)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
writeJSON(w, http.StatusOK, identityRevealResponse{
|
||||
UID: id.UID,
|
||||
Curve: id.Curve,
|
||||
PrivateKeyHex: id.PrivateKeyHex,
|
||||
})
|
||||
}
|
||||
|
||||
// handleIdentityDelete removes one account and everything it wrote.
|
||||
//
|
||||
// Two things happen, in this order and no other:
|
||||
//
|
||||
// 1. the account's User Data Layer rows are purged (cells plus every index
|
||||
// that names the uid, including the cid-scoped ones shared with other
|
||||
// accounts);
|
||||
// 2. the keypair file is removed.
|
||||
//
|
||||
// Purge-then-delete so a failure never strands data: if the purge errors the
|
||||
// key is still on disk, the account still exists, and the caller can retry.
|
||||
// The reverse order would leave rows nobody can ever attribute, reach or
|
||||
// rewrite — the orphaning this endpoint exists to prevent.
|
||||
//
|
||||
// What deletion still does NOT do, and cannot: records already signed under
|
||||
// this uid and replicated to other peers keep verifying there. Removing a key
|
||||
// is not a recall.
|
||||
func (s *Server) handleIdentityDelete(w http.ResponseWriter, r *http.Request) {
|
||||
var body identityDeleteRequest
|
||||
// Tolerate an empty body so the uid may ride in the query — meta-watch's
|
||||
// proxy puts it there. The proof itself must be in the body: a signature in
|
||||
// a URL ends up in every access log between here and the browser.
|
||||
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||
uid := strings.TrimSpace(body.UID)
|
||||
if uid == "" {
|
||||
uid = strings.TrimSpace(r.URL.Query().Get("uid"))
|
||||
}
|
||||
id, status, slug, msg := s.requireProof(uid, body.identityProof, identity.ActionDelete)
|
||||
if status != 0 {
|
||||
writeErrorSlug(w, status, slug, msg, false)
|
||||
return
|
||||
}
|
||||
|
||||
// A purge needs storage. Refusing here — rather than deleting the key and
|
||||
// reporting the purge as "0 records" — is the difference between a retry
|
||||
// and permanent orphaned data: once the key is gone the account cannot be
|
||||
// named for a second attempt through this API.
|
||||
if !s.storage.IsConnected() {
|
||||
writeErrorSlug(w, http.StatusServiceUnavailable, "storage_unavailable",
|
||||
"storage is not connected; refusing to delete the key while its data cannot be purged", true)
|
||||
return
|
||||
}
|
||||
purged, err := s.storage.UDLPurgeUser(id.UID)
|
||||
if err != nil {
|
||||
writeErrorSlug(w, http.StatusInternalServerError, "purge_failed",
|
||||
"could not purge this account's data; the account was NOT deleted: "+err.Error(), true)
|
||||
return
|
||||
}
|
||||
|
||||
if err := identity.DeleteByUID(s.config.IdentityAccountsDir(), id.UID); err != nil {
|
||||
writeErrorSlug(w, http.StatusInternalServerError, "delete_failed", err.Error(), false)
|
||||
return
|
||||
}
|
||||
log.Printf("[API] Deleted account %s and purged %d User Data Layer records across %d cids",
|
||||
id.UID, purged.Records, purged.Cids)
|
||||
writeJSON(w, http.StatusOK, identityDeleteResponse{UID: id.UID, Purged: purged})
|
||||
}
|
||||
|
||||
func (s *Server) handleIdentitySign(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -250,6 +481,10 @@ func (s *Server) handleIdentitySign(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "bytesB64 is not valid base64")
|
||||
return
|
||||
}
|
||||
if identity.IsChallengePayload(raw) {
|
||||
writeErrorSlug(w, http.StatusForbidden, "reserved_payload", reservedPayloadMessage, false)
|
||||
return
|
||||
}
|
||||
sig, err := identity.Sign(id, raw)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
@@ -260,6 +495,58 @@ func (s *Server) handleIdentitySign(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// handleIdentitySignBatch signs every payload in the request under one account.
|
||||
//
|
||||
// All-or-nothing: a single bad base64 or signing failure fails the whole
|
||||
// request rather than returning a short array. The caller pairs signatures with
|
||||
// records by index, so a partial response would silently misalign them — and a
|
||||
// misaligned signature verifies against the wrong record, which is worse than
|
||||
// an error.
|
||||
func (s *Server) handleIdentitySignBatch(w http.ResponseWriter, r *http.Request) {
|
||||
var body identitySignBatchRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
uid := body.UID
|
||||
if strings.TrimSpace(uid) == "" {
|
||||
uid = r.URL.Query().Get("uid")
|
||||
}
|
||||
if len(body.PayloadsB64) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "payloadsB64 is required")
|
||||
return
|
||||
}
|
||||
if len(body.PayloadsB64) > maxSignBatch {
|
||||
writeError(w, http.StatusBadRequest, "too many payloads in one batch")
|
||||
return
|
||||
}
|
||||
id, status, slug, msg := s.resolveAccount(uid)
|
||||
if status != 0 {
|
||||
writeErrorSlug(w, status, slug, msg, false)
|
||||
return
|
||||
}
|
||||
sigs := make([]string, 0, len(body.PayloadsB64))
|
||||
for i, p := range body.PayloadsB64 {
|
||||
raw, err := base64.StdEncoding.DecodeString(p)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "payloadsB64["+strconv.Itoa(i)+"] is not valid base64")
|
||||
return
|
||||
}
|
||||
if identity.IsChallengePayload(raw) {
|
||||
writeErrorSlug(w, http.StatusForbidden, "reserved_payload",
|
||||
"payloadsB64["+strconv.Itoa(i)+"]: "+reservedPayloadMessage, false)
|
||||
return
|
||||
}
|
||||
sig, err := identity.Sign(id, raw)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
sigs = append(sigs, base64.StdEncoding.EncodeToString(sig))
|
||||
}
|
||||
writeJSON(w, http.StatusOK, identitySignBatchResponse{SigsB64: sigs})
|
||||
}
|
||||
|
||||
func (s *Server) handleIdentityAEADKey(w http.ResponseWriter, r *http.Request) {
|
||||
id, status, slug, msg := s.resolveAccount(r.URL.Query().Get("uid"))
|
||||
if status != 0 {
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
package api
|
||||
|
||||
// End-to-end exercise of the account gate against a live router + miniredis:
|
||||
// generate -> write UDL rows -> stats -> challenge -> sign -> delete -> verify
|
||||
// both the key and every row are gone. Deliberately drives the real HTTP
|
||||
// handlers rather than the packages beneath them, because the bugs that matter
|
||||
// here (a challenge accepted for the wrong action, a purge that runs after the
|
||||
// key is gone) live in the wiring.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/metazla/meta-core/internal/config"
|
||||
"github.com/metazla/meta-core/internal/identity"
|
||||
"github.com/metazla/meta-core/internal/storage"
|
||||
)
|
||||
|
||||
func newTestServer(t *testing.T) *Server {
|
||||
t.Helper()
|
||||
mr := miniredis.RunT(t)
|
||||
stor := storage.NewClient("")
|
||||
if err := stor.Connect("redis://" + mr.Addr()); err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = stor.Close() })
|
||||
|
||||
s := &Server{
|
||||
config: &config.Config{MetaCorePath: t.TempDir()},
|
||||
storage: stor,
|
||||
challenges: identity.NewChallengeStore(),
|
||||
router: mux.NewRouter(),
|
||||
}
|
||||
s.setupIdentityTestRoutes()
|
||||
return s
|
||||
}
|
||||
|
||||
// Only the routes under test, so this does not depend on every other subsystem
|
||||
// booting.
|
||||
func (s *Server) setupIdentityTestRoutes() {
|
||||
s.router.HandleFunc("/api/identity/accounts", s.handleIdentityAccounts).Methods("GET")
|
||||
s.router.HandleFunc("/api/identity/challenge", s.handleIdentityChallenge).Methods("POST")
|
||||
s.router.HandleFunc("/api/identity/generate", s.handleIdentityGenerate).Methods("POST")
|
||||
s.router.HandleFunc("/api/identity/reveal", s.handleIdentityReveal).Methods("POST")
|
||||
s.router.HandleFunc("/api/identity", s.handleIdentityDelete).Methods("DELETE")
|
||||
s.router.HandleFunc("/api/identity/sign", s.handleIdentitySign).Methods("POST")
|
||||
s | ||||