propro-utils 1.3.10 → 1.3.12
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/server/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const {
|
|
|
4
4
|
exchangeToken,
|
|
5
5
|
VerifyAccount,
|
|
6
6
|
} = require("./middleware/verifyToken");
|
|
7
|
+
const {refreshTokenMiddleware} = require("./middleware");
|
|
7
8
|
// Main middleware function
|
|
8
9
|
function proproAuthMiddleware(options = {}) {
|
|
9
10
|
const {
|
|
@@ -17,28 +18,26 @@ function proproAuthMiddleware(options = {}) {
|
|
|
17
18
|
} = options;
|
|
18
19
|
|
|
19
20
|
return async (req, res, next) => {
|
|
20
|
-
console.log("req.cookies", req.cookies);
|
|
21
21
|
try {
|
|
22
|
-
// Try to get the token from the Authorization header
|
|
23
22
|
let token;
|
|
24
23
|
if (req.headers.authorization?.startsWith("Bearer ")) {
|
|
25
24
|
token = req.headers.authorization.split(" ")[1];
|
|
26
25
|
} else if (req.cookies && req.cookies['x-access-token']) {
|
|
27
|
-
// If not present in Authorization header, try to get it from cookies
|
|
28
26
|
token = req.cookies['x-access-token'];
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
if (token) {
|
|
32
|
-
|
|
30
|
+
console.log("verifying access token", token);
|
|
31
|
+
const verifiedToken = await verifyJWT(token);
|
|
32
|
+
console.log("verifiedToken", verifiedToken);
|
|
33
33
|
if (verifiedToken) {
|
|
34
34
|
req.account = verifiedToken;
|
|
35
|
+
return next();
|
|
35
36
|
} else if (req.cookies && req.cookies['x-refresh-token']) {
|
|
36
|
-
// If token is invalid or expired, try to refresh it
|
|
37
37
|
const refreshToken = req.cookies['x-refresh-token'];
|
|
38
38
|
if (refreshToken) {
|
|
39
|
-
const newTokenData = await
|
|
39
|
+
const newTokenData = await refreshTokenMiddleware(req, res, next);
|
|
40
40
|
if (newTokenData) {
|
|
41
|
-
// Set the new tokens in the cookies
|
|
42
41
|
res.cookie("x-refresh-token", newTokenData.tokens.refresh.token, {
|
|
43
42
|
httpOnly: true,
|
|
44
43
|
secure: process.env.NODE_ENV === "production",
|
|
@@ -55,6 +54,7 @@ function proproAuthMiddleware(options = {}) {
|
|
|
55
54
|
}
|
|
56
55
|
} else {
|
|
57
56
|
req.account = undefined;
|
|
57
|
+
res.status(401).send("Unauthorized: Invalid or expired token");
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
if (!["/api/auth", "/api/callback"].includes(req.path)) {
|
|
@@ -101,7 +101,7 @@ function proproAuthMiddleware(options = {}) {
|
|
|
101
101
|
}
|
|
102
102
|
} catch (error) {
|
|
103
103
|
console.error("Error in proproAuthMiddleware:", error);
|
|
104
|
-
res.status(
|
|
104
|
+
res.status(401).send("Unauthorized: Invalid or expired token");
|
|
105
105
|
}
|
|
106
106
|
};
|
|
107
107
|
}
|
|
@@ -8,10 +8,26 @@ const tokenCache = new Map();
|
|
|
8
8
|
* @param {string} secret - The secret used to sign the JWT.
|
|
9
9
|
* @returns {object|null} - The decoded payload of the JWT if it is valid, or null if it is not valid.
|
|
10
10
|
*/
|
|
11
|
-
function verifyJWT(token, secret) {
|
|
11
|
+
async function verifyJWT(token, secret= "thisisasamplesecret") {
|
|
12
12
|
try {
|
|
13
13
|
return jwt.verify(token, secret);
|
|
14
14
|
} catch (err) {
|
|
15
|
+
if (err.name === 'TokenExpiredError') {
|
|
16
|
+
const newTokenData = await callTokenEndpoint(token);
|
|
17
|
+
return newTokenData ? jwt.verify(newTokenData, secret) : null;
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function callTokenEndpoint(token) {
|
|
24
|
+
try {
|
|
25
|
+
const response = await axios.post('http://auth:8182/tokens', {
|
|
26
|
+
refreshToken: token,
|
|
27
|
+
});
|
|
28
|
+
return response.data.tokens ? response.data.tokens.access.token : null;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error('Error during token refresh:', error);
|
|
15
31
|
return null;
|
|
16
32
|
}
|
|
17
33
|
}
|