@blossm/permissions-lookup 0.0.179 → 0.0.180

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.
Files changed (3) hide show
  1. package/index.js +7 -1
  2. package/package.json +12 -11
  3. package/test/index.js +64 -0
package/index.js CHANGED
@@ -23,7 +23,13 @@ export default ({ downloadFileFn }) => async ({
23
23
  }) => {
24
24
  if (!principal) throw forbidden.message("Missing required permissions.");
25
25
 
26
- const cacheKey = `${cacheKeyPrefix}.${principal.root}.${principal.service}.${principal.network}`;
26
+ const cacheKey = `${cacheKeyPrefix}.${JSON.stringify([
27
+ process.env.NETWORK,
28
+ principal.root,
29
+ principal.service,
30
+ principal.network,
31
+ context ? [context.root, context.service, context.network] : null,
32
+ ])}`;
27
33
 
28
34
  //Lookup permissions in cache. If found, use them.
29
35
  let { permissions: cachedPermissions } =
package/package.json CHANGED
@@ -1,29 +1,30 @@
1
1
  {
2
2
  "name": "@blossm/permissions-lookup",
3
- "version": "0.0.179",
3
+ "version": "0.0.180",
4
4
  "description": "Looks up permissions for a role.",
5
5
  "main": "index.js",
6
6
  "type": "module",
7
7
  "author": "blossm.network",
8
8
  "engines": {
9
- "node": ">= 20.19.0"
9
+ "node": ">= 24.22.1"
10
10
  },
11
11
  "publishConfig": {
12
12
  "access": "public"
13
13
  },
14
14
  "scripts": {
15
- "test": "echo \"Error: no test specified\" && exit 0"
15
+ "test": "mocha --recursive"
16
16
  },
17
17
  "dependencies": {
18
- "@blossm/errors": "^0.0.40",
19
- "@blossm/fact-rpc": "^0.0.210",
20
- "@blossm/redis": "^0.0.16",
21
- "@blossm/role-permissions": "^0.0.30",
22
- "@blossm/uuid": "^0.0.9",
23
- "yaml": "^2.8.0"
18
+ "@blossm/errors": "^0.0.41",
19
+ "@blossm/fact-rpc": "^0.0.211",
20
+ "@blossm/node-external-token": "^0.0.205",
21
+ "@blossm/redis": "^0.0.17",
22
+ "@blossm/role-permissions": "^0.0.31",
23
+ "@blossm/uuid": "^0.0.10",
24
+ "yaml": "^2.9.1"
24
25
  },
25
26
  "devDependencies": {
26
- "mocha": "^11.7.0"
27
+ "mocha": "^12.0.3"
27
28
  },
28
- "gitHead": "f58d526fd2a4be52946a0b8474d47b64f2b3b0fd"
29
+ "gitHead": "18a2e086ec8fe64a8cf7b0d83262eb968f321d93"
29
30
  }
package/test/index.js ADDED
@@ -0,0 +1,64 @@
1
+ import { expect } from "chai";
2
+ import { replace, restore } from "sinon";
3
+ import redis from "@blossm/redis";
4
+ import permissionsLookup from "../index.js";
5
+ import factDeps from "../../../../rpc/fact/deps.js";
6
+
7
+ const principal = { root: "user", service: "core", network: "example.test" };
8
+ const contextA = { root: "A", service: "core", network: "example.test" };
9
+ const contextB = { ...contextA, root: "B" };
10
+ const permission = { service: "core", domain: "document", privilege: "read" };
11
+
12
+ describe("Contextual permission cache", () => {
13
+ let network;
14
+ let lookup;
15
+ beforeEach(() => {
16
+ network = process.env.NETWORK;
17
+ process.env.NETWORK = "example.test";
18
+ const cache = new Map();
19
+ replace(redis, "readObject", async (key) => cache.get(key));
20
+ replace(redis, "writeObject", async (key, value) => cache.set(key, value));
21
+ replace(redis, "setExpiry", async () => {});
22
+ replace(factDeps, "rpc", ({ operationNameComponents: [name] }) => ({
23
+ get: () => ({
24
+ in: () => ({
25
+ with: async () => ({
26
+ body:
27
+ name === "roles"
28
+ ? [{ id: "reader", subject: contextA }]
29
+ : [permission],
30
+ }),
31
+ }),
32
+ }),
33
+ }));
34
+ lookup = permissionsLookup({ downloadFileFn: async () => {} });
35
+ });
36
+ afterEach(() => {
37
+ restore();
38
+ if (network === undefined) delete process.env.NETWORK;
39
+ else process.env.NETWORK = network;
40
+ });
41
+ it("does not grant A's permissions in B", async () => {
42
+ expect(await lookup({ principal, context: contextA })).to.deep.equal([
43
+ permission,
44
+ ]);
45
+ expect(await lookup({ principal, context: contextB })).to.deep.equal([]);
46
+ });
47
+ it("does not reuse B's missing permissions in A", async () => {
48
+ expect(await lookup({ principal, context: contextB })).to.deep.equal([]);
49
+ expect(await lookup({ principal, context: contextA })).to.deep.equal([
50
+ permission,
51
+ ]);
52
+ });
53
+ it("distinguishes unscoped and scoped permissions", async () => {
54
+ expect(await lookup({ principal })).to.deep.equal([permission]);
55
+ expect(await lookup({ principal, context: contextB })).to.deep.equal([]);
56
+ });
57
+ it("distinguishes the network whose roles are being resolved", async () => {
58
+ expect(await lookup({ principal, context: contextA })).to.deep.equal([
59
+ permission,
60
+ ]);
61
+ process.env.NETWORK = "another.test";
62
+ expect(await lookup({ principal, context: contextA })).to.deep.equal([]);
63
+ });
64
+ });