@jskit-ai/auth-core 0.1.204 → 0.1.206
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/test/plugin.test.js +44 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/auth-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.206",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test"
|
|
@@ -58,9 +58,9 @@
|
|
|
58
58
|
"./shared/commands/authSessionReadCommand": "./src/shared/commands/authSessionReadCommand.js"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@fastify/cookie": "^11.
|
|
62
|
-
"@fastify/csrf-protection": "^
|
|
63
|
-
"@fastify/rate-limit": "^
|
|
61
|
+
"@fastify/cookie": "^11.1.2",
|
|
62
|
+
"@fastify/csrf-protection": "^8.0.1",
|
|
63
|
+
"@fastify/rate-limit": "^11.2.0",
|
|
64
64
|
"json-rest-schema": "^1.0.17"
|
|
65
65
|
},
|
|
66
66
|
"jskit": {
|
|
@@ -135,6 +135,6 @@
|
|
|
135
135
|
}
|
|
136
136
|
},
|
|
137
137
|
"peerDependencies": {
|
|
138
|
-
"@jskit-ai/kernel": "0.1.
|
|
138
|
+
"@jskit-ai/kernel": "0.1.208"
|
|
139
139
|
}
|
|
140
140
|
}
|
package/test/plugin.test.js
CHANGED
|
@@ -1,9 +1,53 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
+
import Fastify from "fastify";
|
|
3
4
|
import { createFakeFastifyPolicyRuntime } from "../../../tooling/testUtils/fakeFastify.mjs";
|
|
4
5
|
|
|
5
6
|
import { authPolicyPlugin } from "../src/server/lib/index.js";
|
|
6
7
|
|
|
8
|
+
test("auth policy protects real Fastify requests with CSRF and route rate limits", async (t) => {
|
|
9
|
+
const app = Fastify();
|
|
10
|
+
t.after(() => app.close());
|
|
11
|
+
await authPolicyPlugin({
|
|
12
|
+
async resolveActor() {
|
|
13
|
+
return { authenticated: false, actor: null, transientFailure: false };
|
|
14
|
+
},
|
|
15
|
+
hasPermission() {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}, { nodeEnv: "test" })(app);
|
|
19
|
+
|
|
20
|
+
app.get("/api/csrf", { config: { authPolicy: "public" } }, (_request, reply) => ({
|
|
21
|
+
token: reply.generateCsrf()
|
|
22
|
+
}));
|
|
23
|
+
app.post("/api/write", { config: { authPolicy: "public" } }, () => ({ accepted: true }));
|
|
24
|
+
app.get("/api/limited", {
|
|
25
|
+
config: { authPolicy: "public", rateLimit: { max: 1, timeWindow: "1 minute" } }
|
|
26
|
+
}, () => ({ accepted: true }));
|
|
27
|
+
|
|
28
|
+
const missingToken = await app.inject({ method: "POST", url: "/api/write" });
|
|
29
|
+
assert.equal(missingToken.statusCode, 403);
|
|
30
|
+
|
|
31
|
+
const csrf = await app.inject({ method: "GET", url: "/api/csrf" });
|
|
32
|
+
assert.equal(csrf.statusCode, 200);
|
|
33
|
+
const cookies = Object.fromEntries(csrf.cookies.map(({ name, value }) => [name, value]));
|
|
34
|
+
const accepted = await app.inject({
|
|
35
|
+
method: "POST",
|
|
36
|
+
url: "/api/write",
|
|
37
|
+
cookies,
|
|
38
|
+
headers: { "x-csrf-token": csrf.json().token }
|
|
39
|
+
});
|
|
40
|
+
assert.equal(accepted.statusCode, 200);
|
|
41
|
+
assert.deepEqual(accepted.json(), { accepted: true });
|
|
42
|
+
|
|
43
|
+
const invalidToken = await app.inject({
|
|
44
|
+
method: "POST", url: "/api/write", cookies, headers: { "x-csrf-token": "invalid" }
|
|
45
|
+
});
|
|
46
|
+
assert.equal(invalidToken.statusCode, 403);
|
|
47
|
+
assert.equal((await app.inject({ method: "GET", url: "/api/limited" })).statusCode, 200);
|
|
48
|
+
assert.equal((await app.inject({ method: "GET", url: "/api/limited" })).statusCode, 429);
|
|
49
|
+
});
|
|
50
|
+
|
|
7
51
|
test("requires resolveActor and hasPermission dependencies", () => {
|
|
8
52
|
assert.throws(() => authPolicyPlugin(), /resolveActor is required/);
|
|
9
53
|
assert.throws(() => authPolicyPlugin({ resolveActor() {} }), /hasPermission is required/);
|