@thzero/library_server_fastify 0.17.5 → 0.17.6

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.
@@ -1,220 +1,220 @@
1
- import LibraryCommonServiceConstants from '@thzero/library_common_service/constants.js';
2
- import LibraryServerConstants from '@thzero/library_server/constants.js';
3
-
4
- // require('../utility/string.cjs');
5
- String.isNullOrEmpty = function(value) {
6
- //return !(typeof value === 'string' && value.length > 0)
7
- return !value;
8
- }
9
-
10
- String.isString = function(value) {
11
- return (typeof value === "string" || value instanceof String);
12
- }
13
-
14
- String.trim = function(value) {
15
- if (!value || !String.isString(value))
16
- return value;
17
- return value.trim();
18
- }
19
-
20
- const logicalAnd = 'and';
21
- const logicalOr = 'or';
22
-
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(LibraryServerConstants.InjectorKeys.SERVICE_SECURITY);
34
- this._serviceUsageMetrics = injector.getService(LibraryServerConstants.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
42
- }
43
- }
44
-
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
- }
153
-
154
- return success;
155
- }
156
-
157
- async _authorizationCheckRoles (request, success, logical) {
158
- if (!request)
159
- return false;
160
-
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;
164
-
165
- this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'logical', logical, request.correlationId);
166
-
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);
173
-
174
- for (const role of request.roles) {
175
- this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'role', role, request.correlationId);
176
-
177
- roleParts = role.split('.');
178
- if (roleParts && roleParts.length < 1)
179
- success = false;
180
-
181
- roleObj = roleParts[0];
182
- roleAct = roleParts.length >= 2 ? roleParts[1] : null
183
-
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;
189
-
190
- success = false;
191
- }
192
- else
193
- success = success && result;
194
- }
195
- }
196
-
197
- return success;
198
- }
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
- }
213
- }
214
- }
215
-
216
- // const authorization = (roles, logical) => {
217
- // if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
218
- // logical = logicalOr;
219
-
220
- export default DefaultAuthenticationMiddleware;
1
+ import LibraryCommonServiceConstants from '@thzero/library_common_service/constants.js';
2
+ import LibraryServerConstants from '@thzero/library_server/constants.js';
3
+
4
+ // require('../utility/string.cjs');
5
+ String.isNullOrEmpty = function(value) {
6
+ //return !(typeof value === 'string' && value.length > 0)
7
+ return !value;
8
+ }
9
+
10
+ String.isString = function(value) {
11
+ return (typeof value === "string" || value instanceof String);
12
+ }
13
+
14
+ String.trim = function(value) {
15
+ if (!value || !String.isString(value))
16
+ return value;
17
+ return value.trim();
18
+ }
19
+
20
+ const logicalAnd = 'and';
21
+ const logicalOr = 'or';
22
+
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(LibraryServerConstants.InjectorKeys.SERVICE_SECURITY);
34
+ this._serviceUsageMetrics = injector.getService(LibraryServerConstants.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
42
+ }
43
+ }
44
+
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
+ }
153
+
154
+ return success;
155
+ }
156
+
157
+ async _authorizationCheckRoles (request, success, logical) {
158
+ if (!request)
159
+ return false;
160
+
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;
164
+
165
+ this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'logical', logical, request.correlationId);
166
+
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);
173
+
174
+ for (const role of request.roles) {
175
+ this._serviceLogger.debug('middleware', '_authorizationCheckRoles', 'role', role, request.correlationId);
176
+
177
+ roleParts = role.split('.');
178
+ if (roleParts && roleParts.length < 1)
179
+ success = false;
180
+
181
+ roleObj = roleParts[0];
182
+ roleAct = roleParts.length >= 2 ? roleParts[1] : null
183
+
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;
189
+
190
+ success = false;
191
+ }
192
+ else
193
+ success = success && result;
194
+ }
195
+ }
196
+
197
+ return success;
198
+ }
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
+ }
213
+ }
214
+ }
215
+
216
+ // const authorization = (roles, logical) => {
217
+ // if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
218
+ // logical = logicalOr;
219
+
220
+ export default DefaultAuthenticationMiddleware;
package/openSource.js CHANGED
@@ -1,81 +1,81 @@
1
- export default () => {
2
- return [
3
- {
4
- category: 'server',
5
- name: '@fastify',
6
- url: 'https://github.com/fastify/fastify',
7
- licenseName: 'MIT',
8
- licenseUrl: 'https://github.com/fastify/fastify/blob/main/LICENSE'
9
- },
10
- {
11
- category: 'server',
12
- name: '@fastify/auth',
13
- url: 'https://github.com/fastify/fastify-auth',
14
- licenseName: 'MIT',
15
- licenseUrl: 'https://github.com/fastify/fastify-auth/blob/master/LICENSE'
16
- },
17
- {
18
- category: 'server',
19
- name: '@fastify/compress',
20
- url: 'https://github.com/fastify/compress',
21
- licenseName: 'MIT',
22
- licenseUrl: 'https://github.com/fastify/compress/blob/main/LICENSE'
23
- },
24
- {
25
- category: 'server',
26
- name: '@fastify/cors',
27
- url: 'https://github.com/fastify/cors',
28
- licenseName: 'MIT',
29
- licenseUrl: 'https://github.com/fastify/cors/blob/main/LICENSE'
30
- },
31
- {
32
- category: 'server',
33
- name: '@fastify/helmet',
34
- url: 'https://github.com/fastify/helmet',
35
- licenseName: 'MIT',
36
- licenseUrl: 'https://github.com/fastify/helmet/blob/main/LICENSE'
37
- },
38
- {
39
- category: 'server',
40
- name: '@fastify/routes',
41
- url: 'https://github.com/fastify/routes',
42
- licenseName: 'MIT',
43
- licenseUrl: 'https://github.com/fastify/routes/blob/main/LICENSE'
44
- },
45
- {
46
- category: 'server',
47
- name: '@fastify/static',
48
- url: 'https://github.com/fastify/static',
49
- licenseName: 'MIT',
50
- licenseUrl: 'https://github.com/fastify/static/blob/main/LICENSE'
51
- },
52
- {
53
- category: 'server',
54
- name: '@thzero/library_common',
55
- url: 'https://github.com/thzero/library_common',
56
- licenseName: 'MIT',
57
- licenseUrl: 'https://github.com/thzero/library_common/blob/master/license.md'
58
- },
59
- {
60
- category: 'server',
61
- name: '@thzero/library_common_service',
62
- url: 'https://github.com/thzero/library_common_service',
63
- licenseName: 'MIT',
64
- licenseUrl: 'https://github.com/thzero/library_common_service/blob/master/license.md'
65
- },
66
- {
67
- category: 'server',
68
- name: '@thzero/library_service',
69
- url: 'https://github.com/thzero/library_service',
70
- licenseName: 'MIT',
71
- licenseUrl: 'https://github.com/thzero/library_service/blob/master/license.md'
72
- },
73
- {
74
- category: 'server',
75
- name: '@thzero/library_server_fastify',
76
- url: 'https://github.com/thzero/library_server_fastify',
77
- licenseName: 'MIT',
78
- licenseUrl: 'https://github.com/thzero/library_server_fastify/blob/master/license.md'
79
- }
80
- ];
1
+ export default () => {
2
+ return [
3
+ {
4
+ category: 'server',
5
+ name: '@fastify',
6
+ url: 'https://github.com/fastify/fastify',
7
+ licenseName: 'MIT',
8
+ licenseUrl: 'https://github.com/fastify/fastify/blob/main/LICENSE'
9
+ },
10
+ {
11
+ category: 'server',
12
+ name: '@fastify/auth',
13
+ url: 'https://github.com/fastify/fastify-auth',
14
+ licenseName: 'MIT',
15
+ licenseUrl: 'https://github.com/fastify/fastify-auth/blob/master/LICENSE'
16
+ },
17
+ {
18
+ category: 'server',
19
+ name: '@fastify/compress',
20
+ url: 'https://github.com/fastify/compress',
21
+ licenseName: 'MIT',
22
+ licenseUrl: 'https://github.com/fastify/compress/blob/main/LICENSE'
23
+ },
24
+ {
25
+ category: 'server',
26
+ name: '@fastify/cors',
27
+ url: 'https://github.com/fastify/cors',
28
+ licenseName: 'MIT',
29
+ licenseUrl: 'https://github.com/fastify/cors/blob/main/LICENSE'
30
+ },
31
+ {
32
+ category: 'server',
33
+ name: '@fastify/helmet',
34
+ url: 'https://github.com/fastify/helmet',
35
+ licenseName: 'MIT',
36
+ licenseUrl: 'https://github.com/fastify/helmet/blob/main/LICENSE'
37
+ },
38
+ {
39
+ category: 'server',
40
+ name: '@fastify/routes',
41
+ url: 'https://github.com/fastify/routes',
42
+ licenseName: 'MIT',
43
+ licenseUrl: 'https://github.com/fastify/routes/blob/main/LICENSE'
44
+ },
45
+ {
46
+ category: 'server',
47
+ name: '@fastify/static',
48
+ url: 'https://github.com/fastify/static',
49
+ licenseName: 'MIT',
50
+ licenseUrl: 'https://github.com/fastify/static/blob/main/LICENSE'
51
+ },
52
+ {
53
+ category: 'server',
54
+ name: '@thzero/library_common',
55
+ url: 'https://github.com/thzero/library_common',
56
+ licenseName: 'MIT',
57
+ licenseUrl: 'https://github.com/thzero/library_common/blob/master/license.md'
58
+ },
59
+ {
60
+ category: 'server',
61
+ name: '@thzero/library_common_service',
62
+ url: 'https://github.com/thzero/library_common_service',
63
+ licenseName: 'MIT',
64
+ licenseUrl: 'https://github.com/thzero/library_common_service/blob/master/license.md'
65
+ },
66
+ {
67
+ category: 'server',
68
+ name: '@thzero/library_service',
69
+ url: 'https://github.com/thzero/library_service',
70
+ licenseName: 'MIT',
71
+ licenseUrl: 'https://github.com/thzero/library_service/blob/master/license.md'
72
+ },
73
+ {
74
+ category: 'server',
75
+ name: '@thzero/library_server_fastify',
76
+ url: 'https://github.com/thzero/library_server_fastify',
77
+ licenseName: 'MIT',
78
+ licenseUrl: 'https://github.com/thzero/library_server_fastify/blob/master/license.md'
79
+ }
80
+ ];
81
81
  }