@defra/lis-hubs-infra-access 0.27.0 → 0.28.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
|
@@ -115,6 +115,7 @@ await server.register(
|
|
|
115
115
|
completeAuthorizationCodeGrant,
|
|
116
116
|
buildLogoutUrl,
|
|
117
117
|
loginRoutes,
|
|
118
|
+
accessDeniedPath: '/auth/access-denied', // optional
|
|
118
119
|
authorize: async (user, request) => true // optional
|
|
119
120
|
})
|
|
120
121
|
)
|
|
@@ -128,6 +129,13 @@ user on a required route is redirected to the first login route with
|
|
|
128
129
|
Credentials are `{ user, authorizedSpecies }`. The optional `authorize`
|
|
129
130
|
predicate runs for authenticated requests only; `false` gives a 403.
|
|
130
131
|
|
|
132
|
+
`resolveAuthSession` can refuse a login (e.g. a user not on an allow-list) by
|
|
133
|
+
returning `{ denied: true }`: the callback then sets no session or JWT and
|
|
134
|
+
signs the user out of the identity provider, which returns them to
|
|
135
|
+
`accessDeniedPath` (or the hub origin when that isn't set). The hub registers
|
|
136
|
+
the `accessDeniedPath` route itself, and the provider must accept it as a
|
|
137
|
+
post-logout redirect URI.
|
|
138
|
+
|
|
131
139
|
### Testing routes
|
|
132
140
|
|
|
133
141
|
Route tests need neither cookies nor service tokens; inject credentials:
|
package/package.json
CHANGED
|
@@ -76,7 +76,9 @@ function createCallbackController({
|
|
|
76
76
|
getHubJwtCookieName,
|
|
77
77
|
completeAuthorizationCodeGrant,
|
|
78
78
|
resolveAuthSession,
|
|
79
|
-
|
|
79
|
+
buildLogoutUrl,
|
|
80
|
+
loginPath,
|
|
81
|
+
accessDeniedPath
|
|
80
82
|
}) {
|
|
81
83
|
return {
|
|
82
84
|
options: {
|
|
@@ -103,6 +105,23 @@ function createCallbackController({
|
|
|
103
105
|
authSession,
|
|
104
106
|
accessToken
|
|
105
107
|
})
|
|
108
|
+
|
|
109
|
+
// The hub refused this identity (e.g. not on an allow-list): no session
|
|
110
|
+
// or JWT is issued, any earlier one is dropped, and the user is signed
|
|
111
|
+
// out of the identity provider so the next login asks for credentials.
|
|
112
|
+
if (authorization?.denied) {
|
|
113
|
+
clearHubAuthSession(request)
|
|
114
|
+
|
|
115
|
+
const logoutUrl = await buildLogoutUrl(request, {
|
|
116
|
+
authSession,
|
|
117
|
+
returnPath: accessDeniedPath
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
return h
|
|
121
|
+
.redirect(logoutUrl)
|
|
122
|
+
.unstate(getHubJwtCookieName(), getCookieOptions())
|
|
123
|
+
}
|
|
124
|
+
|
|
106
125
|
const enrichedAuthSession = {
|
|
107
126
|
...authSession,
|
|
108
127
|
...authorization
|
|
@@ -170,7 +189,8 @@ function createHubAuthRegistration(options) {
|
|
|
170
189
|
buildAuthorizationUrl,
|
|
171
190
|
completeAuthorizationCodeGrant,
|
|
172
191
|
buildLogoutUrl,
|
|
173
|
-
loginRoutes
|
|
192
|
+
loginRoutes,
|
|
193
|
+
accessDeniedPath
|
|
174
194
|
} = options
|
|
175
195
|
const callbackController = createCallbackController({
|
|
176
196
|
getCookieOptions,
|
|
@@ -178,7 +198,9 @@ function createHubAuthRegistration(options) {
|
|
|
178
198
|
getHubJwtCookieName,
|
|
179
199
|
completeAuthorizationCodeGrant,
|
|
180
200
|
resolveAuthSession,
|
|
181
|
-
|
|
201
|
+
buildLogoutUrl,
|
|
202
|
+
loginPath: loginRoutes[0].path,
|
|
203
|
+
accessDeniedPath
|
|
182
204
|
})
|
|
183
205
|
const logoutController = createLogoutController({
|
|
184
206
|
getCookieOptions,
|
|
@@ -273,6 +295,7 @@ function createHubSessionScheme(loginPath) {
|
|
|
273
295
|
* @param {Function} options.completeAuthorizationCodeGrant - Function to complete OIDC authorization code flow.
|
|
274
296
|
* @param {Function} options.buildLogoutUrl - Function to build logout URL.
|
|
275
297
|
* @param {Array<{path: string, providerId: string|Function}>} options.loginRoutes - Login route configurations; the first is where signed-out users are sent.
|
|
298
|
+
* @param {string} [options.accessDeniedPath] - Where the identity provider returns a user after `resolveAuthSession` refuses them with `{ denied: true }` (they are signed out of the provider first); defaults to the hub origin. The hub registers this route itself.
|
|
276
299
|
* @param {(user: object, request: object) => boolean | Promise<boolean>} [options.authorize] - Optional hub-wide rule for authenticated requests; `false` gives a 403.
|
|
277
300
|
* @returns {{plugin: {name: string, register: Function}}} Hapi plugin object with registration function.
|
|
278
301
|
*/
|
|
@@ -295,13 +295,28 @@ function createLogoutUrlBuilder({
|
|
|
295
295
|
getOidcMetadata,
|
|
296
296
|
getHubOrigin
|
|
297
297
|
}) {
|
|
298
|
-
|
|
299
|
-
|
|
298
|
+
/**
|
|
299
|
+
* @param {object} request - The Hapi request.
|
|
300
|
+
* @param {object} [options]
|
|
301
|
+
* @param {object} [options.authSession] - Session to sign out instead of the stored hub session (e.g. a refused login that was never stored).
|
|
302
|
+
* @param {string} [options.returnPath] - Hub path the provider returns to after sign-out; defaults to the hub origin.
|
|
303
|
+
* @returns {Promise<string>} The provider's end-session URL.
|
|
304
|
+
*/
|
|
305
|
+
return async function buildLogoutUrl(
|
|
306
|
+
request,
|
|
307
|
+
{ authSession = getHubAuthSession(request), returnPath } = {}
|
|
308
|
+
) {
|
|
300
309
|
const providerId = authSession?.authProvider ?? getPrimaryProviderId?.()
|
|
301
310
|
const metadata = await getOidcMetadata(providerId)
|
|
302
311
|
const logoutUrl = new URL(metadata.end_session_endpoint)
|
|
312
|
+
const postLogoutRedirectUri = returnPath
|
|
313
|
+
? new URL(returnPath, getHubOrigin()).toString()
|
|
314
|
+
: getHubOrigin()
|
|
303
315
|
|
|
304
|
-
logoutUrl.searchParams.set(
|
|
316
|
+
logoutUrl.searchParams.set(
|
|
317
|
+
'post_logout_redirect_uri',
|
|
318
|
+
postLogoutRedirectUri
|
|
319
|
+
)
|
|
305
320
|
|
|
306
321
|
if (authSession?.idToken) {
|
|
307
322
|
logoutUrl.searchParams.set('id_token_hint', authSession.idToken)
|
|
@@ -75,6 +75,6 @@ export function setHubAuthSession(request, authSession) {
|
|
|
75
75
|
* @param {object} request - The Hapi request object with yar session support.
|
|
76
76
|
*/
|
|
77
77
|
export function clearHubAuthSession(request) {
|
|
78
|
-
request
|
|
78
|
+
request?.yar?.clear?.(authSessionKey)
|
|
79
79
|
clearHubAuthFlow(request)
|
|
80
80
|
}
|