@defra/lis-hubs-infra-access 0.28.0 → 0.29.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/README.md CHANGED
@@ -29,8 +29,8 @@ the server's default strategy. Identity is on `request.auth.credentials.user`
29
29
  context and `demandPermission` read it from there. Routes opt out with
30
30
  `auth: false` (assets, `/health`) or accept signed-out users with
31
31
  `auth: { mode: 'try' }` or `auth: { mode: 'optional' }`. A signed-out
32
- `try`/`optional` spoke request carries no credentials, so `caller` isn't
33
- available on those requests.
32
+ `try`/`optional` request, in a hub or a spoke, carries no credentials, so
33
+ `caller` isn't available on those requests.
34
34
 
35
35
  Entry points:
36
36
 
@@ -107,6 +107,7 @@ called; the CDP platform provides them. The JWKS is fetched with the built-in
107
107
  ```js
108
108
  await server.register(
109
109
  createHubAuth({
110
+ hubId: 'front-office',
110
111
  getHubJwtCookieName,
111
112
  getCookieOptions,
112
113
  getHubJwtConfig,
@@ -126,7 +127,9 @@ Registers the hub JWT cookie state, the login, `/sso`, `/auth/logout` and
126
127
  scheme as the default strategy, backed by the yar hub session. A signed-out
127
128
  user on a required route is redirected to the first login route with
128
129
  `?returnUrl=<path+search>`; a `try` route continues unauthenticated.
129
- Credentials are `{ user, authorizedSpecies }`. The optional `authorize`
130
+ Credentials are `{ user, authorizedSpecies, caller }`, where `caller` is the
131
+ hub's own service name (`lis-hubs-<hubId>`), matching the `caller` a spoke
132
+ sees. `hubId` is required and must be a registry hub id. The optional `authorize`
130
133
  predicate runs for authenticated requests only; `false` gives a 403.
131
134
 
132
135
  `resolveAuthSession` can refuse a login (e.g. a user not on an allow-list) by
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-access",
3
- "version": "0.28.0",
3
+ "version": "0.29.0",
4
4
  "description": "Shared hub access and capability resolution for the livestock platform.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,5 +1,6 @@
1
1
  import Boom from '@hapi/boom'
2
2
  import { logger } from '@defra/lis-hubs-infra-core'
3
+ import { HUBS } from '@defra/lis-hubs-infra-registry'
3
4
 
4
5
  import {
5
6
  clearHubAuthSession,
@@ -249,7 +250,7 @@ function createHubAuthRegistration(options) {
249
250
  }
250
251
  }
251
252
 
252
- function createHubSessionScheme(loginPath) {
253
+ function createHubSessionScheme({ loginPath, caller }) {
253
254
  return () => ({
254
255
  authenticate(request, h) {
255
256
  const authSession = getHubAuthSession(request)
@@ -258,7 +259,11 @@ function createHubSessionScheme(loginPath) {
258
259
  const user = hydrateAuthorization(authSession)
259
260
 
260
261
  return h.authenticated({
261
- credentials: { user, authorizedSpecies: getAuthorizedSpecies(user) }
262
+ credentials: {
263
+ user,
264
+ authorizedSpecies: getAuthorizedSpecies(user),
265
+ caller
266
+ }
262
267
  })
263
268
  }
264
269
 
@@ -283,10 +288,13 @@ function createHubSessionScheme(loginPath) {
283
288
  * strategy, made the server's default. Signed-out requests to `required`
284
289
  * routes redirect to the first login route with a `returnUrl`; `try` routes
285
290
  * run unauthenticated. Identity is on `request.auth.credentials`
286
- * (`{ user, authorizedSpecies }`).
291
+ * (`{ user, authorizedSpecies, caller }`), where `caller` is the hub's own
292
+ * service name (`lis-hubs-<hubId>`) so views and handlers can read which hub
293
+ * a request belongs to the same way in a hub as in a spoke.
287
294
  *
288
295
  * @param {object} options - Plugin configuration options.
289
296
  * @param {string} [options.pluginName='auth'] - Name of the plugin.
297
+ * @param {string} options.hubId - Registry id of this hub: 'front-office' or 'back-office'.
290
298
  * @param {Function} options.getHubJwtCookieName - Function that returns the JWT cookie name.
291
299
  * @param {Function} options.getCookieOptions - Function that returns cookie options.
292
300
  * @param {Function} options.getHubJwtConfig - Function that returns JWT configuration.
@@ -299,15 +307,28 @@ function createHubSessionScheme(loginPath) {
299
307
  * @param {(user: object, request: object) => boolean | Promise<boolean>} [options.authorize] - Optional hub-wide rule for authenticated requests; `false` gives a 403.
300
308
  * @returns {{plugin: {name: string, register: Function}}} Hapi plugin object with registration function.
301
309
  */
302
- export function createHubAuth({ pluginName = 'auth', authorize, ...options }) {
310
+ export function createHubAuth({
311
+ pluginName = 'auth',
312
+ hubId,
313
+ authorize,
314
+ ...options
315
+ }) {
316
+ if (!HUBS.includes(hubId)) {
317
+ throw new Error(`createHubAuth() requires a hubId of ${HUBS.join(' or ')}`)
318
+ }
319
+
303
320
  const register = createHubAuthRegistration(options)
304
321
  const loginPath = options.loginRoutes[0].path
322
+ const caller = `lis-hubs-${hubId}`
305
323
 
306
324
  return {
307
325
  plugin: {
308
326
  name: pluginName,
309
327
  register(server) {
310
- server.auth.scheme(HUB_AUTH_STRATEGY, createHubSessionScheme(loginPath))
328
+ server.auth.scheme(
329
+ HUB_AUTH_STRATEGY,
330
+ createHubSessionScheme({ loginPath, caller })
331
+ )
311
332
  server.auth.strategy(HUB_AUTH_STRATEGY, HUB_AUTH_STRATEGY)
312
333
  server.auth.default(HUB_AUTH_STRATEGY)
313
334