propro-utils 1.3.13 → 1.3.15
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/dist/NotifLocal.js +81 -0
- package/dist/emailStrategy.js +51 -26
- package/dist/mailjetStrategy.js +2 -0
- package/dist/twilloStrategy.js +2 -0
- package/package.json +1 -1
- package/src/server/index.js +85 -41
- package/src/server/middleware/refreshToken.js +16 -6
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
var React = require("react");
|
|
8
|
+
var _require = require("@react-email/components"),
|
|
9
|
+
Button = _require.Button,
|
|
10
|
+
Container = _require.Container,
|
|
11
|
+
Head = _require.Head,
|
|
12
|
+
Html = _require.Html,
|
|
13
|
+
Img = _require.Img,
|
|
14
|
+
Section = _require.Section,
|
|
15
|
+
Text = _require.Text;
|
|
16
|
+
var NotifLocal = function NotifLocal(_ref) {
|
|
17
|
+
var body = _ref.body,
|
|
18
|
+
app = _ref.app,
|
|
19
|
+
label = _ref.label,
|
|
20
|
+
url = _ref.url;
|
|
21
|
+
var getLogoUrl = function getLogoUrl(app) {
|
|
22
|
+
switch (app) {
|
|
23
|
+
case "hubhub":
|
|
24
|
+
return "/assets/logos/hubhub.svg";
|
|
25
|
+
case "propro":
|
|
26
|
+
return "/assets/logos/propro.svg";
|
|
27
|
+
default:
|
|
28
|
+
return "/assets/logos/hubhub.svg";
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return /*#__PURE__*/React.createElement(Html, null, /*#__PURE__*/React.createElement(Head, null), /*#__PURE__*/React.createElement(Section, {
|
|
32
|
+
style: main
|
|
33
|
+
}, /*#__PURE__*/React.createElement(Container, {
|
|
34
|
+
style: container
|
|
35
|
+
}, /*#__PURE__*/React.createElement(Img, {
|
|
36
|
+
src: getLogoUrl(app)
|
|
37
|
+
}), /*#__PURE__*/React.createElement(Section, null, /*#__PURE__*/React.createElement(Text, {
|
|
38
|
+
style: text
|
|
39
|
+
}, body || "Default body text here."), label && /*#__PURE__*/React.createElement(Button, {
|
|
40
|
+
href: url,
|
|
41
|
+
style: btn
|
|
42
|
+
}, label)))));
|
|
43
|
+
};
|
|
44
|
+
var main = {
|
|
45
|
+
backgroundColor: "#ffffff",
|
|
46
|
+
margin: "0 auto"
|
|
47
|
+
};
|
|
48
|
+
var container = {
|
|
49
|
+
border: "1px solid #eaeaea",
|
|
50
|
+
borderRadius: "5px",
|
|
51
|
+
margin: "40px auto",
|
|
52
|
+
padding: "20px",
|
|
53
|
+
width: "465px"
|
|
54
|
+
};
|
|
55
|
+
var h1 = {
|
|
56
|
+
color: "#000",
|
|
57
|
+
fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
|
|
58
|
+
fontSize: "24px",
|
|
59
|
+
fontWeight: "normal",
|
|
60
|
+
textAlign: "center",
|
|
61
|
+
margin: "30px 0",
|
|
62
|
+
padding: "0"
|
|
63
|
+
};
|
|
64
|
+
var text = {
|
|
65
|
+
color: "#000",
|
|
66
|
+
fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
|
|
67
|
+
fontSize: "14px",
|
|
68
|
+
lineHeight: "24px"
|
|
69
|
+
};
|
|
70
|
+
var btn = {
|
|
71
|
+
backgroundColor: "#000",
|
|
72
|
+
borderRadius: "5px",
|
|
73
|
+
color: "#fff",
|
|
74
|
+
fontFamily: "-apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif",
|
|
75
|
+
fontSize: "12px",
|
|
76
|
+
fontWeight: 500,
|
|
77
|
+
lineHeight: "50px",
|
|
78
|
+
textDecoration: "none",
|
|
79
|
+
textAlign: "center"
|
|
80
|
+
};
|
|
81
|
+
var _default = exports["default"] = NotifLocal;
|
package/dist/emailStrategy.js
CHANGED
|
@@ -9,62 +9,87 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+
|
|
|
9
9
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
10
10
|
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
11
11
|
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
12
|
-
|
|
12
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
13
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
14
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
15
|
+
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
16
|
+
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
17
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
18
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
19
|
+
require("dotenv").config();
|
|
13
20
|
var React = require("react");
|
|
14
21
|
var ReactDOMServer = require("react-dom/server");
|
|
15
22
|
var NotifLocal = require("./NotifLocal");
|
|
16
23
|
var nodemailer = require("nodemailer");
|
|
17
24
|
var mailgun = require("nodemailer-mailgun-transport");
|
|
18
|
-
var
|
|
25
|
+
var _require = require("./template.data"),
|
|
26
|
+
templateData = _require.templateData;
|
|
27
|
+
var _require2 = require("./baseStrategy"),
|
|
28
|
+
NotificationStrategy = _require2.NotificationStrategy;
|
|
29
|
+
var mailgunTransport = nodemailer.createTransport(mailgun({
|
|
30
|
+
host: process.env.SMTP_HOST,
|
|
31
|
+
auth: {
|
|
32
|
+
api_key: process.env.MAILGUN_API_KEY,
|
|
33
|
+
domain: process.env.EMAIL_DOMAIN
|
|
34
|
+
}
|
|
35
|
+
}));
|
|
19
36
|
|
|
20
37
|
/**
|
|
21
38
|
* Represents a strategy for sending email notifications.
|
|
22
39
|
*/
|
|
23
|
-
var EmailNotificationStrategy = /*#__PURE__*/function () {
|
|
40
|
+
var EmailNotificationStrategy = /*#__PURE__*/function (_NotificationStrategy) {
|
|
41
|
+
_inherits(EmailNotificationStrategy, _NotificationStrategy);
|
|
42
|
+
var _super = _createSuper(EmailNotificationStrategy);
|
|
24
43
|
function EmailNotificationStrategy() {
|
|
25
44
|
_classCallCheck(this, EmailNotificationStrategy);
|
|
26
|
-
|
|
27
|
-
host: process.env.SMTP_HOST,
|
|
28
|
-
auth: {
|
|
29
|
-
// api_key: process.env.MAILGUN_API_KEY,
|
|
30
|
-
api_key: "3aa03bb5e30f28345ff12a46c7977b20-e438c741-e3e2e5ab",
|
|
31
|
-
domain: process.env.EMAIL_DOMAIN
|
|
32
|
-
}
|
|
33
|
-
}));
|
|
45
|
+
return _super.apply(this, arguments);
|
|
34
46
|
}
|
|
35
47
|
_createClass(EmailNotificationStrategy, [{
|
|
36
48
|
key: "sendNotification",
|
|
37
49
|
value: function () {
|
|
38
50
|
var _sendNotification = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(to, templateName, templateArgs) {
|
|
39
|
-
var selectedTemplate, emailHTML, message, info;
|
|
51
|
+
var templates, selectedTemplate, emailHTML, message, info;
|
|
40
52
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
41
53
|
while (1) switch (_context.prev = _context.next) {
|
|
42
54
|
case 0:
|
|
43
|
-
|
|
44
|
-
|
|
55
|
+
templates = templateData(templateArgs || {});
|
|
56
|
+
selectedTemplate = templates[templateName];
|
|
57
|
+
if (selectedTemplate) {
|
|
58
|
+
_context.next = 4;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
throw new Error("Template '".concat(templateName, "' not found"));
|
|
62
|
+
case 4:
|
|
63
|
+
console.log("selectedTemplate", selectedTemplate);
|
|
64
|
+
console.log("templateData", templateData);
|
|
65
|
+
// const emailHTML = ReactDOMServer.renderToString(
|
|
66
|
+
// React.createElement(NotifLocal, selectedTemplate)
|
|
67
|
+
// );
|
|
68
|
+
emailHTML = "<h1>".concat(selectedTemplate.body, "</h1>");
|
|
45
69
|
message = {
|
|
46
70
|
from: process.env.EMAIL_FROM,
|
|
47
71
|
to: to,
|
|
48
|
-
subject: selectedTemplate.subject,
|
|
72
|
+
subject: selectedTemplate.subject || "New Notification",
|
|
49
73
|
html: emailHTML
|
|
50
74
|
};
|
|
51
|
-
_context.prev =
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
75
|
+
_context.prev = 8;
|
|
76
|
+
console.log("Email Message Object:", message);
|
|
77
|
+
_context.next = 12;
|
|
78
|
+
return mailgunTransport.sendMail(message);
|
|
79
|
+
case 12:
|
|
55
80
|
info = _context.sent;
|
|
56
81
|
console.log("Message sent: %s", info.messageId);
|
|
57
|
-
_context.next =
|
|
82
|
+
_context.next = 19;
|
|
58
83
|
break;
|
|
59
|
-
case
|
|
60
|
-
_context.prev =
|
|
61
|
-
_context.t0 = _context["catch"](
|
|
84
|
+
case 16:
|
|
85
|
+
_context.prev = 16;
|
|
86
|
+
_context.t0 = _context["catch"](8);
|
|
62
87
|
console.error("Error sending email", _context.t0);
|
|
63
|
-
case
|
|
88
|
+
case 19:
|
|
64
89
|
case "end":
|
|
65
90
|
return _context.stop();
|
|
66
91
|
}
|
|
67
|
-
}, _callee,
|
|
92
|
+
}, _callee, null, [[8, 16]]);
|
|
68
93
|
}));
|
|
69
94
|
function sendNotification(_x, _x2, _x3) {
|
|
70
95
|
return _sendNotification.apply(this, arguments);
|
|
@@ -73,7 +98,7 @@ var EmailNotificationStrategy = /*#__PURE__*/function () {
|
|
|
73
98
|
}()
|
|
74
99
|
}]);
|
|
75
100
|
return EmailNotificationStrategy;
|
|
76
|
-
}();
|
|
101
|
+
}(NotificationStrategy);
|
|
77
102
|
module.exports = {
|
|
78
103
|
EmailNotificationStrategy: EmailNotificationStrategy
|
|
79
104
|
};
|
package/dist/mailjetStrategy.js
CHANGED
|
@@ -17,6 +17,8 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen
|
|
|
17
17
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
18
18
|
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
19
19
|
var axios = require("axios");
|
|
20
|
+
var _require = require("./baseStrategy"),
|
|
21
|
+
NotificationStrategy = _require.NotificationStrategy;
|
|
20
22
|
|
|
21
23
|
/**
|
|
22
24
|
* Mailjet SMS Notification Strategy.
|
package/dist/twilloStrategy.js
CHANGED
|
@@ -17,6 +17,8 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen
|
|
|
17
17
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
18
18
|
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
19
19
|
var twilio = require("twilio");
|
|
20
|
+
var _require = require("./baseStrategy"),
|
|
21
|
+
NotificationStrategy = _require.NotificationStrategy;
|
|
20
22
|
|
|
21
23
|
/**
|
|
22
24
|
* Twilio SMS Notification Strategy class.
|
package/package.json
CHANGED
package/src/server/index.js
CHANGED
|
@@ -4,8 +4,90 @@ const {
|
|
|
4
4
|
exchangeToken,
|
|
5
5
|
VerifyAccount,
|
|
6
6
|
} = require("./middleware/verifyToken");
|
|
7
|
-
const {refreshTokenMiddleware} = require("./middleware");
|
|
8
|
-
|
|
7
|
+
// const {refreshTokenMiddleware} = require("./middleware");
|
|
8
|
+
|
|
9
|
+
const refreshLimiter = rateLimit({
|
|
10
|
+
windowMs: 15 * 60 * 1000, // 15 minutes
|
|
11
|
+
max: 100, // Limit each IP to 100 requests per 15 minutes
|
|
12
|
+
message: "Too many refresh requests from this IP, please try again after 15 minutes",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const refreshTokenCache = new Map();
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Middleware to refresh access token using refresh token.
|
|
19
|
+
*
|
|
20
|
+
* @param {Object} req - The request object.
|
|
21
|
+
* @param {Object} res - The response object.
|
|
22
|
+
* @param {Function} next - The next middleware function.
|
|
23
|
+
* @returns {void}
|
|
24
|
+
*/
|
|
25
|
+
async function refreshTokenMiddleware(req, res, next) {
|
|
26
|
+
refreshLimiter(req, res, async () => {
|
|
27
|
+
const refreshToken = req.cookies['x-refresh-token'];
|
|
28
|
+
if (!refreshToken) {
|
|
29
|
+
return res.status(401).json({ error: "No refresh token provided" });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!isValidRefreshTokenFormat(refreshToken)) {
|
|
33
|
+
return res.status(400).json({ error: "Invalid refresh token format" });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (refreshTokenCache.has(refreshToken)) {
|
|
37
|
+
req.newAccessToken = refreshTokenCache.get(refreshToken);
|
|
38
|
+
return next();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const response = await axios.post(
|
|
43
|
+
`${process.env.AUTH_URL}/oauth/token`,
|
|
44
|
+
new URLSearchParams({
|
|
45
|
+
grant_type: "refresh_token",
|
|
46
|
+
refresh_token: refreshToken,
|
|
47
|
+
client_id: process.env.CLIENT_ID,
|
|
48
|
+
client_secret: process.env.CLIENT_SECRET,
|
|
49
|
+
}),
|
|
50
|
+
{ headers: { "Content-Type": "application/x-www-form-urlencoded" } }
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
if (response.data && response.data.access_token) {
|
|
54
|
+
refreshTokenCache.set(refreshToken, response.data.access_token);
|
|
55
|
+
req.newAccessToken = response.data.access_token;
|
|
56
|
+
next();
|
|
57
|
+
} else {
|
|
58
|
+
res.status(401).json({ error: "Unable to refresh token" });
|
|
59
|
+
}
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const statusCode = error.response?.status || 500;
|
|
62
|
+
res.status(statusCode).json({ error: error.response?.data?.error || "Error refreshing token" });
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Checks if the given token has a valid refresh token format.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} token - The token to validate.
|
|
71
|
+
*
|
|
72
|
+
* @return {boolean} - Returns true if the token has a valid refresh token format, otherwise false.
|
|
73
|
+
*/
|
|
74
|
+
function isValidRefreshTokenFormat(token) {
|
|
75
|
+
const jwtPattern = /^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$/;
|
|
76
|
+
return jwtPattern.test(token);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Middleware for handling authentication and authorization.
|
|
80
|
+
*
|
|
81
|
+
* @param {object} [options={}] - The options for configuring the authentication middleware.
|
|
82
|
+
* @param {string} [options.secret="RESTFULAPIs"] - The secret key used for authentication.
|
|
83
|
+
* @param {string} [options.authUrl=process.env.AUTH_URL] - The authentication URL.
|
|
84
|
+
* @param {string} [options.clientId=process.env.CLIENT_ID] - The client ID.
|
|
85
|
+
* @param {string} [options.clientSecret=process.env.CLIENT_SECRET] - The client secret.
|
|
86
|
+
* @param {string} [options.clientUrl=process.env.CLIENT_URL] - The client URL.
|
|
87
|
+
* @param {string} [options.redirectUri=process.env.REDIRECT_URI] - The redirect URI.
|
|
88
|
+
* @param {string} [options.appName=process.env.APP_NAME] - The application name.
|
|
89
|
+
* @returns {Function} - Express middleware function.
|
|
90
|
+
*/
|
|
9
91
|
function proproAuthMiddleware(options = {}) {
|
|
10
92
|
const {
|
|
11
93
|
secret = "RESTFULAPIs",
|
|
@@ -19,44 +101,6 @@ function proproAuthMiddleware(options = {}) {
|
|
|
19
101
|
|
|
20
102
|
return async (req, res, next) => {
|
|
21
103
|
try {
|
|
22
|
-
let token;
|
|
23
|
-
if (req.headers.authorization?.startsWith("Bearer ")) {
|
|
24
|
-
token = req.headers.authorization.split(" ")[1];
|
|
25
|
-
} else if (req.cookies && req.cookies['x-access-token']) {
|
|
26
|
-
token = req.cookies['x-access-token'];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (token) {
|
|
30
|
-
console.log("verifying access token", token);
|
|
31
|
-
const verifiedToken = await verifyJWT(token);
|
|
32
|
-
console.log("verifiedToken", verifiedToken);
|
|
33
|
-
if (verifiedToken) {
|
|
34
|
-
req.account = verifiedToken;
|
|
35
|
-
return next();
|
|
36
|
-
} else if (req.cookies && req.cookies['x-refresh-token']) {
|
|
37
|
-
const refreshToken = req.cookies['x-refresh-token'];
|
|
38
|
-
if (refreshToken) {
|
|
39
|
-
const newTokenData = await refreshTokenMiddleware(req, res, next);
|
|
40
|
-
if (newTokenData) {
|
|
41
|
-
res.cookie("x-refresh-token", newTokenData.tokens.refresh.token, {
|
|
42
|
-
httpOnly: true,
|
|
43
|
-
secure: process.env.NODE_ENV === "production",
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
res.cookie("x-access-token", newTokenData.tokens.access.token, {
|
|
47
|
-
httpOnly: true,
|
|
48
|
-
secure: process.env.NODE_ENV === "production",
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
req.account = verifyJWT(newTokenData.tokens.access.token, secret);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
req.account = undefined;
|
|
57
|
-
res.status(401).send("Unauthorized: Invalid or expired token");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
104
|
if (!["/api/auth", "/api/callback"].includes(req.path)) {
|
|
61
105
|
return next();
|
|
62
106
|
}
|
|
@@ -106,4 +150,4 @@ function proproAuthMiddleware(options = {}) {
|
|
|
106
150
|
};
|
|
107
151
|
}
|
|
108
152
|
|
|
109
|
-
module.exports = proproAuthMiddleware;
|
|
153
|
+
module.exports = {proproAuthMiddleware, refreshTokenMiddleware};
|
|
@@ -24,23 +24,31 @@ const refreshLimiter = rateLimit({
|
|
|
24
24
|
* @returns {Promise<void>} - Promise object that represents the completion of the middleware function.
|
|
25
25
|
*/
|
|
26
26
|
const refreshTokenMiddleware = async (req, res, next) => {
|
|
27
|
+
console.log("refreshTokenMiddleware again");
|
|
27
28
|
// Apply rate limiting
|
|
28
29
|
refreshLimiter(req, res, async () => {
|
|
29
30
|
const refreshToken = req.headers["x-refresh-token"];
|
|
30
31
|
|
|
32
|
+
console.log("refreshToken", refreshToken);
|
|
31
33
|
if (!refreshToken) {
|
|
32
|
-
|
|
34
|
+
res.status(401).json({ error: "No refresh token provided" });
|
|
33
35
|
}
|
|
34
36
|
|
|
37
|
+
console.log("isValidRefreshTokenFormat(refreshToken)", isValidRefreshTokenFormat(refreshToken));
|
|
38
|
+
|
|
35
39
|
if (!isValidRefreshTokenFormat(refreshToken)) {
|
|
36
|
-
|
|
40
|
+
res.status(400).json({ error: "Invalid refresh token format" });
|
|
37
41
|
}
|
|
38
42
|
|
|
43
|
+
console.log("refreshTokenCache.has(refreshToken)", refreshTokenCache.has(refreshToken));
|
|
44
|
+
|
|
39
45
|
if (refreshTokenCache.has(refreshToken)) {
|
|
40
46
|
req.newAccessToken = refreshTokenCache.get(refreshToken);
|
|
41
|
-
|
|
47
|
+
next();
|
|
42
48
|
}
|
|
43
49
|
|
|
50
|
+
console.log("process.env.AUTH_URL", process.env.AUTH_URL);
|
|
51
|
+
|
|
44
52
|
try {
|
|
45
53
|
const response = await axios.post(
|
|
46
54
|
`${process.env.AUTH_URL}/oauth/token`,
|
|
@@ -53,17 +61,19 @@ const refreshTokenMiddleware = async (req, res, next) => {
|
|
|
53
61
|
{ headers: { "Content-Type": "application/x-www-form-urlencoded" } }
|
|
54
62
|
);
|
|
55
63
|
|
|
64
|
+
console.log("response", response);
|
|
65
|
+
|
|
56
66
|
if (response.data && response.data.access_token) {
|
|
57
67
|
refreshTokenCache.set(refreshToken, response.data.access_token);
|
|
58
68
|
req.newAccessToken = response.data.access_token;
|
|
59
|
-
|
|
69
|
+
next();
|
|
60
70
|
} else {
|
|
61
|
-
|
|
71
|
+
res.status(401).json({ error: "Unable to refresh token" });
|
|
62
72
|
}
|
|
63
73
|
} catch (error) {
|
|
64
74
|
const statusCode = error.response?.status || 500;
|
|
65
75
|
const message = error.response?.data?.error || "Error refreshing token";
|
|
66
|
-
|
|
76
|
+
res.status(statusCode).json({ error: message });
|
|
67
77
|
}
|
|
68
78
|
});
|
|
69
79
|
};
|