Skip to content

Commit c62cf64

Browse files
committed
Added validator for query params
1 parent 8b84e85 commit c62cf64

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

controllers/auth.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ const passport = require("passport");
22
const users = require("../models/users");
33
const QrCodeAuthModel = require("../models/qrCodeAuth");
44
const authService = require("../services/authService");
5-
const {
6-
SOMETHING_WENT_WRONG,
7-
DATA_ADDED_SUCCESSFULLY,
8-
BAD_REQUEST,
9-
INVALID_QUERY_PARAM,
10-
} = require("../constants/errorMessages");
5+
const { SOMETHING_WENT_WRONG, DATA_ADDED_SUCCESSFULLY, BAD_REQUEST } = require("../constants/errorMessages");
116

127
/**
138
* Fetches the user info from GitHub and authenticates User
@@ -129,20 +124,15 @@ const updateAuthStatus = async (req, res) => {
129124

130125
const fetchUserDeviceInfo = async (req, res) => {
131126
try {
132-
const queryParamArray = Object.keys(req.query);
133-
if (queryParamArray.length === 1 && queryParamArray[0] === "device_id") {
134-
const deviceId = req.query.device_id;
135-
const userDeviceInfoData = await QrCodeAuthModel.retrieveUserDeviceInfo(deviceId);
136-
if (!userDeviceInfoData.userExists) {
137-
return res.boom.notFound("No Authentication found!");
138-
}
139-
return res.json({
140-
message: "Authentication document retrieved successfully.",
141-
data: { ...userDeviceInfoData.data },
142-
});
143-
} else {
144-
return res.boom.badRequest(INVALID_QUERY_PARAM);
127+
const deviceId = req.query.device_id;
128+
const userDeviceInfoData = await QrCodeAuthModel.retrieveUserDeviceInfo(deviceId);
129+
if (!userDeviceInfoData.userExists) {
130+
return res.boom.notFound(`User with id ${deviceId} does not exist.`);
145131
}
132+
return res.json({
133+
message: "Authentication document retrieved successfully.",
134+
data: { ...userDeviceInfoData.data },
135+
});
146136
} catch (error) {
147137
logger.error(`Error while fetching user: ${error}`);
148138
return res.boom.badImplementation(SOMETHING_WENT_WRONG);

middlewares/validators/qrCodeAuth.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,22 @@ const validateAuthStatus = async (req, res, next) => {
3232
}
3333
};
3434

35+
const validateFetchingUserDocument = async (req, res, next) => {
36+
const schema = joi.object().strict().keys({
37+
device_id: joi.string().required(),
38+
});
39+
40+
try {
41+
await schema.validateAsync(req.query);
42+
next();
43+
} catch (error) {
44+
logger.error(`Invalid Query Parameters Passed`);
45+
res.boom.badRequest(`Invalid Query Parameters Passed`);
46+
}
47+
};
48+
3549
module.exports = {
3650
storeUserDeviceInfo,
3751
validateAuthStatus,
52+
validateFetchingUserDocument,
3853
};

routes/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ router.get("/github/callback", auth.githubAuth);
1212

1313
router.get("/signout", auth.signout);
1414

15-
router.get("/qr-code-auth", auth.fetchUserDeviceInfo);
15+
router.get("/qr-code-auth", userDeviceInfoValidator.validateFetchingUserDocument, auth.fetchUserDeviceInfo);
1616

1717
router.post("/qr-code-auth", userDeviceInfoValidator.storeUserDeviceInfo, auth.storeUserDeviceInfo);
1818

test/integration/qrCodeAuth.test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,12 @@ describe("QrCodeAuth", function () {
166166
let userDeviceInfoData;
167167
beforeEach(async function () {
168168
userId = await addUser(user);
169-
userDeviceInfoData = { ...userDeviceInfoDataArray[0], user_id: userId, authorization_status: "NOT_INIT" };
169+
userDeviceInfoData = {
170+
...userDeviceInfoDataArray[0],
171+
user_id: userId,
172+
authorization_status: "NOT_INIT",
173+
access_token: "ACCESS_TOKEN",
174+
};
170175
});
171176
afterEach(async function () {
172177
await cleanDb();
@@ -183,6 +188,11 @@ describe("QrCodeAuth", function () {
183188
}
184189
expect(res).to.have.status(200);
185190
expect(res.body).to.be.a("object");
191+
expect(res.body.data.user_id).to.be.a("string");
192+
expect(res.body.data.device_info).to.be.a("string");
193+
expect(res.body.data.device_id).to.be.a("string");
194+
expect(res.body.data.authorization_status).to.be.a("string");
195+
expect(res.body.data.access_token).to.be.a("string");
186196
expect(res.body.message).to.equal(`Authentication document retrieved successfully.`);
187197

188198
return done();
@@ -200,7 +210,7 @@ describe("QrCodeAuth", function () {
200210

201211
expect(res).to.have.status(404);
202212
expect(res.body).to.be.a("object");
203-
expect(res.body.message).to.equal("No Authentication found!");
213+
expect(res.body.message).to.equal(`User with id ${userDeviceInfoData.device_id} does not exist.`);
204214
expect(res.body.error).to.equal("Not Found");
205215

206216
return done();

test/unit/models/qrCodeAuth.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,18 @@ describe("mobile auth", function () {
8888
...userDeviceInfoDataArray[0],
8989
user_id: userId,
9090
authorization_status: "NOT_INIT",
91+
access_token: "ACCESS_TOKEN",
9192
};
9293

9394
await qrCodeAuth.storeUserDeviceInfo(userDeviceInfoData);
9495
const response = await qrCodeAuth.retrieveUserDeviceInfo(userDeviceInfoData.device_id);
95-
9696
const userDeviceInfo = response.data;
9797
const {
9898
user_id: userID,
9999
device_info: deviceInfo,
100100
device_id: deviceId,
101101
authorization_status: authorizationStatus,
102+
access_token: accessToken,
102103
} = userDeviceInfo;
103104

104105
const data = (await qrCodeAuthModel.doc(userId).get()).data();
@@ -112,6 +113,7 @@ describe("mobile auth", function () {
112113
expect(deviceInfo).to.be.a("string");
113114
expect(deviceId).to.be.a("string");
114115
expect(authorizationStatus).to.be.a("string");
116+
expect(accessToken).to.be.a("string");
115117
});
116118
});
117119
});

0 commit comments

Comments
 (0)