Skip to content

feat(opts): add opts param to verify, invalidate & cache functions #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ var d = exports.defaults = {

var items = {};

function cache(key) {
if(!items[key] || !d.cache) {
function cache(key, opts) {
if(!items[key] || !(opts && opts.cache || d.cache)) {
if(Object.keys(items).length > 500) {
items = {};
}
items[key] = crypto.createHmac('sha512', d.secret).update(key).digest('base64');
items[key] = crypto.createHmac('sha512', opts && opts.secret || d.secret).update(key).digest('base64');
}
return items[key];
}
Expand All @@ -20,16 +20,17 @@ exports.INVALID = 0;
exports.VALID = 1;
exports.EXPIRING = 2;

exports.verify = function(data, hash) {
exports.verify = function(data, hash, opts) {
if(typeof data !== 'string' || typeof hash !== 'string' ) {
return false;
}
var epoch = Math.floor(new Date().getTime() / 1000 / d.timeStep); // e.g. http://tools.ietf.org/html/rfc6238

var epoch = Math.floor(new Date().getTime() / 1000 / (opts && opts.timeStep || d.timeStep)); // e.g. http://tools.ietf.org/html/rfc6238
// allow data to be empty, always take into account the time
if (hash === cache(data + epoch) || hash === cache(data + (epoch + 1))) {
if (hash === cache(data + epoch, opts) || hash === cache(data + (epoch + 1), opts)) {
return exports.VALID; // truthy, valid and current
}
if (hash === cache(data + (epoch - 1))) {
if (hash === cache(data + (epoch - 1), opts)) {
return exports.EXPIRING; // truthy, expired but still valid
}
return exports.INVALID;
Expand All @@ -46,9 +47,9 @@ exports.generate = function(data, opts) {
return crypto.createHmac('sha512', secret).update(data + epoch).digest('base64');
};

exports.invalidate = function(data, hash) {
exports.invalidate = function(data, hash, opts) {
var isValidHash = exports.verify(data, hash),
epoch = Math.floor(new Date().getTime() / 1000 / d.timeStep);
epoch = Math.floor(new Date().getTime() / 1000 / (opts && opts.timeStep || d.timeStep));

if (!isValidHash) {
throw 'invalid hash';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "token",
"version": "0.1.0",
"version": "0.2.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the version 0.1.1 is better to this case, It's a small change that do not break in nothing the code interface, it's in case of follow the semantic version pattern.

"description": "HMAC token generation and verification with time-based limitation on validity",
"author": "Mikito Takada <[email protected]>",
"main": "index.js",
Expand Down