@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 +1 -1
- package/src/auth.js +1 -0
- package/src/authorization/index.js +1 -1
- package/src/authorization/normalize.js +6 -20
- package/src/authorization/resolve.js +46 -46
package/package.json
CHANGED
package/src/auth.js
CHANGED
|
@@ -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}
|
|
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
|
|
21
|
-
if (!Array.isArray(
|
|
8
|
+
export function normalizeHoldingRoles(holdingRoles) {
|
|
9
|
+
if (!Array.isArray(holdingRoles)) {
|
|
22
10
|
return []
|
|
23
11
|
}
|
|
24
12
|
|
|
25
|
-
return
|
|
26
|
-
(
|
|
27
|
-
|
|
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 {
|
|
7
|
+
import { normalizeHoldingRoles } from './normalize.js'
|
|
8
8
|
import { resolvedRoleDefinitions } from './roles-loader.js'
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
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
|
|
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
|
|
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]) => ({
|
|
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
|
|
32
|
-
* Translates external roles into LIS roles and groups them into
|
|
33
|
-
*
|
|
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
|
|
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
|
-
|
|
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
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
}
|