Skip to content

Commit

Permalink
Merge pull request #58 from jwtk/buffer-fix
Browse files Browse the repository at this point in the history
Buffer fix
  • Loading branch information
swiftone committed Feb 22, 2019
2 parents 3bc7df1 + 610e825 commit d6f8119
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ function nowEpochSeconds(){
return Math.floor(new Date().getTime()/1000);
}

function base64urlEncode(str) {
return new Buffer(str)
function base64urlEncode(data) {
const str = typeof data === 'number' ? data.toString() : data;
return Buffer.from(str)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
Expand Down Expand Up @@ -277,7 +278,7 @@ Parser.prototype.isSupportedAlg = isSupportedAlg;
Parser.prototype.safeJsonParse = function(input) {
var result;
try{
result = JSON.parse(new Buffer(base64urlUnescape(input),'base64'));
result = JSON.parse(Buffer.from(base64urlUnescape(input),'base64'));
}catch(e){
return e;
}
Expand All @@ -297,7 +298,7 @@ Parser.prototype.parse = function parse(jwtString,cb){
var body = this.safeJsonParse(segments[1]);

if(segments[2]){
signature = new Buffer(base64urlUnescape(segments[2]),'base64')
signature = Buffer.from(base64urlUnescape(segments[2]),'base64')
.toString('base64');
}

Expand Down
9 changes: 8 additions & 1 deletion test/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ describe('base64 URL Encoding',function(){
assert.equal(
nJwt.Jwt.prototype.sign(
[compactHeader,compactBody].join('.'),
'HS256',new Buffer(key,'base64')
'HS256',Buffer.from(key,'base64')
),
expectedSignature
);

});

it('does not create an uninitialized Buffer', function() {
// see https://nodejs.org/api/buffer.html#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe
var fromDigits = nJwt.base64urlEncode(10);
var fromString = nJwt.base64urlEncode('10');
assert.equal(fromDigits, fromString);
});
});


Expand Down

0 comments on commit d6f8119

Please sign in to comment.