@defra/lis-hubs-infra-access 0.19.0 → 0.20.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/auth/plugin.js +19 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/lis-hubs-infra-access",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Shared hub access and capability resolution for the livestock platform.",
5
5
  "main": "src/index.js",
6
6
  "repository": {
@@ -2,6 +2,7 @@ import { logger } from '@defra/lis-hubs-infra-core'
2
2
 
3
3
  import {
4
4
  clearHubAuthSession,
5
+ getHubAuthFlow,
5
6
  getHubAuthSession,
6
7
  setHubAuthSession
7
8
  } from './session.js'
@@ -67,12 +68,19 @@ function createLoginController({
67
68
  }
68
69
  }
69
70
 
71
+ function isStaleAuthorizationResponse(request) {
72
+ const authFlow = getHubAuthFlow(request)
73
+
74
+ return !authFlow?.state || request.query?.state !== authFlow.state
75
+ }
76
+
70
77
  function createCallbackController({
71
78
  getCookieOptions,
72
79
  getHubJwtConfig,
73
80
  getHubJwtCookieName,
74
81
  completeAuthorizationCodeGrant,
75
- resolveAuthSession
82
+ resolveAuthSession,
83
+ loginPath
76
84
  }) {
77
85
  return {
78
86
  options: {
@@ -83,6 +91,14 @@ function createCallbackController({
83
91
  throw new Error(request.query?.error_description ?? request.query.error)
84
92
  }
85
93
 
94
+ // A replayed identity provider page (e.g. browser back button after
95
+ // signing in) returns a state the hub no longer holds. Restarting
96
+ // login sends an already-authenticated user straight on and gives
97
+ // anyone else a fresh flow, instead of failing the callback.
98
+ if (isStaleAuthorizationResponse(request)) {
99
+ return h.redirect(loginPath)
100
+ }
101
+
86
102
  const { user, authSession, accessToken, returnUrl } =
87
103
  await completeAuthorizationCodeGrant(request)
88
104
  const authorization = await resolveAuthSession({
@@ -172,7 +188,8 @@ export function createHubAuthPlugin({
172
188
  getHubJwtConfig,
173
189
  getHubJwtCookieName,
174
190
  completeAuthorizationCodeGrant,
175
- resolveAuthSession
191
+ resolveAuthSession,
192
+ loginPath: loginRoutes[0].path
176
193
  })
177
194
  const logoutController = createLogoutController({
178
195
  getCookieOptions,