propro-utils 1.3.44 → 1.3.46
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 +1 -1
- package/src/server/index.js +140 -122
package/package.json
CHANGED
package/src/server/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
require(
|
|
2
|
-
const {
|
|
3
|
-
|
|
4
|
-
} = require(
|
|
5
|
-
const {checkIfUserExists} = require("../../middlewares/account_info");
|
|
6
|
-
const {post} = require("axios");
|
|
1
|
+
require('dotenv').config();
|
|
2
|
+
const { exchangeToken } = require('./middleware/verifyToken');
|
|
3
|
+
const { checkIfUserExists } = require('../../middlewares/account_info');
|
|
4
|
+
const { post } = require('axios');
|
|
7
5
|
|
|
8
6
|
/**
|
|
9
7
|
* Middleware for handling authentication and authorization.
|
|
@@ -20,122 +18,142 @@ const {post} = require("axios");
|
|
|
20
18
|
* @returns {Function} - Express middleware function.
|
|
21
19
|
*/
|
|
22
20
|
function proproAuthMiddleware(options = {}, userSchema) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const response = await post(`${authUrl}/api/v1/auth/refreshTokens`, {
|
|
60
|
-
refreshToken
|
|
61
|
-
}, {
|
|
62
|
-
params: {
|
|
63
|
-
actionType: "refresh",
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const {account, access, refresh} = response.data;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (!account || !access || !refresh) {
|
|
72
|
-
return res.status(401).json({error: "Invalid or expired refresh token"});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const currentDateTime = new Date();
|
|
76
|
-
|
|
77
|
-
const refreshMaxAge = new Date(refresh.expires).getTime() - currentDateTime.getTime();
|
|
78
|
-
|
|
79
|
-
res.cookie("x-refresh-token", refresh.token, {
|
|
80
|
-
httpOnly: true,
|
|
81
|
-
secure: process.env.NODE_ENV === "production",
|
|
82
|
-
maxAge: refreshMaxAge
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
const accessMaxAge = new Date(access.expires).getTime() - currentDateTime.getTime();
|
|
86
|
-
|
|
87
|
-
res.cookie("x-access-token", access.token, {
|
|
88
|
-
httpOnly: true,
|
|
89
|
-
secure: process.env.NODE_ENV === "production",
|
|
90
|
-
maxAge: accessMaxAge
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
return res.status(200).json({message: "Token refreshed successfully"});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (req.path === "/api/callback") {
|
|
97
|
-
const code = req.query.code;
|
|
98
|
-
if (!code) {
|
|
99
|
-
return res.status(400).send("No code received");
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const {tokens, account, redirectUrl} = await exchangeToken(
|
|
103
|
-
authUrl,
|
|
104
|
-
code,
|
|
105
|
-
clientId,
|
|
106
|
-
clientSecret,
|
|
107
|
-
redirectUri
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
await checkIfUserExists(userSchema, account.accountId);
|
|
111
|
-
|
|
112
|
-
const currentDateTime = new Date();
|
|
113
|
-
|
|
114
|
-
const refreshMaxAge = new Date(tokens.refresh.expires).getTime() - currentDateTime.getTime();
|
|
115
|
-
|
|
116
|
-
res.cookie("x-refresh-token", tokens.refresh.token, {
|
|
117
|
-
httpOnly: true,
|
|
118
|
-
secure: process.env.NODE_ENV === "production",
|
|
119
|
-
maxAge: refreshMaxAge
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
const accessMaxAge = new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
123
|
-
|
|
124
|
-
res.cookie("x-access-token", tokens.access.token, {
|
|
125
|
-
httpOnly: true,
|
|
126
|
-
secure: process.env.NODE_ENV === "production",
|
|
127
|
-
maxAge: accessMaxAge
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
const urlToRedirect = `http://${redirectUrl}/`;
|
|
131
|
-
|
|
132
|
-
return res.redirect(urlToRedirect);
|
|
133
|
-
}
|
|
134
|
-
} catch (error) {
|
|
135
|
-
// console.error("Error in proproAuthMiddleware:", error);
|
|
136
|
-
res.status(401).send("Unauthorized: Invalid or expired token");
|
|
21
|
+
const {
|
|
22
|
+
secret = 'RESTFULAPIs',
|
|
23
|
+
authUrl = process.env.AUTH_URL,
|
|
24
|
+
clientId = process.env.CLIENT_ID,
|
|
25
|
+
clientSecret = process.env.CLIENT_SECRET,
|
|
26
|
+
clientUrl = process.env.CLIENT_URL,
|
|
27
|
+
redirectUri = process.env.REDIRECT_URI,
|
|
28
|
+
appName = process.env.APP_NAME,
|
|
29
|
+
} = options;
|
|
30
|
+
|
|
31
|
+
return async (req, res, next) => {
|
|
32
|
+
try {
|
|
33
|
+
if (
|
|
34
|
+
!['/api/auth', '/api/callback', '/api/refreshToken'].includes(req.path)
|
|
35
|
+
) {
|
|
36
|
+
return next();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (req.path === '/api/auth') {
|
|
40
|
+
const authClientUrl = `${clientUrl}/signin`;
|
|
41
|
+
const redirectUrl = `${authClientUrl}?response_type=code&appName=${appName}&client_id=${clientId}&redirect_uri=${encodeURIComponent(
|
|
42
|
+
redirectUri
|
|
43
|
+
)}`;
|
|
44
|
+
return res.status(200).json({ redirectUrl });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (req.path === '/api/refreshToken') {
|
|
48
|
+
const refreshToken = req.cookies['x-refresh-token'];
|
|
49
|
+
if (!refreshToken) {
|
|
50
|
+
const authClientUrl = `${clientUrl}/signin`;
|
|
51
|
+
const redirectUrl = `${authClientUrl}?response_type=code&appName=${appName}&client_id=${clientId}&redirect_uri=${encodeURIComponent(
|
|
52
|
+
redirectUri
|
|
53
|
+
)}`;
|
|
54
|
+
return res
|
|
55
|
+
.status(401)
|
|
56
|
+
.json({ redirectUrl, error: 'No refresh token provided' });
|
|
137
57
|
}
|
|
138
|
-
|
|
58
|
+
|
|
59
|
+
const response = await post(
|
|
60
|
+
`${authUrl}/api/v1/auth/refreshTokens`,
|
|
61
|
+
{
|
|
62
|
+
refreshToken,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
params: {
|
|
66
|
+
actionType: 'refresh',
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const { account, access, refresh } = response.data;
|
|
72
|
+
|
|
73
|
+
if (!account || !access || !refresh) {
|
|
74
|
+
return res
|
|
75
|
+
.status(401)
|
|
76
|
+
.json({ error: 'Invalid or expired refresh token' });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const currentDateTime = new Date();
|
|
80
|
+
|
|
81
|
+
const refreshMaxAge =
|
|
82
|
+
new Date(refresh.expires).getTime() - currentDateTime.getTime();
|
|
83
|
+
|
|
84
|
+
res.cookie('x-refresh-token', refresh.token, {
|
|
85
|
+
httpOnly: true,
|
|
86
|
+
secure: process.env.NODE_ENV === 'production',
|
|
87
|
+
maxAge: refreshMaxAge,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const accessMaxAge =
|
|
91
|
+
new Date(access.expires).getTime() - currentDateTime.getTime();
|
|
92
|
+
|
|
93
|
+
res.cookie('x-access-token', access.token, {
|
|
94
|
+
httpOnly: true,
|
|
95
|
+
secure: process.env.NODE_ENV === 'production',
|
|
96
|
+
maxAge: accessMaxAge,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return res
|
|
100
|
+
.status(200)
|
|
101
|
+
.json({ message: 'Token refreshed successfully' });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (req.path === '/api/callback') {
|
|
105
|
+
const code = req.query.code;
|
|
106
|
+
if (!code) {
|
|
107
|
+
return res.status(400).send('No code received');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
console.log('code', code);
|
|
111
|
+
const { tokens, account, redirectUrl } = await exchangeToken(
|
|
112
|
+
authUrl,
|
|
113
|
+
code,
|
|
114
|
+
clientId,
|
|
115
|
+
clientSecret,
|
|
116
|
+
redirectUri
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
console.log('account', account);
|
|
120
|
+
console.log('tokens', tokens);
|
|
121
|
+
console.log('redirectUrl', redirectUrl);
|
|
122
|
+
|
|
123
|
+
await checkIfUserExists(userSchema, account.accountId);
|
|
124
|
+
|
|
125
|
+
console.log('User exists');
|
|
126
|
+
|
|
127
|
+
const currentDateTime = new Date();
|
|
128
|
+
|
|
129
|
+
const refreshMaxAge =
|
|
130
|
+
new Date(tokens.refresh.expires).getTime() -
|
|
131
|
+
currentDateTime.getTime();
|
|
132
|
+
|
|
133
|
+
res.cookie('x-refresh-token', tokens.refresh.token, {
|
|
134
|
+
httpOnly: true,
|
|
135
|
+
secure: process.env.NODE_ENV === 'production',
|
|
136
|
+
maxAge: refreshMaxAge,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const accessMaxAge =
|
|
140
|
+
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
141
|
+
|
|
142
|
+
res.cookie('x-access-token', tokens.access.token, {
|
|
143
|
+
httpOnly: true,
|
|
144
|
+
secure: process.env.NODE_ENV === 'production',
|
|
145
|
+
maxAge: accessMaxAge,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const urlToRedirect = `http://${redirectUrl}/`;
|
|
149
|
+
|
|
150
|
+
return res.redirect(urlToRedirect);
|
|
151
|
+
}
|
|
152
|
+
} catch (error) {
|
|
153
|
+
// console.error("Error in proproAuthMiddleware:", error);
|
|
154
|
+
res.status(401).send('Unauthorized: Invalid or expired token');
|
|
155
|
+
}
|
|
156
|
+
};
|
|
139
157
|
}
|
|
140
158
|
|
|
141
|
-
module.exports = proproAuthMiddleware;
|
|
159
|
+
module.exports = proproAuthMiddleware;
|