Skip to content
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

Buffer fix #58

Merged
merged 4 commits into from
Feb 22, 2019
Merged
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
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