Skip to content

Commit

Permalink
Merge branch 'release/v0.1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Aug 19, 2015
2 parents af998aa + 5e3e1eb commit 4cc666a
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 37 deletions.
23 changes: 0 additions & 23 deletions .sauce.json
Expand Up @@ -19,29 +19,6 @@
"base": "SauceLabs",
"browserName": "chrome",
"version": "31"
},

"sl_firefox_37": {
"base": "SauceLabs",
"browserName": "firefox",
"version": "37"
},
"sl_firefox_38": {
"base": "SauceLabs",
"browserName": "firefox",
"version": "38"
},
"sl_win_7_ie_11": {
"base": "SauceLabs",
"browserName": "internet explorer",
"version": "11",
"platform": "Windows 7"
},
"sl_win_81_ie_11": {
"base": "SauceLabs",
"browserName": "internet explorer",
"version": "11",
"platform": "Windows 8.1"
}
}
}
43 changes: 43 additions & 0 deletions es5/lib/jsonWebToken.js
@@ -0,0 +1,43 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var _jsonwebtoken = require("jsonwebtoken");

var _jsonwebtoken2 = _interopRequireDefault(_jsonwebtoken);

var JsonWebToken = (function () {
function JsonWebToken() {
_classCallCheck(this, JsonWebToken);
}

_createClass(JsonWebToken, null, [{
key: "sign",
value: function sign() {
return _jsonwebtoken2["default"].sign.apply(_jsonwebtoken2["default"], arguments);
}
}, {
key: "verify",
value: function verify() {
return _jsonwebtoken2["default"].verify.apply(_jsonwebtoken2["default"], arguments);
}
}, {
key: "decode",
value: function decode() {
return _jsonwebtoken2["default"].decode.apply(_jsonwebtoken2["default"], arguments);
}
}]);

return JsonWebToken;
})();

exports["default"] = JsonWebToken;
module.exports = exports["default"];
24 changes: 12 additions & 12 deletions es5/spec/hacher.spec.js
Expand Up @@ -2,60 +2,60 @@

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var _libHacherJs = require("../lib/hacher.js");
var _ = require("../../");

//tentative module to use: https://www.npmjs.com/package/uuid-1345

var _libHacherJs2 = _interopRequireDefault(_libHacherJs);
var _2 = _interopRequireDefault(_);

describe("crypto", function () {
describe("(static methods)", function () {
describe(".getUUID()", function () {
it("should get a UUID hash according to the RFC 4122 v4 (random) by default with no args", function () {
_libHacherJs2["default"].getUUID().length.should.be.greaterThan(0);
_2["default"].getUUID().length.should.be.greaterThan(0);
});
});

describe(".hash(string)", function () {
it("should hash a string with a proper secure hash algorithm", function () {
_libHacherJs2["default"].hash("somestring").length.should.be.greaterThan(0);
_2["default"].hash("somestring").length.should.be.greaterThan(0);
});
});

describe("(Hashids)", function () {
describe(".encodeHashId", function () {
it("should encode with numbers and a salt", function () {
_libHacherJs2["default"].encodeHashId(1, 2, 3, "salt").should.be.a.String;
_2["default"].encodeHashId(1, 2, 3, "salt").should.be.a.String;
});
});

describe(".decodeHashId", function () {
it("should decode with numbers and a salt", function () {
_libHacherJs2["default"].decodeHashId("hash", "salt").should.be.instanceOf(Array);
_2["default"].decodeHashId("hash", "salt").should.be.instanceOf(Array);
});
});

describe("(functionality)", function () {
it("should encode and decode correctly", function () {
var originalValue = 129;
var salt = "salt";
var encodedValues = _libHacherJs2["default"].encodeHashId(originalValue, salt);
var reDecodedValue = _libHacherJs2["default"].decodeHashId(encodedValues, salt);
var encodedValues = _2["default"].encodeHashId(originalValue, salt);
var reDecodedValue = _2["default"].decodeHashId(encodedValues, salt);
reDecodedValue.should.eql([originalValue]);
});

it("should encode and decode correctly many numbers as well", function () {
var originalValues = [129, 123];
var salt = "salt";
var encodedValues = _libHacherJs2["default"].encodeHashId.apply(_libHacherJs2["default"], originalValues.concat([salt]));
var reDecodedValue = _libHacherJs2["default"].decodeHashId(encodedValues, salt);
var encodedValues = _2["default"].encodeHashId.apply(_2["default"], originalValues.concat([salt]));
var reDecodedValue = _2["default"].decodeHashId(encodedValues, salt);
reDecodedValue.should.eql(originalValues);
});

it("should encode and decode correctly many numbers with no salt specified", function () {
var originalValues = [129, 123];
var encodedValues = _libHacherJs2["default"].encodeHashId.apply(_libHacherJs2["default"], originalValues);
var reDecodedValue = _libHacherJs2["default"].decodeHashId(encodedValues);
var encodedValues = _2["default"].encodeHashId.apply(_2["default"], originalValues);
var reDecodedValue = _2["default"].decodeHashId(encodedValues);
reDecodedValue.should.eql(originalValues);
});
});
Expand Down
74 changes: 74 additions & 0 deletions es5/spec/jsonWebToken.spec.js
@@ -0,0 +1,74 @@
"use strict";

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var _ = require("../../");

var _2 = _interopRequireDefault(_);

var _sinon = require("sinon");

var _sinon2 = _interopRequireDefault(_sinon);

describe("Json Web Token", function () {
var payload = undefined,
secret = undefined,
options = undefined,
token = undefined,
clock = undefined;

beforeEach(function () {
clock = _sinon2["default"].useFakeTimers();
payload = {
name: "Bender",
type: "robot"
};

secret = "benderIsEvil";
options = { expiresInSeconds: 60 };
token = _2["default"].jsonWebToken.sign(payload, secret, options);
});

afterEach(function () {
clock.restore();
});

describe(".sign(payload, secret, options)", function () {
it("should sign a json payload and return a json web token", function () {
token.length.should.be.greaterThan(0);
});
});

describe(".verify(token, secret[, options, callback])", function () {
it("should verify a json payload and return a json web token", function () {
(function () {
_2["default"].jsonWebToken.verify(token, secret, options);
}).should.not["throw"]();
});

it("should throw if the token is expired", function () {
clock.tick(60001);
(function () {
_2["default"].jsonWebToken.verify(token, secret, options);
}).should["throw"]("TokenExpiredError: jwt expired");
});

it("should throw if an invalid secret is supplied", function () {
(function () {
_2["default"].jsonWebToken.verify(token, "benderIsNice", options);
}).should["throw"]("JsonWebTokenError: invalid signature");
});

it("should throw if an invalid token is supplied", function () {
(function () {
_2["default"].jsonWebToken.verify("invalidToken", secret, options);
}).should["throw"]("JsonWebTokenError: jwt malformed");
});
});

describe(".decode(token, options)", function () {
it("should decode a json payload and return a json web token", function () {
_2["default"].jsonWebToken.decode(token).should.eql(payload);
});
});
});
15 changes: 15 additions & 0 deletions es6/lib/jsonWebToken.js
@@ -0,0 +1,15 @@
import jwt from "jsonwebtoken";

export default class JsonWebToken {
static sign(...options) {
return jwt.sign(...options);
}

static verify(...options) {
return jwt.verify(...options);
}

static decode(...options) {
return jwt.decode(...options);
}
}
2 changes: 1 addition & 1 deletion es6/spec/hacher.spec.js
@@ -1,4 +1,4 @@
import crypto from "../lib/hacher.js";
import crypto from "../../";

//tentative module to use: https://www.npmjs.com/package/uuid-1345
describe("crypto", () => {
Expand Down
65 changes: 65 additions & 0 deletions es6/spec/jsonWebToken.spec.js
@@ -0,0 +1,65 @@
import crypto from "../../";
import sinon from "sinon";

describe("Json Web Token", () => {
let payload,
secret,
options,
token,
clock;

beforeEach(() => {
clock = sinon.useFakeTimers();
payload = {
name: "Bender",
type: "robot"
};

secret = "benderIsEvil";
options = {expiresInSeconds: 60};
token = crypto.jsonWebToken.sign(payload, secret, options);
});

afterEach(() => {
clock.restore();
});

describe(".sign(payload, secret, options)", () => {
it("should sign a json payload and return a json web token", () => {
token.length.should.be.greaterThan(0);
});
});

describe(".verify(token, secret[, options, callback])", () => {
it("should verify a json payload and return a json web token", () => {
() => {
crypto.jsonWebToken.verify(token, secret, options);
}.should.not.throw();
});

it("should throw if the token is expired", () => {
clock.tick(60001);
() => {
crypto.jsonWebToken.verify(token, secret, options);
}.should.throw("TokenExpiredError: jwt expired");
});

it("should throw if an invalid secret is supplied", () => {
() => {
crypto.jsonWebToken.verify(token, "benderIsNice", options);
}.should.throw("JsonWebTokenError: invalid signature");
});

it("should throw if an invalid token is supplied", () => {
() => {
crypto.jsonWebToken.verify("invalidToken", secret, options);
}.should.throw("JsonWebTokenError: jwt malformed");
});
});

describe(".decode(token, options)", () => {
it("should decode a json payload and return a json web token", () => {
crypto.jsonWebToken.decode(token).should.eql(payload);
});
});
});
1 change: 1 addition & 0 deletions index.js
@@ -1 +1,2 @@
module.exports = require("./es5/lib/hacher.js");
module.exports.jsonWebToken = require("./es5/lib/jsonWebToken.js");
4 changes: 3 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "hacher",
"version": "0.1.3",
"version": "0.1.4",
"description": "Cryptographic component",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"crypto": "0.0.3",
"hashids": "1.0.2",
"jsonwebtoken": "^5.0.4",
"uuid-1345": "^0.99.6"
},
"devDependencies": {
Expand All @@ -61,6 +62,7 @@
"karma-safari-launcher": "^0.1.1",
"karma-sauce-launcher": "^0.2.11",
"mocha": "^2.2.5",
"sinon": "^1.15.4",
"superstack": "0.0.4"
},
"generatorVersion": "0.2.20"
Expand Down

0 comments on commit 4cc666a

Please sign in to comment.