Skip to content

Commit 40535e5

Browse files
committed
Add client authentication
1 parent 8a6837c commit 40535e5

File tree

5 files changed

+166
-44
lines changed

5 files changed

+166
-44
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
APP_VERSION=dev-local
2-
AUTH_PROVIDERS=credentials,keycloak,livekit
2+
APP_SECRET=789123678912345789678912345145623456

middleware/auth.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
import "dotenv/config";
2+
import jwt from 'jsonwebtoken';
23

3-
export function authMiddleware(socket, next) {
4-
const token = socket.handshake.query.appToken;
5-
const provider = socket.handshake.query.provider;
4+
export function auth(backendToken) {
5+
console.log("Backend Token", backendToken);
66

7-
if (!token || !provider) {
8-
// return next(new Error("Unauthorized"));
7+
if (!backendToken) {
8+
return false;
99
}
1010

11-
if ("credentials" === provider) {
12-
return next();
13-
} else if ("keycloak" === provider) {
14-
return next();
15-
} else if ("livekit" === provider) {
16-
return next();
11+
try {
12+
const data = jwt.verify(backendToken, process.env.APP_SECRET);
13+
console.log("Auth data", data);
14+
return data;
15+
} catch (err) {
16+
console.log(err);
17+
return false;
1718
}
1819

19-
return next();
20-
// next(new Error("Unauthorized"));
20+
return true;
21+
22+
// if ("credentials" === provider) {
23+
// return true;
24+
// } else if ("keycloak" === provider) {
25+
// return true;
26+
// } else if ("livekit" === provider) {
27+
// return true;
28+
// }
29+
//
30+
// return false;
31+
}
32+
33+
export function httpAuthMiddleware(req, res, next) {
34+
if (!auth(req.query.backendToken)) {
35+
return res.status(401).json({ error: "Unauthorized" });
36+
}
37+
next();
38+
}
39+
40+
export function socketAuthMiddleware(socket, next) {
41+
const userData = auth(socket.handshake.query.backendToken);
42+
if (!userData) {
43+
return next(new Error("Unauthorized"));
44+
}
45+
socket.user = userData;
46+
next();
2147
}

package-lock.json

Lines changed: 119 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"cors": "^2.8.5",
2222
"dotenv": "^16.4.7",
2323
"express": "^4.18.2",
24+
"jsonwebtoken": "^9.0.2",
2425
"socket.io": "^4.8.1"
2526
},
2627
"devDependencies": {

server.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from "fs/promises";
66
import { Server } from "socket.io";
77
import cors from "cors";
88
import "dotenv/config";
9-
import { authMiddleware } from "./middleware/auth.js";
9+
import { httpAuthMiddleware, socketAuthMiddleware } from './middleware/auth.js';
1010

1111
const __filename = fileURLToPath(import.meta.url);
1212
const __dirname = dirname(__filename);
@@ -39,6 +39,8 @@ app.get("/health/check", async (req, res) => {
3939
res.json('OK');
4040
});
4141

42+
app.use(httpAuthMiddleware);
43+
4244
app.get("/download/:docId", async (req, res) => {
4345
const { docId } = req.params;
4446
const filePath = join(__dirname, UPLOAD_DIR, `${docId}.bin`);
@@ -102,16 +104,16 @@ function handleJoin(socket, docId) {
102104
console.log(`Client ${socket.id} joined channel ${docId}`);
103105
}
104106

105-
io.use(authMiddleware);
107+
io.use(socketAuthMiddleware);
106108

107109
io.on("connection", (socket) => {
108110
console.log(`Client ${socket.id} connected!`);
109111
const channelName = socket.handshake.query.channel ?? "default";
110112
if (!channels.has(channelName)) {
111-
channels.set(channelName, { subscribers: new Set(), publishers: new Map(), docIds: new Set() });
113+
channels.set(channelName, { subscribers: new Map(), publishers: new Map(), docIds: new Set() });
112114
}
113115
const channel = channels.get(channelName);
114-
channel.subscribers.add(socket.id);
116+
channel.subscribers.set(socket.id, socket.user);
115117

116118
if (channel.docIds.size > 0) {
117119
for (const docId of channel.docIds) handleJoin(socket, docId);

0 commit comments

Comments
 (0)