@thzero/library_server_fastify 0.18.13 → 0.18.14
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 +386 -386
- package/boot/plugins/admin/news.js +11 -11
- package/boot/plugins/admin/users.js +11 -11
- package/boot/plugins/api.js +21 -21
- package/boot/plugins/apiFront.js +26 -26
- 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 +112 -112
- package/openSource.js +80 -80
- package/package.json +41 -41
- package/plugins/apiKey.js +48 -48
- package/plugins/auth.js +126 -126
- package/plugins/responseTime.js +111 -111
- package/plugins/settings.js +12 -12
- package/plugins/usageMetrics.js +24 -24
- package/routes/admin/index.js +140 -140
- package/routes/admin/news.js +22 -22
- package/routes/admin/users.js +26 -26
- package/routes/baseNews.js +46 -46
- package/routes/baseUsers.js +180 -180
- package/routes/home.js +28 -28
- package/routes/index.js +41 -41
- package/routes/news.js +6 -6
- package/routes/plans.js +40 -40
- package/routes/usageMetrics.js +39 -39
- package/routes/users.js +6 -6
- package/routes/utility.js +80 -80
- package/routes/version.js +39 -39
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import BaseNewsAdminBootPlugin from '@thzero/library_server/boot/plugins/admin/news.js';
|
|
2
|
-
|
|
3
|
-
import adminNewsRoute from '../../../routes/admin/news.js';
|
|
4
|
-
|
|
5
|
-
class NewsAdminBootPlugin extends BaseNewsAdminBootPlugin {
|
|
6
|
-
_initRoutesAdminNews() {
|
|
7
|
-
return new adminNewsRoute();
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default NewsAdminBootPlugin;
|
|
1
|
+
import BaseNewsAdminBootPlugin from '@thzero/library_server/boot/plugins/admin/news.js';
|
|
2
|
+
|
|
3
|
+
import adminNewsRoute from '../../../routes/admin/news.js';
|
|
4
|
+
|
|
5
|
+
class NewsAdminBootPlugin extends BaseNewsAdminBootPlugin {
|
|
6
|
+
_initRoutesAdminNews() {
|
|
7
|
+
return new adminNewsRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default NewsAdminBootPlugin;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import BaseUsersAdminBootPlugin from '@thzero/library_server/boot/plugins/admin/users.js';
|
|
2
|
-
|
|
3
|
-
import adminUsersRoute from '../../../routes/admin/users.js'
|
|
4
|
-
|
|
5
|
-
class UsersAdminBootPlugin extends BaseUsersAdminBootPlugin {
|
|
6
|
-
_initRoutesAdminUsers() {
|
|
7
|
-
return new adminUsersRoute();
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default UsersAdminBootPlugin;
|
|
1
|
+
import BaseUsersAdminBootPlugin from '@thzero/library_server/boot/plugins/admin/users.js';
|
|
2
|
+
|
|
3
|
+
import adminUsersRoute from '../../../routes/admin/users.js'
|
|
4
|
+
|
|
5
|
+
class UsersAdminBootPlugin extends BaseUsersAdminBootPlugin {
|
|
6
|
+
_initRoutesAdminUsers() {
|
|
7
|
+
return new adminUsersRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default UsersAdminBootPlugin;
|
package/boot/plugins/api.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import BaseApiBootPlugin from '@thzero/library_server/boot/plugins/api.js';
|
|
2
|
-
|
|
3
|
-
import homeRoute from '../../routes/home.js';
|
|
4
|
-
import usageMetricsRoute from '../../routes/usageMetrics.js';
|
|
5
|
-
import versionRoute from '../../routes/version.js';
|
|
6
|
-
|
|
7
|
-
class ApiBootPlugin extends BaseApiBootPlugin {
|
|
8
|
-
_initRoutesHome() {
|
|
9
|
-
return new homeRoute();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
_initRoutesUsageMetrics() {
|
|
13
|
-
return new usageMetricsRoute();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
_initRoutesVersion() {
|
|
17
|
-
return new versionRoute();
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export default ApiBootPlugin;
|
|
1
|
+
import BaseApiBootPlugin from '@thzero/library_server/boot/plugins/api.js';
|
|
2
|
+
|
|
3
|
+
import homeRoute from '../../routes/home.js';
|
|
4
|
+
import usageMetricsRoute from '../../routes/usageMetrics.js';
|
|
5
|
+
import versionRoute from '../../routes/version.js';
|
|
6
|
+
|
|
7
|
+
class ApiBootPlugin extends BaseApiBootPlugin {
|
|
8
|
+
_initRoutesHome() {
|
|
9
|
+
return new homeRoute();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_initRoutesUsageMetrics() {
|
|
13
|
+
return new usageMetricsRoute();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
_initRoutesVersion() {
|
|
17
|
+
return new versionRoute();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default ApiBootPlugin;
|
package/boot/plugins/apiFront.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import BaseApiFrontBootPlugin from '@thzero/library_server/boot/plugins/apiFront.js';
|
|
2
|
-
|
|
3
|
-
import homeRoute from '../../routes/home.js';
|
|
4
|
-
import usageMetricsRoute from '../../routes/usageMetrics.js';
|
|
5
|
-
import utilityRoute from '../../routes/utility.js';
|
|
6
|
-
import versionRoute from '../../routes/version.js';
|
|
7
|
-
|
|
8
|
-
class FrontApiBootPlugin extends BaseApiFrontBootPlugin {
|
|
9
|
-
_initRoutesHome() {
|
|
10
|
-
return new homeRoute();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
_initRoutesUsageMetrics() {
|
|
14
|
-
return new usageMetricsRoute();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
_initRoutesVersion() {
|
|
18
|
-
return new versionRoute();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
_initRoutesUtility() {
|
|
22
|
-
return new utilityRoute();
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export default FrontApiBootPlugin;
|
|
1
|
+
import BaseApiFrontBootPlugin from '@thzero/library_server/boot/plugins/apiFront.js';
|
|
2
|
+
|
|
3
|
+
import homeRoute from '../../routes/home.js';
|
|
4
|
+
import usageMetricsRoute from '../../routes/usageMetrics.js';
|
|
5
|
+
import utilityRoute from '../../routes/utility.js';
|
|
6
|
+
import versionRoute from '../../routes/version.js';
|
|
7
|
+
|
|
8
|
+
class FrontApiBootPlugin extends BaseApiFrontBootPlugin {
|
|
9
|
+
_initRoutesHome() {
|
|
10
|
+
return new homeRoute();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
_initRoutesUsageMetrics() {
|
|
14
|
+
return new usageMetricsRoute();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_initRoutesVersion() {
|
|
18
|
+
return new versionRoute();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
_initRoutesUtility() {
|
|
22
|
+
return new utilityRoute();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default FrontApiBootPlugin;
|
package/boot/plugins/news.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import BaseNewsApiBootPlugin from '@thzero/library_server/boot/plugins/news.js';
|
|
2
|
-
|
|
3
|
-
import newsRoute from '../../routes/news.js';
|
|
4
|
-
|
|
5
|
-
class NewsApiBootPlugin extends BaseNewsApiBootPlugin {
|
|
6
|
-
_initRoutesNews() {
|
|
7
|
-
return new newsRoute();
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default NewsApiBootPlugin;
|
|
1
|
+
import BaseNewsApiBootPlugin from '@thzero/library_server/boot/plugins/news.js';
|
|
2
|
+
|
|
3
|
+
import newsRoute from '../../routes/news.js';
|
|
4
|
+
|
|
5
|
+
class NewsApiBootPlugin extends BaseNewsApiBootPlugin {
|
|
6
|
+
_initRoutesNews() {
|
|
7
|
+
return new newsRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default NewsApiBootPlugin;
|
package/boot/plugins/users.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import BaseUsersApiBootPlugin from '@thzero/library_server/boot/plugins/users.js';
|
|
2
|
-
|
|
3
|
-
import usersRoute from '../../routes/users.js';
|
|
4
|
-
|
|
5
|
-
class UsersApiBootPlugin extends BaseUsersApiBootPlugin {
|
|
6
|
-
_initRoutesUsers() {
|
|
7
|
-
return new usersRoute();
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export default UsersApiBootPlugin;
|
|
1
|
+
import BaseUsersApiBootPlugin from '@thzero/library_server/boot/plugins/users.js';
|
|
2
|
+
|
|
3
|
+
import usersRoute from '../../routes/users.js';
|
|
4
|
+
|
|
5
|
+
class UsersApiBootPlugin extends BaseUsersApiBootPlugin {
|
|
6
|
+
_initRoutesUsers() {
|
|
7
|
+
return new usersRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default UsersApiBootPlugin;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import BaseExtendedUsersApiBootPlugin from '@thzero/library_server/boot/plugins/usersExtended.js';
|
|
2
|
-
|
|
3
|
-
class ExtendedUsersApiBootPlugin extends BaseExtendedUsersApiBootPlugin {
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export default ExtendedUsersApiBootPlugin;
|
|
1
|
+
import BaseExtendedUsersApiBootPlugin from '@thzero/library_server/boot/plugins/usersExtended.js';
|
|
2
|
+
|
|
3
|
+
class ExtendedUsersApiBootPlugin extends BaseExtendedUsersApiBootPlugin {
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export default ExtendedUsersApiBootPlugin;
|
package/license.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020-2024 thZero.com
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
-
|
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
-
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2024 thZero.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
9
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
import LibraryCommonServiceConstants from '@thzero/library_common_service/constants.js';
|
|
2
|
-
import LibraryServerConstants from '@thzero/library_server/constants.js';
|
|
3
|
-
|
|
4
|
-
const separator = ': ';
|
|
5
|
-
|
|
6
|
-
class DefaultAuthenticationMiddleware {
|
|
7
|
-
constructor() {
|
|
8
|
-
this._serviceAuth = null;
|
|
9
|
-
this._serviceLogger = null;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
init(injector) {
|
|
13
|
-
this._serviceAuth = injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_AUTH);
|
|
14
|
-
this._serviceLogger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
15
|
-
this._serviceUsageMetrics = injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_USAGE_METRIC);
|
|
16
|
-
|
|
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
|
-
}
|
|
25
|
-
|
|
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;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// done(); // not for async
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
(async () => {
|
|
54
|
-
const usageMetrics = {
|
|
55
|
-
url: request.routeOptions.url,
|
|
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');
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
_getAuthToken(request) {
|
|
76
|
-
if (!request)
|
|
77
|
-
return null;
|
|
78
|
-
|
|
79
|
-
const token = request.headers[LibraryServerConstants.Headers.AuthKeys.AUTH];
|
|
80
|
-
if (token !== null && token !== undefined) {
|
|
81
|
-
this._serviceLogger.debug('middleware', 'getAuthToken', 'token', token, request.correlationId);
|
|
82
|
-
const split = token.split(LibraryServerConstants.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
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export default DefaultAuthenticationMiddleware;
|
|
1
|
+
import LibraryCommonServiceConstants from '@thzero/library_common_service/constants.js';
|
|
2
|
+
import LibraryServerConstants from '@thzero/library_server/constants.js';
|
|
3
|
+
|
|
4
|
+
const separator = ': ';
|
|
5
|
+
|
|
6
|
+
class DefaultAuthenticationMiddleware {
|
|
7
|
+
constructor() {
|
|
8
|
+
this._serviceAuth = null;
|
|
9
|
+
this._serviceLogger = null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
init(injector) {
|
|
13
|
+
this._serviceAuth = injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_AUTH);
|
|
14
|
+
this._serviceLogger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
15
|
+
this._serviceUsageMetrics = injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_USAGE_METRIC);
|
|
16
|
+
|
|
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
|
+
}
|
|
25
|
+
|
|
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;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// done(); // not for async
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
(async () => {
|
|
54
|
+
const usageMetrics = {
|
|
55
|
+
url: request.routeOptions.url,
|
|
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');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
_getAuthToken(request) {
|
|
76
|
+
if (!request)
|
|
77
|
+
return null;
|
|
78
|
+
|
|
79
|
+
const token = request.headers[LibraryServerConstants.Headers.AuthKeys.AUTH];
|
|
80
|
+
if (token !== null && token !== undefined) {
|
|
81
|
+
this._serviceLogger.debug('middleware', 'getAuthToken', 'token', token, request.correlationId);
|
|
82
|
+
const split = token.split(LibraryServerConstants.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
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export default DefaultAuthenticationMiddleware;
|
|
@@ -1,112 +1,112 @@
|
|
|
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
|
-
class DefaultAuthenticationMiddleware {
|
|
21
|
-
constructor() {
|
|
22
|
-
this._serviceConfig = null;
|
|
23
|
-
this._serviceLogger = null;
|
|
24
|
-
this._serviceSecurity = null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
init(injector) {
|
|
28
|
-
this._serviceConfig = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG);
|
|
29
|
-
this._serviceLogger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
30
|
-
this._serviceSecurity = injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_SECURITY);
|
|
31
|
-
this._serviceUsageMetrics = injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_USAGE_METRIC);
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
callback: async (request, reply, done, options) => {
|
|
35
|
-
const middleware = request.server.authorizationMiddlewareDefault;
|
|
36
|
-
await middleware.authorization(request, reply, done, options)
|
|
37
|
-
},
|
|
38
|
-
service: this
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async authorization(request, reply, done, options) {
|
|
43
|
-
let logical = this._serviceSecurity.initializeOptionsLogical(request.correlationId, options);
|
|
44
|
-
let roles = this._serviceSecurity.initializeOptionsRoles(request.correlationId, options);
|
|
45
|
-
|
|
46
|
-
// this._serviceLogger.debug('token', request.token);
|
|
47
|
-
this._serviceLogger.debug('middleware', 'authorization', 'user', request.user, request.correlationId);
|
|
48
|
-
this._serviceLogger.debug('middleware', 'authorization', 'claims', request.claims, request.correlationId);
|
|
49
|
-
this._serviceLogger.debug('middleware', 'authorization', 'roles1', roles, request.correlationId);
|
|
50
|
-
request.roles = [];
|
|
51
|
-
if (roles) {
|
|
52
|
-
// this._serviceLogger.debug('authorization.roles1', roles);
|
|
53
|
-
// this._serviceLogger.debug('authorization.roles1', (typeof roles));
|
|
54
|
-
// this._serviceLogger.debug('authorization.roles1', Array.isArray(roles));
|
|
55
|
-
// this._serviceLogger.debug('authorization.roles1', ((typeof(roles) === 'string') || (roles instanceof String)));
|
|
56
|
-
// if (Array.isArray(roles)) {
|
|
57
|
-
// // this._serviceLogger.debug('authorization.roles1a', roles);
|
|
58
|
-
// request.roles = roles;
|
|
59
|
-
// }
|
|
60
|
-
// else if ((typeof(roles) === 'string') || (roles instanceof String)) {
|
|
61
|
-
// // this._serviceLogger.debug('authorization.roles1b', roles);
|
|
62
|
-
// request.roles = roles.split(',');
|
|
63
|
-
// request.roles.map(item => item ? item.trim() : item);
|
|
64
|
-
// }
|
|
65
|
-
request.roles = this._serviceSecurity.initializeRoles(request.correlationId, request.roles, roles);
|
|
66
|
-
}
|
|
67
|
-
this._serviceLogger.debug('middleware', 'authorization', 'roles2', request.roles, request.correlationId);
|
|
68
|
-
|
|
69
|
-
let success = false;
|
|
70
|
-
if (request.roles && Array.isArray(request.roles) && (request.roles.length > 0)) {
|
|
71
|
-
const auth = this._serviceConfig.get('auth');
|
|
72
|
-
if (auth) {
|
|
73
|
-
this._serviceLogger.debug('middleware', 'authorization', 'auth.claims', auth.claims, request.correlationId);
|
|
74
|
-
this._serviceLogger.debug('middleware', 'authorization', 'auth.claims.check', auth.claims.check, request.correlationId);
|
|
75
|
-
}
|
|
76
|
-
if (auth && auth.claims && auth.claims.check)
|
|
77
|
-
success = await this._serviceSecurity.authorizationCheckClaims(request.correlationId, request.claims, request.roles, logical);
|
|
78
|
-
|
|
79
|
-
if (!success)
|
|
80
|
-
success = await this._serviceSecurity.authorizationCheckRoles(request.correlationId, request.user, request.roles, logical);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
this._serviceLogger.debug('middleware', 'authorization', 'success', null, request.success, request.correlationId);
|
|
84
|
-
if (success) {
|
|
85
|
-
// done(); // not for async
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
(async () => {
|
|
90
|
-
const usageMetrics = {
|
|
91
|
-
url: request.routeOptions.url,
|
|
92
|
-
correlationId: request.correlationId,
|
|
93
|
-
href: request.url,
|
|
94
|
-
headers: request.headers,
|
|
95
|
-
host: request.hostname,
|
|
96
|
-
hostname: request.hostname,
|
|
97
|
-
querystring: request.query,
|
|
98
|
-
token: request.token
|
|
99
|
-
};
|
|
100
|
-
await this._serviceUsageMetrics.register(usageMetrics).catch((err) => {
|
|
101
|
-
this._serviceLogger.error('middleware', 'authorization', err, null, request.correlationId);
|
|
102
|
-
});
|
|
103
|
-
})();
|
|
104
|
-
|
|
105
|
-
this._serviceLogger.warn('middleware', 'authorization', 'Unauthorized... authorization unknown', null, request.correlationId);
|
|
106
|
-
// reply.code(401);
|
|
107
|
-
// done(new Error('Unauthorized... authentication unknown')); // not for async
|
|
108
|
-
throw new Error('Unauthorized... authentication unknown');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
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
|
+
class DefaultAuthenticationMiddleware {
|
|
21
|
+
constructor() {
|
|
22
|
+
this._serviceConfig = null;
|
|
23
|
+
this._serviceLogger = null;
|
|
24
|
+
this._serviceSecurity = null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
init(injector) {
|
|
28
|
+
this._serviceConfig = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG);
|
|
29
|
+
this._serviceLogger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
|
|
30
|
+
this._serviceSecurity = injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_SECURITY);
|
|
31
|
+
this._serviceUsageMetrics = injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_USAGE_METRIC);
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
callback: async (request, reply, done, options) => {
|
|
35
|
+
const middleware = request.server.authorizationMiddlewareDefault;
|
|
36
|
+
await middleware.authorization(request, reply, done, options)
|
|
37
|
+
},
|
|
38
|
+
service: this
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async authorization(request, reply, done, options) {
|
|
43
|
+
let logical = this._serviceSecurity.initializeOptionsLogical(request.correlationId, options);
|
|
44
|
+
let roles = this._serviceSecurity.initializeOptionsRoles(request.correlationId, options);
|
|
45
|
+
|
|
46
|
+
// this._serviceLogger.debug('token', request.token);
|
|
47
|
+
this._serviceLogger.debug('middleware', 'authorization', 'user', request.user, request.correlationId);
|
|
48
|
+
this._serviceLogger.debug('middleware', 'authorization', 'claims', request.claims, request.correlationId);
|
|
49
|
+
this._serviceLogger.debug('middleware', 'authorization', 'roles1', roles, request.correlationId);
|
|
50
|
+
request.roles = [];
|
|
51
|
+
if (roles) {
|
|
52
|
+
// this._serviceLogger.debug('authorization.roles1', roles);
|
|
53
|
+
// this._serviceLogger.debug('authorization.roles1', (typeof roles));
|
|
54
|
+
// this._serviceLogger.debug('authorization.roles1', Array.isArray(roles));
|
|
55
|
+
// this._serviceLogger.debug('authorization.roles1', ((typeof(roles) === 'string') || (roles instanceof String)));
|
|
56
|
+
// if (Array.isArray(roles)) {
|
|
57
|
+
// // this._serviceLogger.debug('authorization.roles1a', roles);
|
|
58
|
+
// request.roles = roles;
|
|
59
|
+
// }
|
|
60
|
+
// else if ((typeof(roles) === 'string') || (roles instanceof String)) {
|
|
61
|
+
// // this._serviceLogger.debug('authorization.roles1b', roles);
|
|
62
|
+
// request.roles = roles.split(',');
|
|
63
|
+
// request.roles.map(item => item ? item.trim() : item);
|
|
64
|
+
// }
|
|
65
|
+
request.roles = this._serviceSecurity.initializeRoles(request.correlationId, request.roles, roles);
|
|
66
|
+
}
|
|
67
|
+
this._serviceLogger.debug('middleware', 'authorization', 'roles2', request.roles, request.correlationId);
|
|
68
|
+
|
|
69
|
+
let success = false;
|
|
70
|
+
if (request.roles && Array.isArray(request.roles) && (request.roles.length > 0)) {
|
|
71
|
+
const auth = this._serviceConfig.get('auth');
|
|
72
|
+
if (auth) {
|
|
73
|
+
this._serviceLogger.debug('middleware', 'authorization', 'auth.claims', auth.claims, request.correlationId);
|
|
74
|
+
this._serviceLogger.debug('middleware', 'authorization', 'auth.claims.check', auth.claims.check, request.correlationId);
|
|
75
|
+
}
|
|
76
|
+
if (auth && auth.claims && auth.claims.check)
|
|
77
|
+
success = await this._serviceSecurity.authorizationCheckClaims(request.correlationId, request.claims, request.roles, logical);
|
|
78
|
+
|
|
79
|
+
if (!success)
|
|
80
|
+
success = await this._serviceSecurity.authorizationCheckRoles(request.correlationId, request.user, request.roles, logical);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this._serviceLogger.debug('middleware', 'authorization', 'success', null, request.success, request.correlationId);
|
|
84
|
+
if (success) {
|
|
85
|
+
// done(); // not for async
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
(async () => {
|
|
90
|
+
const usageMetrics = {
|
|
91
|
+
url: request.routeOptions.url,
|
|
92
|
+
correlationId: request.correlationId,
|
|
93
|
+
href: request.url,
|
|
94
|
+
headers: request.headers,
|
|
95
|
+
host: request.hostname,
|
|
96
|
+
hostname: request.hostname,
|
|
97
|
+
querystring: request.query,
|
|
98
|
+
token: request.token
|
|
99
|
+
};
|
|
100
|
+
await this._serviceUsageMetrics.register(usageMetrics).catch((err) => {
|
|
101
|
+
this._serviceLogger.error('middleware', 'authorization', err, null, request.correlationId);
|
|
102
|
+
});
|
|
103
|
+
})();
|
|
104
|
+
|
|
105
|
+
this._serviceLogger.warn('middleware', 'authorization', 'Unauthorized... authorization unknown', null, request.correlationId);
|
|
106
|
+
// reply.code(401);
|
|
107
|
+
// done(new Error('Unauthorized... authentication unknown')); // not for async
|
|
108
|
+
throw new Error('Unauthorized... authentication unknown');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export default DefaultAuthenticationMiddleware;
|