-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add token verification for socket connection + refactor code
- Loading branch information
1 parent
ad3c6c4
commit 70c3c6d
Showing
19 changed files
with
175 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
19 changes: 19 additions & 0 deletions
19
backend/collab-service/src/middlewares/basicAccessControl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ExtendedError, Socket } from "socket.io"; | ||
import { verifyToken } from "../api/userService.ts"; | ||
|
||
export const verifyUserToken = ( | ||
socket: Socket, | ||
next: (err?: ExtendedError) => void | ||
) => { | ||
const token = | ||
socket.handshake.headers.authorization || socket.handshake.auth.token; | ||
verifyToken(token) | ||
.then(() => { | ||
console.log("Valid credentials"); | ||
next(); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
next(new Error("Unauthorized")); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import axios from "axios"; | ||
|
||
const USER_SERVICE_URL = | ||
process.env.USER_SERVICE_URL || "http://localhost:3001/api"; | ||
|
||
const userClient = axios.create({ | ||
baseURL: USER_SERVICE_URL, | ||
withCredentials: true, | ||
}); | ||
|
||
export const verifyToken = (token: string | undefined) => { | ||
return userClient.get("/auth/verify-token", { | ||
headers: { authorization: token }, | ||
}); | ||
}; |
19 changes: 19 additions & 0 deletions
19
backend/communication-service/src/middlewares/basicAccessControl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ExtendedError, Socket } from "socket.io"; | ||
import { verifyToken } from "../api/userService"; | ||
|
||
export const verifyUserToken = ( | ||
socket: Socket, | ||
next: (err?: ExtendedError) => void | ||
) => { | ||
const token = | ||
socket.handshake.headers.authorization || socket.handshake.auth.token; | ||
verifyToken(token) | ||
.then(() => { | ||
console.log("Valid credentials"); | ||
next(); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
next(new Error("Unauthorized")); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
backend/matching-service/src/api/questionHistoryService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import axios from "axios"; | ||
|
||
const QN_HISTORY_SERVICE_URL = | ||
process.env.QN_HISTORY_SERVICE_URL || | ||
"http://qn-history-service:3006/api/qnhistories"; | ||
|
||
const qnHistoryService = axios.create({ | ||
baseURL: QN_HISTORY_SERVICE_URL, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
export const createQuestionHistory = ( | ||
questionId: string, | ||
title: string, | ||
submissionStatus: string, | ||
language: string, | ||
...userIds: string[] | ||
) => { | ||
const dateAttempted = new Date(); | ||
return qnHistoryService.post("/", { | ||
userIds, | ||
questionId, | ||
title, | ||
submissionStatus, | ||
language, | ||
dateAttempted, | ||
timeTaken: 0, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import axios from "axios"; | ||
|
||
const QUESTION_SERVICE_URL = | ||
process.env.QUESTION_SERVICE_URL || | ||
"http://question-service:3000/api/questions"; | ||
|
||
const questionClient = axios.create({ | ||
baseURL: QUESTION_SERVICE_URL, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
export const getRandomQuestion = (complexity: string, category: string) => { | ||
return questionClient.get("/random", { params: { complexity, category } }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import axios from "axios"; | ||
|
||
const USER_SERVICE_URL = | ||
process.env.USER_SERVICE_URL || "http://localhost:3001/api"; | ||
|
||
const userClient = axios.create({ | ||
baseURL: USER_SERVICE_URL, | ||
withCredentials: true, | ||
}); | ||
|
||
export const verifyToken = (token: string | undefined) => { | ||
return userClient.get("/auth/verify-token", { | ||
headers: { authorization: token }, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
backend/matching-service/src/middlewares/basicAccessControl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ExtendedError, Socket } from "socket.io"; | ||
import { verifyToken } from "../api/userService"; | ||
|
||
export const verifyUserToken = ( | ||
socket: Socket, | ||
next: (err?: ExtendedError) => void | ||
) => { | ||
const token = | ||
socket.handshake.headers.authorization || socket.handshake.auth.token; | ||
verifyToken(token) | ||
.then(() => { | ||
console.log("Valid credentials"); | ||
next(); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
next(new Error("Unauthorized")); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters