@agilesoft/ags_authrest2 1.0.1 → 1.0.2
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/index.js +94 -90
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -94,95 +94,99 @@ class Auth {
|
|
|
94
94
|
|
|
95
95
|
Middleware(req, res, next) {
|
|
96
96
|
(async () => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
req.headers.authorization
|
|
97
|
+
try {
|
|
98
|
+
var headersEncrypter = false;
|
|
99
|
+
var bodyOriginal = req.body;
|
|
100
|
+
if (req.headers.authorization) {
|
|
101
|
+
let findSecurity = req.headers.authorization.indexOf("securityAgs");
|
|
102
|
+
if (findSecurity == -1) {
|
|
103
|
+
const isb64 = isBase64(bodyOriginal.encrypData);
|
|
104
|
+
if (isb64) {
|
|
105
|
+
req.headers.authorization = await decrypt(
|
|
106
|
+
req.headers.authorization
|
|
107
|
+
);
|
|
108
|
+
headersEncrypter = true;
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
const replaceStr = req.headers.authorization.replace(
|
|
112
|
+
/securityAgs/g,
|
|
113
|
+
"/"
|
|
106
114
|
);
|
|
107
|
-
|
|
115
|
+
const isb64 = isBase64(bodyOriginal.encrypData);
|
|
116
|
+
// console.log(isb64);
|
|
117
|
+
// console.log("decrypt header");
|
|
118
|
+
if (isb64) {
|
|
119
|
+
req.headers.authorization = await decrypt(replaceStr);
|
|
120
|
+
headersEncrypter = true;
|
|
121
|
+
}
|
|
108
122
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
headersEncrypter = true;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
jwt.verify(
|
|
123
|
-
req.headers.authorization,
|
|
124
|
-
await SECRET(R_TOKEN),
|
|
125
|
-
(err, payload) => {
|
|
126
|
-
(async () => {
|
|
127
|
-
if (err) {
|
|
128
|
-
console.log(err);
|
|
129
|
-
return res.sendStatus(400);
|
|
130
|
-
} else {
|
|
131
|
-
var isExpiredToken = false;
|
|
123
|
+
jwt.verify(
|
|
124
|
+
req.headers.authorization,
|
|
125
|
+
await SECRET(R_TOKEN),
|
|
126
|
+
(err, payload) => {
|
|
127
|
+
(async () => {
|
|
128
|
+
if (err) {
|
|
129
|
+
console.log(err);
|
|
130
|
+
return res.sendStatus(400);
|
|
131
|
+
} else {
|
|
132
|
+
var isExpiredToken = false;
|
|
132
133
|
|
|
133
|
-
|
|
134
|
+
var dateNow = new Date();
|
|
134
135
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
if (payload.sub === R_USER && isExpiredToken === false) {
|
|
139
|
-
const chk = await check(req.headers.authorization);
|
|
140
|
-
// console.log(chk);
|
|
141
|
-
arr.push(req.headers.authorization);
|
|
142
|
-
if (
|
|
143
|
-
chk == true &&
|
|
144
|
-
req.headers.testdev !== true &&
|
|
145
|
-
req.headers.testdev !== "true"
|
|
146
|
-
) {
|
|
147
|
-
return res.send("duplicate token");
|
|
148
|
-
}
|
|
149
|
-
if (arr.length > 200) {
|
|
150
|
-
arr = [];
|
|
136
|
+
if (payload.exp < dateNow.getTime()) {
|
|
137
|
+
isExpiredToken = true;
|
|
151
138
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
139
|
+
if (payload.sub === R_USER && isExpiredToken === false) {
|
|
140
|
+
const chk = await check(req.headers.authorization);
|
|
141
|
+
// console.log(chk);
|
|
142
|
+
arr.push(req.headers.authorization);
|
|
143
|
+
if (
|
|
144
|
+
chk == true &&
|
|
145
|
+
req.headers.testdev !== true &&
|
|
146
|
+
req.headers.testdev !== "true"
|
|
147
|
+
) {
|
|
148
|
+
return res.send("duplicate token");
|
|
149
|
+
}
|
|
150
|
+
if (arr.length > 200) {
|
|
151
|
+
arr = [];
|
|
152
|
+
}
|
|
153
|
+
// console.log(`headersEncrypter ${headersEncrypter}`);
|
|
154
|
+
if (headersEncrypter === true) {
|
|
155
|
+
if (bodyOriginal.encrypData) {
|
|
156
|
+
const isb64 = isBase64(bodyOriginal.encrypData);
|
|
157
|
+
if (isb64) {
|
|
158
|
+
const result = await decrypt(bodyOriginal.encrypData);
|
|
159
|
+
try {
|
|
160
|
+
req.body = JSON.parse(result);
|
|
161
|
+
next();
|
|
162
|
+
} catch (error) {
|
|
163
|
+
req.body = error;
|
|
164
|
+
next();
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
req.body = { result: `isb64 false` };
|
|
163
168
|
next();
|
|
164
169
|
}
|
|
165
170
|
} else {
|
|
166
|
-
req.body = { result:
|
|
171
|
+
req.body = { result: "no encrypData" };
|
|
167
172
|
next();
|
|
168
173
|
}
|
|
169
174
|
} else {
|
|
170
|
-
req.body = { result: "no encrypData" };
|
|
171
175
|
next();
|
|
172
176
|
}
|
|
173
177
|
} else {
|
|
174
|
-
|
|
178
|
+
return res.sendStatus(400);
|
|
175
179
|
}
|
|
176
|
-
} else {
|
|
177
|
-
return res.sendStatus(400);
|
|
178
180
|
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
181
|
+
})();
|
|
182
|
+
}
|
|
183
|
+
);
|
|
184
|
+
} else {
|
|
185
|
+
// console.log("bypass");
|
|
186
|
+
// next();
|
|
187
|
+
return res.sendStatus(400);
|
|
188
|
+
}
|
|
189
|
+
} catch (error) {
|
|
186
190
|
return res.sendStatus(400);
|
|
187
191
|
}
|
|
188
192
|
})();
|
|
@@ -284,29 +288,29 @@ class Auth {
|
|
|
284
288
|
return resolve(result);
|
|
285
289
|
} catch (error) {
|
|
286
290
|
console.log(error);
|
|
287
|
-
reject(error);
|
|
291
|
+
return reject(error);
|
|
288
292
|
}
|
|
289
293
|
})();
|
|
290
294
|
});
|
|
291
295
|
}
|
|
292
296
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
+
decrypbody(body, secret) {
|
|
298
|
+
if (secret === "" || secret === undefined) {
|
|
299
|
+
secret = R_TOKEN;
|
|
300
|
+
}
|
|
297
301
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
302
|
+
return new Promise(function (resolve, reject) {
|
|
303
|
+
(async () => {
|
|
304
|
+
try {
|
|
305
|
+
const result = await decrypt(body, secret);
|
|
306
|
+
return resolve(result);
|
|
307
|
+
} catch (error) {
|
|
308
|
+
console.log(error);
|
|
309
|
+
return reject(error);
|
|
310
|
+
}
|
|
311
|
+
})();
|
|
312
|
+
});
|
|
313
|
+
}
|
|
310
314
|
|
|
311
315
|
test(secret) {
|
|
312
316
|
if (secret === "" || secret === undefined) {
|