feat: importer IMVU accounts and settings from the console
The importer sends its RSA public key (POST /importer/hello); the console looks IMVU accounts up, confirms them in a dialog, and seals each password in the browser for that key. The hub keeps only the sealed copy, until the importer confirms it has it. Accounts are tried in order; settings (rate, jobs at once) come from the console too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d716aabbd2
commit
dcc39d82ab
16 files changed
+5505
-9
No files matched your search
@@ -56,6 +56,7 @@ afterAll(async () => {
|
||||
});
|
||||
|
||||
type Person = { cookie: string; name: string };
|
||||
let rootAdmin: Person; // the first person: an admin
|
||||
async function call(p: Person | undefined, method: string, path: string, body?: unknown, headers: Record<string, string> = {}) {
|
||||
const res = await fetch(base + path, {
|
||||
method,
|
||||
@@ -95,6 +96,7 @@ const imvuProduct = (name: string, creator: number, rating = "GA") => ({ product
|
||||
|
||||
test("grants, requests, the importer's jobs, and withdrawal", async () => {
|
||||
const admin = await person("root");
|
||||
rootAdmin = admin;
|
||||
const owner = await person("shelf");
|
||||
const maker = await person("maker", 501);
|
||||
const fan = await person("fan");
|
||||
@@ -217,3 +219,98 @@ test("IMVU's 401 keeps what was last seen; 404 is gone", async () => {
|
||||
imvuProducts.delete(9500);
|
||||
expect(await imports.info(9500, true)).toBeUndefined();
|
||||
});
|
||||
|
||||
test("the importer's key, IMVU accounts with sealed passwords, and settings", async () => {
|
||||
const admin = rootAdmin;
|
||||
const owner = await person("keeper");
|
||||
const outsider = await person("outsider");
|
||||
accounts.set(601, { id: 601, username: "ImportBot", tagline: "", ageVerified: true, accessPass: true, picture: null });
|
||||
accounts.set(602, { id: 602, username: "SpareBot", tagline: "", ageVerified: false, accessPass: false, picture: null });
|
||||
|
||||
const reg = await call(owner, "POST", "/api/v1/catalogs", { name: "Keeper Shelf", url: "https://keeper.example.test", contact: "[email protected]", visibility: "public", membership: "open" });
|
||||
const catalogID = reg.json.catalog.id as string;
|
||||
expect((await call(admin, "POST", `/api/v1/console/catalogs/${catalogID}/approve`)).status).toBe(200);
|
||||
const on = await call(owner, "POST", `/api/v1/catalogs/${catalogID}/importer`, { enabled: true });
|
||||
const importer = { Authorization: `Importer ${catalogID}:${on.json.key}` };
|
||||
const page = async () => (await call(owner, "GET", `/api/v1/catalogs/${catalogID}/importer`)).json;
|
||||
|
||||
// Before the importer has said hello there's no key to seal for.
|
||||
expect((await page()).program).toMatchObject({ version: "", seenAt: null, fingerprint: null, publicKey: null });
|
||||
const keys = async () => {
|
||||
const pair = (await crypto.subtle.generateKey(
|
||||
{ name: "RSA-OAEP", modulusLength: 3072, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" },
|
||||
true,
|
||||
["encrypt", "decrypt"],
|
||||
)) as CryptoKeyPair;
|
||||
return { pair, spki: Buffer.from(await crypto.subtle.exportKey("spki", pair.publicKey)).toString("base64") };
|
||||
};
|
||||
// As the console's browser does: the account's ID and password, sealed for the importer's key.
|
||||
const seal = async (spki: string, imvuId: number, password: string) => {
|
||||
const key = await crypto.subtle.importKey("spki", Buffer.from(spki, "base64"), { name: "RSA-OAEP", hash: "SHA-256" }, false, ["encrypt"]);
|
||||
const data = new TextEncoder().encode(JSON.stringify({ imvuId, password }));
|
||||
return Buffer.from(await crypto.subtle.encrypt({ name: "RSA-OAEP" }, key, data)).toString("base64");
|
||||
};
|
||||
const unseal = async (pair: CryptoKeyPair, sealed: string) =>
|
||||
JSON.parse(new TextDecoder().decode(await crypto.subtle.decrypt({ name: "RSA-OAEP" }, pair.privateKey, Buffer.from(sealed, "base64"))));
|
||||
|
||||
const k1 = await keys();
|
||||
expect((await call(owner, "POST", `/api/v1/catalogs/${catalogID}/importer/accounts`, { imvuId: 601, sealed: await seal(k1.spki, 601, "x"), fingerprint: "00" })).json.error).toBe("no_importer");
|
||||
expect((await call(undefined, "POST", "/api/v1/importer/hello", { version: "0.1.0", publicKey: "not a key" }, importer)).status).toBe(400);
|
||||
const hello = await call(undefined, "POST", "/api/v1/importer/hello", { version: "0.1.0", publicKey: k1.spki }, importer);
|
||||
expect(hello.status).toBe(200);
|
||||
expect(hello.json).toMatchObject({ paused: false, settings: { rate: 2, concurrency: 2 }, accounts: [] });
|
||||
const shown = (await page()).program;
|
||||
expect(shown).toMatchObject({ version: "0.1.0", publicKey: k1.spki });
|
||||
expect(shown.fingerprint).toBe(hello.json.fingerprint);
|
||||
expect(shown.seenAt).not.toBeNull();
|
||||
|
||||
// The console looks the account up, then adds it with the password sealed for that key.
|
||||
expect((await call(outsider, "GET", `/api/v1/catalogs/${catalogID}/importer/imvu-account?username=ImportBot`)).status).toBe(403);
|
||||
const found = (await call(owner, "GET", `/api/v1/catalogs/${catalogID}/importer/imvu-account?username=ImportBot`)).json;
|
||||
expect(found).toMatchObject({ imvuId: 601, username: "ImportBot", accessPass: true, ageVerified: true });
|
||||
const add = (imvuId: number, sealed: string, fingerprint = shown.fingerprint) =>
|
||||
call(owner, "POST", `/api/v1/catalogs/${catalogID}/importer/accounts`, { imvuId, sealed, fingerprint });
|
||||
expect((await add(601, "bm90IHNlYWxlZA==")).json.error).toBe("invalid");
|
||||
expect((await add(601, await seal(k1.spki, 601, "pw1"), "ff".repeat(32))).json.error).toBe("importer_key_changed");
|
||||
const added = await add(601, await seal(k1.spki, 601, "pw1"));
|
||||
expect(added.status).toBe(201);
|
||||
expect(added.json.account).toMatchObject({ username: "ImportBot", state: "waiting", accessPass: true });
|
||||
expect((await add(601, await seal(k1.spki, 601, "pw1"))).json.error).toBe("exists");
|
||||
expect((await add(602, await seal(k1.spki, 602, "pw2"))).status).toBe(201);
|
||||
|
||||
// The importer gets the sealed passwords, in order, and can read them; the hub drops each once it has it.
|
||||
const config = (await call(undefined, "GET", "/api/v1/importer/config", undefined, importer)).json;
|
||||
expect(config.accounts.map((a: any) => a.username)).toEqual(["ImportBot", "SpareBot"]);
|
||||
expect(await unseal(k1.pair, config.accounts[0].sealed)).toEqual({ imvuId: 601, password: "pw1" });
|
||||
const first = config.accounts[0].id as string;
|
||||
expect((await call(undefined, "POST", `/api/v1/importer/accounts/${first}`, { received: true }, importer)).json.account.state).toBe("received");
|
||||
expect((await call(undefined, "GET", "/api/v1/importer/config", undefined, importer)).json.accounts[0].sealed).toBeNull();
|
||||
await call(undefined, "POST", `/api/v1/importer/accounts/${first}`, { state: "ok", accessPass: true }, importer);
|
||||
expect((await call(undefined, "POST", `/api/v1/importer/accounts/${first}`, { state: "ok" }, { Authorization: `Importer ${catalogID}:wrong` })).status).toBe(401);
|
||||
|
||||
// The order can change; settings are bounded.
|
||||
await call(owner, "POST", `/api/v1/catalogs/${catalogID}/importer/accounts/${first}/move`, { direction: "down" });
|
||||
expect((await page()).accounts.map((a: any) => [a.username, a.state])).toEqual([["SpareBot", "waiting"], ["ImportBot", "ok"]]);
|
||||
expect((await call(owner, "POST", `/api/v1/catalogs/${catalogID}/importer/settings`, { rate: 50, concurrency: 2 })).status).toBe(400);
|
||||
expect((await call(owner, "POST", `/api/v1/catalogs/${catalogID}/importer/settings`, { rate: 0.5, concurrency: 4 })).json.settings).toEqual({ rate: 0.5, concurrency: 4 });
|
||||
expect((await call(undefined, "GET", "/api/v1/importer/config", undefined, importer)).json.settings).toEqual({ rate: 0.5, concurrency: 4 });
|
||||
|
||||
// A new install (a new key) can't read the old seals: every account needs its password again.
|
||||
const k2 = await keys();
|
||||
expect((await call(undefined, "POST", "/api/v1/importer/hello", { version: "0.1.1", publicKey: k2.spki }, importer)).json.accounts.every((a: any) => a.sealed === null)).toBe(true);
|
||||
const after = await page();
|
||||
expect(after.accounts.map((a: any) => a.state)).toEqual(["needs_password", "needs_password"]);
|
||||
const spare = after.accounts[0].id as string;
|
||||
const again = await call(owner, "PUT", `/api/v1/catalogs/${catalogID}/importer/accounts/${spare}/password`, { sealed: await seal(k2.spki, 602, "pw3"), fingerprint: after.program.fingerprint });
|
||||
expect(again.json.account.state).toBe("waiting");
|
||||
const sealed = (await call(undefined, "GET", "/api/v1/importer/config", undefined, importer)).json.accounts[0].sealed;
|
||||
expect(await unseal(k2.pair, sealed)).toEqual({ imvuId: 602, password: "pw3" });
|
||||
|
||||
// Removed accounts leave the importer's config; it's all audited.
|
||||
expect((await call(owner, "DELETE", `/api/v1/catalogs/${catalogID}/importer/accounts/${spare}`)).status).toBe(204);
|
||||
expect((await call(undefined, "GET", "/api/v1/importer/config", undefined, importer)).json.accounts.map((a: any) => a.username)).toEqual(["ImportBot"]);
|
||||
const actions = (await call(owner, "GET", "/api/v1/console/audit")).json.entries.map((e: any) => e.action);
|
||||
expect(actions).toEqual(expect.arrayContaining(["catalog.importer_hello", "catalog.importer_account", "catalog.importer_settings"]));
|
||||
// Passwords never show on the console.
|
||||
expect(JSON.stringify(await page())).not.toContain(sealed);
|
||||
});
|
||||
Reference in new issue
Block a user