@thzero/library_server_fastify 0.17.7 → 0.17.8
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/middleware/authorization.js +6 -114
- package/package.json +2 -2
|
@@ -17,9 +17,6 @@ String.trim = function(value) {
|
|
|
17
17
|
return value.trim();
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const logicalAnd = 'and';
|
|
21
|
-
const logicalOr = 'or';
|
|
22
|
-
|
|
23
20
|
class DefaultAuthenticationMiddleware {
|
|
24
21
|
constructor() {
|
|
25
22
|
this._serviceConfig = null;
|
|
@@ -43,16 +40,8 @@ class DefaultAuthenticationMiddleware {
|
|
|
43
40
|
}
|
|
44
41
|
|
|
45
42
|
async authorization(request, reply, done, options) {
|
|
46
|
-
let logical =
|
|
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
|
-
}
|
|
43
|
+
let logical = this._serviceSecurity.initializeOptionsLogical(request.correlationId, options);
|
|
44
|
+
let roles = this._serviceSecurity.initializeOptionsRoles(request.correlationId, options);
|
|
56
45
|
|
|
57
46
|
// this._serviceLogger.debug('token', request.token);
|
|
58
47
|
this._serviceLogger.debug('middleware', 'authorization', 'user', request.user, request.correlationId);
|
|
@@ -73,11 +62,11 @@ class DefaultAuthenticationMiddleware {
|
|
|
73
62
|
// request.roles = roles.split(',');
|
|
74
63
|
// request.roles.map(item => item ? item.trim() : item);
|
|
75
64
|
// }
|
|
76
|
-
this.
|
|
65
|
+
request.roles = this._serviceSecurity.initializeRoles(request.correlationId, request.roles, roles);
|
|
77
66
|
}
|
|
78
67
|
this._serviceLogger.debug('middleware', 'authorization', 'roles2', request.roles, request.correlationId);
|
|
79
68
|
|
|
80
|
-
let success = false;
|
|
69
|
+
let success = false;
|
|
81
70
|
if (request.roles && Array.isArray(request.roles) && (request.roles.length > 0)) {
|
|
82
71
|
const auth = this._serviceConfig.get('auth');
|
|
83
72
|
if (auth) {
|
|
@@ -85,10 +74,10 @@ class DefaultAuthenticationMiddleware {
|
|
|
85
74
|
this._serviceLogger.debug('middleware', 'authorization', 'auth.claims.check', auth.claims.check, request.correlationId);
|
|
86
75
|
}
|
|
87
76
|
if (auth && auth.claims && auth.claims.check)
|
|
88
|
-
success = await this.
|
|
77
|
+
success = await this._serviceSecurity.authorizationCheckClaims(request.correlationId, request.claims, request.roles, logical);
|
|
89
78
|
|
|
90
79
|
if (!success)
|
|
91
|
-
success = await this.
|
|
80
|
+
success = await this._serviceSecurity.authorizationCheckRoles(request.correlationId, request.user, request.roles, logical);
|
|
92
81
|
}
|
|
93
82
|
|
|
94
83
|
this._serviceLogger.debug('middleware', 'authorization', 'success', null, request.success, request.correlationId);
|
|
@@ -118,103 +107,6 @@ class DefaultAuthenticationMiddleware {
|
|
|
118
107
|
// done(new Error('Unauthorized... authentication unknown')); // not for async
|
|
119
108
|
throw new Error('Unauthorized... authentication unknown');
|
|
120
109
|
}
|
|
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
110
|
}
|
|
215
111
|
|
|
216
|
-
// const authorization = (roles, logical) => {
|
|
217
|
-
// if (String.isNullOrEmpty(logical) || (logical !== logicalAnd) || (logical !== logicalOr))
|
|
218
|
-
// logical = logicalOr;
|
|
219
|
-
|
|
220
112
|
export default DefaultAuthenticationMiddleware;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thzero/library_server_fastify",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.17.
|
|
4
|
+
"version": "0.17.8",
|
|
5
5
|
"version_major": 0,
|
|
6
6
|
"version_minor": 17,
|
|
7
|
-
"version_patch":
|
|
7
|
+
"version_patch": 8,
|
|
8
8
|
"version_date": "04/15/2023",
|
|
9
9
|
"description": "An opinionated library of common functionality to bootstrap a Fastify based API application.",
|
|
10
10
|
"author": "thZero",
|