52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { signAircallOAuthState, verifyAircallOAuthState } from "./aircallOAuthState.server";
|
|
|
|
const INPUT = {
|
|
tenantId: "tenant-1",
|
|
tenantSlug: "acme",
|
|
userId: "user-1",
|
|
connectionId: "connection-1",
|
|
} as const;
|
|
const SECRET = "aircall-oauth-state-test-secret";
|
|
const NOW = 1_725_000_000_000;
|
|
|
|
describe("Aircall OAuth state", () => {
|
|
it("round-trips the tenant, user, reserved slot and nonce", () => {
|
|
const state = signAircallOAuthState(INPUT, { secret: SECRET, now: () => NOW, nonce: () => "nonce-1" });
|
|
|
|
expect(verifyAircallOAuthState(state, { secret: SECRET, now: () => NOW + 60_000 })).toEqual({
|
|
ok: true,
|
|
payload: { ...INPUT, nonce: "nonce-1", createdAt: NOW },
|
|
});
|
|
});
|
|
|
|
it("rejects tampering before decoding trusted context", () => {
|
|
const state = signAircallOAuthState(INPUT, { secret: SECRET, now: () => NOW, nonce: () => "nonce-1" });
|
|
const [body, signature] = state.split(".");
|
|
|
|
expect(verifyAircallOAuthState(`${body}x.${signature}`, { secret: SECRET, now: () => NOW })).toEqual({
|
|
ok: false,
|
|
reason: "bad-signature",
|
|
});
|
|
});
|
|
|
|
it("expires after ten minutes and rejects implausible future tokens", () => {
|
|
const state = signAircallOAuthState(INPUT, { secret: SECRET, now: () => NOW, nonce: () => "nonce-1" });
|
|
|
|
expect(verifyAircallOAuthState(state, { secret: SECRET, now: () => NOW + 10 * 60_000 + 1 })).toEqual({
|
|
ok: false,
|
|
reason: "expired",
|
|
});
|
|
expect(verifyAircallOAuthState(state, { secret: SECRET, now: () => NOW - 30_001 })).toEqual({
|
|
ok: false,
|
|
reason: "expired",
|
|
});
|
|
});
|
|
|
|
it("rejects malformed and oversized values without throwing", () => {
|
|
expect(verifyAircallOAuthState("not-signed", { secret: SECRET, now: () => NOW })).toEqual({ ok: false, reason: "malformed" });
|
|
expect(verifyAircallOAuthState("x".repeat(4_097), { secret: SECRET, now: () => NOW })).toEqual({ ok: false, reason: "malformed" });
|
|
});
|
|
});
|