Skip to content

Commit

Permalink
Allow for auth plugin to be a class or non-class function, and resolve
Browse files Browse the repository at this point in the history
issue with ws library retiring upgradeReq property
  • Loading branch information
samfrances committed Nov 3, 2017
1 parent dc31fab commit 5c3ac52
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
23 changes: 21 additions & 2 deletions other/js/auth_plugin_examples.js
Expand Up @@ -50,10 +50,29 @@ exports.tokenAuth = function tokenAuth(source) {
* a token provided as the argument to the `--auth-source` command line
* argument.
*/
return function(info) {
let token = source;
return {
authenticate(info) {
const token = source;
return urlTokenMatch(info.req.url, token, true);
}
}
}

exports.TokenAuthClass = class TokenAuthClass {
/**
* Class-based equivalent of tokenAuth
*/

constructor(source) {
this.source = source;
}

authenticate(info) {
const token = this.source;
console.log(token)
return urlTokenMatch(info.req.url, token, true);
}

}

exports.tokenAuthEnv = function tokenAuthEnv(source) {
Expand Down
5 changes: 4 additions & 1 deletion other/js/package.json
Expand Up @@ -8,7 +8,10 @@
"type": "git",
"url": "git://github.com/kanaka/websockify.git"
},
"files": ["../../docs/LICENSE.LGPL-3","websockify.js"],
"files": [
"../../docs/LICENSE.LGPL-3",
"websockify.js"
],
"bin": {
"websockify": "./websockify.js"
},
Expand Down
24 changes: 24 additions & 0 deletions other/js/utils.js
@@ -0,0 +1,24 @@

/**
* A decorator that will wrap a function or a class. In the case of a non-class
* function, the wrapped function will behave exactly the same as before.
* In the case of a class, the wrapper will allow instantiating the function]
* without using the |new| keyword. This is useful when you don't know
* ahead of time if the function you will be calling is a class or a non-class
* function
*
* @param {function} function_or_class
*/
exports.factorify = function factorify(function_or_class) {
return (...args) => {
try {
return function_or_class(...args);
} catch (e) {
if (e instanceof TypeError) {
return new function_or_class(...args);
} else {
throw e;
}
}
}
}
21 changes: 13 additions & 8 deletions other/js/websockify.js
Expand Up @@ -20,16 +20,18 @@ var argv = require('optimist').argv,
Buffer = require('buffer').Buffer,
WebSocketServer = require('ws').Server,

utils = require('./utils'),

webServer, wsServer,
source_host, source_port, target_host, target_port,
auth_plugin, websocket_server_opts,
web_path = null;


// Handle new WebSocket client
new_client = function(client) {
new_client = function(client, upgradeReq) {
var clientAddr = client._socket.remoteAddress, log;
console.log(client.upgradeReq.url);
console.log(upgradeReq ? upgradeReq.url : client.upgradeReq.url);
log = function (msg) {
console.log(' ' + clientAddr + ': '+ msg);
};
Expand Down Expand Up @@ -164,15 +166,18 @@ if (argv.cert) {
}

if (argv["auth-plugin"]) {
let auth_plugin_arg = argv["auth-plugin"].split(".")
let plugin_name = auth_plugin_arg.pop();
let module_path = auth_plugin_arg.join(".");
const auth_plugin_arg = argv["auth-plugin"].split(".")
const plugin_name = auth_plugin_arg.pop();
const module_path = auth_plugin_arg.join(".");

const auth_source = argv["auth-source"] || undefined;
const auth_plugin = utils.factorify(
require(module_path)[plugin_name]
)(auth_source);

let auth_plugin = require(module_path)[plugin_name];
let auth_source = argv["auth-source"] || undefined;
websocket_server_opts = {
server: webServer,
verifyClient: auth_plugin(auth_source)
verifyClient: auth_plugin.authenticate.bind(auth_plugin)
};
} else {
websocket_server_opts = {server: webServer};
Expand Down

0 comments on commit 5c3ac52

Please sign in to comment.