@jskit-ai/auth-core 0.2.18 → 0.2.20

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/CHANGELOG.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - A route's `permission` now matches the actor's grants as action permissions do: `*` grants every route, and a grant ending in `.*`, such as `crud.labels.*`, grants every permission under that prefix. It used to need the exact name, so a workspace owner holding `*` was refused a route requiring `crud.labels.update`.
5
6
  - The `AuthInvitationContextResolver` type documents what local auth relies on: the resolver throws for an invitation the registration cannot use, and the context it answers names, in `email`, the address the invitation was sent to and, in `expiresAt`, when it stops working, so a registration through it can skip email confirmation.
6
7
  - `AuthProfileProjector.syncIdentityProfile` documents `onlyWhenChanged`, which providers pass on an ordinary request so a profile that already matches is only read; sign-in and session refresh synchronize in full.
7
8
  - `normalizeAuthCapabilities` carries `features.password.selfRegister`: whether anyone may register, not only an invited person or the first account. A provider that doesn't say gets the value of `register`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-core",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -143,6 +143,6 @@
143
143
  }
144
144
  },
145
145
  "peerDependencies": {
146
- "@jskit-ai/kernel": "0.2.18"
146
+ "@jskit-ai/kernel": "0.2.20"
147
147
  }
148
148
  }
@@ -1,11 +1,13 @@
1
+ import { hasPermission } from "@jskit-ai/kernel/shared/support/permissions";
1
2
  import { parseBooleanFlag } from "./booleanFlag.js";
2
3
 
3
4
  function parseList(value) {
4
5
  return String(value || "").split(",").map((entry) => entry.trim()).filter(Boolean);
5
6
  }
6
7
 
8
+ // A route permission matches grants as action permissions do: "*", the exact name, or a grant ending in ".*".
7
9
  function defaultHasPermission({ permission, permissions }) {
8
- return !permission || (Array.isArray(permissions) && permissions.includes(permission));
10
+ return hasPermission(permissions, permission);
9
11
  }
10
12
 
11
13
  /** The authentication an application without an `auth.service` has: every request is signed out. */
@@ -0,0 +1,54 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import Fastify from "fastify";
4
+
5
+ import { createAuthExtensions } from "../src/server/authExtensions.js";
6
+ import { createAuthPolicy } from "../src/server/authPolicy.js";
7
+ import { authPolicyPlugin } from "../src/server/lib/index.js";
8
+
9
+ const ROLE_PERMISSIONS = Object.freeze({
10
+ owner: ["*"],
11
+ editor: ["crud.labels.*"],
12
+ viewer: ["crud.labels.list", "crud.labels.view"]
13
+ });
14
+
15
+ async function createApp(t) {
16
+ const extensions = createAuthExtensions();
17
+ extensions.registerPolicyContextResolver({
18
+ resolverId: "test.roles",
19
+ async resolveAuthPolicyContext({ request }) {
20
+ return { permissions: ROLE_PERMISSIONS[request.headers["x-role"]] ?? [] };
21
+ }
22
+ });
23
+ const policy = createAuthPolicy({
24
+ authService: {
25
+ async authenticateRequest() {
26
+ return { authenticated: true, actor: { id: 1 }, transientFailure: false };
27
+ }
28
+ },
29
+ extensions,
30
+ env: { NODE_ENV: "test" }
31
+ });
32
+
33
+ const app = Fastify();
34
+ t.after(() => app.close());
35
+ await authPolicyPlugin(policy, policy.options)(app);
36
+ app.get("/api/labels/1/edit", {
37
+ config: { authPolicy: "required", permission: "crud.labels.update" }
38
+ }, () => ({ allowed: true }));
39
+ return app;
40
+ }
41
+
42
+ test("a route permission is satisfied by the grant \"*\" and by a grant ending in \".*\", as action permissions are", async (t) => {
43
+ const app = await createApp(t);
44
+
45
+ for (const role of ["owner", "editor"]) {
46
+ const response = await app.inject({ method: "GET", url: "/api/labels/1/edit", headers: { "x-role": role } });
47
+ assert.equal(response.statusCode, 200, `${role} holds a grant covering crud.labels.update`);
48
+ assert.deepEqual(response.json(), { allowed: true });
49
+ }
50
+
51
+ const denied = await app.inject({ method: "GET", url: "/api/labels/1/edit", headers: { "x-role": "viewer" } });
52
+ assert.equal(denied.statusCode, 403);
53
+ assert.equal(denied.json().code, "auth.policy.permissionDenied");
54
+ });