propro-utils 1.3.23 → 1.3.25
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/middlewares/access_token.js +11 -8
- package/package.json +4 -3
- package/src/server/index.js +8 -0
- package/utils/redis.js +18 -0
|
@@ -1,24 +1,27 @@
|
|
|
1
|
-
require("dotenv").config()
|
|
1
|
+
require("dotenv").config();
|
|
2
2
|
const axios = require("axios");
|
|
3
|
+
const {getOrSetCache} = require("../utils/redis");
|
|
3
4
|
|
|
4
|
-
const authValidation = (requiredPermissions = []) => {
|
|
5
|
+
const authValidation = (redisClient, requiredPermissions = []) => {
|
|
5
6
|
return async (req, res, next) => {
|
|
6
7
|
try {
|
|
7
8
|
const accessToken = req.cookies['x-access-token'] || req.headers.authorization?.split(" ")[1];
|
|
8
9
|
|
|
9
10
|
if (!accessToken) {
|
|
10
|
-
|
|
11
|
+
return res.status(403).json({ error: "Access token is required" });
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
const response = await
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const response = await getOrSetCache(redisClient, accessToken, async () => {
|
|
15
|
+
return await axios.post(`${process.env.AUTH_URL}/api/v1/auth/validateToken`, {
|
|
16
|
+
accessToken: accessToken,
|
|
17
|
+
requiredPermissions: requiredPermissions
|
|
18
|
+
});
|
|
16
19
|
});
|
|
17
|
-
const { accountId, validPermissions } = response.data;
|
|
18
20
|
|
|
21
|
+
const { accountId, validPermissions } = response.data;
|
|
19
22
|
|
|
20
23
|
if (!validPermissions) {
|
|
21
|
-
|
|
24
|
+
return res.status(403).json({ error: "Invalid permissions" });
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
req.account = accountId;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "propro-utils",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.25",
|
|
4
4
|
"description": "Auth middleware for propro-auth",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -59,11 +59,12 @@
|
|
|
59
59
|
"@react-email/tailwind": "^0.0.12",
|
|
60
60
|
"@react-email/text": "0.0.5",
|
|
61
61
|
"axios": "^1.6.1",
|
|
62
|
-
"dotenv": "^16.
|
|
62
|
+
"dotenv": "^16.4.1",
|
|
63
63
|
"express-rate-limit": "^7.1.4",
|
|
64
64
|
"nodemailer": "^6.9.7",
|
|
65
65
|
"nodemailer-mailgun-transport": "^2.1.5",
|
|
66
66
|
"querystring": "^0.2.1",
|
|
67
|
-
"react-email": "^1.9.5"
|
|
67
|
+
"react-email": "^1.9.5",
|
|
68
|
+
"redis": "^4.6.12"
|
|
68
69
|
}
|
|
69
70
|
}
|
package/src/server/index.js
CHANGED
|
@@ -57,14 +57,22 @@ function proproAuthMiddleware(options = {}) {
|
|
|
57
57
|
redirectUri
|
|
58
58
|
);
|
|
59
59
|
|
|
60
|
+
const currentDateTime = new Date();
|
|
61
|
+
|
|
62
|
+
const refreshMaxAge = tokenData.tokens.refresh.expires.getTime() - currentDateTime.getTime();
|
|
63
|
+
|
|
60
64
|
res.cookie("x-refresh-token", tokenData.tokens.refresh.token, {
|
|
61
65
|
httpOnly: true,
|
|
62
66
|
secure: process.env.NODE_ENV === "production",
|
|
67
|
+
maxAge: refreshMaxAge
|
|
63
68
|
});
|
|
64
69
|
|
|
70
|
+
const accessMaxAge = tokenData.tokens.access.expires.getTime() - currentDateTime.getTime();
|
|
71
|
+
|
|
65
72
|
res.cookie("x-access-token", tokenData.tokens.access.token, {
|
|
66
73
|
httpOnly: true,
|
|
67
74
|
secure: process.env.NODE_ENV === "production",
|
|
75
|
+
maxAge: accessMaxAge
|
|
68
76
|
});
|
|
69
77
|
|
|
70
78
|
const redirectUrl = `http://${tokenData.redirectUrl}/`;
|
package/utils/redis.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const getOrSetCache = async (redisClient, key, service, time = 1800) => {
|
|
2
|
+
try {
|
|
3
|
+
const cachedValue = await redisClient.get(key);
|
|
4
|
+
if (cachedValue) {
|
|
5
|
+
console.log(`Cache hit for key: ${key}`);
|
|
6
|
+
return JSON.parse(cachedValue);
|
|
7
|
+
}
|
|
8
|
+
console.log(`Cache miss for key: ${key}. Fetching and caching value.`);
|
|
9
|
+
const value = await service();
|
|
10
|
+
await redisClient.setEx(key, time, JSON.stringify(value));
|
|
11
|
+
return value;
|
|
12
|
+
} catch (error) {
|
|
13
|
+
console.error(`Error in getOrSetCache for key ${key}:`, error);
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports = { getOrSetCache };
|