@depup/hapi 18.1.0-depup.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/.npmignore +4 -0
- package/LICENSE +26 -0
- package/README.md +47 -0
- package/lib/auth.js +545 -0
- package/lib/compression.js +107 -0
- package/lib/config.js +436 -0
- package/lib/core.js +680 -0
- package/lib/cors.js +207 -0
- package/lib/ext.js +94 -0
- package/lib/handler.js +165 -0
- package/lib/headers.js +200 -0
- package/lib/index.js +11 -0
- package/lib/methods.js +123 -0
- package/lib/request.js +623 -0
- package/lib/response.js +730 -0
- package/lib/route.js +519 -0
- package/lib/security.js +84 -0
- package/lib/server.js +557 -0
- package/lib/streams.js +37 -0
- package/lib/toolkit.js +201 -0
- package/lib/transmit.js +369 -0
- package/lib/validation.js +198 -0
- package/npm-shrinkwrap.json +2456 -0
- package/package.json +132 -0
package/.npmignore
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Copyright (c) 2011-2019, Project contributors
|
|
2
|
+
Copyright (c) 2011-2014, Walmart
|
|
3
|
+
Copyright (c) 2011, Yahoo Inc.
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
* Redistributions of source code must retain the above copyright
|
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
|
10
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
11
|
+
notice, this list of conditions and the following disclaimer in the
|
|
12
|
+
documentation and/or other materials provided with the distribution.
|
|
13
|
+
* The names of any contributors may not be used to endorse or promote
|
|
14
|
+
products derived from this software without specific prior written
|
|
15
|
+
permission.
|
|
16
|
+
|
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
18
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
19
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
20
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
|
|
21
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
22
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
23
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
24
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @depup/hapi
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [hapi](https://www.npmjs.com/package/hapi)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/hapi
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [hapi](https://www.npmjs.com/package/hapi) @ 18.1.0 |
|
|
17
|
+
| Processed | 2026-03-17 |
|
|
18
|
+
| Smoke test | failed |
|
|
19
|
+
| Deps updated | 17 |
|
|
20
|
+
|
|
21
|
+
## Dependency Changes
|
|
22
|
+
|
|
23
|
+
| Dependency | From | To |
|
|
24
|
+
|------------|------|-----|
|
|
25
|
+
| accept | 3.x.x | ^3.1.3 |
|
|
26
|
+
| ammo | 3.x.x | ^3.0.3 |
|
|
27
|
+
| boom | 7.x.x | ^7.3.0 |
|
|
28
|
+
| bounce | 1.x.x | ^1.2.3 |
|
|
29
|
+
| call | 5.x.x | ^5.0.3 |
|
|
30
|
+
| catbox | 10.x.x | ^10.0.6 |
|
|
31
|
+
| catbox-memory | 4.x.x | ^4.0.1 |
|
|
32
|
+
| heavy | 6.x.x | ^6.1.2 |
|
|
33
|
+
| hoek | 6.x.x | ^6.1.3 |
|
|
34
|
+
| joi | 14.x.x | ^18.0.2 |
|
|
35
|
+
| mimos | 4.x.x | ^4.0.2 |
|
|
36
|
+
| podium | 3.x.x | ^3.2.0 |
|
|
37
|
+
| shot | 4.x.x | ^4.0.7 |
|
|
38
|
+
| statehood | 6.x.x | ^6.0.9 |
|
|
39
|
+
| subtext | 6.x.x | ^6.0.12 |
|
|
40
|
+
| teamwork | 3.x.x | ^3.2.0 |
|
|
41
|
+
| topo | 3.x.x | ^3.0.3 |
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/hapi
|
|
46
|
+
|
|
47
|
+
License inherited from the original package.
|
package/lib/auth.js
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Boom = require('boom');
|
|
4
|
+
const Bounce = require('bounce');
|
|
5
|
+
const Hoek = require('hoek');
|
|
6
|
+
|
|
7
|
+
const Config = require('./config');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const internals = {
|
|
11
|
+
missing: Symbol('missing')
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
exports = module.exports = internals.Auth = class {
|
|
16
|
+
|
|
17
|
+
constructor(core) {
|
|
18
|
+
|
|
19
|
+
this._core = core;
|
|
20
|
+
this._schemes = {};
|
|
21
|
+
this._strategies = {};
|
|
22
|
+
this.settings = {
|
|
23
|
+
default: null // Strategy used as default if route has no auth settings
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
this.api = {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
scheme(name, scheme) {
|
|
30
|
+
|
|
31
|
+
Hoek.assert(name, 'Authentication scheme must have a name');
|
|
32
|
+
Hoek.assert(!this._schemes[name], 'Authentication scheme name already exists:', name);
|
|
33
|
+
Hoek.assert(typeof scheme === 'function', 'scheme must be a function:', name);
|
|
34
|
+
|
|
35
|
+
this._schemes[name] = scheme;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_strategy(server, name, scheme, options = {}) {
|
|
39
|
+
|
|
40
|
+
Hoek.assert(name, 'Authentication strategy must have a name');
|
|
41
|
+
Hoek.assert(typeof options === 'object', 'options must be an object');
|
|
42
|
+
Hoek.assert(!this._strategies[name], 'Authentication strategy name already exists');
|
|
43
|
+
Hoek.assert(scheme, 'Authentication strategy', name, 'missing scheme');
|
|
44
|
+
Hoek.assert(this._schemes[scheme], 'Authentication strategy', name, 'uses unknown scheme:', scheme);
|
|
45
|
+
|
|
46
|
+
server = server._clone();
|
|
47
|
+
const strategy = this._schemes[scheme](server, options);
|
|
48
|
+
|
|
49
|
+
Hoek.assert(strategy.authenticate, 'Invalid scheme:', name, 'missing authenticate() method');
|
|
50
|
+
Hoek.assert(typeof strategy.authenticate === 'function', 'Invalid scheme:', name, 'invalid authenticate() method');
|
|
51
|
+
Hoek.assert(!strategy.payload || typeof strategy.payload === 'function', 'Invalid scheme:', name, 'invalid payload() method');
|
|
52
|
+
Hoek.assert(!strategy.response || typeof strategy.response === 'function', 'Invalid scheme:', name, 'invalid response() method');
|
|
53
|
+
strategy.options = strategy.options || {};
|
|
54
|
+
Hoek.assert(strategy.payload || !strategy.options.payload, 'Cannot require payload validation without a payload method');
|
|
55
|
+
|
|
56
|
+
this._strategies[name] = {
|
|
57
|
+
methods: strategy,
|
|
58
|
+
realm: server.realm
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
if (strategy.api) {
|
|
62
|
+
this.api[name] = strategy.api;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
default(options) {
|
|
67
|
+
|
|
68
|
+
Hoek.assert(!this.settings.default, 'Cannot set default strategy more than once');
|
|
69
|
+
options = Config.apply('auth', options, 'default strategy');
|
|
70
|
+
|
|
71
|
+
this.settings.default = this._setupRoute(Hoek.clone(options)); // Prevent changes to options
|
|
72
|
+
|
|
73
|
+
const routes = this._core.router.table();
|
|
74
|
+
for (const route of routes) {
|
|
75
|
+
route.rebuild();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async test(name, request) {
|
|
80
|
+
|
|
81
|
+
Hoek.assert(name, 'Missing authentication strategy name');
|
|
82
|
+
const strategy = this._strategies[name];
|
|
83
|
+
Hoek.assert(strategy, 'Unknown authentication strategy:', name);
|
|
84
|
+
|
|
85
|
+
const bind = strategy.methods;
|
|
86
|
+
const realm = strategy.realm;
|
|
87
|
+
const response = await request._core.toolkit.execute(strategy.methods.authenticate, request, { bind, realm, auth: true });
|
|
88
|
+
|
|
89
|
+
if (!response.isAuth) {
|
|
90
|
+
throw response;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (response.error) {
|
|
94
|
+
throw response.error;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return response.data;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async verify(request) {
|
|
101
|
+
|
|
102
|
+
const auth = request.auth;
|
|
103
|
+
|
|
104
|
+
if (auth.error) {
|
|
105
|
+
throw auth.error;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!auth.isAuthenticated) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const strategy = this._strategies[auth.strategy];
|
|
113
|
+
Hoek.assert(strategy, 'Unknown authentication strategy:', auth.strategy);
|
|
114
|
+
|
|
115
|
+
if (!strategy.methods.verify) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const bind = strategy.methods;
|
|
120
|
+
await strategy.methods.verify.call(bind, auth);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static testAccess(request, route) {
|
|
124
|
+
|
|
125
|
+
const auth = request._core.auth;
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
return auth._access(request, route);
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
Bounce.rethrow(err, 'system');
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
_setupRoute(options, path) {
|
|
137
|
+
|
|
138
|
+
if (!options) {
|
|
139
|
+
return options; // Preserve the difference between undefined and false
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (typeof options === 'string') {
|
|
143
|
+
options = { strategies: [options] };
|
|
144
|
+
}
|
|
145
|
+
else if (options.strategy) {
|
|
146
|
+
options.strategies = [options.strategy];
|
|
147
|
+
delete options.strategy;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (path &&
|
|
151
|
+
!options.strategies) {
|
|
152
|
+
|
|
153
|
+
Hoek.assert(this.settings.default, 'Route missing authentication strategy and no default defined:', path);
|
|
154
|
+
options = Hoek.applyToDefaults(this.settings.default, options);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
path = path || 'default strategy';
|
|
158
|
+
Hoek.assert(options.strategies && options.strategies.length, 'Missing authentication strategy:', path);
|
|
159
|
+
|
|
160
|
+
options.mode = options.mode || 'required';
|
|
161
|
+
|
|
162
|
+
if (options.entity !== undefined || // Backwards compatibility with <= 11.x.x
|
|
163
|
+
options.scope !== undefined) {
|
|
164
|
+
|
|
165
|
+
options.access = [{ entity: options.entity, scope: options.scope }];
|
|
166
|
+
delete options.entity;
|
|
167
|
+
delete options.scope;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (options.access) {
|
|
171
|
+
for (const access of options.access) {
|
|
172
|
+
access.scope = internals.setupScope(access);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (options.payload === true) {
|
|
177
|
+
options.payload = 'required';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
let hasAuthenticatePayload = false;
|
|
181
|
+
for (const name of options.strategies) {
|
|
182
|
+
const strategy = this._strategies[name];
|
|
183
|
+
Hoek.assert(strategy, 'Unknown authentication strategy', name, 'in', path);
|
|
184
|
+
|
|
185
|
+
Hoek.assert(strategy.methods.payload || options.payload !== 'required', 'Payload validation can only be required when all strategies support it in', path);
|
|
186
|
+
hasAuthenticatePayload = hasAuthenticatePayload || strategy.methods.payload;
|
|
187
|
+
Hoek.assert(!strategy.methods.options.payload || options.payload === undefined || options.payload === 'required', 'Cannot set authentication payload to', options.payload, 'when a strategy requires payload validation in', path);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
Hoek.assert(!options.payload || hasAuthenticatePayload, 'Payload authentication requires at least one strategy with payload support in', path);
|
|
191
|
+
|
|
192
|
+
return options;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
lookup(route) {
|
|
196
|
+
|
|
197
|
+
if (route.settings.auth === false) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return route.settings.auth || this.settings.default;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
_enabled(route, type) {
|
|
205
|
+
|
|
206
|
+
const config = this.lookup(route);
|
|
207
|
+
if (!config) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (type === 'authenticate') {
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (type === 'access') {
|
|
216
|
+
return !!config.access;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
for (const name of config.strategies) {
|
|
220
|
+
const strategy = this._strategies[name];
|
|
221
|
+
if (strategy.methods[type]) {
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
static authenticate(request) {
|
|
230
|
+
|
|
231
|
+
const auth = request._core.auth;
|
|
232
|
+
return auth._authenticate(request);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async _authenticate(request) {
|
|
236
|
+
|
|
237
|
+
const config = this.lookup(request.route);
|
|
238
|
+
|
|
239
|
+
const errors = [];
|
|
240
|
+
request.auth.mode = config.mode;
|
|
241
|
+
|
|
242
|
+
// Injection bypass
|
|
243
|
+
|
|
244
|
+
if (request.auth.credentials) {
|
|
245
|
+
internals.validate(null, { credentials: request.auth.credentials, artifacts: request.auth.artifacts }, request.auth.strategy, config, request, errors);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Try each strategy
|
|
250
|
+
|
|
251
|
+
for (const name of config.strategies) {
|
|
252
|
+
const strategy = this._strategies[name];
|
|
253
|
+
|
|
254
|
+
const bind = strategy.methods;
|
|
255
|
+
const realm = strategy.realm;
|
|
256
|
+
const response = await request._core.toolkit.execute(strategy.methods.authenticate, request, { bind, realm, auth: true });
|
|
257
|
+
|
|
258
|
+
const message = (response.isAuth ? internals.validate(response.error, response.data, name, config, request, errors) : internals.validate(response, null, name, config, request, errors));
|
|
259
|
+
if (!message) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (message !== internals.missing) {
|
|
264
|
+
return message;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// No more strategies
|
|
269
|
+
|
|
270
|
+
const err = Boom.unauthorized('Missing authentication', errors);
|
|
271
|
+
if (config.mode === 'required') {
|
|
272
|
+
throw err;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
request.auth.isAuthenticated = false;
|
|
276
|
+
request.auth.credentials = null;
|
|
277
|
+
request.auth.error = err;
|
|
278
|
+
request._log(['auth', 'unauthenticated']);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
static access(request) {
|
|
282
|
+
|
|
283
|
+
const auth = request._core.auth;
|
|
284
|
+
request.auth.isAuthorized = auth._access(request);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
_access(request, route) {
|
|
288
|
+
|
|
289
|
+
const config = this.lookup(route || request.route);
|
|
290
|
+
if (!config ||
|
|
291
|
+
!config.access) {
|
|
292
|
+
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const credentials = request.auth.credentials;
|
|
297
|
+
if (!credentials) {
|
|
298
|
+
if (config.mode !== 'required') {
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
throw Boom.forbidden('Request is unauthenticated');
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const requestEntity = (credentials.user ? 'user' : 'app');
|
|
306
|
+
|
|
307
|
+
const scopeErrors = [];
|
|
308
|
+
for (const access of config.access) {
|
|
309
|
+
|
|
310
|
+
// Check entity
|
|
311
|
+
|
|
312
|
+
const entity = access.entity;
|
|
313
|
+
if (entity &&
|
|
314
|
+
entity !== 'any' &&
|
|
315
|
+
entity !== requestEntity) {
|
|
316
|
+
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Check scope
|
|
321
|
+
|
|
322
|
+
let scope = access.scope;
|
|
323
|
+
if (scope) {
|
|
324
|
+
if (!credentials.scope) {
|
|
325
|
+
scopeErrors.push(scope);
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
scope = internals.expandScope(request, scope);
|
|
330
|
+
if (!internals.validateScope(credentials, scope, 'required') ||
|
|
331
|
+
!internals.validateScope(credentials, scope, 'selection') ||
|
|
332
|
+
!internals.validateScope(credentials, scope, 'forbidden')) {
|
|
333
|
+
|
|
334
|
+
scopeErrors.push(scope);
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Scope error
|
|
343
|
+
|
|
344
|
+
if (scopeErrors.length) {
|
|
345
|
+
request._log(['auth', 'scope', 'error']);
|
|
346
|
+
throw Boom.forbidden('Insufficient scope', { got: credentials.scope, need: scopeErrors });
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Entity error
|
|
350
|
+
|
|
351
|
+
if (requestEntity === 'app') {
|
|
352
|
+
request._log(['auth', 'entity', 'user', 'error']);
|
|
353
|
+
throw Boom.forbidden('Application credentials cannot be used on a user endpoint');
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
request._log(['auth', 'entity', 'app', 'error']);
|
|
357
|
+
throw Boom.forbidden('User credentials cannot be used on an application endpoint');
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
static async payload(request) {
|
|
361
|
+
|
|
362
|
+
if (!request.auth.isAuthenticated) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const auth = request._core.auth;
|
|
367
|
+
const strategy = auth._strategies[request.auth.strategy];
|
|
368
|
+
Hoek.assert(strategy, 'Unknown authentication strategy:', request.auth.strategy);
|
|
369
|
+
|
|
370
|
+
if (!strategy.methods.payload) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const config = auth.lookup(request.route);
|
|
375
|
+
const setting = config.payload || (strategy.methods.options.payload ? 'required' : false);
|
|
376
|
+
if (!setting) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const bind = strategy.methods;
|
|
381
|
+
const realm = strategy.realm;
|
|
382
|
+
const response = await request._core.toolkit.execute(strategy.methods.payload, request, { bind, realm });
|
|
383
|
+
|
|
384
|
+
if (response.isBoom &&
|
|
385
|
+
response.isMissing) {
|
|
386
|
+
|
|
387
|
+
return (setting === 'optional' ? undefined : Boom.unauthorized('Missing payload authentication'));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return response;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
static async response(request) {
|
|
394
|
+
|
|
395
|
+
const auth = request._core.auth;
|
|
396
|
+
if (!request.auth.isAuthenticated) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const strategy = auth._strategies[request.auth.strategy];
|
|
401
|
+
Hoek.assert(strategy, 'Unknown authentication strategy:', request.auth.strategy);
|
|
402
|
+
|
|
403
|
+
if (!strategy.methods.response) {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const bind = strategy.methods;
|
|
408
|
+
const realm = strategy.realm;
|
|
409
|
+
const error = await request._core.toolkit.execute(strategy.methods.response, request, { bind, realm, continue: 'undefined' });
|
|
410
|
+
if (error) {
|
|
411
|
+
throw error;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
internals.setupScope = function (access) {
|
|
418
|
+
|
|
419
|
+
if (!access.scope) {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const scope = {};
|
|
424
|
+
for (const value of access.scope) {
|
|
425
|
+
const prefix = value[0];
|
|
426
|
+
const type = (prefix === '+' ? 'required' : (prefix === '!' ? 'forbidden' : 'selection'));
|
|
427
|
+
const clean = (type === 'selection' ? value : value.slice(1));
|
|
428
|
+
scope[type] = scope[type] || [];
|
|
429
|
+
scope[type].push(clean);
|
|
430
|
+
|
|
431
|
+
if ((!scope._hasParameters || !scope._hasParameters[type]) &&
|
|
432
|
+
/{([^}]+)}/.test(clean)) {
|
|
433
|
+
|
|
434
|
+
scope._hasParameters = scope._hasParameters || {};
|
|
435
|
+
scope._hasParameters[type] = true;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return scope;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
internals.validate = function (err, result, name, config, request, errors) { // err can be Boom, Error, or a valid response object
|
|
444
|
+
|
|
445
|
+
result = result || {};
|
|
446
|
+
|
|
447
|
+
// Unauthenticated
|
|
448
|
+
|
|
449
|
+
if (err) {
|
|
450
|
+
if (err instanceof Error === false) {
|
|
451
|
+
request._log(['auth', 'unauthenticated', 'response', name], { statusCode: err.statusCode });
|
|
452
|
+
return err; // Non-error response
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (err.isMissing) {
|
|
456
|
+
|
|
457
|
+
// Try next strategy
|
|
458
|
+
|
|
459
|
+
request._log(['auth', 'unauthenticated', 'missing', name], err);
|
|
460
|
+
errors.push(err.output.headers['WWW-Authenticate']);
|
|
461
|
+
return internals.missing;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (config.mode === 'try') {
|
|
465
|
+
request.auth.isAuthenticated = false;
|
|
466
|
+
request.auth.strategy = name;
|
|
467
|
+
request.auth.credentials = result.credentials;
|
|
468
|
+
request.auth.artifacts = result.artifacts;
|
|
469
|
+
request.auth.error = err;
|
|
470
|
+
request._log(['auth', 'unauthenticated', 'try', name], err);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
request._log(['auth', 'unauthenticated', 'error', name], err);
|
|
475
|
+
throw err;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// Authenticated
|
|
479
|
+
|
|
480
|
+
const credentials = result.credentials;
|
|
481
|
+
request.auth.strategy = name;
|
|
482
|
+
request.auth.credentials = credentials;
|
|
483
|
+
request.auth.artifacts = result.artifacts;
|
|
484
|
+
request.auth.isAuthenticated = true;
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
internals.expandScope = function (request, scope) {
|
|
489
|
+
|
|
490
|
+
if (!scope._hasParameters) {
|
|
491
|
+
return scope;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const expanded = {
|
|
495
|
+
required: internals.expandScopeType(request, scope, 'required'),
|
|
496
|
+
selection: internals.expandScopeType(request, scope, 'selection'),
|
|
497
|
+
forbidden: internals.expandScopeType(request, scope, 'forbidden')
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
return expanded;
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
internals.expandScopeType = function (request, scope, type) {
|
|
505
|
+
|
|
506
|
+
if (!scope._hasParameters[type]) {
|
|
507
|
+
return scope[type];
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const expanded = [];
|
|
511
|
+
const context = {
|
|
512
|
+
params: request.params,
|
|
513
|
+
query: request.query,
|
|
514
|
+
payload: request.payload,
|
|
515
|
+
credentials: request.auth.credentials
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
for (const template of scope[type]) {
|
|
519
|
+
expanded.push(Hoek.reachTemplate(context, template));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
return expanded;
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
internals.validateScope = function (credentials, scope, type) {
|
|
527
|
+
|
|
528
|
+
if (!scope[type]) {
|
|
529
|
+
return true;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const count = typeof credentials.scope === 'string' ?
|
|
533
|
+
(scope[type].indexOf(credentials.scope) !== -1 ? 1 : 0) :
|
|
534
|
+
Hoek.intersect(scope[type], credentials.scope).length;
|
|
535
|
+
|
|
536
|
+
if (type === 'forbidden') {
|
|
537
|
+
return count === 0;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (type === 'required') {
|
|
541
|
+
return count === scope.required.length;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
return !!count;
|
|
545
|
+
};
|