@defra/lis-hubs-infra-access 0.43.0 → 0.44.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 +22 -17
- package/package.json +4 -4
- package/src/authentication/hub-auth.js +28 -72
- package/src/authentication/index.js +3 -10
- package/src/authentication/session.js +27 -0
- package/src/authentication/spoke-auth.js +1 -9
- package/src/authentication/tokens/index.js +0 -1
- package/src/authentication/tokens/jwt.js +4 -23
package/README.md
CHANGED
|
@@ -45,20 +45,17 @@ context and `demandPermission` read it from there. Routes opt out with
|
|
|
45
45
|
|
|
46
46
|
Entry points:
|
|
47
47
|
|
|
48
|
-
| Import | Exports
|
|
49
|
-
| --------------------------------------------- |
|
|
50
|
-
| `@defra/lis-hubs-infra-access/authentication` | `createHubAuth`, `HUB_AUTH_STRATEGY`, `createSpokeAuth`, `SPOKE_AUTH_STRATEGY`, `createOidcClient`, `
|
|
51
|
-
| `@defra/lis-hubs-infra-access/authorization` | `PERMISSIONS`, `hasPermission`, `demandPermission`, `resolveAuthorization`, `GLOBAL_CPH_SCOPE`, `AUTHORIZATION_VERSION`
|
|
52
|
-
| `@defra/lis-hubs-infra-access` | both of the above as `authentication` and `authorization` namespaces
|
|
48
|
+
| Import | Exports |
|
|
49
|
+
| --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
50
|
+
| `@defra/lis-hubs-infra-access/authentication` | `createHubAuth`, `HUB_AUTH_STRATEGY`, `createSpokeAuth`, `SPOKE_AUTH_STRATEGY`, `createOidcClient`, `getHubJwt`, `issueHubJwt`, `verifyHubJwt` |
|
|
51
|
+
| `@defra/lis-hubs-infra-access/authorization` | `PERMISSIONS`, `hasPermission`, `demandPermission`, `resolveAuthorization`, `GLOBAL_CPH_SCOPE`, `AUTHORIZATION_VERSION` |
|
|
52
|
+
| `@defra/lis-hubs-infra-access` | both of the above as `authentication` and `authorization` namespaces |
|
|
53
53
|
|
|
54
54
|
Prefer the subpath imports: they give plain named imports and simple mocks
|
|
55
55
|
(`vi.mock('@defra/lis-hubs-infra-access/authorization')`).
|
|
56
56
|
|
|
57
57
|
```js
|
|
58
|
-
import {
|
|
59
|
-
createSpokeAuth,
|
|
60
|
-
getHubJwtCookieOptions
|
|
61
|
-
} from '@defra/lis-hubs-infra-access/authentication'
|
|
58
|
+
import { createSpokeAuth } from '@defra/lis-hubs-infra-access/authentication'
|
|
62
59
|
import {
|
|
63
60
|
hasPermission,
|
|
64
61
|
PERMISSIONS
|
|
@@ -72,8 +69,6 @@ await server.register(
|
|
|
72
69
|
createSpokeAuth({
|
|
73
70
|
spokeId: 'cattle-home',
|
|
74
71
|
hubOrigins,
|
|
75
|
-
cookieName,
|
|
76
|
-
cookieOptions,
|
|
77
72
|
port,
|
|
78
73
|
basePath,
|
|
79
74
|
secret,
|
|
@@ -89,7 +84,8 @@ Registers the `lis-spoke` scheme as the default strategy. A request:
|
|
|
89
84
|
1. must carry a valid service token in `Authorization: Bearer <token>`, else
|
|
90
85
|
`401 { message: 'Service authentication required' }` in every route mode,
|
|
91
86
|
`public` spokes included;
|
|
92
|
-
2. must carry a valid hub JWT
|
|
87
|
+
2. must carry a valid hub JWT in the `x-lis-jwt-assertion` header
|
|
88
|
+
(`JWT_ASSERTION_HEADER` from `@defra/lis-hubs-infra-core`), unless the spoke is `public`. Without one,
|
|
93
89
|
a required route redirects to `<hubOrigin>/auth/login?returnUrl=…`, and a
|
|
94
90
|
`try` route continues unauthenticated;
|
|
95
91
|
3. then gets `credentials: { user, caller }`, where `caller` is the hub's
|
|
@@ -99,6 +95,11 @@ Authenticated requests are then checked against `moduleAccess` (non-public
|
|
|
99
95
|
spokes) and the optional `authorize` predicate; failing either gives a 403.
|
|
100
96
|
The factory throws for an unknown `spokeId` or an invalid `moduleAccess`.
|
|
101
97
|
|
|
98
|
+
The hub proxy sets the `x-lis-jwt-assertion` header on proxied requests; spokes
|
|
99
|
+
never see the hub JWT as a cookie, and a JWT sent as a cookie is ignored.
|
|
100
|
+
The header is only trusted because the service token check runs first, so
|
|
101
|
+
`createSpokeAuth` takes no cookie options.
|
|
102
|
+
|
|
102
103
|
The service token is an AWS STS web identity token, attached by the hub's
|
|
103
104
|
proxy in `@defra/lis-hubs-infra-core`. It must:
|
|
104
105
|
|
|
@@ -119,8 +120,6 @@ called; the CDP platform provides them. The JWKS is fetched with the built-in
|
|
|
119
120
|
await server.register(
|
|
120
121
|
createHubAuth({
|
|
121
122
|
hubId: 'front-office',
|
|
122
|
-
getHubJwtCookieName,
|
|
123
|
-
getCookieOptions,
|
|
124
123
|
getHubJwtConfig,
|
|
125
124
|
resolveAuthSession,
|
|
126
125
|
buildAuthorizationUrl,
|
|
@@ -133,7 +132,7 @@ await server.register(
|
|
|
133
132
|
)
|
|
134
133
|
```
|
|
135
134
|
|
|
136
|
-
Registers the
|
|
135
|
+
Registers the login, `/sso`, `/auth/logout` and
|
|
137
136
|
`/signout` routes (all `auth: false`) and the not-authorised route (`auth: try`), plus the `lis-hub-session`
|
|
138
137
|
scheme as the default strategy, backed by the yar hub session. A signed-out
|
|
139
138
|
user on a required route is redirected to the first login route with
|
|
@@ -144,13 +143,19 @@ sees. `hubId` is required and must be a registry hub id. The optional `authorize
|
|
|
144
143
|
predicate runs for authenticated requests only; `false` refuses the request
|
|
145
144
|
(see below).
|
|
146
145
|
|
|
146
|
+
The hub JWT minted at sign-in is kept in the hub's yar session, not in a
|
|
147
|
+
browser cookie, so the browser holds only the session cookie. `getHubJwt(request)`
|
|
148
|
+
returns it (or `null`), and the hub's proxy injects it on proxied spoke
|
|
149
|
+
requests in the `x-lis-jwt-assertion` header (`JWT_ASSERTION_HEADER` from `@defra/lis-hubs-infra-core`).
|
|
150
|
+
Signing out resets the session, which drops the JWT with it.
|
|
151
|
+
|
|
147
152
|
A hub-level refusal keeps authentication and authorisation apart: the user
|
|
148
153
|
stays signed in to the hub but is not authorised to use it. `resolveAuthSession`
|
|
149
154
|
refuses a login (e.g. a user not on an allow-list) by returning
|
|
150
155
|
`{ denied: true }`, and `authorize` refuses a signed-in user by returning
|
|
151
156
|
`false`. `authorize` runs on every authenticated request and also in the login
|
|
152
|
-
and `/sso` routes before a JWT is minted. Either way no JWT
|
|
153
|
-
(any existing one is
|
|
157
|
+
and `/sso` routes before a JWT is minted. Either way no JWT is stored in the
|
|
158
|
+
session (any existing one is cleared), so no spoke is reachable, and the user is
|
|
154
159
|
redirected to `notAuthorisedPath`. A refused
|
|
155
160
|
login is stored as a session flagged `refused`, and every later authenticated
|
|
156
161
|
request, including the login route, is sent to `notAuthorisedPath` without
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defra/lis-hubs-infra-access",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.44.0",
|
|
4
4
|
"description": "Shared hub access and capability resolution for the livestock platform.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -38,13 +38,13 @@
|
|
|
38
38
|
"jose": "6.2.8"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@defra/lis-hubs-infra-core": ">=0.
|
|
41
|
+
"@defra/lis-hubs-infra-core": ">=0.31.0 <1.0.0",
|
|
42
42
|
"@defra/lis-hubs-infra-registry": ">=0.5.0 <1.0.0",
|
|
43
43
|
"@hapi/boom": "10.0.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@defra/lis-hubs-infra-core": "^0.
|
|
47
|
-
"@defra/lis-hubs-infra-registry": "^0.
|
|
46
|
+
"@defra/lis-hubs-infra-core": "^0.31.0",
|
|
47
|
+
"@defra/lis-hubs-infra-registry": "^0.13.0",
|
|
48
48
|
"@defra/lis-infra-eslint-config": "^0.6.0",
|
|
49
49
|
"@hapi/boom": "10.0.1",
|
|
50
50
|
"@hapi/hapi": "21.4.10",
|
|
@@ -4,14 +4,12 @@ import { HUBS } from '@defra/lis-hubs-infra-registry'
|
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
6
|
clearHubAuthSession,
|
|
7
|
+
clearHubJwt,
|
|
7
8
|
getHubAuthSession,
|
|
8
|
-
setHubAuthSession
|
|
9
|
+
setHubAuthSession,
|
|
10
|
+
setHubJwt
|
|
9
11
|
} from './session.js'
|
|
10
|
-
import {
|
|
11
|
-
getHubJwtCookieOptions,
|
|
12
|
-
getReturnUrlFromRequest,
|
|
13
|
-
issueHubJwt
|
|
14
|
-
} from './tokens/index.js'
|
|
12
|
+
import { getReturnUrlFromRequest, issueHubJwt } from './tokens/index.js'
|
|
15
13
|
import { getAuthorizedSpecies } from '../authorization/modules/species.js'
|
|
16
14
|
import { hydrateAuthorization } from '../authorization/permissions/hydrate.js'
|
|
17
15
|
|
|
@@ -70,29 +68,23 @@ function createIdpError(query) {
|
|
|
70
68
|
/**
|
|
71
69
|
* Builds the shared response for a hub-level refusal. Authentication and
|
|
72
70
|
* authorisation are kept apart: the user stays signed in to the hub, but no
|
|
73
|
-
* spoke JWT is
|
|
71
|
+
* spoke JWT is held in their session and they land on the not-authorised page, which offers
|
|
74
72
|
* Sign out. A refused sign-in stores its session flagged `refused` so every
|
|
75
73
|
* later request is sent to the same page without re-running the hub's checks.
|
|
76
74
|
*
|
|
77
75
|
* @param {object} options
|
|
78
|
-
* @param {Function} options.getHubJwtCookieName - Returns the hub JWT cookie name.
|
|
79
|
-
* @param {Function} options.getCookieOptions - Returns the hub JWT cookie options.
|
|
80
76
|
* @param {string} options.notAuthorisedPath - Hub path the user lands on.
|
|
81
77
|
* @returns {(request: object, h: object, authSession?: object) => object} Returns the redirect.
|
|
82
78
|
*/
|
|
83
|
-
function createRefuseAccess({
|
|
84
|
-
getHubJwtCookieName,
|
|
85
|
-
getCookieOptions,
|
|
86
|
-
notAuthorisedPath
|
|
87
|
-
}) {
|
|
79
|
+
function createRefuseAccess({ notAuthorisedPath }) {
|
|
88
80
|
return function refuseAccess(request, h, authSession) {
|
|
89
81
|
if (authSession) {
|
|
90
82
|
setHubAuthSession(request, { ...authSession, refused: true })
|
|
91
83
|
}
|
|
92
84
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
85
|
+
clearHubJwt(request)
|
|
86
|
+
|
|
87
|
+
return h.redirect(notAuthorisedPath)
|
|
96
88
|
}
|
|
97
89
|
}
|
|
98
90
|
|
|
@@ -101,9 +93,7 @@ function isRefusedSession(authSession) {
|
|
|
101
93
|
}
|
|
102
94
|
|
|
103
95
|
function createLoginController({
|
|
104
|
-
getCookieOptions,
|
|
105
96
|
getHubJwtConfig,
|
|
106
|
-
getHubJwtCookieName,
|
|
107
97
|
providerId,
|
|
108
98
|
buildAuthorizationUrl,
|
|
109
99
|
notAuthorisedPath,
|
|
@@ -129,11 +119,9 @@ function createLoginController({
|
|
|
129
119
|
return refuseAccess(request, h)
|
|
130
120
|
}
|
|
131
121
|
|
|
132
|
-
|
|
122
|
+
setHubJwt(request, await issueHubJwt(authSession, getHubJwtConfig()))
|
|
133
123
|
|
|
134
|
-
return h
|
|
135
|
-
.redirect(returnUrl)
|
|
136
|
-
.state(getHubJwtCookieName(), jwt, getCookieOptions())
|
|
124
|
+
return h.redirect(returnUrl)
|
|
137
125
|
}
|
|
138
126
|
|
|
139
127
|
const resolvedProviderId =
|
|
@@ -166,9 +154,7 @@ function createLoginController({
|
|
|
166
154
|
}
|
|
167
155
|
|
|
168
156
|
function createCallbackController({
|
|
169
|
-
getCookieOptions,
|
|
170
157
|
getHubJwtConfig,
|
|
171
|
-
getHubJwtCookieName,
|
|
172
158
|
completeAuthorizationCodeGrant,
|
|
173
159
|
resolveAuthSession,
|
|
174
160
|
refuseAccess,
|
|
@@ -222,20 +208,17 @@ function createCallbackController({
|
|
|
222
208
|
return refuseAccess(request, h)
|
|
223
209
|
}
|
|
224
210
|
|
|
225
|
-
|
|
211
|
+
setHubJwt(
|
|
212
|
+
request,
|
|
213
|
+
await issueHubJwt(enrichedAuthSession, getHubJwtConfig())
|
|
214
|
+
)
|
|
226
215
|
|
|
227
|
-
return h
|
|
228
|
-
.redirect(returnUrl)
|
|
229
|
-
.state(getHubJwtCookieName(), jwt, getCookieOptions())
|
|
216
|
+
return h.redirect(returnUrl)
|
|
230
217
|
}
|
|
231
218
|
}
|
|
232
219
|
}
|
|
233
220
|
|
|
234
|
-
function createLogoutController({
|
|
235
|
-
getCookieOptions,
|
|
236
|
-
getHubJwtCookieName,
|
|
237
|
-
buildLogoutUrl
|
|
238
|
-
}) {
|
|
221
|
+
function createLogoutController({ buildLogoutUrl }) {
|
|
239
222
|
return {
|
|
240
223
|
options: {
|
|
241
224
|
auth: false
|
|
@@ -244,12 +227,11 @@ function createLogoutController({
|
|
|
244
227
|
// Build the logout URL while the session is still alive (it reads the
|
|
245
228
|
// id token for id_token_hint), but invalidate the session in `finally`
|
|
246
229
|
// so a failure building the URL cannot leave the session replayable.
|
|
230
|
+
// Resetting the session also drops the hub JWT it holds.
|
|
247
231
|
try {
|
|
248
232
|
const logoutUrl = await buildLogoutUrl(request)
|
|
249
233
|
|
|
250
|
-
return h
|
|
251
|
-
.redirect(logoutUrl)
|
|
252
|
-
.unstate(getHubJwtCookieName(), getCookieOptions())
|
|
234
|
+
return h.redirect(logoutUrl)
|
|
253
235
|
} finally {
|
|
254
236
|
clearHubAuthSession(request)
|
|
255
237
|
}
|
|
@@ -257,19 +239,6 @@ function createLogoutController({
|
|
|
257
239
|
}
|
|
258
240
|
}
|
|
259
241
|
|
|
260
|
-
/**
|
|
261
|
-
* Creates cookie options for hub JWT authentication.
|
|
262
|
-
*
|
|
263
|
-
* @param {object} options - Cookie configuration options.
|
|
264
|
-
* @param {number} options.ttlSeconds - Time to live in seconds for the cookie.
|
|
265
|
-
* @returns {object} Cookie options compatible with Hapi server state configuration.
|
|
266
|
-
*/
|
|
267
|
-
export function createHubCookieOptions({ ttlSeconds }) {
|
|
268
|
-
return getHubJwtCookieOptions({
|
|
269
|
-
ttlSeconds
|
|
270
|
-
})
|
|
271
|
-
}
|
|
272
|
-
|
|
273
242
|
function createNotAuthorisedRoute({ path, loginPath }) {
|
|
274
243
|
return {
|
|
275
244
|
method: 'GET',
|
|
@@ -295,19 +264,17 @@ function createNotAuthorisedRoute({ path, loginPath }) {
|
|
|
295
264
|
}
|
|
296
265
|
|
|
297
266
|
/**
|
|
298
|
-
* Builds the
|
|
267
|
+
* Builds the login, `/sso`, `/auth/logout` and
|
|
299
268
|
* `/signout` routes (all `auth: false`), plus the shared not-authorised route
|
|
300
269
|
* when the hub uses the default not-authorised path.
|
|
301
270
|
*
|
|
302
271
|
* @param {object} options - `createHubAuth` options, minus `pluginName` and `authorize`.
|
|
303
272
|
* @param {Function} options.refuseAccess - Shared hub-level refusal response.
|
|
304
273
|
* @param {boolean} options.registerNotAuthorisedRoute - Whether to register the shared not-authorised route.
|
|
305
|
-
* @returns {(server: object) => void} Registers the
|
|
274
|
+
* @returns {(server: object) => void} Registers the routes on a server.
|
|
306
275
|
*/
|
|
307
276
|
function createHubAuthRegistration(options) {
|
|
308
277
|
const {
|
|
309
|
-
getHubJwtCookieName,
|
|
310
|
-
getCookieOptions,
|
|
311
278
|
getHubJwtConfig,
|
|
312
279
|
resolveAuthSession,
|
|
313
280
|
buildAuthorizationUrl,
|
|
@@ -321,28 +288,20 @@ function createHubAuthRegistration(options) {
|
|
|
321
288
|
} = options
|
|
322
289
|
const loginPath = loginRoutes[0].path
|
|
323
290
|
const callbackController = createCallbackController({
|
|
324
|
-
getCookieOptions,
|
|
325
291
|
getHubJwtConfig,
|
|
326
|
-
getHubJwtCookieName,
|
|
327
292
|
completeAuthorizationCodeGrant,
|
|
328
293
|
resolveAuthSession,
|
|
329
294
|
refuseAccess,
|
|
330
295
|
isAuthorized,
|
|
331
296
|
loginPath
|
|
332
297
|
})
|
|
333
|
-
const logoutController = createLogoutController({
|
|
334
|
-
getCookieOptions,
|
|
335
|
-
getHubJwtCookieName,
|
|
336
|
-
buildLogoutUrl
|
|
337
|
-
})
|
|
298
|
+
const logoutController = createLogoutController({ buildLogoutUrl })
|
|
338
299
|
const routes = [
|
|
339
300
|
...loginRoutes.map(({ path, providerId }) => ({
|
|
340
301
|
method: 'GET',
|
|
341
302
|
path,
|
|
342
303
|
...createLoginController({
|
|
343
|
-
getCookieOptions,
|
|
344
304
|
getHubJwtConfig,
|
|
345
|
-
getHubJwtCookieName,
|
|
346
305
|
providerId,
|
|
347
306
|
buildAuthorizationUrl,
|
|
348
307
|
notAuthorisedPath,
|
|
@@ -378,7 +337,6 @@ function createHubAuthRegistration(options) {
|
|
|
378
337
|
]
|
|
379
338
|
|
|
380
339
|
return (server) => {
|
|
381
|
-
server.state(getHubJwtCookieName(), getCookieOptions())
|
|
382
340
|
server.route(routes)
|
|
383
341
|
}
|
|
384
342
|
}
|
|
@@ -417,7 +375,7 @@ function createHubSessionScheme({ loginPath, caller }) {
|
|
|
417
375
|
/**
|
|
418
376
|
* Creates a Hapi plugin for hub authentication built on a Hapi auth scheme.
|
|
419
377
|
*
|
|
420
|
-
* Registers the
|
|
378
|
+
* Registers the login, `/sso`, `/auth/logout` and
|
|
421
379
|
* `/signout` routes (all `auth: false`), the shared not-authorised route
|
|
422
380
|
* (`auth: { mode: 'try' }`, unless the hub sets its own `notAuthorisedPath`), and the
|
|
423
381
|
* `lis-hub-session` scheme and
|
|
@@ -428,11 +386,13 @@ function createHubSessionScheme({ loginPath, caller }) {
|
|
|
428
386
|
* service name (`lis-hubs-<hubId>`) so views and handlers can read which hub
|
|
429
387
|
* a request belongs to the same way in a hub as in a spoke.
|
|
430
388
|
*
|
|
389
|
+
* The hub JWT minted at sign-in is held in the hub's yar session (read it with
|
|
390
|
+
* `getHubJwt`), so the browser only carries the session cookie; the hub's
|
|
391
|
+
* proxy sends it to spokes in the `x-lis-jwt-assertion` header.
|
|
392
|
+
*
|
|
431
393
|
* @param {object} options - Plugin configuration options.
|
|
432
394
|
* @param {string} [options.pluginName='auth'] - Name of the plugin.
|
|
433
395
|
* @param {string} options.hubId - Registry id of this hub: 'front-office' or 'back-office'.
|
|
434
|
-
* @param {Function} options.getHubJwtCookieName - Function that returns the JWT cookie name.
|
|
435
|
-
* @param {Function} options.getCookieOptions - Function that returns cookie options.
|
|
436
396
|
* @param {Function} options.getHubJwtConfig - Function that returns JWT configuration.
|
|
437
397
|
* @param {Function} options.resolveAuthSession - Function to resolve and enrich auth session with authorization data.
|
|
438
398
|
* @param {Function} options.buildAuthorizationUrl - Function to build OIDC authorization URL.
|
|
@@ -474,11 +434,7 @@ export function createHubAuth({
|
|
|
474
434
|
}
|
|
475
435
|
|
|
476
436
|
const landingPath = notAuthorisedPath ?? DEFAULT_NOT_AUTHORISED_PATH
|
|
477
|
-
const refuseAccess = createRefuseAccess({
|
|
478
|
-
getHubJwtCookieName: options.getHubJwtCookieName,
|
|
479
|
-
getCookieOptions: options.getCookieOptions,
|
|
480
|
-
notAuthorisedPath: landingPath
|
|
481
|
-
})
|
|
437
|
+
const refuseAccess = createRefuseAccess({ notAuthorisedPath: landingPath })
|
|
482
438
|
// The hub-wide rule, applied wherever a JWT is issued and on every
|
|
483
439
|
// authenticated request. Without a rule every signed-in user is authorised.
|
|
484
440
|
const isAuthorized = (authSession, request) =>
|
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
createHubCookieOptions,
|
|
4
|
-
HUB_AUTH_STRATEGY
|
|
5
|
-
} from './hub-auth.js'
|
|
1
|
+
export { createHubAuth, HUB_AUTH_STRATEGY } from './hub-auth.js'
|
|
2
|
+
export { getHubJwt } from './session.js'
|
|
6
3
|
export { createOidcClient } from './oidc.js'
|
|
7
4
|
export { createSpokeAuth, SPOKE_AUTH_STRATEGY } from './spoke-auth.js'
|
|
8
|
-
export {
|
|
9
|
-
getHubJwtCookieOptions,
|
|
10
|
-
issueHubJwt,
|
|
11
|
-
verifyHubJwt
|
|
12
|
-
} from './tokens/index.js'
|
|
5
|
+
export { issueHubJwt, verifyHubJwt } from './tokens/index.js'
|
|
@@ -2,6 +2,7 @@ import crypto from 'node:crypto'
|
|
|
2
2
|
|
|
3
3
|
const authFlowKey = 'hub-auth-flow'
|
|
4
4
|
const authSessionKey = 'hub-auth-session'
|
|
5
|
+
const hubJwtKey = 'hub-jwt'
|
|
5
6
|
const keyLength = 32
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -70,6 +71,32 @@ export function setHubAuthSession(request, authSession) {
|
|
|
70
71
|
request?.yar?.set?.(authSessionKey, authSession)
|
|
71
72
|
}
|
|
72
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Retrieves the hub JWT minted for the signed-in user from the request session.
|
|
76
|
+
* @param {object} request - The Hapi request object with yar session support.
|
|
77
|
+
* @returns {string|null} The stored hub JWT or null if not found.
|
|
78
|
+
*/
|
|
79
|
+
export function getHubJwt(request) {
|
|
80
|
+
return request?.yar?.get ? (request.yar.get(hubJwtKey) ?? null) : null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Stores the hub JWT in the request session.
|
|
85
|
+
* @param {object} request - The Hapi request object with yar session support.
|
|
86
|
+
* @param {string} jwt - The signed hub JWT to store.
|
|
87
|
+
*/
|
|
88
|
+
export function setHubJwt(request, jwt) {
|
|
89
|
+
request?.yar?.set?.(hubJwtKey, jwt)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Clears the hub JWT from the request session.
|
|
94
|
+
* @param {object} request - The Hapi request object with yar session support.
|
|
95
|
+
*/
|
|
96
|
+
export function clearHubJwt(request) {
|
|
97
|
+
request?.yar?.clear?.(hubJwtKey)
|
|
98
|
+
}
|
|
99
|
+
|
|
73
100
|
/**
|
|
74
101
|
* Fully resets the request's yar session (auth session and flow included).
|
|
75
102
|
* @param {object} request - The Hapi request object with yar session support.
|
|
@@ -57,7 +57,6 @@ function createAuthenticate({
|
|
|
57
57
|
verifyCaller,
|
|
58
58
|
isPublic,
|
|
59
59
|
hubOrigins,
|
|
60
|
-
cookieName,
|
|
61
60
|
port,
|
|
62
61
|
basePath,
|
|
63
62
|
secret,
|
|
@@ -71,7 +70,6 @@ function createAuthenticate({
|
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
const payload = await getHubJwtPayloadFromRequest(request, {
|
|
74
|
-
cookieName,
|
|
75
73
|
secret,
|
|
76
74
|
issuer: hubOrigins,
|
|
77
75
|
audience
|
|
@@ -120,14 +118,12 @@ function createAccessCheck({ isPublic, moduleAccess, authorize }) {
|
|
|
120
118
|
* Creates a Hapi plugin that registers the `lis-spoke` auth scheme as the
|
|
121
119
|
* server's default strategy. Every authenticated request needs a valid hub
|
|
122
120
|
* service token (`Authorization: Bearer`) and, unless the spoke is public, a
|
|
123
|
-
* valid hub JWT
|
|
121
|
+
* valid hub JWT in the `x-lis-jwt-assertion` header set by the hub proxy. Routes opt out with `auth: false`, or accept signed-out
|
|
124
122
|
* users with `auth: { mode: 'try' }` or `auth: { mode: 'optional' }`.
|
|
125
123
|
*
|
|
126
124
|
* @param {object} options
|
|
127
125
|
* @param {string} options.spokeId - Registry id of this spoke; the service token audience is `lis-apps-<spokeId>`.
|
|
128
126
|
* @param {string[]} options.hubOrigins - Accepted hub JWT issuers; the first is the default login origin.
|
|
129
|
-
* @param {string} options.cookieName - Hub JWT cookie name.
|
|
130
|
-
* @param {object} options.cookieOptions - Hapi state options for the hub JWT cookie.
|
|
131
127
|
* @param {number} options.port - Spoke port, used to build the login return URL.
|
|
132
128
|
* @param {string} [options.basePath] - Spoke base path, used to build the login return URL.
|
|
133
129
|
* @param {string} options.secret - Hub JWT signing secret.
|
|
@@ -139,8 +135,6 @@ function createAccessCheck({ isPublic, moduleAccess, authorize }) {
|
|
|
139
135
|
export function createSpokeAuth({
|
|
140
136
|
spokeId,
|
|
141
137
|
hubOrigins,
|
|
142
|
-
cookieName,
|
|
143
|
-
cookieOptions,
|
|
144
138
|
port,
|
|
145
139
|
basePath,
|
|
146
140
|
secret,
|
|
@@ -165,7 +159,6 @@ export function createSpokeAuth({
|
|
|
165
159
|
verifyCaller: createCallerVerifier(spokeId),
|
|
166
160
|
isPublic,
|
|
167
161
|
hubOrigins,
|
|
168
|
-
cookieName,
|
|
169
162
|
port,
|
|
170
163
|
basePath,
|
|
171
164
|
secret,
|
|
@@ -181,7 +174,6 @@ export function createSpokeAuth({
|
|
|
181
174
|
plugin: {
|
|
182
175
|
name: 'spokeAuth',
|
|
183
176
|
register(server) {
|
|
184
|
-
server.state(cookieName, cookieOptions)
|
|
185
177
|
server.auth.scheme(SPOKE_AUTH_STRATEGY, () => ({ authenticate }))
|
|
186
178
|
server.auth.strategy(SPOKE_AUTH_STRATEGY, SPOKE_AUTH_STRATEGY)
|
|
187
179
|
server.auth.default(SPOKE_AUTH_STRATEGY)
|
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
import { TextEncoder } from 'node:util'
|
|
3
3
|
|
|
4
4
|
import { SignJWT, jwtVerify } from 'jose'
|
|
5
|
-
import { logger } from '@defra/lis-hubs-infra-core'
|
|
5
|
+
import { JWT_ASSERTION_HEADER, logger } from '@defra/lis-hubs-infra-core'
|
|
6
6
|
|
|
7
7
|
import { AUTHORIZATION_VERSION } from '../../authorization/constants.js'
|
|
8
8
|
|
|
9
9
|
const encoder = new TextEncoder()
|
|
10
|
-
const MILLISECONDS_PER_SECOND = 1000
|
|
11
10
|
const JWT_EXPIRED_CODE = 'ERR_JWT_EXPIRED'
|
|
12
11
|
const AUTHZ_VERSION_UNSUPPORTED_CODE = 'ERR_AUTHZ_VERSION_UNSUPPORTED'
|
|
13
12
|
const ROUTINE_FAILURE_CODES = new Set([
|
|
@@ -19,24 +18,6 @@ function getHubJwtSecret(secret) {
|
|
|
19
18
|
return encoder.encode(secret)
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
/**
|
|
23
|
-
* @param {{ ttlSeconds: number }} options
|
|
24
|
-
* @returns {object}
|
|
25
|
-
*/
|
|
26
|
-
export function getHubJwtCookieOptions({ ttlSeconds }) {
|
|
27
|
-
return {
|
|
28
|
-
encoding: 'none',
|
|
29
|
-
ttl: ttlSeconds * MILLISECONDS_PER_SECOND,
|
|
30
|
-
isHttpOnly: true,
|
|
31
|
-
// Secure is a fixed invariant: the browser always reaches the app over
|
|
32
|
-
// HTTPS (Caddy locally, the CDP edge deployed).
|
|
33
|
-
isSecure: true,
|
|
34
|
-
isSameSite: 'Lax',
|
|
35
|
-
clearInvalid: true,
|
|
36
|
-
path: '/'
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
21
|
/**
|
|
41
22
|
* @param {object} user
|
|
42
23
|
* @param {{ secret: string, issuer: string, audience: string, ttlSeconds: number }} options
|
|
@@ -105,14 +86,14 @@ export function getAuthorizationBearerToken(request) {
|
|
|
105
86
|
|
|
106
87
|
/**
|
|
107
88
|
* @param {Request} request
|
|
108
|
-
* @param {{
|
|
89
|
+
* @param {{ secret: string, issuer: string, audience: string }} options
|
|
109
90
|
* @returns {Promise<object | null>}
|
|
110
91
|
*/
|
|
111
92
|
export async function getHubJwtPayloadFromRequest(
|
|
112
93
|
request,
|
|
113
|
-
{
|
|
94
|
+
{ secret, issuer, audience }
|
|
114
95
|
) {
|
|
115
|
-
const token = request.
|
|
96
|
+
const token = request.headers?.[JWT_ASSERTION_HEADER]
|
|
116
97
|
|
|
117
98
|
if (!token) {
|
|
118
99
|
return null
|