Files
2026-11-16 17:26:44 -08:00

155 lines
5.3 KiB
TypeScript

import { test, expect, describe } from "bun:test";
import { mkdtempSync, writeFileSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { parseAuthStatus } from "../src/lark_cli";
import {
applyOAuthRefresh,
encryptAesGcm,
decryptAesGcm,
parseStoredUAToken,
readStoredUserToken,
writeStoredUserToken,
safeFileName,
seedStoredAppSecret,
readStoredAppSecret,
masterKeyFileName,
userTokenAccount,
type StoredUAToken,
} from "../src/lark_cli_store";
/**
* Both of these shapes were guessed wrong on the first try, against a CLI that
* was right there to ask. The fixtures below are verbatim from lark-cli 1.0.86
* on the control plane — so a CLI upgrade that moves a field fails here instead
* of silently turning every satellite token fetch into a miss.
*/
const AUTH_OK = JSON.stringify({
appId: "cli_xxx",
brand: "feishu",
defaultAs: "auto",
identities: {
bot: { status: "ready", available: true },
user: {
status: "ready",
available: true,
openId: "ou_xxx",
userName: "Jingyuan Yi",
tokenStatus: "valid",
scope: "docx:document:readonly wiki:node:read offline_access",
expiresAt: "2026-08-13T16:42:52+08:00",
refreshExpiresAt: "2026-08-20T14:42:52+08:00",
},
},
identity: "user",
});
const AUTH_NO_USER = JSON.stringify({
appId: "cli_xxx",
identities: { bot: { status: "ready", available: true } },
});
describe("parseAuthStatus", () => {
test("reads identities from the TOP level, not under data", () => {
const a = parseAuthStatus(AUTH_OK);
expect(a.authorized).toBe(true);
expect(a.userName).toBe("Jingyuan Yi");
expect(a.refreshExpiresAt).toBe("2026-08-20T14:42:52+08:00");
expect(a.appId).toBe("cli_xxx");
expect(a.openId).toBe("ou_xxx");
expect(a.brand).toBe("feishu");
});
test("splits the space-joined scope string", () => {
expect(parseAuthStatus(AUTH_OK).scopes).toContain("docx:document:readonly");
expect(parseAuthStatus(AUTH_OK).scopes).toContain("offline_access");
});
test("bot-only is NOT authorized — reading docs needs the user identity", () => {
expect(parseAuthStatus(AUTH_NO_USER).authorized).toBe(false);
});
test("garbage never throws", () => {
expect(parseAuthStatus("not json").authorized).toBe(false);
expect(parseAuthStatus("").authorized).toBe(false);
});
});
describe("lark-cli AES-GCM store", () => {
test("safeFileName matches the CLI's colon-to-underscore sanitiser", () => {
expect(safeFileName(userTokenAccount("cli_a1", "ou_abc"))).toBe("cli_a1_ou_abc.enc");
expect(safeFileName("appsecret:cli_a1")).toBe("appsecret_cli_a1.enc");
});
test("encrypt then decrypt is identity", () => {
const key = Buffer.alloc(32, 7);
const round = decryptAesGcm(encryptAesGcm("hello-token", key), key);
expect(round).toBe("hello-token");
});
test("writeStoredUserToken round-trips through the on-disk layout", () => {
const dir = mkdtempSync(path.join(tmpdir(), "lark-cli-store-"));
writeFileSync(path.join(dir, "master.key"), Buffer.alloc(32, 9));
writeFileSync(path.join(dir, "master.key.file"), Buffer.alloc(32, 9));
const token: StoredUAToken = {
userOpenId: "ou_abc",
appId: "cli_a1",
accessToken: "u-access",
refreshToken: "ur-refresh",
expiresAt: 1_700_000_000_000,
refreshExpiresAt: 1_700_600_000_000,
scope: "offline_access",
grantedAt: 1_699_000_000_000,
};
writeStoredUserToken(dir, token);
expect(readStoredUserToken(dir, "cli_a1", "ou_abc")).toEqual(token);
});
/**
* The empty-store case is the one that broke production: a per-user slot with
* no master key and no app secret cannot even ask Feishu for a device code,
* and every authorisation attempt died at a 400 nobody logged.
*/
test("seedStoredAppSecret makes an empty store usable, and keeps its master key", () => {
const dir = path.join(mkdtempSync(path.join(tmpdir(), "lark-cli-seed-")), "lark-cli");
seedStoredAppSecret(dir, "cli_a1", "secret-one");
expect(readStoredAppSecret(dir, "cli_a1")).toBe("secret-one");
const key = readFileSync(path.join(dir, masterKeyFileName()));
seedStoredAppSecret(dir, "cli_a1", "secret-two");
expect(readStoredAppSecret(dir, "cli_a1")).toBe("secret-two");
expect(readFileSync(path.join(dir, masterKeyFileName()))).toEqual(key);
});
test("parseStoredUAToken rejects a truncated blob", () => {
expect(() => parseStoredUAToken("{}")).toThrow();
});
test("applyOAuthRefresh keeps the identity and slides both expiries", () => {
const now = 1_800_000_000_000;
const next = applyOAuthRefresh({
userOpenId: "ou_abc",
appId: "cli_a1",
accessToken: "old",
refreshToken: "old-r",
expiresAt: now - 1000,
refreshExpiresAt: now + 1000,
scope: "old-scope",
grantedAt: now - 10_000,
}, {
access_token: "new",
refresh_token: "new-r",
expires_in: 7200,
refresh_token_expires_in: 604800,
scope: "docs:document:readonly offline_access",
}, now);
expect(next.accessToken).toBe("new");
expect(next.refreshToken).toBe("new-r");
expect(next.expiresAt).toBe(now + 7200_000);
expect(next.refreshExpiresAt).toBe(now + 604800_000);
expect(next.userOpenId).toBe("ou_abc");
expect(next.grantedAt).toBe(now - 10_000);
});
});