propro-utils 1.4.0 → 1.4.2
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 +45 -45
- package/package.json +1 -1
- package/src/server/index.js +141 -135
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require('dotenv').config();
|
|
2
2
|
const axios = require('axios');
|
|
3
|
-
const {
|
|
4
|
-
const {
|
|
3
|
+
const {getOrSetCache} = require('../utils/redis');
|
|
4
|
+
const {checkIfUserExists} = require('./account_info');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Middleware for authenticating and authorizing API requests.
|
|
@@ -9,7 +9,7 @@ const { checkIfUserExists } = require('./account_info');
|
|
|
9
9
|
* and propro authentication service.
|
|
10
10
|
*
|
|
11
11
|
* @param {object} redisClient - The Redis client used for caching permission data.
|
|
12
|
-
* @param {
|
|
12
|
+
* @param {Schema} userSchema - The user schema/model object.
|
|
13
13
|
* @param {string[]} [requiredPermissions=[]] - An array of permissions required to access the endpoint.
|
|
14
14
|
* This function first attempts to retrieve the account's permissions from the cache.
|
|
15
15
|
* If the cache is empty or expired, it fetches permissions from propro authentication service
|
|
@@ -34,53 +34,53 @@ const { checkIfUserExists } = require('./account_info');
|
|
|
34
34
|
* });
|
|
35
35
|
*/
|
|
36
36
|
const authValidation = (redisClient, userSchema, requiredPermissions = []) => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
return async (req, res, next) => {
|
|
38
|
+
try {
|
|
39
|
+
const accessToken =
|
|
40
|
+
req.cookies['x-access-token'] ||
|
|
41
|
+
req.headers.authorization?.split(' ')[1];
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
if (!accessToken) {
|
|
44
|
+
return res.status(403).json({error: 'Access token is required'});
|
|
45
|
+
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
47
|
+
const fetchPermission = async () => {
|
|
48
|
+
const response = await axios.post(
|
|
49
|
+
`${process.env.AUTH_URL}/api/v1/auth/validateToken`,
|
|
50
|
+
{
|
|
51
|
+
accessToken: accessToken,
|
|
52
|
+
requiredPermissions: requiredPermissions,
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
return response.data;
|
|
56
|
+
};
|
|
57
|
+
const cacheKey = `account:permissions:${accessToken}`;
|
|
58
|
+
const {accountId, validPermissions} = await getOrSetCache(
|
|
59
|
+
redisClient,
|
|
60
|
+
cacheKey,
|
|
61
|
+
fetchPermission,
|
|
62
|
+
1800
|
|
63
|
+
);
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
if (!validPermissions) {
|
|
66
|
+
return res.status(403).json({error: 'Invalid permissions'});
|
|
67
|
+
}
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
req.account = accountId;
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
71
|
+
const user = await checkIfUserExists(userSchema, accountId);
|
|
72
|
+
if (!user) {
|
|
73
|
+
return res.status(403).json({error: 'User not found'});
|
|
74
|
+
}
|
|
75
|
+
req.user = user.id;
|
|
76
|
+
next();
|
|
77
|
+
} catch (error) {
|
|
78
|
+
if (error.response && error.response.status) {
|
|
79
|
+
next(new Error(error.response.data.message));
|
|
80
|
+
}
|
|
81
|
+
next(new Error('Error validating token'));
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
84
|
};
|
|
85
85
|
|
|
86
86
|
module.exports = authValidation;
|
package/package.json
CHANGED
package/src/server/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require('dotenv').config();
|
|
2
|
-
const {
|
|
3
|
-
const {
|
|
4
|
-
const {
|
|
2
|
+
const {exchangeToken} = require('./middleware/verifyToken');
|
|
3
|
+
const {checkIfUserExists} = require('../../middlewares/account_info');
|
|
4
|
+
const {post} = require('axios');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Middleware for handling authentication and authorization.
|
|
@@ -18,139 +18,145 @@ const { post } = require('axios');
|
|
|
18
18
|
* @returns {Function} - Express middleware function.
|
|
19
19
|
*/
|
|
20
20
|
function proproAuthMiddleware(options = {}, userSchema) {
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
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
|
+
console.log('proproAuthMiddleware');
|
|
33
|
+
try {
|
|
34
|
+
if (
|
|
35
|
+
!['/api/auth', '/api/callback', '/api/refreshToken'].includes(req.path)
|
|
36
|
+
) {
|
|
37
|
+
return next();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
console.log('req.path', req.path);
|
|
41
|
+
if (req.path === '/api/auth') {
|
|
42
|
+
const authClientUrl = `${clientUrl}/signin`;
|
|
43
|
+
const redirectUrl = `${authClientUrl}?response_type=code&appName=${appName}&client_id=${clientId}&redirect_uri=${encodeURIComponent(
|
|
44
|
+
redirectUri
|
|
45
|
+
)}`;
|
|
46
|
+
return res.status(200).json({redirectUrl});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (req.path === '/api/refreshToken') {
|
|
50
|
+
console.log('refreshToken');
|
|
51
|
+
const refreshToken = req.cookies['x-refresh-token'];
|
|
52
|
+
console.log('refreshToken', refreshToken);
|
|
53
|
+
if (!refreshToken) {
|
|
54
|
+
console.log('No refresh token provided');
|
|
55
|
+
const authClientUrl = `${clientUrl}/signin`;
|
|
56
|
+
const redirectUrl = `${authClientUrl}?response_type=code&appName=${appName}&client_id=${clientId}&redirect_uri=${encodeURIComponent(
|
|
57
|
+
redirectUri
|
|
58
|
+
)}`;
|
|
59
|
+
console.log('redirectUrl', redirectUrl);
|
|
60
|
+
res
|
|
61
|
+
.status(401)
|
|
62
|
+
.json({redirectUrl, error: 'No refresh token provided'});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const response = await post(
|
|
66
|
+
`${authUrl}/api/v1/auth/refreshTokens`,
|
|
67
|
+
{
|
|
68
|
+
refreshToken,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
params: {
|
|
72
|
+
actionType: 'refresh',
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const {account, access, refresh} = response.data;
|
|
78
|
+
|
|
79
|
+
if (!account || !access || !refresh) {
|
|
80
|
+
return res
|
|
81
|
+
.status(401)
|
|
82
|
+
.json({error: 'Invalid or expired refresh token'});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const currentDateTime = new Date();
|
|
86
|
+
|
|
87
|
+
const refreshMaxAge =
|
|
88
|
+
new Date(refresh.expires).getTime() - currentDateTime.getTime();
|
|
89
|
+
|
|
90
|
+
res.cookie('x-refresh-token', refresh.token, {
|
|
91
|
+
httpOnly: true,
|
|
92
|
+
secure: process.env.NODE_ENV === 'production',
|
|
93
|
+
maxAge: refreshMaxAge,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const accessMaxAge =
|
|
97
|
+
new Date(access.expires).getTime() - currentDateTime.getTime();
|
|
98
|
+
|
|
99
|
+
res.cookie('x-access-token', access.token, {
|
|
100
|
+
httpOnly: true,
|
|
101
|
+
secure: process.env.NODE_ENV === 'production',
|
|
102
|
+
maxAge: accessMaxAge,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
return res
|
|
106
|
+
.status(200)
|
|
107
|
+
.json({message: 'Token refreshed successfully'});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (req.path === '/api/callback') {
|
|
111
|
+
const code = req.query.code;
|
|
112
|
+
if (!code) {
|
|
113
|
+
return res.status(400).send('No code received');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const {tokens, account, redirectUrl} = await exchangeToken(
|
|
117
|
+
authUrl,
|
|
118
|
+
code,
|
|
119
|
+
clientId,
|
|
120
|
+
clientSecret,
|
|
121
|
+
redirectUri
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const user = await checkIfUserExists(userSchema, account.accountId);
|
|
125
|
+
|
|
126
|
+
const currentDateTime = new Date();
|
|
127
|
+
|
|
128
|
+
const refreshMaxAge =
|
|
129
|
+
new Date(tokens.refresh.expires).getTime() -
|
|
130
|
+
currentDateTime.getTime();
|
|
131
|
+
|
|
132
|
+
res.cookie('x-refresh-token', tokens.refresh.token, {
|
|
133
|
+
httpOnly: true,
|
|
134
|
+
secure: process.env.NODE_ENV === 'production',
|
|
135
|
+
maxAge: refreshMaxAge,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const accessMaxAge =
|
|
139
|
+
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
140
|
+
|
|
141
|
+
res.cookie('x-access-token', tokens.access.token, {
|
|
142
|
+
httpOnly: true,
|
|
143
|
+
secure: process.env.NODE_ENV === 'production',
|
|
144
|
+
maxAge: accessMaxAge,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
res.cookie('user', JSON.stringify(user));
|
|
148
|
+
|
|
149
|
+
res.cookie('account', JSON.stringify(account));
|
|
150
|
+
|
|
151
|
+
const urlToRedirect = `http://${redirectUrl}/`;
|
|
152
|
+
|
|
153
|
+
return res.redirect(urlToRedirect);
|
|
154
|
+
}
|
|
155
|
+
} catch (error) {
|
|
156
|
+
// console.error("Error in proproAuthMiddleware:", error);
|
|
157
|
+
res.status(401).send('Unauthorized: Invalid or expired token');
|
|
57
158
|
}
|
|
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
|
-
const { tokens, account, redirectUrl } = await exchangeToken(
|
|
111
|
-
authUrl,
|
|
112
|
-
code,
|
|
113
|
-
clientId,
|
|
114
|
-
clientSecret,
|
|
115
|
-
redirectUri
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
const user = await checkIfUserExists(userSchema, account.accountId);
|
|
119
|
-
|
|
120
|
-
const currentDateTime = new Date();
|
|
121
|
-
|
|
122
|
-
const refreshMaxAge =
|
|
123
|
-
new Date(tokens.refresh.expires).getTime() -
|
|
124
|
-
currentDateTime.getTime();
|
|
125
|
-
|
|
126
|
-
res.cookie('x-refresh-token', tokens.refresh.token, {
|
|
127
|
-
httpOnly: true,
|
|
128
|
-
secure: process.env.NODE_ENV === 'production',
|
|
129
|
-
maxAge: refreshMaxAge,
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const accessMaxAge =
|
|
133
|
-
new Date(tokens.access.expires).getTime() - currentDateTime.getTime();
|
|
134
|
-
|
|
135
|
-
res.cookie('x-access-token', tokens.access.token, {
|
|
136
|
-
httpOnly: true,
|
|
137
|
-
secure: process.env.NODE_ENV === 'production',
|
|
138
|
-
maxAge: accessMaxAge,
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
res.cookie('user', JSON.stringify(user));
|
|
142
|
-
|
|
143
|
-
res.cookie('account', JSON.stringify(account));
|
|
144
|
-
|
|
145
|
-
const urlToRedirect = `http://${redirectUrl}/`;
|
|
146
|
-
|
|
147
|
-
return res.redirect(urlToRedirect);
|
|
148
|
-
}
|
|
149
|
-
} catch (error) {
|
|
150
|
-
// console.error("Error in proproAuthMiddleware:", error);
|
|
151
|
-
res.status(401).send('Unauthorized: Invalid or expired token');
|
|
152
|
-
}
|
|
153
|
-
};
|
|
159
|
+
};
|
|
154
160
|
}
|
|
155
161
|
|
|
156
162
|
module.exports = proproAuthMiddleware;
|