feat: harden install and resumable OAuth login
CI / test (macos-latest) (push) Canceled after 0s
CI / test (ubuntu-latest) (push) Canceled after 0s
CI / test (windows-latest) (push) Canceled after 0s

This commit is contained in:
nac
2026-08-28 07:01:20 +00:00
parent 6b5b3a413a
commit 9f3d3ad5fc
19 changed files with 1721 additions and 472 deletions
+174 -1
View File
@@ -1,6 +1,14 @@
import assert from "node:assert/strict";
import test from "node:test";
import { type AuthorizationPrompt, loginWithBrowser, refreshOAuthCredentials } from "../src/oauth.js";
import {
type AuthorizationPrompt,
beginBrowserLogin,
loginWithBrowser,
type PendingOAuthAuthorization,
pollBrowserLogin,
refreshOAuthCredentials,
validatePendingOAuthAuthorization,
} from "../src/oauth.js";
const discovery = {
issuer: "https://justconn.top/api",
@@ -77,6 +85,7 @@ test("uses NocoBase dynamic registration and browser device authorization withou
assert.equal(credentials.accessToken, "oauth-access-token");
assert.equal(credentials.refreshToken, "oauth-refresh-token");
assert.equal(credentials.expiresAt, 901_000);
assert.equal(calls.filter((call) => call.url.endsWith("/.well-known/openid-configuration")).length, 1);
assert.doesNotMatch(JSON.stringify(credentials), /must-not-be-stored/);
assert.deepEqual(prompt, {
opened: true,
@@ -98,6 +107,147 @@ test("uses NocoBase dynamic registration and browser device authorization withou
assert.equal(deviceForm.get("resource"), "https://justconn.top/api/");
});
test("resumes one persisted browser authorization without creating another link", async () => {
let currentTime = 1_000;
let deviceStarts = 0;
let tokenPolls = 0;
const fetchMock = (async (input: string | URL | Request) => {
const url = String(input);
if (url.endsWith("/.well-known/openid-configuration")) return jsonResponse(discovery);
if (url.endsWith("/idpOAuth/device/auth")) {
deviceStarts += 1;
return jsonResponse({
device_code: "private-resumable-device-code",
user_code: "RESUME-CODE",
verification_uri: "https://justconn.top/idpOAuth/device",
verification_uri_complete: "https://justconn.top/idpOAuth/device?user_code=RESUME-CODE",
expires_in: 600,
interval: 5,
});
}
if (url.endsWith("/idpOAuth/token")) {
tokenPolls += 1;
return jsonResponse({
access_token: "resumed-access-token",
refresh_token: "resumed-refresh-token",
token_type: "Bearer",
scope: "api offline_access",
expires_in: 900,
});
}
throw new Error(`Unexpected request: ${url}`);
}) as typeof fetch;
const authorization = await beginBrowserLogin({
clientId: "desktop-client",
fetch: fetchMock,
now: () => currentTime,
openBrowser: async () => false,
});
assert.equal(deviceStarts, 1);
assert.equal(authorization.deviceCode, "private-resumable-device-code");
assert.equal(authorization.nextPollAt, 6_000);
currentTime = 5_000;
const tooEarly = await pollBrowserLogin(authorization, { fetch: fetchMock, now: () => currentTime });
assert.equal(tooEarly.status, "authorization_pending");
assert.equal(tokenPolls, 0);
currentTime = 6_000;
const resumed = await pollBrowserLogin(authorization, { fetch: fetchMock, now: () => currentTime });
assert.equal(resumed.status, "authenticated");
if (resumed.status === "authenticated") {
assert.equal(resumed.credentials.accessToken, "resumed-access-token");
assert.equal(resumed.credentials.authorizationSessionId, authorization.sessionId);
}
assert.equal(tokenPolls, 1);
assert.equal(deviceStarts, 1);
});
test("normalizes pending and slow-down responses for resumable browser login", async () => {
for (const scenario of [
{ code: "authorization_pending", intervalSeconds: 5, nextPollAt: 11_000 },
{ code: "slow_down", intervalSeconds: 10, nextPollAt: 16_000 },
]) {
let discoveries = 0;
const fetchMock = (async (input: string | URL | Request) => {
const url = String(input);
if (url.endsWith("/.well-known/openid-configuration")) {
discoveries += 1;
return jsonResponse(discovery);
}
if (url.endsWith("/idpOAuth/token")) return jsonResponse({ error: scenario.code }, 400);
throw new Error(`Unexpected request: ${url}`);
}) as typeof fetch;
const result = await pollBrowserLogin(persistedAuthorization(), {
fetch: fetchMock,
now: () => 6_000,
});
assert.equal(result.status, "authorization_pending");
if (result.status === "authorization_pending") {
assert.equal(result.authorization.intervalSeconds, scenario.intervalSeconds);
assert.equal(result.authorization.nextPollAt, scenario.nextPollAt);
}
assert.equal(discoveries, 1);
}
});
test("maps cancelled and expired resumable browser login to stable errors", async () => {
for (const scenario of [
{ code: "access_denied", message: /cancelled/ },
{ code: "expired_token", message: /expired/ },
]) {
const fetchMock = (async (input: string | URL | Request) => {
const url = String(input);
if (url.endsWith("/.well-known/openid-configuration")) return jsonResponse(discovery);
if (url.endsWith("/idpOAuth/token")) return jsonResponse({ error: scenario.code }, 400);
throw new Error(`Unexpected request: ${url}`);
}) as typeof fetch;
await assert.rejects(
pollBrowserLogin(persistedAuthorization(), { fetch: fetchMock, now: () => 6_000 }),
scenario.message,
);
}
let calls = 0;
await assert.rejects(
pollBrowserLogin(persistedAuthorization({ expiresAt: 6_000 }), {
fetch: (async () => {
calls += 1;
throw new Error("must not fetch");
}) as typeof fetch,
now: () => 6_000,
}),
/expired/,
);
assert.equal(calls, 0);
});
test("rejects a persisted browser authorization that was redirected off origin", () => {
assert.throws(
() =>
validatePendingOAuthAuthorization({
schemaVersion: 1,
type: "oauth-device-authorization",
sessionId: "session",
issuer: "https://justconn.top/api",
clientId: "client",
deviceCode: "device-code",
prompt: {
opened: false,
url: "https://evil.example/device",
userCode: "CODE",
expiresIn: 600,
},
expiresAt: 601_000,
intervalSeconds: 5,
nextPollAt: 6_000,
}),
/untrusted browser URL/,
);
});
test("rejects OAuth discovery that points tokens at another origin", async () => {
const fetchMock = (async () =>
jsonResponse({ ...discovery, token_endpoint: "https://evil.example/token" })) as typeof fetch;
@@ -148,3 +298,26 @@ function jsonResponse(value: unknown, status = 200): Response {
headers: { "content-type": "application/json" },
});
}
function persistedAuthorization(
overrides: Partial<PendingOAuthAuthorization> = {},
): PendingOAuthAuthorization {
return {
schemaVersion: 1,
type: "oauth-device-authorization",
sessionId: "persisted-session",
issuer: "https://justconn.top/api",
clientId: "desktop-client",
deviceCode: "private-device-code",
prompt: {
opened: false,
url: "https://justconn.top/idpOAuth/device?user_code=RESUME-CODE",
userCode: "RESUME-CODE",
expiresIn: 600,
},
expiresAt: 601_000,
intervalSeconds: 5,
nextPollAt: 6_000,
...overrides,
};
}