Skip to content

Commit

Permalink
chore: update dependencies
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit a61cace46f4f745283177a6ca4551e6237aa4d8a
Author: Hugo Ferrando Seage <hugoseage@gmail.com>
Date:   Wed May 13 15:33:19 2020 +0200

    Downgrade eslint to version 6.8.0

    Version 7 dropped support for node 8

commit f08aa1e125738fe00183306f925d4e2ecf9e5704
Author: Hugo Ferrando Seage <hugoseage@gmail.com>
Date:   Wed May 13 14:52:18 2020 +0200

    FIX unit tests

    - Made environment.js work with the newer mocha version
    - Remove all Buffer constructors
    - Change test order to fix broken tests in node < 10.7
      - nodejs/node#21288
    - Changed minimum node version to 8, due to the updated dependencies

commit ce1f40d4e5ae79f4bcf6a3cca0f914ef51effd3b
Author: Hugo Ferrando Seage <hugoseage@gmail.com>
Date:   Wed May 13 14:52:04 2020 +0200

    Relint jwt-utils.js with newer eslint rules

commit 59940f5552dbfb8353e71250b1a0d3038913a17e
Author: Hugo Ferrando Seage <hugoseage@gmail.com>
Date:   Wed May 13 14:50:51 2020 +0200

    Replace new Buffer with Buffer.from and Buffer.alloc

    The Buffer constructor was deprecated: https://nodejs.org/en/docs/guides/buffer-constructor-deprecation/

commit b46245788a6235b0490f9902e9218945cb6d585c
Author: Hugo Ferrando Seage <hugoseage@gmail.com>
Date:   Wed May 13 14:48:56 2020 +0200

    Remove travis and coveralls badges

    Also removed the .travis file and npm script. Neither service is
    executing their pipelines for this repo anymore

commit 23f537ad6fdc4b8924add6feb16628512529a47a
Author: Hugo Ferrando Seage <hugoseage@gmail.com>
Date:   Wed May 13 14:48:42 2020 +0200

    Integrate jscsrc rules into eslintrc

commit c0e23b53a27d82d14430d05b63426dcae868cfd9
Author: Hugo Ferrando Seage <hugoseage@gmail.com>
Date:   Wed May 13 14:44:52 2020 +0200

    CHORE update dependencies

    - coveralls removed as the pipeline is not executed anymore
    - jscs removed as it was deprecated
    - added eslint-google dependency to replace jscs with eslint
    - jwa NOT updated as it would break compatibility
    - therror NOT updated
  • Loading branch information
hugo19941994 committed May 13, 2020
1 parent 58911e6 commit 89f62cb
Show file tree
Hide file tree
Showing 9 changed files with 3,077 additions and 155 deletions.
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

0 comments on commit 89f62cb

Please sign in to comment.