Skip to content

Commit 8a6837c

Browse files
committed
Prepare authentication middleware
1 parent 09e7a2f commit 8a6837c

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

.env

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

middleware/auth.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import "dotenv/config";
2+
3+
export function authMiddleware(socket, next) {
4+
const token = socket.handshake.query.appToken;
5+
const provider = socket.handshake.query.provider;
6+
7+
if (!token || !provider) {
8+
// return next(new Error("Unauthorized"));
9+
}
10+
11+
if ("credentials" === provider) {
12+
return next();
13+
} else if ("keycloak" === provider) {
14+
return next();
15+
} else if ("livekit" === provider) {
16+
return next();
17+
}
18+
19+
return next();
20+
// next(new Error("Unauthorized"));
21+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "secure-document-share",
33
"version": "1.0.0",
44
"description": "This package creates a service to share pdfs which are converted to images and then shared via a link so it is like a share slideshow",
5+
"type": "module",
56
"main": "server.js",
67
"scripts": {
78
"start": "node server.mjs",

server.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +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";
910

1011
const __filename = fileURLToPath(import.meta.url);
1112
const __dirname = dirname(__filename);
@@ -16,6 +17,7 @@ const argPort = process.argv.find(arg => arg.startsWith("--port="));
1617
const PORT = argPort ? parseInt(argPort.split("=")[1], 10) : (process.env.PORT || DEFAULT_PORT);
1718
const version = process.env.APP_VERSION || "not_set";
1819
const slideshows = new Map();
20+
const channels = new Map();
1921
const MAX_CONCURRENT_WRITES = 5;
2022
let activeWrites = 0;
2123
const uploadQueue = [];
@@ -64,7 +66,6 @@ app.get("/download/:docId", async (req, res) => {
6466

6567
const server = app.listen(PORT, () => console.log(`Server running at http://localhost:${PORT}`));
6668
const io = new Server(server, { maxHttpBufferSize: 1e8, pingTimeout: 60000 });
67-
const channels = new Map();
6869

6970
async function processUploadQueue() {
7071
if (activeWrites >= MAX_CONCURRENT_WRITES || uploadQueue.length === 0) return;
@@ -101,6 +102,8 @@ function handleJoin(socket, docId) {
101102
console.log(`Client ${socket.id} joined channel ${docId}`);
102103
}
103104

105+
io.use(authMiddleware);
106+
104107
io.on("connection", (socket) => {
105108
console.log(`Client ${socket.id} connected!`);
106109
const channelName = socket.handshake.query.channel ?? "default";

0 commit comments

Comments
 (0)