@defra/lis-hubs-infra-access 0.13.0 → 0.14.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.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Shared hub access and capability resolution for the livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -1,4 +1,5 @@
1
1
  /** @import { Permission } from '../constants/permissions.js' */
2
+ /** @import { HydratedAuthorization } from './types.js' */
2
3
  import { GLOBAL_CPH_SCOPE } from './constants.js'
3
4
 
4
5
  /**
@@ -19,7 +20,7 @@ function matchesCphScope(cphs, cph) {
19
20
  * permission, optionally scoped to a CPH. Expects statements to already
20
21
  * carry a `permissions` array, as attached by hydrateAuthorization() at
21
22
  * the session/JWT boundary - callers should not re-hydrate per check.
22
- * @param {object} authorization - A hydrated authorization object.
23
+ * @param {HydratedAuthorization} authorization - A hydrated authorization object.
23
24
  * @param {object} [params={}] - Permission check parameters.
24
25
  * @param {Permission} [params.permission] - The LIS permission to check for.
25
26
  * @param {string} [params.cph] - The CPH scope for the permission check (if applicable).
@@ -1,14 +1,18 @@
1
+ /** @import { Authorization, HydratedAuthorization } from './types.js' */
1
2
  import { AUTHORIZATION_VERSION } from './constants.js'
2
3
  import { normalizeStatements } from './normalize.js'
3
4
  import { resolvedRoleDefinitions } from './roles-loader.js'
5
+ import { logger } from '@defra/lis-hubs-infra-core'
4
6
 
5
7
  /**
6
8
  * Rehydrates an authorization object with current role definitions.
7
9
  * Validates each statement's role and cphs, and attaches a permissions
8
- * array to each one, expanded from the current roles.json.
9
- * @param {object} [authorization={}] - The authorization object to hydrate.
10
- * @param {Array<{role: string, cphs: '*'|string[]}>} [authorization.statements] - Statements to rehydrate.
11
- * @returns {object} Hydrated authorization object with each statement carrying its permissions.
10
+ * array to each one, expanded from the current roles.json. Runs on every
11
+ * guarded request, so also hashes the user's email into logger.context as
12
+ * user_email_hash when present, keeping it set for the lifetime of the
13
+ * request rather than only at the initial login.
14
+ * @param {Authorization} [authorization={}] - The authorization object to hydrate.
15
+ * @returns {HydratedAuthorization} Hydrated authorization object with each statement carrying its permissions.
12
16
  */
13
17
  export function hydrateAuthorization(authorization = {}) {
14
18
  const statements = normalizeStatements(authorization.statements).map(
@@ -17,6 +21,9 @@ export function hydrateAuthorization(authorization = {}) {
17
21
  permissions: [...(resolvedRoleDefinitions.get(statement.role) ?? [])]
18
22
  })
19
23
  )
24
+ if (authorization.email) {
25
+ logger.context.set('user_email_hash', authorization.email, true)
26
+ }
20
27
 
21
28
  return {
22
29
  ...authorization,
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @typedef {object} Statement
3
+ * @property {string} role
4
+ * @property {'*'|string[]} cphs
5
+ */
6
+
7
+ /**
8
+ * An authorization object before hydration - the shape a hub's own mapUser()
9
+ * returns (see e.g. front-office/back-office's oidc.js), read from a hub
10
+ * session, or read from a hub-issued JWT. statements is only present once a
11
+ * hub has resolved it via resolveAuthorization() and attached it before
12
+ * issuing the JWT - roles carries the raw, unresolved provider roles until
13
+ * then. Every other property here passes through hydrateAuthorization()
14
+ * untouched.
15
+ * @typedef {object} Authorization
16
+ * @property {string} [sub]
17
+ * @property {string} [email] - The authenticated user's email, if known.
18
+ * @property {string} [firstName]
19
+ * @property {string} [lastName]
20
+ * @property {string} [serviceId]
21
+ * @property {string[]} [roles] - Raw, unresolved provider roles.
22
+ * @property {string} [loa]
23
+ * @property {string[]} [amr]
24
+ * @property {string} [authProvider]
25
+ * @property {Statement[]} [statements] - Resolved statements, once attached.
26
+ */
27
+
28
+ /**
29
+ * An Authorization after hydrateAuthorization() - every other property
30
+ * passes through unchanged, but statements is now guaranteed present with
31
+ * each one carrying its expanded permissions, and authzVersion reflects
32
+ * the current roles.json.
33
+ * @typedef {Authorization & { statements: Array<Statement & { permissions: string[] }>, authzVersion: number }} HydratedAuthorization
34
+ */
35
+
36
+ export {}