@oondemand/oon-core-back 0.3.15 → 0.3.16
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/package.json
CHANGED
package/src/api/auth.routes.js
CHANGED
|
@@ -4,6 +4,7 @@ const auth = require("../core/middlewares/auth");
|
|
|
4
4
|
const ctx = require("../core/context");
|
|
5
5
|
const GenericError = require("../core/errors/GenericError");
|
|
6
6
|
const { asyncHandler, sendResponse } = require("../core/utils/helpers");
|
|
7
|
+
const { ensureInstance } = require("../core/utils/instanceActivation");
|
|
7
8
|
const {
|
|
8
9
|
LOGIN_PATH,
|
|
9
10
|
getBaseUrl,
|
|
@@ -55,17 +56,18 @@ function translateProviderError(error) {
|
|
|
55
56
|
return new GenericError(message, { statusCode });
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
async function
|
|
59
|
+
async function authenticateRoot(req, res, authorization, instance) {
|
|
59
60
|
const authenticate = ctx.config.auth?.authenticate;
|
|
60
|
-
if (typeof authenticate
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
61
|
+
if (instance.ecosystemRole === "root" && typeof authenticate === "function") {
|
|
62
|
+
const result = await authenticate({ authorization, req });
|
|
63
|
+
const statusCode = result?.statusCode || 200;
|
|
64
|
+
const data = result?.data || result;
|
|
65
|
+
|
|
66
|
+
res.set("Cache-Control", "no-store");
|
|
67
|
+
res.status(statusCode).json(data);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
router.post(
|
|
@@ -73,7 +75,8 @@ router.post(
|
|
|
73
75
|
asyncHandler(async (req, res) => {
|
|
74
76
|
const authorization = readBasicAuthorization(req);
|
|
75
77
|
|
|
76
|
-
|
|
78
|
+
const instance = await ensureInstance();
|
|
79
|
+
if (await authenticateRoot(req, res, authorization, instance)) return;
|
|
77
80
|
|
|
78
81
|
const baseUrl = getBaseUrl(req);
|
|
79
82
|
const appCode = getAppCode();
|
|
@@ -81,10 +84,7 @@ router.post(
|
|
|
81
84
|
try {
|
|
82
85
|
const response = await axios.post(
|
|
83
86
|
`${baseUrl}${LOGIN_PATH}`,
|
|
84
|
-
{
|
|
85
|
-
codigoApp: appCode,
|
|
86
|
-
codigo: appCode,
|
|
87
|
-
},
|
|
87
|
+
{}, // codigoApp: appCode é enviado no header x-app-code.
|
|
88
88
|
{
|
|
89
89
|
timeout: getTimeout(),
|
|
90
90
|
maxRedirects: 0,
|
|
@@ -17,14 +17,25 @@ async function defaultVerifyToken(token, { req } = {}) {
|
|
|
17
17
|
const baseUrl = getBaseUrl(req);
|
|
18
18
|
const appCode = getAppCode();
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
let response;
|
|
21
|
+
try {
|
|
22
|
+
response = await axios.get(`${baseUrl}${PERMISSION_PATH}`, {
|
|
23
|
+
timeout: getTimeout(),
|
|
24
|
+
maxRedirects: 0,
|
|
25
|
+
headers: {
|
|
26
|
+
Authorization: `Bearer ${token}`,
|
|
27
|
+
"x-app-code": appCode,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
} catch (error) {
|
|
31
|
+
const err = new Error(
|
|
32
|
+
error.response?.data?.message ||
|
|
33
|
+
error.message ||
|
|
34
|
+
"Token inválido ou erro na autenticação.",
|
|
35
|
+
);
|
|
36
|
+
err.statusCode = error.response?.status || error.statusCode || 401;
|
|
37
|
+
throw err;
|
|
38
|
+
}
|
|
28
39
|
|
|
29
40
|
const data = response.data.usuario;
|
|
30
41
|
if (!data) {
|
|
@@ -34,7 +45,7 @@ async function defaultVerifyToken(token, { req } = {}) {
|
|
|
34
45
|
}
|
|
35
46
|
|
|
36
47
|
const tipoAcesso =
|
|
37
|
-
data.aplicativo?.tipoAcesso || data.tipoAcesso || data.
|
|
48
|
+
data.aplicativo?.tipoAcesso || data.tipoAcesso || data.tipo || data.perfil;
|
|
38
49
|
|
|
39
50
|
return {
|
|
40
51
|
...data,
|