-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from FreeAllMedia/feature/jwt
jwt implementation
- Loading branch information
Showing
9 changed files
with
213 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
module.exports = require("./es5/lib/hacher.js"); | ||
module.exports.jsonWebToken = require("./es5/lib/jsonWebToken.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters