Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Add ability to register custom services to Authom #15

Merged
merged 2 commits into from Nov 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Expand Up @@ -224,6 +224,39 @@ server.on("request", function(req, res) {
server.listen(8000) 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 ### 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\/([^\/]+)\/?$/`. 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\/([^\/]+)\/?$/`.
Expand All @@ -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. This is a convenience Express app, which should be mounted at a path containing a `:service` parameter.



Providers Providers
--------- ---------


Expand Down
11 changes: 9 additions & 2 deletions lib/authom.js
Expand Up @@ -6,6 +6,7 @@ var fs = require("fs")
, authom = module.exports = new EventEmitter , authom = module.exports = new EventEmitter


authom.servers = {} authom.servers = {}
authom.customServices = {}
authom.route = /^\/auth\/([^\/]+)\/?$/ authom.route = /^\/auth\/([^\/]+)\/?$/


authom.createServer = function(options, listener) { authom.createServer = function(options, listener) {
Expand All @@ -15,8 +16,10 @@ authom.createServer = function(options, listener) {
, Service , Service
, server , server


try { Service = require(path) } if (!(Service = authom.customServices[name])) {
catch (err) { throw "No such service: " + path } try { Service = require(path) }
catch (err) { throw "No such service: " + path }
}


server = authom.servers[name] = new Service(options) server = authom.servers[name] = new Service(options)


Expand All @@ -35,6 +38,10 @@ authom.createServer = function(options, listener) {
return server return server
} }


authom.registerService = function(serviceName, Service) {
authom.customServices[serviceName] = Service
}

authom.listener = function(req, res) { authom.listener = function(req, res) {
var path = url.parse(req.url).pathname var path = url.parse(req.url).pathname
, match = path.match(authom.route) , match = path.match(authom.route)
Expand Down