Skip to content

Commit 8143a0a

Browse files
authored
Merge pull request #2111 from rteas/refactor-permission-management
fix: rename TOKEN_SECRET to JWT_SECRET across backend, small fixes
2 parents feff213 + 3fc3245 commit 8143a0a

5 files changed

Lines changed: 42 additions & 45 deletions

File tree

backend/config/auth.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
TOKEN_SECRET: process.env.TOKEN_SECRET || 'placeholder_secret_key_for_development_only',
2+
JWT_SECRET: process.env.JWT_SECRET || 'placeholder_secret_key_for_development_only',
33
CUSTOM_REQUEST_HEADER: process.env.CUSTOM_REQUEST_HEADER,
44
// 15 minutes as a string for JWT expiration
55
ACCESS_TOKEN_EXPIRATION: '15m',

backend/controllers/user.controller.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const expectedHeader = process.env.CUSTOM_REQUEST_HEADER;
1717
const UserController = {};
1818

1919
// Get list of Users with GET
20-
UserController.user_list = async function (req, res) {
20+
UserController.user_list = async (req, res) => {
2121
const { headers } = req;
2222
const { query } = req;
2323

@@ -34,7 +34,7 @@ UserController.user_list = async function (req, res) {
3434
}
3535
};
3636

37-
UserController.user_by_email = async function (req, res) {
37+
UserController.user_by_email = async (req, res) => {
3838
const { headers } = req;
3939
const { email } = req.params;
4040

@@ -54,23 +54,25 @@ UserController.user_by_email = async function (req, res) {
5454
};
5555

5656
// Get list of Users with accessLevel 'admin' or 'superadmin' with GET
57-
UserController.admin_list = async function (req, res) {
57+
UserController.admin_list = async (req, res) => {
5858
const { headers } = req;
5959

6060
if (headers['x-customrequired-header'] !== expectedHeader) {
6161
return res.sendStatus(403);
6262
}
6363

6464
try {
65-
const admins = await User.find({ accessLevel: { $in: ['admin', 'superadmin'] } });
65+
const admins = await User.find({
66+
accessLevel: { $in: ['admin', 'superadmin'] },
67+
});
6668
return res.status(200).send(admins);
6769
} catch (err) {
6870
console.error(err);
6971
return res.sendStatus(400);
7072
}
7173
};
7274

73-
UserController.projectManager_list = async function (req, res) {
75+
UserController.projectManager_list = async (req, res) => {
7476
const { headers } = req;
7577

7678
if (headers['x-customrequired-header'] !== expectedHeader) {
@@ -120,7 +122,7 @@ UserController.projectManager_list = async function (req, res) {
120122
};
121123

122124
// Get User by id with GET
123-
UserController.user_by_id = async function (req, res) {
125+
UserController.user_by_id = async (req, res) => {
124126
const { headers } = req;
125127
const { UserId } = req.params;
126128

@@ -138,7 +140,7 @@ UserController.user_by_id = async function (req, res) {
138140
};
139141

140142
// Add User with POST
141-
UserController.create = async function (req, res) {
143+
UserController.create = async (req, res) => {
142144
const { headers } = req;
143145

144146
if (headers['x-customrequired-header'] !== expectedHeader) {
@@ -164,7 +166,7 @@ UserController.create = async function (req, res) {
164166
};
165167

166168
// Update User with PATCH
167-
UserController.update = async function (req, res) {
169+
UserController.update = async (req, res) => {
168170
const { headers } = req;
169171
const { UserId } = req.params;
170172

@@ -173,7 +175,9 @@ UserController.update = async function (req, res) {
173175
}
174176

175177
try {
176-
const user = await User.findOneAndUpdate({ _id: UserId }, req.body, { new: true });
178+
const user = await User.findOneAndUpdate({ _id: UserId }, req.body, {
179+
new: true,
180+
});
177181
return res.status(200).send(user);
178182
} catch (err) {
179183
console.error(err);
@@ -182,7 +186,7 @@ UserController.update = async function (req, res) {
182186
};
183187

184188
// Add User with POST
185-
UserController.delete = async function (req, res) {
189+
UserController.delete = async (req, res) => {
186190
const { headers } = req;
187191
const { UserId } = req.params;
188192

@@ -199,7 +203,7 @@ UserController.delete = async function (req, res) {
199203
}
200204
};
201205

202-
UserController.createUser = function (req, res) {
206+
UserController.createUser = async (req, res) => {
203207
const { firstName, lastName, email } = req.body;
204208
const { origin } = req.headers;
205209

@@ -224,7 +228,7 @@ UserController.createUser = function (req, res) {
224228
EmailController.sendLoginLink(req.body.email, user.name.firstName, jsonToken, req.cookie, origin);
225229
};
226230

227-
UserController.signin = function (req, res) {
231+
UserController.signin = (req, res) => {
228232
const { email, auth_origin } = req.body;
229233
const { origin } = req.headers;
230234

@@ -251,15 +255,15 @@ UserController.signin = function (req, res) {
251255
});
252256
};
253257

254-
UserController.verifySignIn = async function (req, res) {
258+
UserController.verifySignIn = async (req, res) => {
255259
let token = req.headers['x-access-token'] || req.headers['authorization'];
256260
if (token.startsWith('Bearer ')) {
257261
// Remove Bearer from string
258262
token = token.slice(7, token.length);
259263
}
260264

261265
try {
262-
const payload = jwt.verify(token, CONFIG_AUTH.SECRET);
266+
const payload = jwt.verify(token, CONFIG_AUTH.JWT_SECRET);
263267
const user = await User.findById(payload.id);
264268
const refreshToken = generateRefreshToken();
265269
const accessToken = generateAccessToken(user, payload.auth_origin);
@@ -287,11 +291,9 @@ UserController.verifySignIn = async function (req, res) {
287291
}
288292
};
289293

290-
UserController.verifyMe = async function (req, res) {
291-
return res.status(200).send(req.user);
292-
};
294+
UserController.verifyMe = async (req, res) => res.status(200).send(req.user);
293295

294-
UserController.logout = async function (req, res) {
296+
UserController.logout = async (req, res) => {
295297
try {
296298
await RefreshToken.deleteOne({ _id: req.refreshToken._id });
297299
return res.clearCookie('token').status(200).send('Successfully logged out.');
@@ -301,7 +303,7 @@ UserController.logout = async function (req, res) {
301303
}
302304
};
303305

304-
UserController.refreshAccessToken = async function (req, res) {
306+
UserController.refreshAccessToken = async (req, res) => {
305307
const accessToken = generateAccessToken(req.user, req.auth_origin);
306308
const decoded = jwt.decode(accessToken);
307309

@@ -315,7 +317,7 @@ UserController.refreshAccessToken = async function (req, res) {
315317
};
316318

317319
// Update user's managedProjects
318-
UserController.updateManagedProjects = async function (req, res) {
320+
UserController.updateManagedProjects = async (req, res) => {
319321
const { headers } = req;
320322
const { UserId } = req.params;
321323
const { action, projectId } = req.body; // action - 'add' or 'remove'
@@ -357,7 +359,7 @@ UserController.updateManagedProjects = async function (req, res) {
357359
}
358360
};
359361

360-
UserController.bulkUpdateManagedProjects = async function (req, res) {
362+
UserController.bulkUpdateManagedProjects = async (req, res) => {
361363
const { bulkOps } = req.body;
362364

363365
// Convert string IDs to ObjectId in bulkOps

backend/middleware/auth.middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { RefreshToken, User } = require('../models');
55
const crypto = require('crypto');
66
const AuthUtils = require('../../shared/authorizationUtils');
77

8-
const SECRET = CONFIG_AUTH.TOKEN_SECRET;
8+
const SECRET = CONFIG_AUTH.JWT_SECRET;
99

1010
// Utility functions
1111

backend/routers/projects.router.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const { ProjectController } = require('../controllers');
88
// Require user to be project manager or higher (commented out for now for current app to work succesfully without auth, will re-enable when auth is ready)
99
// router.use(Auth.authUser, Auth.requireMinimumRole(ROLES.PROJECT_MANAGER));
1010
// The base is /api/projects
11+
12+
const { AuthUtil } = require('../middleware/auth.middleware');
13+
1114
router.get('/', ProjectController.project_list);
1215

1316
// Its a put because we have to send the PM projects to be filtered here
@@ -26,6 +29,10 @@ router.patch('/:ProjectId', ProjectController.updateManagedByUsers);
2629
router.post('/bulk-updates', ProjectController.bulkUpdateManagedByUsers);
2730

2831
// Update onboard/offboard visibility for a project
29-
router.patch('/:ProjectId/visibility', AuthUtil.verifyCookie, ProjectController.updateOnboardOffboardVisibility);
32+
router.patch(
33+
'/:ProjectId/visibility',
34+
AuthUtil.verifyCookie,
35+
ProjectController.updateOnboardOffboardVisibility,
36+
);
3037

3138
module.exports = router;

backend/test/old-tests/projects.router.test.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ describe('CREATE', () => {
2828
};
2929
const user = await User.create(submittedData);
3030
const auth_origin = 'TEST';
31-
token = jwt.sign(
32-
{ id: user.id, role: user.accessLevel, auth_origin },
33-
CONFIG_AUTH.TOKEN_SECRET,
34-
{
35-
expiresIn: `${CONFIG_AUTH.ACCESS_TOKEN_EXPIRATION_SEC}s`,
36-
},
37-
);
31+
token = jwt.sign({ id: user.id, role: user.accessLevel, auth_origin }, CONFIG_AUTH.JWT_SECRET, {
32+
expiresIn: `${CONFIG_AUTH.ACCESS_TOKEN_EXPIRATION_SEC}s`,
33+
});
3834
});
3935
test('Create a Project with POST to /api/projects/ without token', async (done) => {
4036
// Test Data
@@ -101,13 +97,9 @@ describe('UPDATE', () => {
10197
};
10298
const user = await User.create(submittedData);
10399
const auth_origin = 'TEST';
104-
token = jwt.sign(
105-
{ id: user.id, role: user.accessLevel, auth_origin },
106-
CONFIG_AUTH.TOKEN_SECRET,
107-
{
108-
expiresIn: `${CONFIG_AUTH.ACCESS_TOKEN_EXPIRATION_SEC}s`,
109-
},
110-
);
100+
token = jwt.sign({ id: user.id, role: user.accessLevel, auth_origin }, CONFIG_AUTH.JWT_SECRET, {
101+
expiresIn: `${CONFIG_AUTH.ACCESS_TOKEN_EXPIRATION_SEC}s`,
102+
});
111103
});
112104
test('Update a project with PATCH to /api/projects/:id without a token', async (done) => {
113105
// Test Data
@@ -189,13 +181,9 @@ describe('DELETE', () => {
189181
};
190182
const user = await User.create(submittedData);
191183
const auth_origin = 'TEST';
192-
token = jwt.sign(
193-
{ id: user.id, role: user.accessLevel, auth_origin },
194-
CONFIG_AUTH.TOKEN_SECRET,
195-
{
196-
expiresIn: `${CONFIG_AUTH.ACCESS_TOKEN_EXPIRATION_SEC}s`,
197-
},
198-
);
184+
token = jwt.sign({ id: user.id, role: user.accessLevel, auth_origin }, CONFIG_AUTH.JWT_SECRET, {
185+
expiresIn: `${CONFIG_AUTH.ACCESS_TOKEN_EXPIRATION_SEC}s`,
186+
});
199187
});
200188
test('Delete a project with POST to /api/projects/:id without a token', async (done) => {
201189
// Test Data

0 commit comments

Comments
 (0)