Skip to content

Commit 70c3c6d

Browse files
committed
Add token verification for socket connection + refactor code
1 parent ad3c6c4 commit 70c3c6d

File tree

19 files changed

+175
-60
lines changed

19 files changed

+175
-60
lines changed

backend/collab-service/.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ SERVICE_PORT=3003
44
# Origins for cors
55
ORIGINS=http://localhost:5173,http://127.0.0.1:5173
66

7+
# Other service APIs
8+
USER_SERVICE_URL=http://user-service:3001/api
9+
710
# Redis configuration
811
REDIS_URI=redis://collab-service-redis:6379
912

File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ExtendedError, Socket } from "socket.io";
2+
import { verifyToken } from "../api/userService.ts";
3+
4+
export const verifyUserToken = (
5+
socket: Socket,
6+
next: (err?: ExtendedError) => void
7+
) => {
8+
const token =
9+
socket.handshake.headers.authorization || socket.handshake.auth.token;
10+
verifyToken(token)
11+
.then(() => {
12+
console.log("Valid credentials");
13+
next();
14+
})
15+
.catch((err) => {
16+
console.error(err);
17+
next(new Error("Unauthorized"));
18+
});
19+
};

backend/collab-service/src/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import app, { allowedOrigins } from "./app.ts";
33
import { handleWebsocketCollabEvents } from "./handlers/websocketHandler.ts";
44
import { Server, Socket } from "socket.io";
55
import { connectRedis } from "./config/redis.ts";
6+
import { verifyUserToken } from "./middlewares/basicAccessControl.ts";
67

78
const server = http.createServer(app);
9+
810
export const io = new Server(server, {
911
cors: {
1012
origin: allowedOrigins,
@@ -13,6 +15,8 @@ export const io = new Server(server, {
1315
connectionStateRecovery: {},
1416
});
1517

18+
io.use(verifyUserToken);
19+
1620
io.on("connection", (socket: Socket) => {
1721
handleWebsocketCollabEvents(socket);
1822
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import axios from "axios";
2+
3+
const USER_SERVICE_URL =
4+
process.env.USER_SERVICE_URL || "http://localhost:3001/api";
5+
6+
const userClient = axios.create({
7+
baseURL: USER_SERVICE_URL,
8+
withCredentials: true,
9+
});
10+
11+
export const verifyToken = (token: string | undefined) => {
12+
return userClient.get("/auth/verify-token", {
13+
headers: { authorization: token },
14+
});
15+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ExtendedError, Socket } from "socket.io";
2+
import { verifyToken } from "../api/userService";
3+
4+
export const verifyUserToken = (
5+
socket: Socket,
6+
next: (err?: ExtendedError) => void
7+
) => {
8+
const token =
9+
socket.handshake.headers.authorization || socket.handshake.auth.token;
10+
verifyToken(token)
11+
.then(() => {
12+
console.log("Valid credentials");
13+
next();
14+
})
15+
.catch((err) => {
16+
console.error(err);
17+
next(new Error("Unauthorized"));
18+
});
19+
};

backend/communication-service/src/server.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import app, { allowedOrigins } from "./app";
22
import { createServer } from "http";
33
import { Server } from "socket.io";
44
import { handleWebsocketCommunicationEvents } from "./handlers/websocketHandler";
5-
import { verifyToken } from "./utils/userServiceApi";
5+
import { verifyUserToken } from "./middlewares/basicAccessControl";
66

77
const PORT = process.env.SERVICE_PORT || 3005;
88

@@ -13,19 +13,7 @@ export const io = new Server(server, {
1313
connectionStateRecovery: {},
1414
});
1515

16-
io.use((socket, next) => {
17-
const token =
18-
socket.handshake.headers.authorization || socket.handshake.auth.token;
19-
verifyToken(token)
20-
.then(() => {
21-
console.log("Valid credentials");
22-
next();
23-
})
24-
.catch((err) => {
25-
console.error(err);
26-
next(new Error("Unauthorized"));
27-
});
28-
});
16+
io.use(verifyUserToken);
2917

3018
io.on("connection", handleWebsocketCommunicationEvents);
3119

backend/matching-service/.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ORIGINS=http://localhost:5173,http://127.0.0.1:5173
77
# Other service APIs
88
QUESTION_SERVICE_URL=http://question-service:3000/api/questions
99
QN_HISTORY_SERVICE_URL=http://qn-history-service:3006/api/qnhistories
10+
USER_SERVICE_URL=http://user-service:3001/api
1011

1112
# RabbitMq configuration
1213
RABBITMQ_DEFAULT_USER=admin
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import axios from "axios";
2+
3+
const QN_HISTORY_SERVICE_URL =
4+
process.env.QN_HISTORY_SERVICE_URL ||
5+
"http://qn-history-service:3006/api/qnhistories";
6+
7+
const qnHistoryService = axios.create({
8+
baseURL: QN_HISTORY_SERVICE_URL,
9+
headers: {
10+
"Content-Type": "application/json",
11+
},
12+
});
13+
14+
export const createQuestionHistory = (
15+
questionId: string,
16+
title: string,
17+
submissionStatus: string,
18+
language: string,
19+
...userIds: string[]
20+
) => {
21+
const dateAttempted = new Date();
22+
return qnHistoryService.post("/", {
23+
userIds,
24+
questionId,
25+
title,
26+
submissionStatus,
27+
language,
28+
dateAttempted,
29+
timeTaken: 0,
30+
});
31+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import axios from "axios";
2+
3+
const QUESTION_SERVICE_URL =
4+
process.env.QUESTION_SERVICE_URL ||
5+
"http://question-service:3000/api/questions";
6+
7+
const questionClient = axios.create({
8+
baseURL: QUESTION_SERVICE_URL,
9+
headers: {
10+
"Content-Type": "application/json",
11+
},
12+
});
13+
14+
export const getRandomQuestion = (complexity: string, category: string) => {
15+
return questionClient.get("/random", { params: { complexity, category } });
16+
};

0 commit comments

Comments
 (0)