@thzero/library_server_fastify 0.16.9 → 0.16.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -17
- package/boot/index.js +367 -367
- package/boot/plugins/admin/news.js +11 -11
- package/boot/plugins/admin/users.js +11 -11
- package/boot/plugins/api.js +16 -16
- package/boot/plugins/apiFront.js +21 -21
- package/boot/plugins/news.js +11 -11
- package/boot/plugins/users.js +11 -11
- package/boot/plugins/usersExtended.js +6 -6
- package/license.md +8 -8
- package/middleware/authentication.js +94 -94
- package/middleware/authorization.js +220 -220
- package/package.json +41 -41
- package/plugins/apiKey.js +48 -48
- package/plugins/auth.js +124 -124
- package/plugins/responseTime.js +111 -111
- package/plugins/settings.js +12 -12
- package/plugins/usageMetrics.js +24 -24
- package/routes/admin/index.js +136 -136
- package/routes/admin/news.js +22 -22
- package/routes/admin/users.js +26 -26
- package/routes/baseNews.js +44 -44
- package/routes/baseUsers.js +148 -148
- package/routes/home.js +28 -28
- package/routes/index.js +41 -41
- package/routes/news.js +6 -6
- package/routes/plans.js +38 -38
- package/routes/users.js +6 -6
- package/routes/utility.js +51 -51
- package/routes/version.js +38 -38
|
@@ -1,220 +1,220 @@
|
|
|
1
|
-
import LibraryConstants from '@thzero/library_server/constants.js';
|
|
2
|
-
import LibraryCommonServiceConstants from '@thzero/library_common_service/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(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
|
|
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 LibraryConstants from '@thzero/library_server/constants.js';
|
|
2
|
+
import LibraryCommonServiceConstants from '@thzero/library_common_service/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(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
|
|
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/package.json
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@thzero/library_server_fastify",
|
|
3
|
-
"type": "module",
|
|
4
|
-
"version": "0.16.
|
|
5
|
-
"version_major": 0,
|
|
6
|
-
"version_minor": 16,
|
|
7
|
-
"version_patch":
|
|
8
|
-
"version_date": "12/
|
|
9
|
-
"description": "An opinionated library of common functionality to bootstrap a Fastify based API application.",
|
|
10
|
-
"author": "thZero",
|
|
11
|
-
"license": "MIT",
|
|
12
|
-
"repository": {
|
|
13
|
-
"type": "git",
|
|
14
|
-
"url": "git+https://github.com/thzero/library_server_fastify.git"
|
|
15
|
-
},
|
|
16
|
-
"bugs": {
|
|
17
|
-
"url": "https://github.com/thzero/library_server_fastify/issues"
|
|
18
|
-
},
|
|
19
|
-
"homepage": "https://github.com/thzero/library_server_fastify#readme",
|
|
20
|
-
"engines": {
|
|
21
|
-
"node": ">=12.8.3"
|
|
22
|
-
},
|
|
23
|
-
"scripts": {
|
|
24
|
-
"cli-update": "library-cli --updateversion --pi",
|
|
25
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
26
|
-
},
|
|
27
|
-
"dependencies": {
|
|
28
|
-
"@fastify/auth": "^4.
|
|
29
|
-
"@fastify/compress": "^6.2.0",
|
|
30
|
-
"@fastify/cors": "^8.2.0",
|
|
31
|
-
"@fastify/helmet": "^10.1.0",
|
|
32
|
-
"@fastify/routes": "^5.
|
|
33
|
-
"@fastify/static": "^6.
|
|
34
|
-
"async-mutex": "^0.4.0",
|
|
35
|
-
"fastify": "^4.10.2"
|
|
36
|
-
},
|
|
37
|
-
"peerDependencies": {
|
|
38
|
-
"@thzero/library_common": "^0.16",
|
|
39
|
-
"@thzero/library_common_service": "^0.16",
|
|
40
|
-
"@thzero/library_server": "^0.16"
|
|
41
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@thzero/library_server_fastify",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.16.10",
|
|
5
|
+
"version_major": 0,
|
|
6
|
+
"version_minor": 16,
|
|
7
|
+
"version_patch": 10,
|
|
8
|
+
"version_date": "12/11/2022",
|
|
9
|
+
"description": "An opinionated library of common functionality to bootstrap a Fastify based API application.",
|
|
10
|
+
"author": "thZero",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/thzero/library_server_fastify.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/thzero/library_server_fastify/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/thzero/library_server_fastify#readme",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12.8.3"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"cli-update": "library-cli --updateversion --pi",
|
|
25
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@fastify/auth": "^4.2.0",
|
|
29
|
+
"@fastify/compress": "^6.2.0",
|
|
30
|
+
"@fastify/cors": "^8.2.0",
|
|
31
|
+
"@fastify/helmet": "^10.1.0",
|
|
32
|
+
"@fastify/routes": "^5.1.0",
|
|
33
|
+
"@fastify/static": "^6.6.0",
|
|
34
|
+
"async-mutex": "^0.4.0",
|
|
35
|
+
"fastify": "^4.10.2"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@thzero/library_common": "^0.16",
|
|
39
|
+
"@thzero/library_common_service": "^0.16",
|
|
40
|
+
"@thzero/library_server": "^0.16"
|
|
41
|
+
}
|
|
42
42
|
}
|
package/plugins/apiKey.js
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import fastifyPlugin from 'fastify-plugin';
|
|
2
|
-
|
|
3
|
-
import LibraryConstants from '@thzero/library_server/constants.js';
|
|
4
|
-
|
|
5
|
-
export default fastifyPlugin((instance, opts, done) => {
|
|
6
|
-
instance.addHook('onRequest', (request, reply, next) => {
|
|
7
|
-
if (request.originalUrl === '/favicon.ico') {
|
|
8
|
-
next();
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const key = request.headers[LibraryConstants.Headers.AuthKeys.API];
|
|
13
|
-
// opts.logger.debug('KoaBootMain', 'start', 'auth-api-token.key', key);
|
|
14
|
-
if (!String.isNullOrEmpty(key)) {
|
|
15
|
-
const auth = request.config.get('auth');
|
|
16
|
-
if (auth) {
|
|
17
|
-
const apiKey = auth.apiKey;
|
|
18
|
-
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.apiKey', apiKey);
|
|
19
|
-
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key===apiKey', (key === apiKey));
|
|
20
|
-
if (key === apiKey) {
|
|
21
|
-
request.apiKey = key;
|
|
22
|
-
next();
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
(async () => {
|
|
29
|
-
const usageMetrics = {
|
|
30
|
-
url: request.routerPath,
|
|
31
|
-
correlationId: request.correlationId,
|
|
32
|
-
href: request.url,
|
|
33
|
-
headers: request.headers,
|
|
34
|
-
host: request.hostname,
|
|
35
|
-
hostname: request.hostname,
|
|
36
|
-
querystring: request.query,
|
|
37
|
-
token: request.token
|
|
38
|
-
};
|
|
39
|
-
await opts.usageMetrics.register(usageMetrics).catch((err) => {
|
|
40
|
-
opts.logger.error('FastifyBootMain', 'start', 'usageMetrics', err);
|
|
41
|
-
});
|
|
42
|
-
})();
|
|
43
|
-
|
|
44
|
-
console.log('Unauthorized... auth-api-token failure');
|
|
45
|
-
reply.status(401).send();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
done();
|
|
1
|
+
import fastifyPlugin from 'fastify-plugin';
|
|
2
|
+
|
|
3
|
+
import LibraryConstants from '@thzero/library_server/constants.js';
|
|
4
|
+
|
|
5
|
+
export default fastifyPlugin((instance, opts, done) => {
|
|
6
|
+
instance.addHook('onRequest', (request, reply, next) => {
|
|
7
|
+
if (request.originalUrl === '/favicon.ico') {
|
|
8
|
+
next();
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const key = request.headers[LibraryConstants.Headers.AuthKeys.API];
|
|
13
|
+
// opts.logger.debug('KoaBootMain', 'start', 'auth-api-token.key', key);
|
|
14
|
+
if (!String.isNullOrEmpty(key)) {
|
|
15
|
+
const auth = request.config.get('auth');
|
|
16
|
+
if (auth) {
|
|
17
|
+
const apiKey = auth.apiKey;
|
|
18
|
+
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.apiKey', apiKey);
|
|
19
|
+
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key===apiKey', (key === apiKey));
|
|
20
|
+
if (key === apiKey) {
|
|
21
|
+
request.apiKey = key;
|
|
22
|
+
next();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
(async () => {
|
|
29
|
+
const usageMetrics = {
|
|
30
|
+
url: request.routerPath,
|
|
31
|
+
correlationId: request.correlationId,
|
|
32
|
+
href: request.url,
|
|
33
|
+
headers: request.headers,
|
|
34
|
+
host: request.hostname,
|
|
35
|
+
hostname: request.hostname,
|
|
36
|
+
querystring: request.query,
|
|
37
|
+
token: request.token
|
|
38
|
+
};
|
|
39
|
+
await opts.usageMetrics.register(usageMetrics).catch((err) => {
|
|
40
|
+
opts.logger.error('FastifyBootMain', 'start', 'usageMetrics', err);
|
|
41
|
+
});
|
|
42
|
+
})();
|
|
43
|
+
|
|
44
|
+
console.log('Unauthorized... auth-api-token failure');
|
|
45
|
+
reply.status(401).send();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
done();
|
|
49
49
|
});
|