Skip to content

Commit 7b90c58

Browse files
Daniel HochleitnerDaniel Hochleitner
authored andcommitted
added a random auth token mechanism to have some sort of authentication between websocket client and server
1 parent 7a17142 commit 7b90c58

File tree

7 files changed

+262
-179
lines changed

7 files changed

+262
-179
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [Usage](#usage)
1919
- [Node.js Server](#nodejs-server)
2020
- [REST-Service](#rest-service)
21+
- [PL/SQL API](#plsql-api)
2122
- [APEX](#apex)
2223
- [License](#license)
2324

@@ -47,8 +48,12 @@ Developers don´t need to be experts in Javascript or JQuery and stuff like that
4748
A demo application is available under
4849
https://apex.danielh.de/ords/f?p=WSNOTIFY
4950

50-
And of course you find a APEX export (demo_app.sql) of it in [../apex/](https://github.com/Dani3lSun/apex-websocket-notify-bundle/tree/master/apex) folder. To use it just import the app and then go through the installation steps below.
51-
Under Shared Components --> Edit Application Definition --> Substitutions Strings, set "G_WS_SERVER_HOST" to the hostname or ip address and "G_WS_SERVER_PORT" to the port of your node notification server.
51+
And of course you find a APEX export (**demo_app.sql**) of it in [../apex/](https://github.com/Dani3lSun/apex-websocket-notify-bundle/tree/master/apex) folder. To use it just import the app and then go through the installation steps below.
52+
Under Shared Components --> Edit Application Definition --> Substitutions Strings, set
53+
54+
- **G_WS_SERVER_HOST** to the hostname or ip address of your node notification server
55+
- **G_WS_SERVER_PORT** to the port of your node notification server
56+
- **G_WS_SERVER_AUTHTOKEN** to your secure and random authToken of your node notification server (read further for more informations)
5257

5358
The demo includes all plugins and shows the most common preferences and possibilities.
5459

@@ -129,7 +134,8 @@ You can change the default behavior of the server by editing the JSON config fil
129134
},
130135
"socket": {
131136
"private": true, // activate private websocket room/namespace of server
132-
"public": true // activate public websocket room/namespace of server
137+
"public": true, // activate public websocket room/namespace of server
138+
"authToken":"please-change-me" // authentication token, client should have the same to connect with websocket, please change it to some random string
133139
}
134140
}
135141
```
@@ -272,16 +278,20 @@ http://[host-ip-of-server]:[port]/notifyuser
272278
- **notify-title** (required) - Title of notification
273279
- **notify-message** (required) - Message content of notification
274280

275-
A demo call using curl could look like this:
281+
A demo call using curl looks like this:
276282

277283
```
278284
curl -H "notify-title: Test Title Text" -H "notify-message: Test Message Text" "http://[host-ip-of-server]:[port]/notifyuser?userid=daniel&room=private&type=info&optparam=myoptionalinfo123"
279285
```
280286

287+
### PL/SQL API
288+
289+
281290

282291
### APEX
283292

293+
294+
284295
## License
285-
This software is under **MIT License**.
286296

287-
---
297+
This software is under **MIT License**.

apex/demo_app.sql

Lines changed: 103 additions & 82 deletions
Large diffs are not rendered by default.

apex/plugins/dynamic_action_plugin_de_danielh_initwsnotifyconnection.sql

Lines changed: 91 additions & 74 deletions
Large diffs are not rendered by default.

node/node-notify-server/client.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
<script type="text/javascript">
1515
// connect info
1616
var userid = 'daniel';
17-
var connectString = 'userid=' + userid;
17+
var authToken = 'please-change-me';
18+
var connectString = 'userid=' + userid + '&authtoken=' + authToken;
1819
console.log(connectString);
1920
// connect to sockets
2021
var privateSocket = io.connect('/private', {
@@ -40,6 +41,14 @@
4041
console.log('time', data.time);
4142
console.log('optparam', data.optparam);
4243
});
44+
// socket disconnect events
45+
// socket message events
46+
privateSocket.on('disconnect', function() {
47+
console.log('private-socket disconnected');
48+
});
49+
publicSocket.on('disconnect', function() {
50+
console.log('public-socket disconnected');
51+
});
4352
</script>
4453

4554
</body>

node/node-notify-server/prefs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ module.exports = {
3131
} else if (pType == 'socket') {
3232
var lPrivate = jsonObject.socket.private;
3333
var lPublic = jsonObject.socket.public;
34+
var lAuthToken = jsonObject.socket.authToken;
3435
return {
3536
private: lPrivate,
36-
public: lPublic
37+
public: lPublic,
38+
authToken: lAuthToken
3739
};
3840
}
3941
},

node/node-notify-server/prefs.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"socket": {
1212
"private": true,
13-
"public": true
13+
"public": true,
14+
"authToken":"please-change-me"
1415
}
1516
}

node/node-notify-server/server.js

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var sslKeyPath = serverPrefs.sslKeyPath;
1515
var sslCertPath = serverPrefs.sslCertPath;
1616
var isPrivate = socketPrefs.private;
1717
var isPublic = socketPrefs.public;
18+
var socketAuthToken = socketPrefs.authToken;
1819
var server;
1920
// Create HTTP Server
2021
// SSL HTTP
@@ -172,28 +173,50 @@ var socketio = {
172173
if (isPrivate) {
173174
ioPrivate.on('connection', function(socket) {
174175
var userid = socket.handshake.query.userid;
175-
socket.userid = userid;
176-
// logging
177-
prefs.doLog(userid + ' connected to Private');
178-
// save session
179-
localstore.saveUserSession(userid, 'private', socket.id, function() {
176+
var authToken = socket.handshake.query.authtoken;
177+
// check authToken
178+
if (authToken == socketAuthToken) {
179+
// token success
180+
socket.userid = userid;
180181
// logging
181-
prefs.doLog(userid + ' private session saved in DB');
182-
});
182+
prefs.doLog(userid + ' connected to Private');
183+
// save session
184+
localstore.saveUserSession(userid, 'private', socket.id, function() {
185+
// logging
186+
prefs.doLog(userid + ' private session saved in DB');
187+
});
188+
} else {
189+
// token error
190+
// logging
191+
prefs.doLog(userid + ' with wrong authToken: ' + authToken);
192+
// disconnect
193+
socket.disconnect();
194+
}
183195
});
184196
}
185197
// Public connect
186198
if (isPublic) {
187199
ioPublic.on('connection', function(socket) {
188200
var userid = socket.handshake.query.userid;
189-
socket.userid = userid;
190-
// logging
191-
prefs.doLog(userid + ' connected to Public');
192-
// save session
193-
localstore.saveUserSession(userid, 'public', socket.id, function() {
201+
var authToken = socket.handshake.query.authtoken;
202+
// check authToken
203+
if (authToken == socketAuthToken) {
204+
// token success
205+
socket.userid = userid;
194206
// logging
195-
prefs.doLog(userid + ' public session saved in DB');
196-
});
207+
prefs.doLog(userid + ' connected to Public');
208+
// save session
209+
localstore.saveUserSession(userid, 'public', socket.id, function() {
210+
// logging
211+
prefs.doLog(userid + ' public session saved in DB');
212+
});
213+
} else {
214+
// token error
215+
// logging
216+
prefs.doLog(userid + ' with wrong authToken: ' + authToken);
217+
// disconnect
218+
socket.disconnect();
219+
}
197220
});
198221
}
199222
},

0 commit comments

Comments
 (0)