@defra/lis-hubs-infra-access 0.7.0 → 0.8.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-access",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Shared hub access and capability resolution for the livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
package/src/auth.js CHANGED
@@ -41,6 +41,7 @@ export { createHoldingService } from './holding-service.js'
41
41
  export {
42
42
  AUTHORIZATION_VERSION,
43
43
  demandPermission,
44
+ GLOBAL_CPH_SCOPE,
44
45
  hasPermission,
45
46
  hydrateAuthorization,
46
47
  PERMISSIONS,
@@ -1,4 +1,4 @@
1
- export { AUTHORIZATION_VERSION } from './constants.js'
1
+ export { AUTHORIZATION_VERSION, GLOBAL_CPH_SCOPE } from './constants.js'
2
2
  export { resolveAuthorization } from './resolve.js'
3
3
  export { hydrateAuthorization } from './hydrate.js'
4
4
  export { hasPermission } from './checks.js'
@@ -2,31 +2,17 @@ import { GLOBAL_CPH_SCOPE } from './constants.js'
2
2
  import { resolvedRoleDefinitions } from './roles-loader.js'
3
3
 
4
4
  /**
5
- * @param {unknown} sourceRoles
6
- * @returns {string[]}
7
- */
8
- export function normalizeSourceRoles(sourceRoles) {
9
- if (!Array.isArray(sourceRoles)) {
10
- return []
11
- }
12
-
13
- return [...new Set(sourceRoles.filter((role) => typeof role === 'string'))]
14
- }
15
-
16
- /**
17
- * @param {unknown} assignments
5
+ * @param {unknown} holdingRoles
18
6
  * @returns {Array<{role: string, cph: string}>}
19
7
  */
20
- export function normalizeRoleAssignments(assignments) {
21
- if (!Array.isArray(assignments)) {
8
+ export function normalizeHoldingRoles(holdingRoles) {
9
+ if (!Array.isArray(holdingRoles)) {
22
10
  return []
23
11
  }
24
12
 
25
- return assignments.filter(
26
- (assignment) =>
27
- assignment &&
28
- typeof assignment.role === 'string' &&
29
- typeof assignment.cph === 'string'
13
+ return holdingRoles.filter(
14
+ (grant) =>
15
+ grant && typeof grant.role === 'string' && typeof grant.cph === 'string'
30
16
  )
31
17
  }
32
18
 
@@ -4,19 +4,41 @@ import {
4
4
  DEFAULT_ROLE,
5
5
  GLOBAL_CPH_SCOPE
6
6
  } from './constants.js'
7
- import { normalizeRoleAssignments, normalizeSourceRoles } from './normalize.js'
7
+ import { normalizeHoldingRoles } from './normalize.js'
8
8
  import { resolvedRoleDefinitions } from './roles-loader.js'
9
9
 
10
10
  /**
11
- * Groups translated {role, cph} pairs into one statement per role, with
12
- * that role's cphs collected into a single list.
13
- * @param {Array<{role: string, cph: string}>} assignments
14
- * @returns {Array<{role: string, cphs: string[]}>}
11
+ * Translates a single source-provider role into its internal LIS role -
12
+ * either it's already a valid LIS role name (some identity adapters pass
13
+ * these through directly), or it's a source-specific name mapped via
14
+ * role-mappings.json.
15
+ * @param {string} sourceRole
16
+ * @param {Record<string, string>} mappings
17
+ * @returns {string|null}
15
18
  */
16
- function groupAssignmentsIntoStatements(assignments) {
19
+ function translateRole(sourceRole, mappings) {
20
+ if (resolvedRoleDefinitions.has(sourceRole)) {
21
+ return sourceRole
22
+ }
23
+
24
+ const mappedRole = mappings[sourceRole.toLowerCase()]
25
+
26
+ return mappedRole && resolvedRoleDefinitions.has(mappedRole)
27
+ ? mappedRole
28
+ : null
29
+ }
30
+
31
+ /**
32
+ * Groups translated {role, cph} grants into one statement per role. A role
33
+ * granted globally (cph '*') anywhere subsumes any CPH-specific grants for
34
+ * that same role, since the global grant already covers them.
35
+ * @param {Array<{role: string, cph: string}>} grants
36
+ * @returns {Array<{role: string, cphs: '*'|string[]}>}
37
+ */
38
+ function groupHoldingRolesIntoStatements(grants) {
17
39
  const cphsByRole = new Map()
18
40
 
19
- for (const { role, cph } of assignments) {
41
+ for (const { role, cph } of grants) {
20
42
  if (!cphsByRole.has(role)) {
21
43
  cphsByRole.set(role, new Set())
22
44
  }
@@ -24,62 +46,40 @@ function groupAssignmentsIntoStatements(assignments) {
24
46
  cphsByRole.get(role).add(cph)
25
47
  }
26
48
 
27
- return [...cphsByRole].map(([role, cphs]) => ({ role, cphs: [...cphs] }))
49
+ return [...cphsByRole].map(([role, cphs]) => ({
50
+ role,
51
+ cphs: cphs.has(GLOBAL_CPH_SCOPE) ? GLOBAL_CPH_SCOPE : [...cphs]
52
+ }))
28
53
  }
29
54
 
30
55
  /**
31
- * Resolves authorization from source identity provider roles and role assignments.
32
- * Translates external roles into LIS roles and groups them into statements -
33
- * one per role, scoped to the CPHs it applies to (or '*' for everywhere).
56
+ * Resolves authorization from a source identity provider's role grants.
57
+ * Translates external roles into LIS roles and groups them into one
58
+ * statement per role, scoped to the CPHs it applies to (or '*' for
59
+ * everywhere).
34
60
  * @param {object} params - Authorization resolution parameters.
35
61
  * @param {string} params.source - The identity provider source (e.g., 'defra-ci', 'entra-id').
36
- * @param {string[]} [params.sourceRoles=[]] - Roles from the identity provider, granted everywhere.
37
- * @param {Array<{role: string, cph: string}>} [params.roleAssignments=[]] - CPH-scoped role assignments.
62
+ * @param {Array<{role: string, cph: string}>} [params.holdingRoles=[]] - Role grants, each scoped to a CPH or '*' for everywhere.
38
63
  * @param {Array} [params.holdings=[]] - Holdings associated with the user.
39
64
  * @returns {{authzVersion: number, statements: Array<{role: string, cphs: '*'|string[]}>, holdings: Array}} Resolved authorization object.
40
65
  */
41
66
  export function resolveAuthorization({
42
67
  source,
43
- sourceRoles = [],
44
- roleAssignments = [],
68
+ holdingRoles = [],
45
69
  holdings = []
46
70
  }) {
47
71
  const mappings = roleMappings[source] ?? {}
48
- const globalRoles = new Set([DEFAULT_ROLE])
49
-
50
- for (const sourceRole of normalizeSourceRoles(sourceRoles)) {
51
- if (resolvedRoleDefinitions.has(sourceRole)) {
52
- globalRoles.add(sourceRole)
53
- }
54
-
55
- const mappedRole = mappings[sourceRole.toLowerCase()]
56
-
57
- if (mappedRole && resolvedRoleDefinitions.has(mappedRole)) {
58
- globalRoles.add(mappedRole)
59
- }
60
- }
61
-
62
- const globalStatements = [...globalRoles].map((role) => ({
63
- role,
64
- cphs: GLOBAL_CPH_SCOPE
65
- }))
66
72
 
67
- const translatedAssignments = normalizeRoleAssignments(roleAssignments)
68
- .map((assignment) => ({
69
- role: mappings[assignment.role.toLowerCase()],
70
- cph: assignment.cph
71
- }))
72
- .filter(
73
- (assignment) =>
74
- assignment.role && resolvedRoleDefinitions.has(assignment.role)
75
- )
73
+ const grants = [
74
+ { role: DEFAULT_ROLE, cph: GLOBAL_CPH_SCOPE },
75
+ ...normalizeHoldingRoles(holdingRoles)
76
+ ]
77
+ .map(({ role, cph }) => ({ role: translateRole(role, mappings), cph }))
78
+ .filter((grant) => grant.role !== null)
76
79
 
77
80
  return {
78
81
  authzVersion: AUTHORIZATION_VERSION,
79
- statements: [
80
- ...globalStatements,
81
- ...groupAssignmentsIntoStatements(translatedAssignments)
82
- ],
82
+ statements: groupHoldingRolesIntoStatements(grants),
83
83
  holdings: Array.isArray(holdings) ? holdings : []
84
84
  }
85
85
  }