passwordless-cache-manager
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -0,3 +1,8 @@ | ||
# 1.0.1 (2016-07-14) | ||
* Fix bugs relating to ttl comparisons for validity | ||
* add in `debug` under `passwordless-cache-manager` | ||
# 1.0.0 (2016-07-13) | ||
* First version |
61
index.js
var bcrypt = require('bcryptjs') | ||
var TokenStore = require('passwordless-tokenstore') | ||
var debug = require('debug')('passwordless-cache-manager') | ||
@@ -20,7 +21,11 @@ /** | ||
if (!token || !uid || !callback) { | ||
debug('[auth] missing params uid: %s, token: %s', uid, token) | ||
throw new Error('TokenStore:authenticate called with invalid parameters') | ||
} | ||
debug('[auth] fetching user: %s, token: %s', uid, token) | ||
this._cache.get(uid, function (err, item) { | ||
if (err) { | ||
debug('[auth.get] storage error - user: %s, token: %s', uid, token) | ||
return callback(err, false, null) | ||
@@ -30,2 +35,3 @@ } | ||
if (!item) { | ||
debug('[auth.get] could not get data - user: %s, token: %s', uid, token) | ||
return callback(null, false, null) | ||
@@ -39,17 +45,31 @@ } | ||
CacheManagerStore.prototype._validateToken = function (token, storedItem, callback) { | ||
if (storedItem && storedItem.ttl > new Date()) { | ||
bcrypt.compare(token, storedItem.hashedToken, function (err, res) { | ||
if (err) { | ||
return callback(err, false, null) | ||
} | ||
if (storedItem && storedItem.ttl) { | ||
const storedDate = storedItem.ttl | ||
const currDate = new Date() | ||
if (res) { | ||
return callback(null, true, storedItem.originUrl || '') | ||
} | ||
if (storedDate >= currDate.getTime()) { | ||
debug('[validateToken] comparing against hashed token: %s, hashed: %s', token, storedItem.hashedToken) | ||
callback(null, false, null) | ||
}) | ||
} else { | ||
callback(null, false, null) | ||
bcrypt.compare(token, storedItem.hashedToken, function (err, res) { | ||
if (err) { | ||
debug('[validateToken] bcrypt error token: %s', token) | ||
return callback(err, false, null) | ||
} | ||
if (res) { | ||
debug('[validateToken] validation success token: %s', token) | ||
return callback(null, true, storedItem.originUrl || '') | ||
} | ||
debug('[validateToken] bcrypt compare failure token: %s', token) | ||
return callback(null, false, null) | ||
}) | ||
return | ||
} | ||
debug('[validateToken] expired token: %s, expDate: %s, now: %s, result: %s', token, storedDate, currDate, (storedDate > currDate)) | ||
} | ||
callback(null, false, null) | ||
} | ||
@@ -59,7 +79,10 @@ | ||
if (!token || !uid || !msToLive || !callback) { | ||
debug('[storeOrUpdate] missing params uid: %s, token: %s, msToLive: %s', uid, token, msToLive) | ||
throw new Error('TokenStore:storeOrUpdate called with invalid parameters') | ||
} | ||
debug('[storeOrUpdate] hashing uid: %s, token: %s', uid, token) | ||
bcrypt.hash(token, 10, function (err, hashedToken) { | ||
if (err) { | ||
debug('[storeOrUpdate] bcrypt error uid: %s, token: %s', uid, token) | ||
return callback(err) | ||
@@ -71,13 +94,16 @@ } | ||
uid: uid, | ||
ttl: new Date(Date.now() + msToLive), | ||
ttl: (new Date(Date.now() + msToLive)).getTime(), | ||
originUrl: originUrl | ||
} | ||
var seconds = Math.floor(msToLive / 1000) | ||
var seconds = Math.ceil(msToLive / 1000) | ||
debug('[storeOrUpdate] setting hash uid: %s, token: %s', uid, token) | ||
this._cache.set(uid, newRecord, { ttl: seconds }, function (err) { | ||
if (err) { | ||
debug('[storeOrUpdate] storage error uid: %s, token: %s', uid, token) | ||
return callback(err, false, null) | ||
} | ||
debug('[storeOrUpdate] token stored uid: %s, token: %s', uid, token) | ||
callback() | ||
@@ -90,10 +116,14 @@ }) | ||
if (!uid || !callback) { | ||
debug('[invalidateUser] missing param uid') | ||
throw new Error('TokenStore:invalidateUser called with invalid parameters') | ||
} | ||
debug('[invalidateUser] deleting token uid: %s', uid) | ||
this._cache.del(uid, function (err) { | ||
if (err) { | ||
debug('[invalidateUser] storage error uid: %s', uid) | ||
return callback(err, false, null) | ||
} | ||
debug('[invalidateUser] token invalidated uid: %s', uid) | ||
callback() | ||
@@ -108,7 +138,10 @@ }) | ||
debug('[clear] resetting storage') | ||
this._cache.reset(function (err) { | ||
if (err) { | ||
debug('[clear] storage error in resetting') | ||
return callback(err, false, null) | ||
} | ||
debug('[clear] storage has been cleared') | ||
callback() | ||
@@ -115,0 +148,0 @@ }) |
{ | ||
"name": "passwordless-cache-manager", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Use node-cache-manager as a passwordless token store", | ||
@@ -18,3 +18,4 @@ "main": "index.js", | ||
"nopassword", | ||
"passwordless" | ||
"passwordless", | ||
"cache-manager" | ||
], | ||
@@ -29,2 +30,3 @@ "author": "Theo Gravity <theo@suteki.nu>", | ||
"bcryptjs": "~2.3.0", | ||
"debug": "2.2.0", | ||
"passwordless-tokenstore": "0.0.10" | ||
@@ -31,0 +33,0 @@ }, |
# passwordless-cache-manager | ||
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](http://standardjs.com) [![Build Status](https://travis-ci.org/theogravity/passwordless-cache-manager.svg?branch=master)](https://travis-ci.org/theogravity/passwordless-cache-manager) [![npm version](https://badge.fury.io/js/passwordless-cache-manager.svg)](https://badge.fury.io/js/passwordless-cache-manager) | ||
A token store for [Passwordless](https://github.com/florianheinemann/passwordless), a node.js module for express that allows website authentication without password using verification through email or other means. | ||
@@ -62,4 +64,10 @@ | ||
## Debugging | ||
The `debug` module is used to log debug statements. It can be enabled via the environment variable: | ||
`DEBUG=passwordless-cache-manager` | ||
## License | ||
[MIT License](http://opensource.org/licenses/MIT) |
@@ -27,3 +27,3 @@ /*global describe, beforeEach, afterEach, it */ | ||
standardTests(TokenStoreFactory, beforeEachTest, afterEachTest) | ||
standardTests(TokenStoreFactory, beforeEachTest, afterEachTest, 450) | ||
@@ -30,0 +30,0 @@ describe('Specific tests', function () { |
Sorry, the diff of this file is not supported yet
192
73
14358
3
9
+ Addeddebug@2.2.0
+ Addeddebug@2.2.0(transitive)
+ Addedms@0.7.1(transitive)