Skip to content

Commit 67b2b12

Browse files
committed
add JWT auth to socket server
1 parent 694292b commit 67b2b12

File tree

5 files changed

+123
-38
lines changed

5 files changed

+123
-38
lines changed

.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
REACT_APP_JWT=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.NoZrv5qD6od5nK7CtV_qxMIj2tWyBDkFkNb2hl8Ns3E

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"private": true,
55
"dependencies": {
66
"@teamwork/websocket-json-stream": "^2.0.0",
7-
"@types/jest": "^24.0.0",
87
"@types/node": "^12.0.0",
98
"@types/react": "^16.9.43",
109
"@types/react-dom": "^16.9.8",
1110
"@types/uuid": "^8.0.0",
1211
"express": "^4.17.1",
12+
"jsonwebtoken": "^8.5.1",
1313
"random-words": "^1.1.1",
1414
"react": "^0.0.0-experimental-4c8c98ab9",
1515
"react-dom": "^0.0.0-experimental-4c8c98ab9",
@@ -27,7 +27,6 @@
2727
"scripts": {
2828
"start": "concurrently 'node server' 'react-scripts start'",
2929
"build": "react-scripts build",
30-
"test": "react-scripts test",
3130
"eject": "react-scripts eject"
3231
},
3332
"eslintConfig": {

server.js

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
const WebSocketJSONStream = require("@teamwork/websocket-json-stream");
2+
const ShareDB = require("sharedb");
23
const express = require("express");
34
const http = require("http");
4-
const ShareDB = require("sharedb");
55
const { Server } = require("ws");
6+
const jwt = require("jsonwebtoken");
67

7-
var backend = new ShareDB();
8-
9-
// createDoc(startServer);
8+
const backend = new ShareDB();
109

11-
// (now doing this in the client instead, so that the ID can be dynamic)
12-
// function createDoc(callback) {
13-
// var connection = backend.connect();
14-
// var doc = connection.get("examples", "test");
15-
// doc.fetch(function (err) {
16-
// if (err) throw err;
17-
// if (doc.type === null) {
18-
// doc.create({ nodes: {}, edges: [] }, callback);
19-
// return;
20-
// }
21-
// callback();
22-
// });
23-
// }
10+
const JWT_SECRET = "shh";
2411

2512
function startServer() {
26-
// Create a web server to serve files and listen to WebSocket connections
27-
var app = express();
28-
app.use(express.static("static"));
29-
var server = http.createServer(app);
30-
31-
// Connect any incoming WebSocket connection to ShareDB
32-
var wss = new Server({ server });
33-
wss.on("connection", function (ws) {
34-
var stream = new WebSocketJSONStream(ws);
35-
backend.listen(stream);
13+
const app = express();
14+
15+
const server = http.createServer(app);
16+
17+
const wss = new Server({
18+
server,
19+
verifyClient: (info, cb) => {
20+
// checks if JWT is included in cookies, does not allow connection if invalid
21+
const [, token] = info.req.headers.cookie.match(/Authorization\=([^;]+)/);
22+
23+
if (!token) {
24+
cb(false, 401, "Unauthorized");
25+
} else {
26+
jwt.verify(token, JWT_SECRET, (err, decoded) => {
27+
if (err) {
28+
cb(false, 401, "Unauthorized");
29+
} else {
30+
console.log({ newConnection: decoded });
31+
info.req.user = decoded;
32+
cb(true);
33+
}
34+
});
35+
}
36+
},
37+
});
38+
39+
wss.on("connection", function (ws, req) {
40+
const stream = new WebSocketJSONStream(ws);
41+
backend.listen(stream, req.user);
3642
});
3743

3844
server.listen(8080);
45+
3946
console.log("Listening on http://localhost:8080");
4047
}
4148

src/sharedb.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import ReconnectingWebSocket from "reconnecting-websocket";
22
import sharedb from "sharedb/lib/client";
33

4+
const JWT = process.env.REACT_APP_JWT;
5+
6+
document.cookie = `X-Authorization=${JWT}; path=/`;
7+
48
const socket = new ReconnectingWebSocket("ws://localhost:8080");
5-
const connection = new sharedb.Connection(socket);
9+
10+
const connection = new sharedb.Connection(socket, {});
611

712
export const getConnection = (id) => connection.get("examples", id);
813

yarn.lock

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,13 +1497,6 @@
14971497
"@types/istanbul-lib-coverage" "*"
14981498
"@types/istanbul-lib-report" "*"
14991499

1500-
"@types/jest@^24.0.0":
1501-
version "24.9.1"
1502-
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-24.9.1.tgz#02baf9573c78f1b9974a5f36778b366aa77bd534"
1503-
integrity sha512-Fb38HkXSVA4L8fGKEZ6le5bB8r6MRWlOCZbVuWZcmOMSCd2wCYOwN1ibj8daIoV9naq7aaOZjrLCoCMptKU/4Q==
1504-
dependencies:
1505-
jest-diff "^24.3.0"
1506-
15071500
"@types/json-schema@^7.0.3", "@types/json-schema@^7.0.4":
15081501
version "7.0.5"
15091502
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.5.tgz#dcce4430e64b443ba8945f0290fb564ad5bac6dd"
@@ -2559,6 +2552,11 @@ [email protected]:
25592552
dependencies:
25602553
node-int64 "^0.4.0"
25612554

2555+
2556+
version "1.0.1"
2557+
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
2558+
integrity sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=
2559+
25622560
buffer-from@^1.0.0:
25632561
version "1.1.1"
25642562
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
@@ -3851,6 +3849,13 @@ ecc-jsbn@~0.1.1:
38513849
jsbn "~0.1.0"
38523850
safer-buffer "^2.1.0"
38533851

3852+
3853+
version "1.0.11"
3854+
resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
3855+
integrity sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==
3856+
dependencies:
3857+
safe-buffer "^5.0.1"
3858+
38543859
38553860
version "1.1.1"
38563861
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
@@ -5888,7 +5893,7 @@ jest-config@^24.9.0:
58885893
pretty-format "^24.9.0"
58895894
realpath-native "^1.1.0"
58905895

5891-
jest-diff@^24.3.0, jest-diff@^24.9.0:
5896+
jest-diff@^24.9.0:
58925897
version "24.9.0"
58935898
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-24.9.0.tgz#931b7d0d5778a1baf7452cb816e325e3724055da"
58945899
integrity sha512-qMfrTs8AdJE2iqrTp0hzh7kTd2PQWrsFyj9tORoKmu32xjPjeE4NyjVRDz8ybYwqS2ik8N4hsIpiVTyFeo2lBQ==
@@ -6392,6 +6397,22 @@ jsonify@~0.0.0:
63926397
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
63936398
integrity sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=
63946399

6400+
jsonwebtoken@^8.5.1:
6401+
version "8.5.1"
6402+
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz#00e71e0b8df54c2121a1f26137df2280673bcc0d"
6403+
integrity sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==
6404+
dependencies:
6405+
jws "^3.2.2"
6406+
lodash.includes "^4.3.0"
6407+
lodash.isboolean "^3.0.3"
6408+
lodash.isinteger "^4.0.4"
6409+
lodash.isnumber "^3.0.3"
6410+
lodash.isplainobject "^4.0.6"
6411+
lodash.isstring "^4.0.1"
6412+
lodash.once "^4.0.0"
6413+
ms "^2.1.1"
6414+
semver "^5.6.0"
6415+
63956416
jsprim@^1.2.2:
63966417
version "1.4.1"
63976418
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@@ -6410,6 +6431,23 @@ jsx-ast-utils@^2.2.1, jsx-ast-utils@^2.2.3:
64106431
array-includes "^3.1.1"
64116432
object.assign "^4.1.0"
64126433

6434+
jwa@^1.4.1:
6435+
version "1.4.1"
6436+
resolved "https://registry.yarnpkg.com/jwa/-/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a"
6437+
integrity sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==
6438+
dependencies:
6439+
buffer-equal-constant-time "1.0.1"
6440+
ecdsa-sig-formatter "1.0.11"
6441+
safe-buffer "^5.0.1"
6442+
6443+
jws@^3.2.2:
6444+
version "3.2.2"
6445+
resolved "https://registry.yarnpkg.com/jws/-/jws-3.2.2.tgz#001099f3639468c9414000e99995fa52fb478304"
6446+
integrity sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==
6447+
dependencies:
6448+
jwa "^1.4.1"
6449+
safe-buffer "^5.0.1"
6450+
64136451
killable@^1.0.1:
64146452
version "1.0.1"
64156453
resolved "https://registry.yarnpkg.com/killable/-/killable-1.0.1.tgz#4c8ce441187a061c7474fb87ca08e2a638194892"
@@ -6585,11 +6623,46 @@ lodash._reinterpolate@^3.0.0:
65856623
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
65866624
integrity sha1-DM8tiRZq8Ds2Y8eWU4t1rG4RTZ0=
65876625

6626+
lodash.includes@^4.3.0:
6627+
version "4.3.0"
6628+
resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f"
6629+
integrity sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=
6630+
6631+
lodash.isboolean@^3.0.3:
6632+
version "3.0.3"
6633+
resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6"
6634+
integrity sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=
6635+
6636+
lodash.isinteger@^4.0.4:
6637+
version "4.0.4"
6638+
resolved "https://registry.yarnpkg.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343"
6639+
integrity sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=
6640+
6641+
lodash.isnumber@^3.0.3:
6642+
version "3.0.3"
6643+
resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
6644+
integrity sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=
6645+
6646+
lodash.isplainobject@^4.0.6:
6647+
version "4.0.6"
6648+
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
6649+
integrity sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=
6650+
6651+
lodash.isstring@^4.0.1:
6652+
version "4.0.1"
6653+
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
6654+
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
6655+
65886656
lodash.memoize@^4.1.2:
65896657
version "4.1.2"
65906658
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
65916659
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
65926660

6661+
lodash.once@^4.0.0:
6662+
version "4.1.1"
6663+
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
6664+
integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=
6665+
65936666
lodash.sortby@^4.7.0:
65946667
version "4.7.0"
65956668
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"

0 commit comments

Comments
 (0)