@thzero/library_server 0.15.9 → 0.15.17

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.
Files changed (66) hide show
  1. package/README.md +89 -89
  2. package/boot/index.js +387 -384
  3. package/boot/koa/index.js +157 -157
  4. package/boot/plugins/admin/index.js +6 -6
  5. package/boot/plugins/admin/news.js +35 -35
  6. package/boot/plugins/admin/users.js +35 -35
  7. package/boot/plugins/api.js +58 -58
  8. package/boot/plugins/apiFront.js +31 -31
  9. package/boot/plugins/index.js +71 -71
  10. package/boot/plugins/news.js +46 -46
  11. package/boot/plugins/users.js +48 -48
  12. package/boot/plugins/usersExtended.js +32 -32
  13. package/constants.js +45 -45
  14. package/data/baseNews.js +42 -42
  15. package/data/baseSettingsUser.js +9 -9
  16. package/data/baseUser.js +28 -28
  17. package/data/index.js +24 -24
  18. package/data/named.js +20 -20
  19. package/errors/tokenExpired.js +7 -7
  20. package/license.md +8 -8
  21. package/middleware/authentication.js +67 -67
  22. package/middleware/authorization.js +180 -180
  23. package/package.json +50 -60
  24. package/repository/index.js +151 -151
  25. package/repository/usageMetrics/devnull.js +11 -11
  26. package/routes/admin/index.js +111 -111
  27. package/routes/admin/news.js +14 -14
  28. package/routes/admin/users.js +18 -18
  29. package/routes/baseNews.js +27 -27
  30. package/routes/baseUsers.js +106 -106
  31. package/routes/home.js +16 -16
  32. package/routes/index.js +31 -31
  33. package/routes/news.js +6 -6
  34. package/routes/plans.js +24 -24
  35. package/routes/users.js +6 -6
  36. package/routes/utility.js +34 -34
  37. package/routes/version.js +24 -24
  38. package/service/admin/baseNews.js +45 -45
  39. package/service/admin/index.js +130 -130
  40. package/service/admin/news.js +6 -6
  41. package/service/admin/users.js +107 -107
  42. package/service/baseSecurity.js +44 -44
  43. package/service/baseUser.js +122 -122
  44. package/service/communication.js +6 -6
  45. package/service/config.js +32 -32
  46. package/service/crypto.js +16 -16
  47. package/service/discovery/index.js +6 -6
  48. package/service/discovery/resources/index.js +101 -101
  49. package/service/external.js +19 -19
  50. package/service/externalRest.js +19 -19
  51. package/service/index.js +20 -20
  52. package/service/monitoring.js +13 -0
  53. package/service/news/base.js +49 -49
  54. package/service/news/index.js +6 -6
  55. package/service/news/validation/index.js +6 -6
  56. package/service/plans.js +27 -27
  57. package/service/restCommunication.js +21 -21
  58. package/service/usageMetrics.js +63 -63
  59. package/service/utility.js +37 -37
  60. package/service/version.js +32 -32
  61. package/utility/injector.js +59 -59
  62. package/utility/internalIp/index.js +48 -48
  63. package/utility/list/doubleLinked.js +88 -88
  64. package/utility/list/priorityQueue.js +109 -109
  65. package/utility/list/queue.js +72 -72
  66. package/utility/os.js +22 -22
package/data/index.js CHANGED
@@ -1,24 +1,24 @@
1
- import Utility from '@thzero/library_common/utility';
2
-
3
- class Data {
4
- constructor() {
5
- this.id = Utility.generateId();
6
- this.createdTimestamp = Utility.getTimestamp();
7
- this.createdUserId = null;
8
- this.updatedTimestamp = Utility.getTimestamp();
9
- this.updatedUserId = null;
10
- }
11
-
12
- map(requested) {
13
- if (!requested)
14
- return;
15
-
16
- this.id = requested.id;
17
- this.createdTimestamp = requested.createdTimestamp;
18
- this.createdUserId = requested.createdUserId;
19
- this.updatedTimestamp = requested.updatedTimestamp;
20
- this.updatedUserId = requested.updatedUserId;
21
- }
22
- }
23
-
24
- export default Data;
1
+ import Utility from '@thzero/library_common/utility';
2
+
3
+ class Data {
4
+ constructor() {
5
+ this.id = Utility.generateId();
6
+ this.createdTimestamp = Utility.getTimestamp();
7
+ this.createdUserId = null;
8
+ this.updatedTimestamp = Utility.getTimestamp();
9
+ this.updatedUserId = null;
10
+ }
11
+
12
+ map(requested) {
13
+ if (!requested)
14
+ return;
15
+
16
+ this.id = requested.id;
17
+ this.createdTimestamp = requested.createdTimestamp;
18
+ this.createdUserId = requested.createdUserId;
19
+ this.updatedTimestamp = requested.updatedTimestamp;
20
+ this.updatedUserId = requested.updatedUserId;
21
+ }
22
+ }
23
+
24
+ export default Data;
package/data/named.js CHANGED
@@ -1,20 +1,20 @@
1
- import Data from './index';
2
-
3
- class NamedData extends Data {
4
- constructor() {
5
- super();
6
-
7
- this.name = '';
8
- }
9
-
10
- map(requested) {
11
- super.map(requested);
12
-
13
- if (!requested)
14
- return;
15
-
16
- this.name = requested.name;
17
- }
18
- }
19
-
20
- export default NamedData;
1
+ import Data from './index';
2
+
3
+ class NamedData extends Data {
4
+ constructor() {
5
+ super();
6
+
7
+ this.name = '';
8
+ }
9
+
10
+ map(requested) {
11
+ super.map(requested);
12
+
13
+ if (!requested)
14
+ return;
15
+
16
+ this.name = requested.name;
17
+ }
18
+ }
19
+
20
+ export default NamedData;
@@ -1,7 +1,7 @@
1
- class TokenExpiredError extends Error {
2
- constructor() {
3
- super('Token Expired.');
4
- }
5
- }
6
-
7
- export default TokenExpiredError;
1
+ class TokenExpiredError extends Error {
2
+ constructor() {
3
+ super('Token Expired.');
4
+ }
5
+ }
6
+
7
+ export default TokenExpiredError;
package/license.md CHANGED
@@ -1,9 +1,9 @@
1
- MIT License
2
-
3
- Copyright (c) 2020-2022 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-2022 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,67 +1,67 @@
1
- import LibraryConstants from '../constants';
2
- import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
3
-
4
- import injector from '@thzero/library_common/utility/injector';
5
-
6
- const separator = ': ';
7
-
8
- function getAuthToken(context) {
9
- if (!context)
10
- return null;
11
-
12
- const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
13
- const token = context.get(LibraryConstants.Headers.AuthKeys.AUTH);
14
- logger.debug('middleware', 'getAuthToken', 'token', token, context.correlationId);
15
- const split = token.split(LibraryConstants.Headers.AuthKeys.AUTH_BEARER + separator);
16
- logger.debug('middleware', 'getAuthToken', 'split', split, context.correlationId);
17
- logger.debug('middleware', 'getAuthToken', 'split.length', split.length, context.correlationId);
18
- if (split.length > 1)
19
- return split[1];
20
-
21
- logger.debug('middleware', 'getAuthToken', 'fail', null, context.correlationId);
22
- return null;
23
- }
24
-
25
- const authentication = (required) => {
26
- return async (ctx, next) => {
27
- const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
28
-
29
- const token = getAuthToken(ctx);
30
- logger.debug('middleware', 'authentication', 'token', token, ctx.correlationId);
31
- logger.debug('middleware', 'authentication', 'required', required, ctx.correlationId);
32
- const valid = ((required && !String.isNullOrEmpty(token)) || !required);
33
- logger.debug('middleware', 'authentication', 'valid', valid, ctx.correlationId);
34
- if (valid) {
35
- if (!String.isNullOrEmpty(token)) {
36
- const service = injector.getService(LibraryConstants.InjectorKeys.SERVICE_AUTH);
37
- const results = await service.verifyToken(ctx.correlationId, token);
38
- logger.debug('middleware', 'authentication', 'results', results, ctx.correlationId);
39
- if (!results || !results.success) {
40
- logger.warn('middleware', 'authentication', 'Unauthenticated... invalid token', null, ctx.correlationId);
41
- ctx.throw(401);
42
- return;
43
- }
44
-
45
- ctx.state.token = token;
46
- ctx.state.user = results.user;
47
- ctx.state.claims = results.claims;
48
- }
49
-
50
- await next();
51
- return;
52
- }
53
-
54
- (async () => {
55
- const usageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
56
- await usageMetrics.register(ctx).catch((err) => {
57
- const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
58
- logger.error('middleware', 'authentication', err, null, ctx.correlationId);
59
- });
60
- })();
61
-
62
- logger.warn('middleware', 'authentication', 'Unauthorized... authentication unknown', null, ctx.correlationId);
63
- ctx.throw(401);
64
- }
65
- }
66
-
67
- export default authentication;
1
+ import LibraryConstants from '../constants';
2
+ import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
3
+
4
+ import injector from '@thzero/library_common/utility/injector';
5
+
6
+ const separator = ': ';
7
+
8
+ function getAuthToken(context) {
9
+ if (!context)
10
+ return null;
11
+
12
+ const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
13
+ const token = context.get(LibraryConstants.Headers.AuthKeys.AUTH);
14
+ logger.debug('middleware', 'getAuthToken', 'token', token, context.correlationId);
15
+ const split = token.split(LibraryConstants.Headers.AuthKeys.AUTH_BEARER + separator);
16
+ logger.debug('middleware', 'getAuthToken', 'split', split, context.correlationId);
17
+ logger.debug('middleware', 'getAuthToken', 'split.length', split.length, context.correlationId);
18
+ if (split.length > 1)
19
+ return split[1];
20
+
21
+ logger.debug('middleware', 'getAuthToken', 'fail', null, context.correlationId);
22
+ return null;
23
+ }
24
+
25
+ const authentication = (required) => {
26
+ return async (ctx, next) => {
27
+ const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
28
+
29
+ const token = getAuthToken(ctx);
30
+ logger.debug('middleware', 'authentication', 'token', token, ctx.correlationId);
31
+ logger.debug('middleware', 'authentication', 'required', required, ctx.correlationId);
32
+ const valid = ((required && !String.isNullOrEmpty(token)) || !required);
33
+ logger.debug('middleware', 'authentication', 'valid', valid, ctx.correlationId);
34
+ if (valid) {
35
+ if (!String.isNullOrEmpty(token)) {
36
+ const service = injector.getService(LibraryConstants.InjectorKeys.SERVICE_AUTH);
37
+ const results = await service.verifyToken(ctx.correlationId, token);
38
+ logger.debug('middleware', 'authentication', 'results', results, ctx.correlationId);
39
+ if (!results || !results.success) {
40
+ logger.warn('middleware', 'authentication', 'Unauthenticated... invalid token', null, ctx.correlationId);
41
+ ctx.throw(401);
42
+ return;
43
+ }
44
+
45
+ ctx.state.token = token;
46
+ ctx.state.user = results.user;
47
+ ctx.state.claims = results.claims;
48
+ }
49
+
50
+ await next();
51
+ return;
52
+ }
53
+
54
+ (async () => {
55
+ const usageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
56
+ await usageMetrics.register(ctx).catch((err) => {
57
+ const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
58
+ logger.error('middleware', 'authentication', err, null, ctx.correlationId);
59
+ });
60
+ })();
61
+
62
+ logger.warn('middleware', 'authentication', 'Unauthorized... authentication unknown', null, ctx.correlationId);
63
+ ctx.throw(401);
64
+ }
65
+ }
66
+
67
+ export default authentication;
@@ -1,180 +1,180 @@
1
- import LibraryConstants from '../constants';
2
- import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
3
-
4
- import injector from '@thzero/library_common/utility/injector';
5
-
6
- // require('../utility/string.cjs');
7
- String.isNullOrEmpty = function(value) {
8
- //return !(typeof value === 'string' && value.length > 0)
9
- return !value;
10
- }
11
-
12
- String.isString = function(value) {
13
- return (typeof value === "string" || value instanceof String);
14
- }
15
-
16
- String.trim = function(value) {
17
- if (!value || !String.isString(value))
18
- return value;
19
- return value.trim();
20
- }
21
-
22
- const logicalAnd = 'and';
23
- const logicalOr = 'or';
24
-
25
- const authorizationCheckClaims = async (ctx, success, logical, security, logger) => {
26
- if (!ctx)
27
- return false;
28
- if (!(ctx.state.claims && Array.isArray(ctx.state.claims)))
29
- return false;
30
-
31
- let result;
32
- let roleAct;
33
- let roleObj;
34
- let roleParts;
35
- for (const claim of ctx.state.claims) {
36
- logger.debug('middleware', 'authorization', 'authorization.claim', claim, ctx.correlationId);
37
-
38
- for (const role of ctx.state.roles) {
39
- logger.debug('middleware', 'authorization', 'role', role, ctx.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, ctx.correlationId);
50
- if (logical === logicalOr)
51
- success = success || result;
52
- else
53
- success = success && result;
54
- }
55
- }
56
-
57
- return success;
58
- }
59
-
60
- const authorizationCheckRoles = async (ctx, success, logical, security, logger) => {
61
- if (!ctx)
62
- return false;
63
-
64
- logger.debug('middleware', 'authorizationCheckRoles', 'user', ctx.state.user, ctx.correlationId);
65
- if (!(ctx.state.user && ctx.state.user.roles && Array.isArray(ctx.state.user.roles)))
66
- return false;
67
-
68
- logger.debug('middleware', 'authorizationCheckRoles', 'logical', logical, ctx.correlationId);
69
-
70
- let result;
71
- let roleAct;
72
- let roleObj;
73
- let roleParts;
74
- for (const userRole of ctx.state.user.roles) {
75
- logger.debug('middleware', 'authorizationCheckRoles', 'userRole', userRole, ctx.correlationId);
76
-
77
- for (const role of ctx.state.roles) {
78
- logger.debug('middleware', 'authorizationCheckRoles', 'role', role, ctx.correlationId);
79
-
80
- roleParts = role.split('.');
81
- if (roleParts && roleParts.length < 1)
82
- success = false;
83
-
84
- roleObj = roleParts[0];
85
- roleAct = roleParts.length >= 2 ? roleParts[1] : null
86
-
87
- result = await security.validate(userRole, null, roleObj, roleAct);
88
- logger.debug('middleware', 'authorizationCheckRoles', 'result', result, ctx.correlationId);
89
- if (logical === logicalOr) {
90
- if (result)
91
- return result;
92
-
93
- success = false;
94
- }
95
- else
96
- success = success && result;
97
- }
98
- }
99
-
100
- return success;
101
- }
102
-
103
- const initalizeRoles = (ctx, roles, logger) => {
104
- if (Array.isArray(roles)) {
105
- // logger.debug('middleware', 'initalizeRoles', 'roles1a', roles);
106
- ctx.state.roles = roles;
107
- }
108
- else if ((typeof(roles) === 'string') || (roles instanceof String)) {
109
- // logger.debug('middleware', 'initalizeRoles', 'roles1b', roles);
110
- ctx.state.roles = roles.split(',');
111
- ctx.state.roles.map(item => item ? item.trim() : item);
112
- }
113
- }
114
-
115
- const authorization = (roles, logical) => {
116
- if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
117
- logical = logicalOr;
118
-
119
- return async (ctx, next) => {
120
- const config = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG);
121
- const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
122
- const security = injector.getService(LibraryConstants.InjectorKeys.SERVICE_SECURITY);
123
-
124
- // logger.debug('token', ctx.state.token);
125
- logger.debug('middleware', 'authorization', 'user', ctx.state.user, ctx.correlationId);
126
- logger.debug('middleware', 'authorization', 'claims', ctx.state.claims, ctx.correlationId);
127
- logger.debug('middleware', 'authorization', 'roles1', roles, ctx.correlationId);
128
- ctx.state.roles = [];
129
- if (roles) {
130
- // logger.debug('authorization.roles1', roles);
131
- // logger.debug('authorization.roles1', (typeof roles));
132
- // logger.debug('authorization.roles1', Array.isArray(roles));
133
- // logger.debug('authorization.roles1', ((typeof(roles) === 'string') || (roles instanceof String)));
134
- // if (Array.isArray(roles)) {
135
- // // logger.debug('authorization.roles1a', roles);
136
- // ctx.state.roles = roles;
137
- // }
138
- // else if ((typeof(roles) === 'string') || (roles instanceof String)) {
139
- // // logger.debug('authorization.roles1b', roles);
140
- // ctx.state.roles = roles.split(',');
141
- // ctx.state.roles.map(item => item ? item.trim() : item);
142
- // }
143
- initalizeRoles(ctx, roles, logger);
144
- }
145
- logger.debug('middleware', 'authorization', 'roles2', ctx.state.roles, ctx.correlationId);
146
-
147
- let success = false; //(logical === logicalOr ? false : true);
148
- if (ctx.state.roles && Array.isArray(ctx.state.roles) && (ctx.state.roles.length > 0)) {
149
- const auth = config.get('auth');
150
- if (auth) {
151
- logger.debug('middleware', 'authorization', 'auth.claims', auth.claims, ctx.correlationId);
152
- logger.debug('middleware', 'authorization', 'auth.claims.check', auth.claims.check, ctx.correlationId);
153
- }
154
- if (auth && auth.claims && auth.claims.check)
155
- success = await authorizationCheckClaims(ctx, (logical === logicalOr ? false : true), logical, security, logger);
156
-
157
- if (!success)
158
- success = await authorizationCheckRoles(ctx, (logical === logicalOr ? false : true), logical, security, logger);
159
- }
160
-
161
- logger.debug('middleware', 'authorization', 'success', null, ctx.state.success, ctx.correlationId);
162
- if (success) {
163
- await next();
164
- return;
165
- }
166
-
167
- (async () => {
168
- const usageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
169
- await usageMetrics.register(ctx).catch((err) => {
170
- const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
171
- logger.error(null, err);
172
- });
173
- })();
174
-
175
- logger.warn('middleware', 'authorization', 'Unauthorized... authorization unknown', null, ctx.correlationId);
176
- ctx.throw(401);
177
- }
178
- }
179
-
180
- export default authorization;
1
+ import LibraryConstants from '../constants';
2
+ import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
3
+
4
+ import injector from '@thzero/library_common/utility/injector';
5
+
6
+ // require('../utility/string.cjs');
7
+ String.isNullOrEmpty = function(value) {
8
+ //return !(typeof value === 'string' && value.length > 0)
9
+ return !value;
10
+ }
11
+
12
+ String.isString = function(value) {
13
+ return (typeof value === "string" || value instanceof String);
14
+ }
15
+
16
+ String.trim = function(value) {
17
+ if (!value || !String.isString(value))
18
+ return value;
19
+ return value.trim();
20
+ }
21
+
22
+ const logicalAnd = 'and';
23
+ const logicalOr = 'or';
24
+
25
+ const authorizationCheckClaims = async (ctx, success, logical, security, logger) => {
26
+ if (!ctx)
27
+ return false;
28
+ if (!(ctx.state.claims && Array.isArray(ctx.state.claims)))
29
+ return false;
30
+
31
+ let result;
32
+ let roleAct;
33
+ let roleObj;
34
+ let roleParts;
35
+ for (const claim of ctx.state.claims) {
36
+ logger.debug('middleware', 'authorization', 'authorization.claim', claim, ctx.correlationId);
37
+
38
+ for (const role of ctx.state.roles) {
39
+ logger.debug('middleware', 'authorization', 'role', role, ctx.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, ctx.correlationId);
50
+ if (logical === logicalOr)
51
+ success = success || result;
52
+ else
53
+ success = success && result;
54
+ }
55
+ }
56
+
57
+ return success;
58
+ }
59
+
60
+ const authorizationCheckRoles = async (ctx, success, logical, security, logger) => {
61
+ if (!ctx)
62
+ return false;
63
+
64
+ logger.debug('middleware', 'authorizationCheckRoles', 'user', ctx.state.user, ctx.correlationId);
65
+ if (!(ctx.state.user && ctx.state.user.roles && Array.isArray(ctx.state.user.roles)))
66
+ return false;
67
+
68
+ logger.debug('middleware', 'authorizationCheckRoles', 'logical', logical, ctx.correlationId);
69
+
70
+ let result;
71
+ let roleAct;
72
+ let roleObj;
73
+ let roleParts;
74
+ for (const userRole of ctx.state.user.roles) {
75
+ logger.debug('middleware', 'authorizationCheckRoles', 'userRole', userRole, ctx.correlationId);
76
+
77
+ for (const role of ctx.state.roles) {
78
+ logger.debug('middleware', 'authorizationCheckRoles', 'role', role, ctx.correlationId);
79
+
80
+ roleParts = role.split('.');
81
+ if (roleParts && roleParts.length < 1)
82
+ success = false;
83
+
84
+ roleObj = roleParts[0];
85
+ roleAct = roleParts.length >= 2 ? roleParts[1] : null
86
+
87
+ result = await security.validate(userRole, null, roleObj, roleAct);
88
+ logger.debug('middleware', 'authorizationCheckRoles', 'result', result, ctx.correlationId);
89
+ if (logical === logicalOr) {
90
+ if (result)
91
+ return result;
92
+
93
+ success = false;
94
+ }
95
+ else
96
+ success = success && result;
97
+ }
98
+ }
99
+
100
+ return success;
101
+ }
102
+
103
+ const initalizeRoles = (ctx, roles, logger) => {
104
+ if (Array.isArray(roles)) {
105
+ // logger.debug('middleware', 'initalizeRoles', 'roles1a', roles);
106
+ ctx.state.roles = roles;
107
+ }
108
+ else if ((typeof(roles) === 'string') || (roles instanceof String)) {
109
+ // logger.debug('middleware', 'initalizeRoles', 'roles1b', roles);
110
+ ctx.state.roles = roles.split(',');
111
+ ctx.state.roles.map(item => item ? item.trim() : item);
112
+ }
113
+ }
114
+
115
+ const authorization = (roles, logical) => {
116
+ if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
117
+ logical = logicalOr;
118
+
119
+ return async (ctx, next) => {
120
+ const config = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG);
121
+ const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
122
+ const security = injector.getService(LibraryConstants.InjectorKeys.SERVICE_SECURITY);
123
+
124
+ // logger.debug('token', ctx.state.token);
125
+ logger.debug('middleware', 'authorization', 'user', ctx.state.user, ctx.correlationId);
126
+ logger.debug('middleware', 'authorization', 'claims', ctx.state.claims, ctx.correlationId);
127
+ logger.debug('middleware', 'authorization', 'roles1', roles, ctx.correlationId);
128
+ ctx.state.roles = [];
129
+ if (roles) {
130
+ // logger.debug('authorization.roles1', roles);
131
+ // logger.debug('authorization.roles1', (typeof roles));
132
+ // logger.debug('authorization.roles1', Array.isArray(roles));
133
+ // logger.debug('authorization.roles1', ((typeof(roles) === 'string') || (roles instanceof String)));
134
+ // if (Array.isArray(roles)) {
135
+ // // logger.debug('authorization.roles1a', roles);
136
+ // ctx.state.roles = roles;
137
+ // }
138
+ // else if ((typeof(roles) === 'string') || (roles instanceof String)) {
139
+ // // logger.debug('authorization.roles1b', roles);
140
+ // ctx.state.roles = roles.split(',');
141
+ // ctx.state.roles.map(item => item ? item.trim() : item);
142
+ // }
143
+ initalizeRoles(ctx, roles, logger);
144
+ }
145
+ logger.debug('middleware', 'authorization', 'roles2', ctx.state.roles, ctx.correlationId);
146
+
147
+ let success = false; //(logical === logicalOr ? false : true);
148
+ if (ctx.state.roles && Array.isArray(ctx.state.roles) && (ctx.state.roles.length > 0)) {
149
+ const auth = config.get('auth');
150
+ if (auth) {
151
+ logger.debug('middleware', 'authorization', 'auth.claims', auth.claims, ctx.correlationId);
152
+ logger.debug('middleware', 'authorization', 'auth.claims.check', auth.claims.check, ctx.correlationId);
153
+ }
154
+ if (auth && auth.claims && auth.claims.check)
155
+ success = await authorizationCheckClaims(ctx, (logical === logicalOr ? false : true), logical, security, logger);
156
+
157
+ if (!success)
158
+ success = await authorizationCheckRoles(ctx, (logical === logicalOr ? false : true), logical, security, logger);
159
+ }
160
+
161
+ logger.debug('middleware', 'authorization', 'success', null, ctx.state.success, ctx.correlationId);
162
+ if (success) {
163
+ await next();
164
+ return;
165
+ }
166
+
167
+ (async () => {
168
+ const usageMetrics = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USAGE_METRIC);
169
+ await usageMetrics.register(ctx).catch((err) => {
170
+ const logger = injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER);
171
+ logger.error(null, err);
172
+ });
173
+ })();
174
+
175
+ logger.warn('middleware', 'authorization', 'Unauthorized... authorization unknown', null, ctx.correlationId);
176
+ ctx.throw(401);
177
+ }
178
+ }
179
+
180
+ export default authorization;