Permalink
Browse files

Merge pull request #15 from ovaillancourt/customservice

Add ability to register custom services to Authom
  • Loading branch information...
jed committed Nov 8, 2012
2 parents 84d2a29 + c3277c5 commit 584ea916c16b5a4401eca054147196500e1d3341
Showing with 43 additions and 2 deletions.
  1. +34 −0 README.md
  2. +9 −2 lib/authom.js
View
@@ -231,6 +231,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._
@ryedin

ryedin Nov 8, 2012

Contributor

Yayyyyyyyyyyyyyy!!!

Thanks for the committer, and thanks jed for merging this. This will be huge!

+
+```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\/([^\/]+)\/?$/`.
@@ -239,6 +272,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
---------
View
@@ -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)

0 comments on commit 584ea91

Please sign in to comment.