-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthforge.test.mjs
More file actions
162 lines (147 loc) · 5.16 KB
/
authforge.test.mjs
File metadata and controls
162 lines (147 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import assert from "node:assert/strict";
import test from "node:test";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { AuthForgeClient, verifyPayloadSignatureEd25519 } from "./authforge.mjs";
const here = path.dirname(fileURLToPath(import.meta.url));
async function readVectors() {
const raw = await readFile(path.join(here, "test_vectors.json"), "utf8");
return JSON.parse(raw);
}
test("ed25519 vectors verify expected signatures", async () => {
const vectors = await readVectors();
for (const vectorCase of vectors.cases) {
const valid = verifyPayloadSignatureEd25519(
vectorCase.payload,
vectorCase.signature,
vectors.publicKey,
);
assert.equal(valid, vectorCase.shouldVerify);
}
});
test("client constructor requires public key", () => {
assert.throws(() => {
// @ts-expect-error constructor hard break
new AuthForgeClient("app-id", "app-secret", "LOCAL");
});
});
test("local heartbeat verifies stored signature with public key", async () => {
const vectors = await readVectors();
const validateCase = vectors.cases.find((item) => item.id === "validate_success");
assert.ok(validateCase);
const client = new AuthForgeClient(
"app-id",
"app-secret",
vectors.publicKey,
"LOCAL",
900,
undefined,
() => {},
);
client._rawPayloadB64 = validateCase.payload;
client._signature = validateCase.signature;
client._sessionExpiresIn = Math.floor(Date.now() / 1000) + 60;
client._localHeartbeat();
});
test("validateLicense verifies response without heartbeat or session mutation", async () => {
const vectors = await readVectors();
const validateCase = vectors.cases.find((item) => item.id === "validate_success");
assert.ok(validateCase);
const client = new AuthForgeClient(
"app-id",
"app-secret",
vectors.publicKey,
"LOCAL",
900,
undefined,
() => {},
);
client._generateNonce = () => "nonce-validate-001";
client._postJson = async (path, body, opts) => {
assert.equal(path, "/auth/validate");
assert.equal(opts?.skipFailureHook, true);
assert.equal(body.nonce, "nonce-validate-001");
return {
status: "ok",
payload: validateCase.payload,
signature: validateCase.signature,
keyId: "signing-key-1",
};
};
const result = await client.validateLicense("license-key");
assert.equal(result.valid, true);
assert.equal(client._heartbeatStarted, false);
assert.equal(client._heartbeatTimer, null);
assert.equal(client.isAuthenticated(), false);
assert.equal(result.sessionToken, "session.validate.token");
assert.deepEqual(result.appVariables, { tier: "pro" });
});
test("verifyPayloadSignatureEd25519 accepts an array of trusted keys", async () => {
const vectors = await readVectors();
const validateCase = vectors.cases.find((item) => item.id === "validate_success");
assert.ok(validateCase);
// Bogus key first, real key second — verification must succeed by trying
// each entry instead of bailing on the first miss.
const decoyKey = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
const valid = verifyPayloadSignatureEd25519(
validateCase.payload,
validateCase.signature,
[decoyKey, vectors.publicKey],
);
assert.equal(valid, true);
});
test("verifyPayloadSignatureEd25519 also accepts comma-separated env-var form", async () => {
const vectors = await readVectors();
const validateCase = vectors.cases.find((item) => item.id === "validate_success");
const decoyKey = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
const combined = `${decoyKey},${vectors.publicKey}`;
const valid = verifyPayloadSignatureEd25519(
validateCase.payload,
validateCase.signature,
combined,
);
assert.equal(valid, true);
});
test("client constructor accepts an array of public keys (rotation set)", async () => {
const vectors = await readVectors();
const validateCase = vectors.cases.find((item) => item.id === "validate_success");
const decoyKey = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
const client = new AuthForgeClient(
"app-id",
"app-secret",
[decoyKey, vectors.publicKey],
"LOCAL",
900,
undefined,
() => {},
);
assert.deepEqual(client.publicKeys, [decoyKey, vectors.publicKey]);
assert.equal(client.publicKey, decoyKey);
// Local heartbeat verification must still succeed because the *second* key
// in the trust list matches the signature.
client._rawPayloadB64 = validateCase.payload;
client._signature = validateCase.signature;
client._sessionExpiresIn = Math.floor(Date.now() / 1000) + 60;
client._localHeartbeat();
});
test("validateLicense returns structured failure without starting heartbeat", async () => {
const vectors = await readVectors();
const client = new AuthForgeClient(
"app-id",
"app-secret",
vectors.publicKey,
"LOCAL",
900,
undefined,
() => {},
);
client._postJson = async () => ({
status: "invalid_key",
error: "invalid_key",
});
const result = await client.validateLicense("bad-key");
assert.equal(result.valid, false);
assert.equal(result.code, "invalid_key");
assert.equal(client._heartbeatStarted, false);
});