Add comments to example authorisation plugins
This commit is contained in:
parent
0e1824c0ff
commit
c6d7444388
|
|
@ -10,9 +10,20 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const querystring = require('querystring');
|
const querystring = require('querystring');
|
||||||
fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
function urlTokenMatch(url, token, verbose=false) {
|
function urlTokenMatch(url, token, verbose=false) {
|
||||||
|
/**
|
||||||
|
* 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("?")
|
let splitUrl = url.split("?")
|
||||||
if (splitUrl.length !== 2) {
|
if (splitUrl.length !== 2) {
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
|
|
@ -34,6 +45,11 @@ function urlTokenMatch(url, token, verbose=false) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.tokenAuth = function tokenAuth(source) {
|
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 function(info) {
|
return function(info) {
|
||||||
let token = source;
|
let token = source;
|
||||||
return urlTokenMatch(info.req.url, token, true);
|
return urlTokenMatch(info.req.url, token, true);
|
||||||
|
|
@ -41,6 +57,12 @@ exports.tokenAuth = function tokenAuth(source) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.tokenAuthEnv = function tokenAuthEnv(source) {
|
exports.tokenAuthEnv = function tokenAuthEnv(source) {
|
||||||
|
/**
|
||||||
|
* Authorisation plugin which validates the token query parameter against
|
||||||
|
* 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) {
|
return function(info) {
|
||||||
let token = process.env[source];
|
let token = process.env[source];
|
||||||
return urlTokenMatch(info.req.url, token, true);
|
return urlTokenMatch(info.req.url, token, true);
|
||||||
|
|
@ -48,6 +70,11 @@ exports.tokenAuthEnv = function tokenAuthEnv(source) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.tokenAuthFile = function tokenEnvFile(source) {
|
exports.tokenAuthFile = function tokenEnvFile(source) {
|
||||||
|
/**
|
||||||
|
* Authorisation plugin which validates the token query parameter against a
|
||||||
|
* token which is contained in a text file, the path to which is specified
|
||||||
|
* as the value of the --auth-source command line argument
|
||||||
|
*/
|
||||||
return function(info, cb) {
|
return function(info, cb) {
|
||||||
fs.readFile(source, 'utf8', function(err, data) {
|
fs.readFile(source, 'utf8', function(err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue