Replace token auth examples
This commit is contained in:
parent
ff1897ee94
commit
a543c35943
|
|
@ -9,101 +9,78 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const querystring = require('querystring');
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
function urlTokenMatch(url, token, verbose=false) {
|
class BaseAuth {
|
||||||
/**
|
|
||||||
* Parse the url path, extract the `token` querystring value, and check if
|
|
||||||
* it matches the token argument. If verbose is set to true, log messages
|
|
||||||
* are enabled.
|
|
||||||
*
|
|
||||||
* Args:
|
|
||||||
* url (string): the path section of the URL
|
|
||||||
* token (string): the token which the token provided in the URL should
|
|
||||||
* match
|
|
||||||
* verbose (boolean): If True, extra console.log messages will be output
|
|
||||||
*/
|
|
||||||
let splitUrl = url.split("?")
|
|
||||||
if (splitUrl.length !== 2) {
|
|
||||||
if (verbose) {
|
|
||||||
console.log("Permission denied. No token provided.");
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
let qs = splitUrl[1];
|
|
||||||
let qs_parsed = querystring.parse(qs);
|
|
||||||
let success = (qs_parsed.token === token);
|
|
||||||
if (verbose) {
|
|
||||||
if (!success) {
|
|
||||||
console.log("Permission denied for token: " + qs_parsed.token);
|
|
||||||
} else {
|
|
||||||
console.log("Permission granted for token: " + qs_parsed.token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.tokenAuth = function tokenAuth(source) {
|
|
||||||
/**
|
|
||||||
* Authorisation plugin which validates the token query parameter against
|
|
||||||
* a token provided as the argument to the `--auth-source` command line
|
|
||||||
* argument.
|
|
||||||
*/
|
|
||||||
return {
|
|
||||||
authenticate(info) {
|
|
||||||
const token = source;
|
|
||||||
return urlTokenMatch(info.req.url, token, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.TokenAuthClass = class TokenAuthClass {
|
|
||||||
/**
|
|
||||||
* Class-based equivalent of tokenAuth
|
|
||||||
*/
|
|
||||||
|
|
||||||
constructor(source) {
|
constructor(source) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticate(info) {
|
authenticate(info) {
|
||||||
const token = this.source;
|
return false;
|
||||||
console.log(token)
|
|
||||||
return urlTokenMatch(info.req.url, token, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.tokenAuthEnv = function tokenAuthEnv(source) {
|
/**
|
||||||
/**
|
* Authorisation plugin which validates origin of the request against a single
|
||||||
* Authorisation plugin which validates the token query parameter against
|
* permitted origin
|
||||||
* a token which is the value of an environment variable. The name of this
|
|
||||||
* environment variable is specified as the argument to the command line
|
|
||||||
* argument `--auth-source`
|
|
||||||
*/
|
*/
|
||||||
return function(info) {
|
exports.AuthByOrigin = class AuthByOrigin extends BaseAuth {
|
||||||
let token = process.env[source];
|
|
||||||
return urlTokenMatch(info.req.url, token, true);
|
authenticate(info) {
|
||||||
|
const expected = this.source;
|
||||||
|
const actual = info.origin;
|
||||||
|
const allow = expected === actual;
|
||||||
|
if (!allow) {
|
||||||
|
console.log("Denied access from origin: " + actual)
|
||||||
}
|
}
|
||||||
|
return allow;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.tokenAuthFile = function tokenEnvFile(source) {
|
/**
|
||||||
/**
|
* Function-based version of AuthByOrigin
|
||||||
* Authorisation plugin which validates the token query parameter against a
|
*/
|
||||||
* token which is contained in a text file, the path to which is specified
|
exports.AuthByOriginFunctional = function(source) {
|
||||||
|
return {
|
||||||
|
authenticate(info) {
|
||||||
|
const expected = source;
|
||||||
|
const actual = info.origin;
|
||||||
|
const success = expected === actual;
|
||||||
|
if (!success) {
|
||||||
|
console.log("Denied access from origin: " + actual)
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authorisation plugin which validates the origin of the request against
|
||||||
|
* an origin contained in a text file, the path to which is specified
|
||||||
* as the value of the `--auth-source` command line argument
|
* as the value of the `--auth-source` command line argument
|
||||||
*/
|
*/
|
||||||
return function(info, cb) {
|
exports.AuthByOriginFile = class AuthByOriginFile extends BaseAuth {
|
||||||
fs.readFile(source, 'utf8', function(err, data) {
|
|
||||||
|
|
||||||
|
authenticate(info, cb) {
|
||||||
|
fs.readFile(this.source, 'utf8', function(err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
cb(false);
|
cb(false);
|
||||||
} else {
|
} else {
|
||||||
let token = data.trim();
|
const expected = data.trim();
|
||||||
let success = urlTokenMatch(info.req.url, token, true);
|
const actual = info.origin;
|
||||||
|
const success = expected === actual;
|
||||||
|
if (!success) {
|
||||||
|
console.log("Denied access from origin: " + actual);
|
||||||
|
}
|
||||||
cb(success);
|
cb(success);
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -29,7 +29,7 @@ var argv = require('optimist').argv,
|
||||||
|
|
||||||
|
|
||||||
// Handle new WebSocket client
|
// Handle new WebSocket client
|
||||||
new_client = function(client, req) {
|
const new_client = function(client, req) {
|
||||||
var clientAddr = client._socket.remoteAddress, log;
|
var clientAddr = client._socket.remoteAddress, log;
|
||||||
console.log(req ? req.url : client.upgradeReq.url);
|
console.log(req ? req.url : client.upgradeReq.url);
|
||||||
log = function (msg) {
|
log = function (msg) {
|
||||||
|
|
@ -76,7 +76,7 @@ new_client = function(client, req) {
|
||||||
|
|
||||||
|
|
||||||
// Send an HTTP error response
|
// Send an HTTP error response
|
||||||
http_error = function (response, code, msg) {
|
const http_error = function (response, code, msg) {
|
||||||
response.writeHead(code, {"Content-Type": "text/plain"});
|
response.writeHead(code, {"Content-Type": "text/plain"});
|
||||||
response.write(msg + "\n");
|
response.write(msg + "\n");
|
||||||
response.end();
|
response.end();
|
||||||
|
|
@ -84,7 +84,7 @@ http_error = function (response, code, msg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process an HTTP static file request
|
// Process an HTTP static file request
|
||||||
http_request = function (request, response) {
|
const http_request = function (request, response) {
|
||||||
// console.log("pathname: " + url.parse(req.url).pathname);
|
// console.log("pathname: " + url.parse(req.url).pathname);
|
||||||
// res.writeHead(200, {'Content-Type': 'text/plain'});
|
// res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||||
// res.end('okay');
|
// res.end('okay');
|
||||||
|
|
@ -183,7 +183,7 @@ if (argv["auth-plugin"]) {
|
||||||
|
|
||||||
const auth_source = argv["auth-source"] || undefined;
|
const auth_source = argv["auth-source"] || undefined;
|
||||||
|
|
||||||
const auth_plugin = plugin_factory(auth_source);
|
auth_plugin = plugin_factory(auth_source);
|
||||||
|
|
||||||
websocket_server_opts = {
|
websocket_server_opts = {
|
||||||
server: webServer,
|
server: webServer,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue