Skip to content

Commit

Permalink
jsstyle clean
Browse files Browse the repository at this point in the history
  • Loading branch information
trentm committed Mar 16, 2012
1 parent e1e5bd0 commit af5beb9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 49 deletions.
21 changes: 10 additions & 11 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* An expiring LRU cache.
*
* Usage:
* var Cache = require("amon-common").Cache;
* var Cache = require('amon-common').Cache;
* // size, expiry, log, name
* this.accountCache = new Cache( 100, 300, log, "account");
* this.accountCache.set("hamish", {...});
* this.accountCache = new Cache( 100, 300, log, 'account');
* this.accountCache.set('hamish', {...});
* ...
* this.accountCache.get("hamish") // -> {...}
* this.accountCache.get('hamish') // -> {...}
*/

var debug = console.warn;
Expand All @@ -32,7 +32,7 @@ function Cache(size, expiry, log, name) {
this.size = size;
this.expiry = expiry * 1000;
this.log = log;
this.name = (name ? name + " " : "");
this.name = (name ? name + ' ' : '');
this.items = LRU(this.size);
}

Expand All @@ -43,7 +43,7 @@ function Cache(size, expiry, log, name) {

Cache.prototype.reset = function reset() {
if (this.log) {
this.log.trace("%scache reset", this.name);
this.log.trace('%scache reset', this.name);
}
this.items.reset();
}
Expand All @@ -54,13 +54,13 @@ Cache.prototype.get = function get(key) {
if (cached) {
if (((new Date()).getTime() - cached.ctime) <= this.expiry) {
if (this.log) {
this.log.trace("%scache hit: key='%s': %o", this.name, key, cached);
this.log.trace('%scache hit: key="%s": %o', this.name, key, cached);
}
return cached.value;
}
}
if (this.log) {
this.log.trace("%scache miss: key='%s'", this.name, key);
this.log.trace('%scache miss: key="%s"', this.name, key);
}
return null;
}
Expand All @@ -72,19 +72,18 @@ Cache.prototype.set = function set(key, value) {
ctime: new Date().getTime()
};
if (this.log) {
this.log.trace("%scache set: key='%s': %o", this.name, key, item);
this.log.trace('%scache set: key="%s": %o', this.name, key, item);
}
this.items.set(key, item);
return item;
}

Cache.prototype.del = function del(key) {
if (this.log) {
this.log.trace("%scache del: key='%s'", this.name, key);
this.log.trace('%scache del: key="%s"', this.name, key);
}
this.items.del(key);
}


module.exports = Cache;

86 changes: 48 additions & 38 deletions lib/ldapauth.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
/* Copyright 2011 (c) Trent Mick.
/**
* Copyright 2011 (c) Trent Mick.
*
* LDAP auth.
*
* Usage:
* var LdapAuth = require('ldapauth');
* var auth = new LdapAuth({url: 'ldaps://ldap.example.com:663', ...});
* ...
* auth.authenticate(username, password, function(err, user) { ... });
* auth.authenticate(username, password, function (err, user) { ... });
* ...
* auth.close(function(err) { ... })
* auth.close(function (err) { ... })
*/

var assert = require('assert');
var bcrypt = require('bcrypt');
var ldap = require("ldapjs");
var ldap = require('ldapjs');
var debug = console.warn;
var format = require('util').format;



/**
* Create an LDAP auth class. Primary usage is the `.authenticate` method.
*
* @param opts {Object} Config options. Keys (required, unless says otherwise) are:
* url {String} E.g. "ldaps://ldap.example.com:663"
* adminDn {String} E.g. "uid=myapp,ou=users,o=example.com"
* @param opts {Object} Config options. Keys (required, unless says
* otherwise) are:
* url {String} E.g. 'ldaps://ldap.example.com:663'
* adminDn {String} E.g. 'uid=myapp,ou=users,o=example.com'
* adminPassword {String} Password for adminDn.
* searchBase {String} The base DN from which to search for users by
* username. E.g. "ou=users,o=example.com"
* username. E.g. 'ou=users,o=example.com'
* searchFilter {String} LDAP search filter with which to find a user by
* username, e.g. "(uid={{username}})". Use the literal "{{username}}"
* username, e.g. '(uid={{username}})'. Use the literal '{{username}}'
* to have the given username be interpolated in for the LDAP
* search.
* log4js {Module} Optional. The require'd log4js module to use for logging.
Expand All @@ -46,12 +49,12 @@ function LdapAuth(opts) {
assert.ok(opts.adminPassword);
assert.ok(opts.searchBase);
assert.ok(opts.searchFilter);
this.log = opts.log4js && opts.log4js.getLogger("ldapauth");

this.log = opts.log4js && opts.log4js.getLogger('ldapauth');

if (opts.cache) {
var Cache = require("./cache");
this.userCache = new Cache(100, 300, this.log, "user");
var Cache = require('./cache');
this.userCache = new Cache(100, 300, this.log, 'user');
}

var clientOpts = {url: opts.url};
Expand All @@ -70,7 +73,7 @@ LdapAuth.prototype.close = function (callback) {
if (! this._adminBound) {
callback()
} else {
this._adminClient.unbind(function(err) {
this._adminClient.unbind(function (err) {
callback(err);
});
}
Expand All @@ -85,9 +88,10 @@ LdapAuth.prototype._adminBind = function (callback) {
return callback();
}
var self = this;
this._adminClient.bind(this.opts.adminDn, this.opts.adminPassword, function (err) {
this._adminClient.bind(this.opts.adminDn, this.opts.adminPassword,
function (err) {
if (err) {
self.log && self.log.trace("ldap authenticate: bind error: %s", err);
self.log && self.log.trace('ldap authenticate: bind error: %s', err);
return callback(err);
}
return callback();
Expand All @@ -106,27 +110,30 @@ LdapAuth.prototype._adminBind = function (callback) {
LdapAuth.prototype._findUser = function (username, callback) {
var self = this;
self._adminBind(function (err) {
if (err) return callback(err);

var searchFilter = self.opts.searchFilter.replace("{{username}}", username);
var opts = {filter: searchFilter, scope: "sub"};
self._adminClient.search(self.opts.searchBase, opts, function (err, result) {
if (err)
return callback(err);

var searchFilter = self.opts.searchFilter.replace('{{username}}', username);
var opts = {filter: searchFilter, scope: 'sub'};
self._adminClient.search(self.opts.searchBase, opts,
function (err, result) {
if (err) {
self.log && self.log.trace("ldap authenticate: search error: %s", err);
self.log && self.log.trace('ldap authenticate: search error: %s', err);
return callback(err);
}
var items = [];
result.on('searchEntry', function(entry) {
result.on('searchEntry', function (entry) {
items.push(entry.object);
});
result.on('error', function(err) {
self.log && self.log.trace("ldap authenticate: search error event: %s", err);
result.on('error', function (err) {
self.log && self.log.trace(
'ldap authenticate: search error event: %s', err);
return callback(err);
});
result.on('end', function(result) {
result.on('end', function (result) {
if (result.status !== 0) {
var err = "non-zero status from LDAP search: " + result.status;
self.log && self.log.trace("ldap authenticate: %s", err);
var err = 'non-zero status from LDAP search: ' + result.status;
self.log && self.log.trace('ldap authenticate: %s', err);
return callback(err);
}
switch (items.length) {
Expand All @@ -135,8 +142,9 @@ LdapAuth.prototype._findUser = function (username, callback) {
case 1:
return callback(null, items[0])
default:
return callback("unexpected number of matches (" + items.length
+ ") for '" + username + "' username");
return callback(format(
'unexpected number of matches (%s) for "%s" username',
items.length, username));
}
});
});
Expand All @@ -149,27 +157,29 @@ LdapAuth.prototype._findUser = function (username, callback) {
*/
LdapAuth.prototype.authenticate = function (username, password, callback) {
var self = this;

if (self.opts.cache) {
// Check cache. "cached" is `{password: <hashed-password>, user: <user>}`.
// Check cache. 'cached' is `{password: <hashed-password>, user: <user>}`.
var cached = self.userCache.get(username);
if (cached && bcrypt.compareSync(password, cached.password)) {
return callback(null, cached.user)
}
}

// 1. Find the user DN in question.
self._findUser(username, function (err, user) {
if (err) return callback(err);
if (!user) return callback("no such user: '" + username + "'");
if (err)
return callback(err);
if (!user)
return callback(format('no such user: "%s"', username));
// 2. Attempt to bind as that user to check password.
self._userClient.bind(user.dn, password, function (err) {
if (err) {
self.log && self.log.trace("ldap authenticate: bind error: %s", err);
self.log && self.log.trace('ldap authenticate: bind error: %s', err);
return callback(err);
}
if (self.opts.cache) {
bcrypt.hash(password, self._salt, function(err, hash) {
bcrypt.hash(password, self._salt, function (err, hash) {
self.userCache.set(username, {password: hash, user: user});
return callback(null, user);
});
Expand Down

0 comments on commit af5beb9

Please sign in to comment.