From 131eeea4244682063c2bbcedcd63902094255f3d Mon Sep 17 00:00:00 2001 From: Olivier Vaillancourt Date: Wed, 20 Jun 2012 12:14:39 -0400 Subject: [PATCH 1/2] Added a "registerService" method to authom. Makes it possible to add services without having to place them into the /lib folder inside the authom module. Useful for local business-domain auth services that wouldn't be suited for a pull in authom's core. (For example a company specific in-house login/password system.) --- README.md | 34 ++++++++++++++++++++++++++++++++++ lib/authom.js | 11 +++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dedb266..e7b3146 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,39 @@ server.on("request", function(req, res) { server.listen(8000) ``` +### authom.registerService(serviceName, Service) + +Authom-compliant services can be registered using this method. This is useful for adding custom authentication services not suited to be part of the ```/lib``` core services. (For example a business-specific in-house authentication service.) _Custom services will override existing services of the same name._ + +```javascript +var authom = require("authom") + , EventEmitter = require("events").EventEmitter + +//Custom authentication service +var IpAuth = function(options) { + var server = new EventEmitter + var whiteList = options.whiteList || ["127.0.0.1", "::1"] + + server.on("request", function(req, res) { + if (~whiteList.indexOf(req.connection.remoteAddress)) { + server.emit("auth", req, res, {status: "yay"}) + } + else { + server.emit("error", req, res, {status: "boo"}) + } + }) + + return server +} + +authom.registerService("ip-auth", IpAuth) + +auth.createServer({ + service: "ip-auth", + whiteList : ["127.0.0.1", "::1", "192.168.0.1"] +}) +``` + ### authom.route A regular expression that is run on the pathname of every request. authom will only run if this expression is matched. By default, it is `/^\/auth\/([^\/]+)\/?$/`. @@ -232,6 +265,7 @@ A regular expression that is run on the pathname of every request. authom will o This is a convenience Express app, which should be mounted at a path containing a `:service` parameter. + Providers --------- diff --git a/lib/authom.js b/lib/authom.js index 9f92fe8..109a4c3 100644 --- a/lib/authom.js +++ b/lib/authom.js @@ -6,6 +6,7 @@ var fs = require("fs") , authom = module.exports = new EventEmitter authom.servers = {} +authom.customServices = {} authom.route = /^\/auth\/([^\/]+)\/?$/ authom.createServer = function(options, listener) { @@ -15,8 +16,10 @@ authom.createServer = function(options, listener) { , Service , server - try { Service = require(path) } - catch (err) { throw "No such service: " + path } + if(!(Service = authom.customServices[name])){ + try { Service = require(path) } + catch (err) { throw "No such service: " + path } + } server = authom.servers[name] = new Service(options) @@ -35,6 +38,10 @@ authom.createServer = function(options, listener) { return server } +authom.registerService = function(serviceName, Service){ + authom.customServices[serviceName] = Service +} + authom.listener = function(req, res) { var path = url.parse(req.url).pathname , match = path.match(authom.route) From c3277c5bac68d7e6235c2dee115a8303a775df44 Mon Sep 17 00:00:00 2001 From: Olivier Vaillancourt Date: Fri, 6 Jul 2012 12:14:35 -0300 Subject: [PATCH 2/2] Small programming style tweaks. --- lib/authom.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/authom.js b/lib/authom.js index 109a4c3..bc87323 100644 --- a/lib/authom.js +++ b/lib/authom.js @@ -16,7 +16,7 @@ authom.createServer = function(options, listener) { , Service , server - if(!(Service = authom.customServices[name])){ + if (!(Service = authom.customServices[name])) { try { Service = require(path) } catch (err) { throw "No such service: " + path } } @@ -38,7 +38,7 @@ authom.createServer = function(options, listener) { return server } -authom.registerService = function(serviceName, Service){ +authom.registerService = function(serviceName, Service) { authom.customServices[serviceName] = Service }