diff --git a/index.js b/index.js index 87246ea..21f802d 100644 --- a/index.js +++ b/index.js @@ -258,6 +258,9 @@ Jwt.prototype.isNotBefore = function() { }; function Parser(options){ + if(!(this instanceof Parser)){ + return new Parser(options); + } return this; } @@ -402,6 +405,7 @@ var jwtLib = { Jwt: Jwt, JwtBody: JwtBody, JwtHeader: JwtHeader, + Parser: Parser, Verifier: Verifier, base64urlEncode: base64urlEncode, base64urlUnescape:base64urlUnescape, diff --git a/test/parser.js b/test/parser.js new file mode 100644 index 0000000..b20b1c4 --- /dev/null +++ b/test/parser.js @@ -0,0 +1,29 @@ +var assert = require('chai').assert; +var uuid = require('uuid'); +var nJwt = require('../'); + + +describe('Parser', function () { + it('should construct itself if called without new', function () { + assert(nJwt.Parser() instanceof nJwt.Parser); + }); +}); +describe('Parser.parse()', function () { + var result = null + var claims = {hello: 'world'} + before(function(done){ + var token = new nJwt.Jwt(claims, false) + .setSigningAlgorithm('none') + .compact(); + var parser = nJwt.Parser(); + parser.parse(token, function(err,res){ + result = [err,res]; + done(); + }); + }); + it('should parse a valid token', function () { + assert.isNull(result[0], 'An error was not returned'); + assert.equal(result[1].body.hello,claims.hello); + }); + +}); \ No newline at end of file