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