openvibers/openvibe.coupons Commits
Staff powers from the contracts staff map (WS-D, ADR-022; Contracts 0.38.0): Coupons staff are staff.editorial.manage (admin, as before) or COUPONS_STAFF_SUBJECTS (the product's own, break-glass), not a role comparison. test/staff-map.test.js fails on raw role checks
Changes
@@@ -32,7 +32,8 @@ OV_OAUTH_CLIENT_SECRET=# same-day deduplication. Unset: a development key (and /api/ready says so).#COUPONS_REPORTER_KEY_SECRET=--# Staff (review, moderation): Network admins, plus these subjects (usr_…, comma-separated)++# Staff (review, moderation): staff with staff.editorial.manage (Network admins), plus these subjects++# (usr_…, comma-separated; a product role, and the break-glass way in)#COUPONS_STAFF_SUBJECTS=# Browser helper: extension origins allowed by CORS on the two lookup routes only.@@@ -15,7 +15,7 @@"express": "^4.21.2","express-rate-limit": "^7.5.0","helmet": "^8.1.0",-- "openvibe-contracts": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.33.0",++ "openvibe-contracts": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.38.0","openvibe-publishing": "https://codeload.github.com/OpenVibers/OpenVibe.Publishing/tar.gz/refs/tags/v0.4.0","openvibe-sdk": "https://codeload.github.com/OpenVibers/OpenVibe.SDK/tar.gz/refs/tags/v0.5.0","openvibe-shared": "https://codeload.github.com/OpenVibers/OpenVibe.Shared/tar.gz/refs/tags/v1.12.0"@@@ -1015,9 +1015,9 @@}},"node_modules/openvibe-contracts": {-- "version": "0.33.0",-- "resolved": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.33.0",-- "integrity": "sha512-7q8NUhcPxgb8lxrVdC3pFbiPdjzliJM1mMP/1G5a2PmOr2tJVAXwYa8+a8YgCJvYeMSeMHWijLkIEiZ9IhllOg==",++ "version": "0.38.0",++ "resolved": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.38.0",++ "integrity": "sha512-nHYCcKM9N4TJS+BlHOax9xwt4jU87qhXsLZ2gnuK4nOHpGlkLk2OI49PCQVvfHPe+V7bzBK7L4MKjFcTNv8ORA==","license": "AGPL-3.0-only","dependencies": {"ajv": "^8.17.1",@@@ -20,7 +20,7 @@"express": "^4.21.2","express-rate-limit": "^7.5.0","helmet": "^8.1.0",-- "openvibe-contracts": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.33.0",++ "openvibe-contracts": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.38.0","openvibe-publishing": "https://codeload.github.com/OpenVibers/OpenVibe.Publishing/tar.gz/refs/tags/v0.4.0","openvibe-sdk": "https://codeload.github.com/OpenVibers/OpenVibe.SDK/tar.gz/refs/tags/v0.5.0","openvibe-shared": "https://codeload.github.com/OpenVibers/OpenVibe.Shared/tar.gz/refs/tags/v1.12.0"@@@ -28,7 +28,7 @@ const { extractToken, claimsToUser, decodeJwtPayload } = require('./sso');const { checkCapability } = require('./capabilities');const { ApiError } = require('../http/errors');--const { ids, serviceAuth, http } = contracts;++const { ids, serviceAuth, http, staff: staffMap } = contracts;const PRINCIPAL_SUB = /^(svc|app|mod):/;const AUDIENCE = 'openvibe.coupons';@@@ -40,7 +40,8 @@ function createViewerResolver({ auth, config, installs }) {function userFromClaims(claims, token) {if (!claims || (typeof claims.sub === 'string' && PRINCIPAL_SUB.test(claims.sub))) return null;const subject = ids.isSubjectId('user', claims.subject_id) ? claims.subject_id : null;-- const staff = Boolean(subject) && (claims.role === 'admin' || staffSubjects.has(subject));++ // Staff = the contracts staff map's staff.editorial.manage (ADR-022), or a subject in COUPONS_STAFF_SUBJECTS.++ const staff = Boolean(subject) && (staffMap.can(claims, 'staff.editorial.manage') || staffSubjects.has(subject));return { kind: 'user', subject, staff, origin: 'user', user: claimsToUser(claims), token };}@@@ -48,7 +48,7 @@ function load(env = process.env) {// /api/ready says so. Changing it restarts same-day deduplication.reporterKeySecret: env.COUPONS_REPORTER_KEY_SECRET || '',-- // Staff (merchant approval, moderation): Network admins, plus these subjects (usr_…).++ // Staff (merchant approval, moderation): staff.editorial.manage (Network admins), plus these subjects (usr_…).staffSubjects: list(env.COUPONS_STAFF_SUBJECTS),// Browser-extension origins allowed by CORS on the two lookup routes ONLY (never onnnew file mode 100644@@@ -0,0 +1,26 @@++'use strict';++// Staff powers come from the openvibe-contracts staff map (ADR-022): staff.editorial.manage makes a person Coupons staff (COUPONS_STAFF_SUBJECTS names more).++// No server file compares a person's role by hand.++const assert = require('assert');++const fs = require('fs');++const path = require('path');++const { staff } = require('openvibe-contracts');++++const CAPS = ["staff.editorial.manage"];++for (const [claims, want] of [[{"role": "user"}, [false]], [{"role": "global_mod"}, [false]], [{"role": "admin"}, [true]], [{"role": "admin", "is_owner": true}, [true]]]) {++ CAPS.forEach((cap, i) => assert.strictEqual(staff.can(claims, cap), want[i], `${JSON.stringify(claims)} ${cap}`));++}++++const offenders = [];++(function walk(dir) {++ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {++ const f = path.join(dir, e.name);++ if (e.isDirectory()) { if (e.name !== 'node_modules') walk(f); continue; }++ if (!/\.(js|ts)$/.test(e.name)) continue;++ fs.readFileSync(f, 'utf8').split('\n').forEach((line, i) => {++ if (/STAFF_ROLES|claims\.role\s*[!=]==?|\.(has|includes)\(\s*(claims|u|user|p)\.role\b|\brole\s*[!=]==?\s*['"](admin|global_mod|moderator)['"]/.test(line)) offenders.push(`${path.relative(path.join(__dirname, '..'), f)}:${i + 1}`);++ });++ }++})(path.join(__dirname, '..', "server"));++assert.deepStrictEqual(offenders, [], 'raw role checks; ask staff.can(claims, \'staff.…\')');++console.log('staff map: all checks passed');
@@ -32,7 +32,8 @@ OV_OAUTH_CLIENT_SECRET=
@@ -32,7 +32,8 @@ OV_OAUTH_CLIENT_SECRET=
# same-day deduplication. Unset: a development key (and /api/ready says so).
# same-day deduplication. Unset: a development key (and /api/ready says so).
#COUPONS_REPORTER_KEY_SECRET=
#COUPONS_REPORTER_KEY_SECRET=
-# Staff (review, moderation): Network admins, plus these subjects (usr_…, comma-separated)
+# Staff (review, moderation): staff with staff.editorial.manage (Network admins), plus these subjects
+# (usr_…, comma-separated; a product role, and the break-glass way in)
#COUPONS_STAFF_SUBJECTS=
#COUPONS_STAFF_SUBJECTS=
# Browser helper: extension origins allowed by CORS on the two lookup routes only.
# Browser helper: extension origins allowed by CORS on the two lookup routes only.
@@ -15,7 +15,7 @@
@@ -15,7 +15,7 @@
"express": "^4.21.2",
"express": "^4.21.2",
"express-rate-limit": "^7.5.0",
"express-rate-limit": "^7.5.0",
"helmet": "^8.1.0",
"helmet": "^8.1.0",
- "openvibe-contracts": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.33.0",
+ "openvibe-contracts": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.38.0",
"openvibe-publishing": "https://codeload.github.com/OpenVibers/OpenVibe.Publishing/tar.gz/refs/tags/v0.4.0",
"openvibe-publishing": "https://codeload.github.com/OpenVibers/OpenVibe.Publishing/tar.gz/refs/tags/v0.4.0",
"openvibe-sdk": "https://codeload.github.com/OpenVibers/OpenVibe.SDK/tar.gz/refs/tags/v0.5.0",
"openvibe-sdk": "https://codeload.github.com/OpenVibers/OpenVibe.SDK/tar.gz/refs/tags/v0.5.0",
"openvibe-shared": "https://codeload.github.com/OpenVibers/OpenVibe.Shared/tar.gz/refs/tags/v1.12.0"
"openvibe-shared": "https://codeload.github.com/OpenVibers/OpenVibe.Shared/tar.gz/refs/tags/v1.12.0"
@@ -1015,9 +1015,9 @@
@@ -1015,9 +1015,9 @@
}
}
},
},
"node_modules/openvibe-contracts": {
"node_modules/openvibe-contracts": {
- "version": "0.33.0",
- "resolved": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.33.0",
- "integrity": "sha512-7q8NUhcPxgb8lxrVdC3pFbiPdjzliJM1mMP/1G5a2PmOr2tJVAXwYa8+a8YgCJvYeMSeMHWijLkIEiZ9IhllOg==",
+ "version": "0.38.0",
+ "resolved": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.38.0",
+ "integrity": "sha512-nHYCcKM9N4TJS+BlHOax9xwt4jU87qhXsLZ2gnuK4nOHpGlkLk2OI49PCQVvfHPe+V7bzBK7L4MKjFcTNv8ORA==",
"license": "AGPL-3.0-only",
"license": "AGPL-3.0-only",
"dependencies": {
"dependencies": {
"ajv": "^8.17.1",
"ajv": "^8.17.1",
@@ -20,7 +20,7 @@
@@ -20,7 +20,7 @@
"express": "^4.21.2",
"express": "^4.21.2",
"express-rate-limit": "^7.5.0",
"express-rate-limit": "^7.5.0",
"helmet": "^8.1.0",
"helmet": "^8.1.0",
- "openvibe-contracts": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.33.0",
+ "openvibe-contracts": "https://codeload.github.com/OpenVibers/OpenVibe.Contracts/tar.gz/refs/tags/v0.38.0",
"openvibe-publishing": "https://codeload.github.com/OpenVibers/OpenVibe.Publishing/tar.gz/refs/tags/v0.4.0",
"openvibe-publishing": "https://codeload.github.com/OpenVibers/OpenVibe.Publishing/tar.gz/refs/tags/v0.4.0",
"openvibe-sdk": "https://codeload.github.com/OpenVibers/OpenVibe.SDK/tar.gz/refs/tags/v0.5.0",
"openvibe-sdk": "https://codeload.github.com/OpenVibers/OpenVibe.SDK/tar.gz/refs/tags/v0.5.0",
"openvibe-shared": "https://codeload.github.com/OpenVibers/OpenVibe.Shared/tar.gz/refs/tags/v1.12.0"
"openvibe-shared": "https://codeload.github.com/OpenVibers/OpenVibe.Shared/tar.gz/refs/tags/v1.12.0"
@@ -28,7 +28,7 @@ const { extractToken, claimsToUser, decodeJwtPayload } = require('./sso');
@@ -28,7 +28,7 @@ const { extractToken, claimsToUser, decodeJwtPayload } = require('./sso');
const { checkCapability } = require('./capabilities');
const { checkCapability } = require('./capabilities');
const { ApiError } = require('../http/errors');
const { ApiError } = require('../http/errors');
-const { ids, serviceAuth, http } = contracts;
+const { ids, serviceAuth, http, staff: staffMap } = contracts;
const PRINCIPAL_SUB = /^(svc|app|mod):/;
const PRINCIPAL_SUB = /^(svc|app|mod):/;
const AUDIENCE = 'openvibe.coupons';
const AUDIENCE = 'openvibe.coupons';
@@ -40,7 +40,8 @@ function createViewerResolver({ auth, config, installs }) {
@@ -40,7 +40,8 @@ function createViewerResolver({ auth, config, installs }) {
function userFromClaims(claims, token) {
function userFromClaims(claims, token) {
if (!claims || (typeof claims.sub === 'string' && PRINCIPAL_SUB.test(claims.sub))) return null;
if (!claims || (typeof claims.sub === 'string' && PRINCIPAL_SUB.test(claims.sub))) return null;
const subject = ids.isSubjectId('user', claims.subject_id) ? claims.subject_id : null;
const subject = ids.isSubjectId('user', claims.subject_id) ? claims.subject_id : null;
- const staff = Boolean(subject) && (claims.role === 'admin' || staffSubjects.has(subject));
+ // Staff = the contracts staff map's staff.editorial.manage (ADR-022), or a subject in COUPONS_STAFF_SUBJECTS.
+ const staff = Boolean(subject) && (staffMap.can(claims, 'staff.editorial.manage') || staffSubjects.has(subject));
return { kind: 'user', subject, staff, origin: 'user', user: claimsToUser(claims), token };
return { kind: 'user', subject, staff, origin: 'user', user: claimsToUser(claims), token };
}
}
@@ -48,7 +48,7 @@ function load(env = process.env) {
@@ -48,7 +48,7 @@ function load(env = process.env) {
// /api/ready says so. Changing it restarts same-day deduplication.
// /api/ready says so. Changing it restarts same-day deduplication.
reporterKeySecret: env.COUPONS_REPORTER_KEY_SECRET || '',
reporterKeySecret: env.COUPONS_REPORTER_KEY_SECRET || '',
- // Staff (merchant approval, moderation): Network admins, plus these subjects (usr_…).
+ // Staff (merchant approval, moderation): staff.editorial.manage (Network admins), plus these subjects (usr_…).
staffSubjects: list(env.COUPONS_STAFF_SUBJECTS),
staffSubjects: list(env.COUPONS_STAFF_SUBJECTS),
// Browser-extension origins allowed by CORS on the two lookup routes ONLY (never on
// Browser-extension origins allowed by CORS on the two lookup routes ONLY (never on
new file mode 100644
new file mode 100644
@@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
+'use strict';
+// Staff powers come from the openvibe-contracts staff map (ADR-022): staff.editorial.manage makes a person Coupons staff (COUPONS_STAFF_SUBJECTS names more).
+// No server file compares a person's role by hand.
+const assert = require('assert');
+const fs = require('fs');
+const path = require('path');
+const { staff } = require('openvibe-contracts');
+
+const CAPS = ["staff.editorial.manage"];
+for (const [claims, want] of [[{"role": "user"}, [false]], [{"role": "global_mod"}, [false]], [{"role": "admin"}, [true]], [{"role": "admin", "is_owner": true}, [true]]]) {
+ CAPS.forEach((cap, i) => assert.strictEqual(staff.can(claims, cap), want[i], `${JSON.stringify(claims)} ${cap}`));
+}
+
+const offenders = [];
+(function walk(dir) {
+ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
+ const f = path.join(dir, e.name);
+ if (e.isDirectory()) { if (e.name !== 'node_modules') walk(f); continue; }
+ if (!/\.(js|ts)$/.test(e.name)) continue;
+ fs.readFileSync(f, 'utf8').split('\n').forEach((line, i) => {
+ if (/STAFF_ROLES|claims\.role\s*[!=]==?|\.(has|includes)\(\s*(claims|u|user|p)\.role\b|\brole\s*[!=]==?\s*['"](admin|global_mod|moderator)['"]/.test(line)) offenders.push(`${path.relative(path.join(__dirname, '..'), f)}:${i + 1}`);
+ });
+ }
+})(path.join(__dirname, '..', "server"));
+assert.deepStrictEqual(offenders, [], 'raw role checks; ask staff.can(claims, \'staff.…\')');
+console.log('staff map: all checks passed');