@thzero/library_server_fastify 0.15.35 → 0.15.38

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/boot/index.js CHANGED
@@ -32,9 +32,15 @@ class FastifyBootMain extends BootMain {
32
32
 
33
33
  // return server;
34
34
  // };
35
+
36
+ let http2 = this._appConfig.get('http2', false);
37
+ http2 = http2 === 'true' ? true : false;
35
38
 
36
39
  // const fastify = Fastify({ serverFactory, logger: true });
37
- const fastify = Fastify({ logger: true });
40
+ const fastify = Fastify({
41
+ http2: http2,
42
+ logger: true
43
+ });
38
44
  const serverHttp = fastify.server;
39
45
 
40
46
  await fastify.register(fastifyRoutes);
@@ -249,10 +255,18 @@ class FastifyBootMain extends BootMain {
249
255
  const capitalize = (word) => {
250
256
  return word[0].toUpperCase() + word.slice(1).toLowerCase();
251
257
  };
252
- for (let [key, value] of this._initAuthentication(new Map()).entries())
253
- fastify.decorate('authentication' + capitalize(key), value);
254
- for (let [key, value] of this._initAuthorization(new Map()).entries())
255
- fastify.decorate('authorization' + capitalize(key), value);
258
+
259
+ let item;
260
+ for (let [key, value] of this._initAuthentication(new Map()).entries()) {
261
+ item = value.init(injector);
262
+ fastify.decorate('authentication' + capitalize(key), item.callback);
263
+ fastify.decorate('authenticationMiddleware' + capitalize(key), item.service);
264
+ }
265
+ for (let [key, value] of this._initAuthorization(new Map()).entries()) {
266
+ item = value.init(injector);
267
+ fastify.decorate('authorization' + capitalize(key), item.callback);
268
+ fastify.decorate('authorizationMiddleware' + capitalize(key), item.service);
269
+ }
256
270
 
257
271
  this._initPostAuth(fastify);
258
272
 
@@ -293,21 +307,25 @@ class FastifyBootMain extends BootMain {
293
307
  usageMetrics: this.usageMetricsServiceI
294
308
  });
295
309
 
296
- return { app: fastify, server: serverHttp, listen: fastify.listen };
310
+ return {
311
+ app: fastify,
312
+ server: serverHttp,
313
+ listen: fastify.listen
314
+ };
297
315
  }
298
316
 
299
317
  _initAuthentication(map) {
300
- map.set('default', authenticationDefault);
318
+ map.set('default', new authenticationDefault());
301
319
  return map;
302
320
  }
303
321
 
304
322
  _initAuthorization(map) {
305
- map.set('default', authorizationDefault);
323
+ map.set('default', new authorizationDefault());
306
324
  return map;
307
325
  }
308
326
 
309
- _initAppListen(app, server, port, err) {
310
- app.listen(port, err);
327
+ _initAppListen(app, server, address, port, err) {
328
+ app.listen(port, address, err);
311
329
  }
312
330
 
313
331
  async _initAppPost(app, args) {
@@ -1,78 +1,94 @@
1
1
  import LibraryConstants from '@thzero/library_server/constants';
2
2
  import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
3
3
 
4
- import injector from '@thzero/library_common/utility/injector';
5
-
6
4
  const separator = ': ';
7
5
 
8
- function getAuthToken(request, logger) {
9
- if (!request)
10
- return null;
11
- if (!logger)
12
- return null;
13
-
14
- const token = request.headers[LibraryConstants.Headers.AuthKeys.AUTH];
15
- logger.debug('middleware', 'getAuthToken', 'token', token, request.correlationId);
16
- const split = token.split(LibraryConstants.Headers.AuthKeys.AUTH_BEARER + separator);
17
- logger.debug('middleware', 'getAuthToken', 'split', split, request.correlationId);
18
- logger.debug('middleware', 'getAuthToken', 'split.length', split.length, request.correlationId);
19
- if (split.length > 1)
20
- return split[1];
21
-
22
- logger.debug('middleware', 'getAuthToken', 'fail', null, request.correlationId);
23
- return null;
24
- }
6
+ class DefaultAuthenticationMiddleware {
7
+ constructor() {
8
+ this._serviceAuth = null;
9
+ this._serviceLogger = null;
10
+ }
25
11
 
26
- export default async (request, reply, done, options) => {
27
- const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
12
+ init(injector) {
13
+ this._serviceAuth = injector.getService(LibraryConstants.InjectorKeys.SERVICE_AUTH);
14
+ this._serviceLogger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
15
+ this._serviceUsageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
28
16
 
29
- const required = options && (options.required !== null) && (options.required !== undefined) ? options.required : true;
17
+ return {
18
+ callback: async (request, reply, done, options) => {
19
+ const middleware = request.server.authenticationMiddlewareDefault;
20
+ await middleware.authenticate(request, reply, done, options)
21
+ },
22
+ service: this
23
+ }
24
+ }
30
25
 
31
- const token = getAuthToken(request, logger);
32
- logger.debug('middleware', 'authentication', 'token', token, request.correlationId);
33
- logger.debug('middleware', 'authentication', 'required', required, request.correlationId);
34
- const valid = ((required && !String.isNullOrEmpty(token)) || !required);
35
- logger.debug('middleware', 'authentication', 'valid', valid, request.correlationId);
36
- if (valid) {
37
- if (!String.isNullOrEmpty(token)) {
38
- const service = injector.getService(LibraryConstants.InjectorKeys.SERVICE_AUTH);
39
- const results = await service.verifyToken(request.correlationId, token);
40
- logger.debug('middleware', 'authentication', 'results', results, request.correlationId);
41
- if (!results || !results.success) {
42
- logger.warn('middleware', 'authentication', 'Unauthenticated... invalid token', null, request.correlationId);
43
- ctx.throw(401);
44
- return;
26
+ async authenticate(request, reply, done, options) {
27
+ const required = options && (options.required !== null) && (options.required !== undefined) ? options.required : true;
28
+
29
+ const token = this._getAuthToken(request);
30
+ this._serviceLogger.debug('middleware', 'authentication', 'token', token, request.correlationId);
31
+ this._serviceLogger.debug('middleware', 'authentication', 'required', required, request.correlationId);
32
+ const valid = ((required && !String.isNullOrEmpty(token)) || !required);
33
+ this._serviceLogger.debug('middleware', 'authentication', 'valid', valid, request.correlationId);
34
+ if (valid) {
35
+ if (!String.isNullOrEmpty(token)) {
36
+ const results = await this._serviceAuth.verifyToken(request.correlationId, token);
37
+ this._serviceLogger.debug('middleware', 'authentication', 'results', results, request.correlationId);
38
+ if (!results || !results.success) {
39
+ this._serviceLogger.warn('middleware', 'authentication', 'Unauthenticated... invalid token', null, request.correlationId);
40
+ ctx.throw(401);
41
+ return;
42
+ }
43
+
44
+ request.token = token;
45
+ request.user = results.user;
46
+ request.claims = results.claims;
45
47
  }
46
-
47
- request.token = token;
48
- request.user = results.user;
49
- request.claims = results.claims;
48
+
49
+ // done(); // not for async
50
+ return;
50
51
  }
51
-
52
- // done(); // not for async
53
- return;
52
+
53
+ (async () => {
54
+ const usageMetrics = {
55
+ url: request.routerPath,
56
+ correlationId: request.correlationId,
57
+ href: request.url,
58
+ headers: request.headers,
59
+ host: request.hostname,
60
+ hostname: request.hostname,
61
+ querystring: request.query,
62
+ token: request.token
63
+ };
64
+ await this._serviceUsageMetrics.register(usageMetrics).catch((err) => {
65
+ this._serviceLogger.error('middleware', 'authentication', err, null, request.correlationId);
66
+ });
67
+ })();
68
+
69
+ this._serviceLogger.warn('middleware', 'authentication', 'Unauthorized... authentication unknown', null, request.correlationId);
70
+ // reply.code(401);
71
+ // done(new Error('Unauthorized... authentication unknown')); // not for async
72
+ throw new Error('Unauthorized... authentication unknown');
54
73
  }
55
74
 
56
- (async () => {
57
- const usageMetrics = {
58
- url: request.routerPath,
59
- correlationId: request.correlationId,
60
- href: request.url,
61
- headers: request.headers,
62
- host: request.hostname,
63
- hostname: request.hostname,
64
- querystring: request.query,
65
- token: request.token
66
- };
67
- const serviceUsageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
68
- await serviceUsageMetrics.register(usageMetrics).catch((err) => {
69
- // const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
70
- logger.error('middleware', 'authentication', err, null, request.correlationId);
71
- });
72
- })();
73
-
74
- logger.warn('middleware', 'authentication', 'Unauthorized... authentication unknown', null, request.correlationId);
75
- // reply.code(401);
76
- // done(new Error('Unauthorized... authentication unknown')); // not for async
77
- throw new Error('Unauthorized... authentication unknown');
75
+ _getAuthToken(request) {
76
+ if (!request)
77
+ return null;
78
+
79
+ const token = request.headers[LibraryConstants.Headers.AuthKeys.AUTH];
80
+ if (token !== null && token !== undefined) {
81
+ this._serviceLogger.debug('middleware', 'getAuthToken', 'token', token, request.correlationId);
82
+ const split = token.split(LibraryConstants.Headers.AuthKeys.AUTH_BEARER + separator);
83
+ this._serviceLogger.debug('middleware', 'getAuthToken', 'split', split, request.correlationId);
84
+ this._serviceLogger.debug('middleware', 'getAuthToken', 'split.length', split.length, request.correlationId);
85
+ if (split.length > 1)
86
+ return split[1];
87
+ }
88
+
89
+ this._serviceLogger.debug('middleware', 'getAuthToken', 'fail', null, request.correlationId);
90
+ return null;
91
+ }
78
92
  }
93
+
94
+ export default DefaultAuthenticationMiddleware;
@@ -1,8 +1,6 @@
1
1
  import LibraryConstants from '@thzero/library_server/constants';
2
2
  import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
3
3
 
4
- import injector from '@thzero/library_common/utility/injector';
5
-
6
4
  // require('../utility/string.cjs');
7
5
  String.isNullOrEmpty = function(value) {
8
6
  //return !(typeof value === 'string' && value.length > 0)
@@ -22,93 +20,196 @@ String.trim = function(value) {
22
20
  const logicalAnd = 'and';
23
21
  const logicalOr = 'or';
24
22
 
25
- const authorizationCheckClaims = async (request, success, logical, security, logger) => {
26
- if (!request)
27
- return false;
28
- if (!(request.claims && Array.isArray(request.claims)))
29
- return false;
30
-
31
- let result;
32
- let roleAct;
33
- let roleObj;
34
- let roleParts;
35
- for (const claim of request.claims) {
36
- logger.debug('middleware', 'authorization', 'authorization.claim', claim, request.correlationId);
37
-
38
- for (const role of request.roles) {
39
- logger.debug('middleware', 'authorization', 'role', role, request.correlationId);
40
-
41
- roleParts = role.split('.');
42
- if (roleParts && roleParts.length < 1)
43
- success = false;
44
-
45
- roleObj = roleParts[0];
46
- roleAct = roleParts.length >= 2 ? roleParts[1] : null
47
-
48
- result = await security.validate(claim, null, roleObj, roleAct);
49
- logger.debug('middleware', 'authorization', 'result', result, request.correlationId);
50
- if (logical === logicalOr)
51
- success = success || result;
52
- else
53
- success = success && result;
23
+ class DefaultAuthenticationMiddleware {
24
+ constructor() {
25
+ this._serviceConfig = null;
26
+ this._serviceLogger = null;
27
+ this._serviceSecurity = null;
28
+ }
29
+
30
+ init(injector) {
31
+ this._serviceConfig = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG);
32
+ this._serviceLogger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
33
+ this._serviceSecurity = injector.getService(LibraryConstants.InjectorKeys.SERVICE_SECURITY);
34
+ this._serviceUsageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
35
+
36
+ return {
37
+ callback: async (request, reply, done, options) => {
38
+ const middleware = request.server.authorizationMiddlewareDefault;
39
+ await middleware.authorization(request, reply, done, options)
40
+ },
41
+ service: this
54
42
  }
55
43
  }
56
44
 
57
- return success;
58
- }
45
+ async authorization(request, reply, done, options) {
46
+ let logical = logicalOr;
47
+ let roles = [];
48
+ if (options) {
49
+ logical = options.logical;
50
+ if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
51
+ logical = logicalOr;
52
+
53
+ if (options.roles && Array.isArray(options.roles) && (options.roles.length > 0))
54
+ roles = options.roles;
55
+ }
56
+
57
+ // this._serviceLogger.debug('token', request.token);
58
+ this._serviceLogger.debug('middleware', 'authorization', 'user', request.user, request.correlationId);
59
+ this._serviceLogger.debug('middleware', 'authorization', 'claims', request.claims, request.correlationId);
60
+ this._serviceLogger.debug('middleware', 'authorization', 'roles1', roles, request.correlationId);
61
+ request.roles = [];
62
+ if (roles) {
63
+ // this._serviceLogger.debug('authorization.roles1', roles);
64
+ // this._serviceLogger.debug('authorization.roles1', (typeof roles));
65
+ // this._serviceLogger.debug('authorization.roles1', Array.isArray(roles));
66
+ // this._serviceLogger.debug('authorization.roles1', ((typeof(roles) === 'string') || (roles instanceof String)));
67
+ // if (Array.isArray(roles)) {
68
+ // // this._serviceLogger.debug('authorization.roles1a', roles);
69
+ // request.roles = roles;
70
+ // }
71
+ // else if ((typeof(roles) === 'string') || (roles instanceof String)) {
72
+ // // this._serviceLogger.debug('authorization.roles1b', roles);
73
+ // request.roles = roles.split(',');
74
+ // request.roles.map(item => item ? item.trim() : item);
75
+ // }
76
+ this._initalizeRoles(request, roles);
77
+ }
78
+ this._serviceLogger.debug('middleware', 'authorization', 'roles2', request.roles, request.correlationId);
79
+
80
+ let success = false; //(logical === logicalOr ? false : true);
81
+ if (request.roles && Array.isArray(request.roles) && (request.roles.length > 0)) {
82
+ const auth = this._serviceConfig.get('auth');
83
+ if (auth) {
84
+ this._serviceLogger.debug('middleware', 'authorization', 'auth.claims', auth.claims, request.correlationId);
85
+ this._serviceLogger.debug('middleware', 'authorization', 'auth.claims.check', auth.claims.check, request.correlationId);
86
+ }
87
+ if (auth && auth.claims && auth.claims.check)
88
+ success = await this._authorizationCheckClaims(request, (logical === logicalOr ? false : true), logical);
89
+
90
+ if (!success)
91
+ success = await this._authorizationCheckRoles(request, (logical === logicalOr ? false : true), logical);
92
+ }
93
+
94
+ this._serviceLogger.debug('middleware', 'authorization', 'success', null, request.success, request.correlationId);
95
+ if (success) {
96
+ // done(); // not for async
97
+ return;
98
+ }
99
+
100
+ (async () => {
101
+ const usageMetrics = {
102
+ url: request.routerPath,
103
+ correlationId: request.correlationId,
104
+ href: request.url,
105
+ headers: request.headers,
106
+ host: request.hostname,
107
+ hostname: request.hostname,
108
+ querystring: request.query,
109
+ token: request.token
110
+ };
111
+ await this._serviceUsageMetrics.register(usageMetrics).catch((err) => {
112
+ this._serviceLogger.error('middleware', 'authorization', err, null, request.correlationId);
113
+ });
114
+ })();
115
+
116
+ this._serviceLogger.warn('middleware', 'authorization', 'Unauthorized... authorization unknown', null, request.correlationId);
117
+ // reply.code(401);
118
+ // done(new Error('Unauthorized... authentication unknown')); // not for async
119
+ throw new Error('Unauthorized... authentication unknown');
120
+ }
121
+
122
+ async _authorizationCheckClaims (request, success, logical) {
123
+ if (!request)
124
+ return false;
125
+ if (!(request.claims && Array.isArray(request.claims)))
126
+ return false;
127
+
128
+ let result;
129
+ let roleAct;
130
+ let roleObj;
131
+ let roleParts;
132
+ for (const claim of request.claims) {
133
+ this._serviceLogger.debug('middleware', 'authorization', 'authorization.claim', claim, request.correlationId);
134
+
135
+ for (const role of request.roles) {
136
+ this._serviceLogger.debug('middleware', 'authorization', 'role', role, request.correlationId);
137
+
138
+ roleParts = role.split('.');
139
+ if (roleParts && roleParts.length < 1)
140
+ success = false;
141
+
142
+ roleObj = roleParts[0];
143
+ roleAct = roleParts.length >= 2 ? roleParts[1] : null
144
+
145
+ result = await this._serviceSecurity.validate(claim, null, roleObj, roleAct);
146
+ this._serviceLogger.debug('middleware', 'authorization', 'result', result, request.correlationId);
147
+ if (logical === logicalOr)
148
+ success = success || result;
149
+ else
150
+ success = success && result;
151
+ }
152
+ }
59
153
 
60
- const authorizationCheckRoles = async (request, success, logical, security, logger) => {
61
- if (!request)
62
- return false;
154
+ return success;
155
+ }
156
+
157
+ async _authorizationCheckRoles (request, success, logical) {
158
+ if (!request)
159
+ return false;
63
160
 
64
- logger.debug('middleware', 'authorizationCheckRoles', 'user', request.user, request.correlationId);
65
- if (!(request.user && request.user.roles && Array.isArray(request.user.roles)))
66
- return false;
161
+ this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'user', request.user, request.correlationId);
162
+ if (!(request.user && request.user.roles && Array.isArray(request.user.roles)))
163
+ return false;
67
164
 
68
- logger.debug('middleware', 'authorizationCheckRoles', 'logical', logical, request.correlationId);
165
+ this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'logical', logical, request.correlationId);
69
166
 
70
- let result;
71
- let roleAct;
72
- let roleObj;
73
- let roleParts;
74
- for (const userRole of request.user.roles) {
75
- logger.debug('middleware', 'authorizationCheckRoles', 'userRole', userRole, request.correlationId);
167
+ let result;
168
+ let roleAct;
169
+ let roleObj;
170
+ let roleParts;
171
+ for (const userRole of request.user.roles) {
172
+ this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'userRole', userRole, request.correlationId);
76
173
 
77
- for (const role of request.roles) {
78
- logger.debug('middleware', 'authorizationCheckRoles', 'role', role, request.correlationId);
174
+ for (const role of request.roles) {
175
+ this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'role', role, request.correlationId);
79
176
 
80
- roleParts = role.split('.');
81
- if (roleParts && roleParts.length < 1)
82
- success = false;
177
+ roleParts = role.split('.');
178
+ if (roleParts && roleParts.length < 1)
179
+ success = false;
83
180
 
84
- roleObj = roleParts[0];
85
- roleAct = roleParts.length >= 2 ? roleParts[1] : null
181
+ roleObj = roleParts[0];
182
+ roleAct = roleParts.length >= 2 ? roleParts[1] : null
86
183
 
87
- result = await security.validate(userRole, null, roleObj, roleAct);
88
- logger.debug('middleware', 'authorizationCheckRoles', 'result', result, request.correlationId);
89
- if (logical === logicalOr) {
90
- if (result)
91
- return result;
184
+ result = await this._serviceSecurity.validate(userRole, null, roleObj, roleAct);
185
+ this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'result', result, request.correlationId);
186
+ if (logical === logicalOr) {
187
+ if (result)
188
+ return result;
92
189
 
93
- success = false;
190
+ success = false;
191
+ }
192
+ else
193
+ success = success && result;
94
194
  }
95
- else
96
- success = success && result;
97
195
  }
98
- }
99
-
100
- return success;
101
- }
102
196
 
103
- const initalizeRoles = (request, roles, logger) => {
104
- if (Array.isArray(roles)) {
105
- // logger.debug('middleware', 'initalizeRoles', 'roles1a', roles);
106
- request.roles = roles;
197
+ return success;
107
198
  }
108
- else if ((typeof(roles) === 'string') || (roles instanceof String)) {
109
- // logger.debug('middleware', 'initalizeRoles', 'roles1b', roles);
110
- request.roles = roles.split(',');
111
- request.roles.map(item => item ? item.trim() : item);
199
+
200
+ _initalizeRoles (request, roles) {
201
+ if (Array.isArray(roles)) {
202
+ this._serviceLogger.debug('middleware', '_initalizeRoles', 'roles1a', roles);
203
+ request.roles = roles;
204
+ return;
205
+ }
206
+
207
+ if ((typeof(roles) === 'string') || (roles instanceof String)) {
208
+ // logger.debug('middleware', '_initalizeRoles', 'roles1b', roles);
209
+ request.roles = roles.split(',');
210
+ request.roles.map(item => item ? item.trim() : item);
211
+ return;
212
+ }
112
213
  }
113
214
  }
114
215
 
@@ -116,85 +217,4 @@ const initalizeRoles = (request, roles, logger) => {
116
217
  // if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
117
218
  // logical = logicalOr;
118
219
 
119
- export default async (request, reply, done, options) => {
120
- let logical = logicalOr;
121
- let roles = [];
122
- if (options) {
123
- logical = options.logical;
124
- if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
125
- logical = logicalOr;
126
-
127
- if (options.roles && Array.isArray(options.roles) && (options.roles.length > 0))
128
- roles = options.roles;
129
- }
130
-
131
- const config = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG);
132
- const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
133
- const security = injector.getService(LibraryConstants.InjectorKeys.SERVICE_SECURITY);
134
-
135
- // logger.debug('token', request.token);
136
- logger.debug('middleware', 'authorization', 'user', request.user, request.correlationId);
137
- logger.debug('middleware', 'authorization', 'claims', request.claims, request.correlationId);
138
- logger.debug('middleware', 'authorization', 'roles1', roles, request.correlationId);
139
- request.roles = [];
140
- if (roles) {
141
- // logger.debug('authorization.roles1', roles);
142
- // logger.debug('authorization.roles1', (typeof roles));
143
- // logger.debug('authorization.roles1', Array.isArray(roles));
144
- // logger.debug('authorization.roles1', ((typeof(roles) === 'string') || (roles instanceof String)));
145
- // if (Array.isArray(roles)) {
146
- // // logger.debug('authorization.roles1a', roles);
147
- // request.roles = roles;
148
- // }
149
- // else if ((typeof(roles) === 'string') || (roles instanceof String)) {
150
- // // logger.debug('authorization.roles1b', roles);
151
- // request.roles = roles.split(',');
152
- // request.roles.map(item => item ? item.trim() : item);
153
- // }
154
- initalizeRoles(request, roles, logger);
155
- }
156
- logger.debug('middleware', 'authorization', 'roles2', request.roles, request.correlationId);
157
-
158
- let success = false; //(logical === logicalOr ? false : true);
159
- if (request.roles && Array.isArray(request.roles) && (request.roles.length > 0)) {
160
- const auth = config.get('auth');
161
- if (auth) {
162
- logger.debug('middleware', 'authorization', 'auth.claims', auth.claims, request.correlationId);
163
- logger.debug('middleware', 'authorization', 'auth.claims.check', auth.claims.check, request.correlationId);
164
- }
165
- if (auth && auth.claims && auth.claims.check)
166
- success = await authorizationCheckClaims(request, (logical === logicalOr ? false : true), logical, security, logger);
167
-
168
- if (!success)
169
- success = await authorizationCheckRoles(request, (logical === logicalOr ? false : true), logical, security, logger);
170
- }
171
-
172
- logger.debug('middleware', 'authorization', 'success', null, request.success, request.correlationId);
173
- if (success) {
174
- // done(); // not for async
175
- return;
176
- }
177
-
178
- (async () => {
179
- const usageMetrics = {
180
- url: request.routerPath,
181
- correlationId: request.correlationId,
182
- href: request.url,
183
- headers: request.headers,
184
- host: request.hostname,
185
- hostname: request.hostname,
186
- querystring: request.query,
187
- token: request.token
188
- };
189
- const serviceUsageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
190
- await serviceUsageMetrics.register(usageMetrics).catch((err) => {
191
- // const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
192
- logger.error('middleware', 'authorization', err, null, request.correlationId);
193
- });
194
- })();
195
-
196
- logger.warn('middleware', 'authorization', 'Unauthorized... authorization unknown', null, request.correlationId);
197
- // reply.code(401);
198
- // done(new Error('Unauthorized... authentication unknown')); // not for async
199
- throw new Error('Unauthorized... authentication unknown');
200
- }
220
+ export default DefaultAuthenticationMiddleware;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@thzero/library_server_fastify",
3
3
  "type": "module",
4
- "version": "0.15.35",
4
+ "version": "0.15.38",
5
5
  "version_major": 0,
6
6
  "version_minor": 15,
7
- "version_patch": 35,
8
- "version_date": "04/20/2022",
7
+ "version_patch": 38,
8
+ "version_date": "04/25/2022",
9
9
  "description": "An opinionated library of common functionality to bootstrap a Fastify based API application.",
10
10
  "author": "thZero",
11
11
  "license": "MIT",
@@ -1,5 +1,3 @@
1
- import LibraryUtility from '@thzero/library_common/utility';
2
-
3
1
  import BaseRoute from '../index';
4
2
 
5
3
  class AdminBaseRoute extends BaseRoute {
@@ -52,7 +50,7 @@ class AdminBaseRoute extends BaseRoute {
52
50
  // eslint-disable-next-line
53
51
  async (request, reply) => {
54
52
  const response = (await router[this._options.serviceKey].create(request.correlationId, request.user, request.body)).check(request);
55
- this._jsonResponse(reply, LibraryUtility.stringify(response));
53
+ this._jsonResponse(reply, response);
56
54
  }
57
55
  );
58
56
  }
@@ -74,7 +72,7 @@ class AdminBaseRoute extends BaseRoute {
74
72
  // eslint-disable-next-line
75
73
  async (request, reply) => {
76
74
  const response = (await router[this._options.serviceKey].delete(request.correlationId, request.user, request.params.id)).check(request);
77
- this._jsonResponse(reply, LibraryUtility.stringify(response));
75
+ this._jsonResponse(reply, response);
78
76
  }
79
77
  );
80
78
  }
@@ -96,7 +94,7 @@ class AdminBaseRoute extends BaseRoute {
96
94
  // eslint-disable-next-line
97
95
  async (request, reply) => {
98
96
  const response = (await router[this._options.serviceKey].update(request.correlationId, request.user, request.params.id, request.body)).check(request);
99
- this._jsonResponse(reply, LibraryUtility.stringify(response));
97
+ this._jsonResponse(reply, response);
100
98
  }
101
99
  );
102
100
  }
@@ -121,7 +119,7 @@ class AdminBaseRoute extends BaseRoute {
121
119
  // eslint-disable-next-line
122
120
  async (request, reply) => {
123
121
  const response = (await router[this._options.serviceKey].search(request.correlationId, request.user, request.body)).check(request);
124
- this._jsonResponse(reply, LibraryUtility.stringify(response));
122
+ this._jsonResponse(reply, response);
125
123
  }
126
124
  );
127
125
 
@@ -38,7 +38,7 @@ class BaseNewsRoute extends BaseRoute {
38
38
  async (request, reply) => {
39
39
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_NEWS);
40
40
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_NEWS].latest(request.correlationId, request.user, parseInt(request.params.date))).check(request);
41
- this._jsonResponse(reply, Utility.stringify(response));
41
+ this._jsonResponse(reply, response);
42
42
  });
43
43
  }
44
44
  }
@@ -48,7 +48,7 @@ class BaseUsersRoute extends BaseRoute {
48
48
  async (request, reply) => {
49
49
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
50
50
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].fetchByGamerId(request.correlationId, request.params.gamerId)).check(request);
51
- this._jsonResponse(reply, Utility.stringify(response));
51
+ this._jsonResponse(reply, response);
52
52
  }
53
53
  );
54
54
  }
@@ -72,7 +72,7 @@ class BaseUsersRoute extends BaseRoute {
72
72
  async (request, reply) => {
73
73
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
74
74
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].fetchByGamerTag(request.correlationId, request.params.gamerTag)).check(request);
75
- this._jsonResponse(reply, Utility.stringify(response));
75
+ this._jsonResponse(reply, response);
76
76
  }
77
77
  );
78
78
  }
@@ -95,7 +95,7 @@ class BaseUsersRoute extends BaseRoute {
95
95
  async (request, reply) => {
96
96
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
97
97
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].refreshSettings(request.correlationId, request.body)).check(request);
98
- this._jsonResponse(reply, Utility.stringify(response));
98
+ this._jsonResponse(reply, response);
99
99
  }
100
100
  );
101
101
  }
@@ -118,7 +118,7 @@ class BaseUsersRoute extends BaseRoute {
118
118
  async (request, reply) => {
119
119
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
120
120
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].update(request.correlationId, request.body)).check(request);
121
- this._jsonResponse(reply, Utility.stringify(response));
121
+ this._jsonResponse(reply, response);
122
122
  }
123
123
  );
124
124
  }
@@ -141,7 +141,7 @@ class BaseUsersRoute extends BaseRoute {
141
141
  async (request, reply) => {
142
142
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
143
143
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].updateSettings(request.correlationId, request.body)).check(request);
144
- this._jsonResponse(reply, Utility.stringify(response));
144
+ this._jsonResponse(reply, response);
145
145
  }
146
146
  );
147
147
  }
package/routes/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import fastifyPlugin from 'fastify-plugin';
2
2
 
3
+ import Utility from '@thzero/library_common/utility';
4
+
3
5
  import BaseRoute from'@thzero/library_server/routes/index';
4
6
 
5
7
  class FastifyBaseRoute extends BaseRoute {
@@ -32,7 +34,7 @@ class FastifyBaseRoute extends BaseRoute {
32
34
  reply
33
35
  .code(200)
34
36
  .header('Content-Type', 'application/json; charset=utf-8')
35
- .send(json);
37
+ .send((typeof user === 'string') ? json : Utility.stringify(json));
36
38
  }
37
39
  }
38
40
 
package/routes/plans.js CHANGED
@@ -27,7 +27,7 @@ class PlansRoute extends BaseRoute {
27
27
  async (request, reply) => {
28
28
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_PLANS);
29
29
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_PLANS].listing(request.correlationId, request.body)).check(request);
30
- this._jsonResponse(reply, Utility.stringify(response));
30
+ this._jsonResponse(reply, response);
31
31
  }
32
32
  );
33
33
  }
package/routes/utility.js CHANGED
@@ -40,7 +40,7 @@ class UtilityRoute extends BaseRoute {
40
40
  async (request, reply) => {
41
41
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_UTILITY);
42
42
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_UTILITY].logger(request.correlationId, request.body)).check(request);
43
- this._jsonResponse(reply, Utility.stringify(response));
43
+ this._jsonResponse(reply, response);
44
44
  }
45
45
  );
46
46
  }
package/routes/version.js CHANGED
@@ -27,7 +27,7 @@ class VersionRoute extends BaseRoute {
27
27
  async (request, reply) => {
28
28
  // const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_VERSION);
29
29
  const response = (await router[LibraryConstants.InjectorKeys.SERVICE_VERSION].version(request.correlationId)).check(request);
30
- this._jsonResponse(reply, Utility.stringify(response));
30
+ this._jsonResponse(reply, response);
31
31
  }
32
32
  );
33
33
  }