Generated
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
|
||||
// Exporting connect integration.
|
||||
module.exports = (auth) => {
|
||||
return (req, res, next) => {
|
||||
auth.check(req, res, (req, res, err) => {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Generated
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
|
||||
// HTTP authentication scheme.
|
||||
const httpScheme = (server, auth) => {
|
||||
return {
|
||||
authenticate: (request, reply) => {
|
||||
// Is auth.
|
||||
auth.isAuthenticated(request, (result) => {
|
||||
if (result instanceof Error) {
|
||||
return reply(result, null, { credentials: null });
|
||||
} else if (!result.pass) {
|
||||
let header = auth.generateHeader(result);
|
||||
return reply(auth.options.msg401).code(401).header('WWW-Authenticate', header);
|
||||
} else {
|
||||
return reply.continue({credentials: { name: result.user }});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Export plugin.
|
||||
exports.register = (plugin, options, next) => {
|
||||
plugin.auth.scheme('http', httpScheme);
|
||||
next();
|
||||
};
|
||||
|
||||
// Export attributes.
|
||||
exports.register.attributes = {
|
||||
pkg: require('../../package.json')
|
||||
};
|
||||
Generated
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
"use strict";
|
||||
|
||||
// HTTP module.
|
||||
const http = require('http');
|
||||
|
||||
// Base module.
|
||||
const Base = require('../auth/base');
|
||||
|
||||
// Backup old server creation.
|
||||
const oldCreateServer = http.createServer;
|
||||
|
||||
// Add authentication method.
|
||||
http.createServer = function() {
|
||||
let server = undefined;
|
||||
|
||||
// Mutated mode.
|
||||
if (arguments[0] instanceof Base) {
|
||||
let auth = arguments[0];
|
||||
|
||||
// With listener.
|
||||
if (arguments[1]) {
|
||||
let listener = arguments[1];
|
||||
let newListener = (req, res) => {
|
||||
auth.check(req, res, (req, res, err) => {
|
||||
if (err) {
|
||||
console.error (err);
|
||||
res.statusCode = 400;
|
||||
res.end(err.message);
|
||||
} else {
|
||||
listener(req, res);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Mutate server.
|
||||
server = oldCreateServer.apply(http, [newListener]);
|
||||
} else {
|
||||
// Without listener.
|
||||
server = oldCreateServer.apply(http, []);
|
||||
server.on('request', (req, res) => {
|
||||
auth.check(req, res);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
server = oldCreateServer.apply(http, arguments);
|
||||
}
|
||||
|
||||
// Return server.
|
||||
return server;
|
||||
};
|
||||
Generated
Vendored
+48
@@ -0,0 +1,48 @@
|
||||
"use strict";
|
||||
|
||||
// HTTPS module.
|
||||
const https = require('https');
|
||||
|
||||
// Base module.
|
||||
const Base = require('../auth/base');
|
||||
|
||||
// Backup old server creation.
|
||||
let oldCreateServer = https.createServer;
|
||||
|
||||
// Mutate server.
|
||||
https.createServer = function() {
|
||||
let server = undefined;
|
||||
|
||||
if (arguments[0] instanceof Base) {
|
||||
let auth = arguments[0];
|
||||
|
||||
if (arguments[2]) {
|
||||
let listener = arguments[2];
|
||||
let newListener = (req, res) => {
|
||||
auth.check(req, res, (req, res, err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.statusCode = 400;
|
||||
res.end(err.message);
|
||||
} else {
|
||||
listener(req, res);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// HTTPS options and listener.
|
||||
server = oldCreateServer.apply(https, [arguments[1], newListener]);
|
||||
} else {
|
||||
// Only HTTPS options.
|
||||
server = oldCreateServer.apply(https, [arguments[1]]);
|
||||
server.on('request', (req, res) => {
|
||||
auth.check(req, res);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
server = oldCreateServer.apply(https, arguments);
|
||||
}
|
||||
|
||||
// Return server.
|
||||
return server;
|
||||
};
|
||||
Generated
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
|
||||
// Export middleware.
|
||||
module.exports = (auth) => {
|
||||
// Middleware for koa.
|
||||
const koa = (req, res, next) => {
|
||||
auth.check(req, res, (req, res, err) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Return middleware.
|
||||
return function *(next) {
|
||||
yield koa.bind(null, this.req, this.res);
|
||||
yield next;
|
||||
};
|
||||
};
|
||||
Generated
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
"use strict";
|
||||
|
||||
// Imports.
|
||||
const passport = require('passport');
|
||||
const util = require('util');
|
||||
|
||||
// Define strategy.
|
||||
function HttpStrategy(auth) {
|
||||
this.name = 'http';
|
||||
this.authentication = auth;
|
||||
|
||||
passport.Strategy.call(this);
|
||||
}
|
||||
|
||||
// Inherit basic strategy.
|
||||
util.inherits(HttpStrategy, passport.Strategy);
|
||||
|
||||
// Define auth method.
|
||||
HttpStrategy.prototype.authenticate = function (req) {
|
||||
let self = this;
|
||||
|
||||
// Is auth.
|
||||
this.authentication.isAuthenticated(req, (result) => {
|
||||
if (result instanceof Error) {
|
||||
self.error(result);
|
||||
} else if (!result.pass) {
|
||||
let header = self.authentication.generateHeader(result);
|
||||
self.fail(header);
|
||||
} else {
|
||||
self.success(result.user);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Export.
|
||||
module.exports = (auth) => {
|
||||
return new HttpStrategy(auth);
|
||||
};
|
||||
Generated
Vendored
+55
@@ -0,0 +1,55 @@
|
||||
"use strict";
|
||||
|
||||
// Proxy module.
|
||||
const httpProxy = require('http-proxy');
|
||||
|
||||
// Base module.
|
||||
const Base = require('../auth/base');
|
||||
|
||||
// Backup old server creation.
|
||||
const oldCreateServer = httpProxy.createServer;
|
||||
|
||||
// New create server.
|
||||
const newCreateServer = function(auth, options) {
|
||||
// Mutated mode.
|
||||
if (auth instanceof Base) {
|
||||
// Set proxy flag.
|
||||
auth.proxy = true;
|
||||
} else {
|
||||
// Set correct options.
|
||||
options = auth;
|
||||
|
||||
// Clear authentication value.
|
||||
auth = null;
|
||||
}
|
||||
|
||||
// Default listener plus authentication check.
|
||||
let server = oldCreateServer.apply(httpProxy, [options]);
|
||||
|
||||
// Authentication provided.
|
||||
if (auth) {
|
||||
// Override proxyRequest.
|
||||
let oldProxyRequest = server.web;
|
||||
server.web = function (req, res) {
|
||||
// Fetch external arguments.
|
||||
let externalArguments = arguments;
|
||||
|
||||
// Check for authentication.
|
||||
auth.check(req, res, (req, res, err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.statusCode = 400;
|
||||
res.end (err.message);
|
||||
} else {
|
||||
oldProxyRequest.apply(server, externalArguments)
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// Return server.
|
||||
return server;
|
||||
};
|
||||
|
||||
// Add authentication method.
|
||||
httpProxy.createServer = httpProxy.createProxyServer = httpProxy.createProxy = newCreateServer;
|
||||
Reference in New Issue
Block a user