Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add ability to register custom services to Authom #15

Merged
merged 2 commits into from

3 participants

@ovaillancourt

This pull adds the registerService method that allows someone to register an authom-compatible service that is too specific to go into the core services folder. (For example a business-specific login/pass system.)

This also allows to override the behavior of existing "core" services.

Also added documentation + usage example.

ovaillancourt added some commits
@ovaillancourt ovaillancourt 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.)
131eeea
@ovaillancourt ovaillancourt Small programming style tweaks. c3277c5
@deedubs

+1 to this. Seems like a more efficient way to allow people to add services. I would almost like to see all the 'services' registered in this way.

@deedubs

@jed Is there something we can do to improve this PR to get it accepted? Or are you against the idea outright?

@jed jed merged commit 584ea91 into from
@jed
Owner
jed commented

it's a good idea, just fell through the cracks. merged, about to update npm.

@deedubs
@jed
Owner
jed commented

don't thank me, thank @ovaillancourt!

@ovaillancourt

haha, well thanks jed anyways! Man I had completely forgot about that pull too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 6, 2012
  1. @ovaillancourt

    Added a "registerService" method to authom. Makes it possible to add …

    ovaillancourt authored
    …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.)
  2. @ovaillancourt
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 2 deletions.
  1. +34 −0 README.md
  2. +9 −2 lib/authom.js
View
34 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
---------
View
11 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)
Something went wrong with that request. Please try again.