@defra/lis-hubs-infra-access 0.20.0 → 0.21.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.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "Shared hub access and capability resolution for the livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -34,7 +34,6 @@
34
34
  "author": "Defra DDTS",
35
35
  "license": "OGL-UK-3.0",
36
36
  "dependencies": {
37
- "@defra/lis-hubs-infra-registry": "^0.3.0",
38
37
  "convict": "6.2.5",
39
38
  "convict-format-with-validator": "6.2.0",
40
39
  "eslint-plugin-jsdoc": "^63.0.8",
@@ -49,7 +48,8 @@
49
48
  "eslint": "^9.0.0"
50
49
  },
51
50
  "devDependencies": {
52
- "@defra/lis-hubs-infra-core": "^0.8.0",
51
+ "@defra/lis-hubs-infra-core": "^0.12.0",
52
+ "@defra/lis-hubs-infra-registry": "^0.4.0",
53
53
  "@defra/lis-infra-eslint-config": "^0.6.0",
54
54
  "@hapi/boom": "10.0.1",
55
55
  "@hapi/wreck": "18.1.2",
package/src/auth/oidc.js CHANGED
@@ -176,6 +176,12 @@ function resolveAuthorizationFlow(request, getPrimaryProviderId) {
176
176
  return { authFlow, providerId }
177
177
  }
178
178
 
179
+ function isStaleAuthorizationResponse(request) {
180
+ const authFlow = getHubAuthFlow(request)
181
+
182
+ return !authFlow?.state || request.query?.state !== authFlow.state
183
+ }
184
+
179
185
  function validateAuthorizationResponse(request, authFlow) {
180
186
  if (request.query?.state !== authFlow.state) {
181
187
  throw new Error('State mismatch')
@@ -234,6 +240,13 @@ function createAuthorizationCodeCompleter({
234
240
  mapUser
235
241
  }) {
236
242
  return async function completeAuthorizationCodeGrant(request) {
243
+ // A replayed authorization response carries a state the hub no longer
244
+ // holds. Report it to the caller rather than throwing, so the hub can
245
+ // restart login instead of failing the callback.
246
+ if (isStaleAuthorizationResponse(request)) {
247
+ return { stale: true }
248
+ }
249
+
237
250
  const { authFlow, providerId } = resolveAuthorizationFlow(
238
251
  request,
239
252
  getPrimaryProviderId
@@ -2,7 +2,6 @@ import { logger } from '@defra/lis-hubs-infra-core'
2
2
 
3
3
  import {
4
4
  clearHubAuthSession,
5
- getHubAuthFlow,
6
5
  getHubAuthSession,
7
6
  setHubAuthSession
8
7
  } from './session.js'
@@ -68,12 +67,6 @@ function createLoginController({
68
67
  }
69
68
  }
70
69
 
71
- function isStaleAuthorizationResponse(request) {
72
- const authFlow = getHubAuthFlow(request)
73
-
74
- return !authFlow?.state || request.query?.state !== authFlow.state
75
- }
76
-
77
70
  function createCallbackController({
78
71
  getCookieOptions,
79
72
  getHubJwtConfig,
@@ -91,16 +84,17 @@ function createCallbackController({
91
84
  throw new Error(request.query?.error_description ?? request.query.error)
92
85
  }
93
86
 
87
+ const grant = await completeAuthorizationCodeGrant(request)
88
+
94
89
  // A replayed identity provider page (e.g. browser back button after
95
90
  // signing in) returns a state the hub no longer holds. Restarting
96
91
  // login sends an already-authenticated user straight on and gives
97
92
  // anyone else a fresh flow, instead of failing the callback.
98
- if (isStaleAuthorizationResponse(request)) {
93
+ if (grant?.stale) {
99
94
  return h.redirect(loginPath)
100
95
  }
101
96
 
102
- const { user, authSession, accessToken, returnUrl } =
103
- await completeAuthorizationCodeGrant(request)
97
+ const { user, authSession, accessToken, returnUrl } = grant
104
98
  const authorization = await resolveAuthSession({
105
99
  user,
106
100
  authSession,