propro-utils 1.3.9 → 1.3.11

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "propro-utils",
3
- "version": "1.3.9",
3
+ "version": "1.3.11",
4
4
  "description": "Auth middleware for propro-auth",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -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 {
@@ -23,20 +24,22 @@ function proproAuthMiddleware(options = {}) {
23
24
  let token;
24
25
  if (req.headers.authorization?.startsWith("Bearer ")) {
25
26
  token = req.headers.authorization.split(" ")[1];
26
- } else {
27
+ } else if (req.cookies && req.cookies['x-access-token']) {
27
28
  // If not present in Authorization header, try to get it from cookies
28
29
  token = req.cookies['x-access-token'];
29
30
  }
30
31
 
31
32
  if (token) {
32
- const verifiedToken = verifyJWT(token, secret);
33
+ console.log("verifying access token", token);
34
+ const verifiedToken = await verifyJWT(token, secret);
33
35
  if (verifiedToken) {
34
36
  req.account = verifiedToken;
35
- } else {
37
+ return next();
38
+ } else if (req.cookies && req.cookies['x-refresh-token']) {
36
39
  // If token is invalid or expired, try to refresh it
37
40
  const refreshToken = req.cookies['x-refresh-token'];
38
41
  if (refreshToken) {
39
- const newTokenData = await refreshToken(authUrl, refreshToken, clientId, clientSecret);
42
+ const newTokenData = await refreshTokenMiddleware(req, res, next);
40
43
  if (newTokenData) {
41
44
  // Set the new tokens in the cookies
42
45
  res.cookie("x-refresh-token", newTokenData.tokens.refresh.token, {
@@ -55,6 +58,7 @@ function proproAuthMiddleware(options = {}) {
55
58
  }
56
59
  } else {
57
60
  req.account = undefined;
61
+ res.status(401).send("Unauthorized: Invalid or expired token");
58
62
  }
59
63
 
60
64
  if (!["/api/auth", "/api/callback"].includes(req.path)) {
@@ -101,7 +105,7 @@ function proproAuthMiddleware(options = {}) {
101
105
  }
102
106
  } catch (error) {
103
107
  console.error("Error in proproAuthMiddleware:", error);
104
- res.status(500).send("Internal Server Error");
108
+ res.status(401).send("Unauthorized: Invalid or expired token");
105
109
  }
106
110
  };
107
111
  }
@@ -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
  }