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

chore: update dependencies #15

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "google",
"env": {
"node": true
},
Expand All @@ -7,6 +8,20 @@
"quotes": [2, "single"],
"no-underscore-dangle": 0,
"new-cap": [1, {"capIsNew": false}],
"no-mixed-requires": [1, true]
"no-mixed-requires": [1, true],
"require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": false,
"MethodDefinition": false,
"ClassDeclaration": false,
"ArrowFunctionExpression": false,
"FunctionExpression": false
}
}],
"valid-jsdoc": 0,
"no-var": 0,
"one-var": 0,
"max-len": ["error", { "code": 120 }],
"comma-dangle": ["error", "never"]
}
}
10 changes: 0 additions & 10 deletions .jscsrc

This file was deleted.

5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ jwt-utils
JSON Web Tokens (JWT) utils.

[![npm version](https://badge.fury.io/js/jwt-utils.svg)](http://badge.fury.io/js/jwt-utils)
[![Build Status](https://travis-ci.org/telefonica/node-jwt-utils.svg)](https://travis-ci.org/telefonica/node-jwt-utils)
[![Coverage Status](https://img.shields.io/coveralls/telefonica/node-jwt-utils.svg)](https://coveralls.io/r/telefonica/node-jwt-utils)

This module is able to parse and generate both encrypted and unencrypted JWT tokens.

Expand Down
63 changes: 30 additions & 33 deletions lib/jwt-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

var crypto = require('crypto');
var debugLib = require('debug'),
_ = require('underscore'),
jwa = require('jwa');
_ = require('underscore'),
jwa = require('jwa');
var errors = require('./errors');

var debug = debugLib('tef:base:jwtUtils');
Expand All @@ -41,12 +41,12 @@ var authenticationTagBits = {
'sha512': 32
};

/*jshint -W069 */
/*jshint -W072*/
/*jshint -W098*/
/*jshint -W117*/
/* jshint -W069 */
/* jshint -W072*/
/* jshint -W098*/
/* jshint -W117*/
var DEFAULT_CONFIG = {
//Number of seconds to consider a jwt expired
// Number of seconds to consider a jwt expired
expiration: 600,
futureTolerance: 4
};
Expand All @@ -57,7 +57,6 @@ var DEFAULT_CONFIG = {
* @return {Object}
*/
module.exports = function(configuration) {

var config = {};

function base64urlEscape(str) {
Expand All @@ -70,19 +69,19 @@ module.exports = function(configuration) {
}

function base64urlEncode(str) {
return base64urlEscape(new Buffer(str).toString('base64'));
return base64urlEscape(Buffer.from(str).toString('base64'));
}

function base64urlDecode(str) {
return new Buffer(base64urlUnescape(str), 'base64');
return Buffer.from(base64urlUnescape(str), 'base64');
}

function encodeBase64url(base64) {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
}

function convertSlowBufToBuf(slowBuffer) {
var buffer = new Buffer(slowBuffer.length);
var buffer = Buffer.alloc(slowBuffer.length);
slowBuffer.copy(buffer);
return buffer;
}
Expand Down Expand Up @@ -130,9 +129,9 @@ module.exports = function(configuration) {
segments.push(base64urlEncode(JSON.stringify(header)));
segments.push(base64urlEncode(JSON.stringify(payload)));
var signer = jwa(header.alg);
//If we use RS256, the key is the private Key
// If we use RS256, the key is the private Key
if (header.alg.indexOf('HS') === 0) {
key = new Buffer(key, 'hex');
key = Buffer.from(key, 'hex');
}
segments.push(signer.sign(segments.join('.'), key));

Expand All @@ -155,7 +154,7 @@ module.exports = function(configuration) {

var signer = jwa(algorithm);
if (algorithm.indexOf('HS') === 0) {
key = new Buffer(key, 'hex');
key = Buffer.from(key, 'hex');
}
if (!signer.verify(segments[0] + '.' + segments[1], segments[2], key)) {
throw errors.WRONG_TOKEN_SIGNATURE();
Expand Down Expand Up @@ -200,7 +199,7 @@ module.exports = function(configuration) {
origAuthenticationTag = segments[4];
}

var encKeyBuffer = new Buffer(encKey, 'hex');
var encKeyBuffer = Buffer.from(encKey, 'hex');
var result, hashBuf;
try {
var decipher = crypto.createDecipheriv(algorithms.cipherAlgorithm, encKeyBuffer, initializationVector);
Expand All @@ -211,21 +210,21 @@ module.exports = function(configuration) {
var endBody = decipher.final();
result = Buffer.concat([convertSlowBufToBuf(mainBody), convertSlowBufToBuf(endBody)]).toString();

var b64Header = new Buffer(segments[0]);
var iv = new Buffer(initializationVector);
var encryptedBody = new Buffer(cypherText);
var b64Header = Buffer.from(segments[0]);
var iv = Buffer.from(initializationVector);
var encryptedBody = Buffer.from(cypherText);

// create al vector
var al = new Buffer(4);
var al = Buffer.alloc(4);
al.writeInt32BE(b64Header.length * 8, 0);

var buf4Clear = new Buffer(4);
var buf4Clear = Buffer.alloc(4);
buf4Clear.fill(0);
var alResult = Buffer.concat([buf4Clear, al]);

var authTag = new Buffer(Buffer.concat([b64Header, iv, encryptedBody, alResult]));
var authTag = Buffer.from(Buffer.concat([b64Header, iv, encryptedBody, alResult]));

var hashKeyBuffer = new Buffer(hashKey, 'hex');
var hashKeyBuffer = Buffer.from(hashKey, 'hex');
var authTagHash = crypto.createHmac(algorithms.hashAlgorithm, hashKeyBuffer).update(authTag).digest();

var authTagHashBuf = convertSlowBufToBuf(authTagHash);
Expand All @@ -251,11 +250,10 @@ module.exports = function(configuration) {
iv.push(Math.round(Math.random() * 255));
}

return new Buffer(iv);
return Buffer.from(iv);
}

function encryptJWT(payload, header, encKey, hashKey) {

header = _.defaults(header, {
alg: 'dir',
enc: 'A256CBC-HS512'
Expand All @@ -271,7 +269,7 @@ module.exports = function(configuration) {

var segments = [];

var b64Header = encodeBase64url(new Buffer(JSON.stringify(header)).toString('base64'));
var b64Header = encodeBase64url(Buffer.from(JSON.stringify(header)).toString('base64'));
segments.push(b64Header);

var b64Jek = '';
Expand All @@ -280,10 +278,10 @@ module.exports = function(configuration) {
var b64IV = encodeBase64url(iv.toString('base64'));
segments.push(b64IV);

var encKeyBuffer = new Buffer(encKey, 'hex');
var encKeyBuffer = Buffer.from(encKey, 'hex');
var cipher = crypto.createCipheriv(algorithms.cipherAlgorithm, encKeyBuffer, iv);

var cipherTextBegin = cipher.update(new Buffer(JSON.stringify(payload), 'utf8'));
var cipherTextBegin = cipher.update(Buffer.from(JSON.stringify(payload), 'utf8'));

var cipherTextEnd = cipher.final();

Expand All @@ -294,21 +292,21 @@ module.exports = function(configuration) {

// Calculate AuthTag

var b64HeaderBuf = new Buffer(b64Header);
var b64HeaderBuf = Buffer.from(b64Header);

// We have iv Buffer and cipherTextBuf Buffer calculated.

var al = new Buffer(4);
var al = Buffer.alloc(4);
al.writeInt32BE(b64HeaderBuf.length * 8, 0);

var buf4Clear = new Buffer(4);
var buf4Clear = Buffer.alloc(4);
buf4Clear.fill(0);

var alResult = Buffer.concat([buf4Clear, al]);

var authTag = new Buffer(Buffer.concat([b64HeaderBuf, iv, cipherTextBuf, alResult]));
var authTag = Buffer.from(Buffer.concat([b64HeaderBuf, iv, cipherTextBuf, alResult]));

var hashKeyBuffer = new Buffer(hashKey, 'hex');
var hashKeyBuffer = Buffer.from(hashKey, 'hex');
var base64str = crypto.createHmac(algorithms.hashAlgorithm, hashKeyBuffer).update(authTag).digest();

var buf = convertSlowBufToBuf(base64str);
Expand Down Expand Up @@ -405,7 +403,6 @@ module.exports = function(configuration) {
} else {
return cb(errors.ENCRYPTION_ERROR(err.message));
}

}
cb(null, result);
},
Expand Down
Loading