-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
authentication and json encoding moved to separate files
- Loading branch information
Robin Gottfried
committed
Feb 17, 2020
1 parent
36816dc
commit 19b5091
Showing
6 changed files
with
133 additions
and
53 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
Empty file.
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,55 @@ | ||
'use strict'; | ||
//-- vim: ft=javascript tabstop=2 softtabstop=2 expandtab shiftwidth=2 | ||
|
||
class DecodeError extends Error {} | ||
|
||
class WsJsonProtocol extends Object { | ||
|
||
constructor(ws) { | ||
super(); | ||
this._ws = ws; | ||
} | ||
|
||
_encode(messageObject) { | ||
return JSON.stringify(messageObject, undefined, 3); | ||
} | ||
|
||
_decode(rawMessage) { | ||
try { | ||
return JSON.parse(rawMessage); | ||
} catch(err) { | ||
throw DecodeError(`Could not decode message ... ${err.toString()}`); | ||
} | ||
} | ||
|
||
send(messageObject) { | ||
if (! messageObject instanceof Object) { | ||
throw new Error(`Type error, expected Object but got ${typeof messageObject}.`); | ||
} | ||
this._ws.send(this._encode(messageObject)); | ||
} | ||
|
||
on(eventName, callback) { | ||
let decode_ = this._decode.bind(this); | ||
let decoderCallback = function (message) { | ||
let decodedMessage; | ||
try { | ||
let decodedMessage = decode_(message); | ||
} catch(err) { | ||
console.error(err); | ||
} | ||
if (decodedMessage !== undefined) { | ||
callback(decodedMessage); | ||
} | ||
} | ||
if (eventName == "message") { | ||
return this._ws.on(eventName, decoderCallback); | ||
} else { | ||
return this._ws.on(eventName, callback); | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
module.exports = WsJsonProtocol; |
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,33 @@ | ||
'use strict'; | ||
//-- vim: ft=javascript tabstop=2 softtabstop=2 expandtab shiftwidth=2 | ||
|
||
class KeyAuthenticator extends Object { | ||
constructor(allowed_keys) { | ||
super(); | ||
this.allowed_keys = new Set(allowed_keys); | ||
this._clients_by_id = {}; | ||
} | ||
|
||
authenticate(request, callback) { | ||
console.log(`Authenticating request ${request} on ${request.url}.`); | ||
const match = /^\/ws\/pill\/(?<client_key>.*)$/.exec(request.url); | ||
if (! match) return callback(new Error("Unknown url.")); | ||
if (this.allowed_keys.has(match.groups.client_key)) { | ||
console.log(`Key ${match.groups.client_key} accepted`); | ||
callback(null, {"key": match.groups.client_key}); | ||
} else { | ||
console.log('Invalid key'); | ||
callback(new Error("Invalid key."), null); | ||
} | ||
} | ||
|
||
onConnected (ws, client) { | ||
this._clients_by_id[client.key] = ws; | ||
} | ||
|
||
clientFromId (id) { | ||
return this._clients_by_id[id]; | ||
} | ||
} | ||
|
||
module.exports = KeyAuthenticator; |
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