Skip to content

Commit

Permalink
fix some audit issues with dev dependencies
Browse files Browse the repository at this point in the history
fix version

locking to the last versions of Node
  • Loading branch information
joaquimserafim committed Mar 29, 2019
1 parent 412c2c8 commit 277ae0a
Show file tree
Hide file tree
Showing 11 changed files with 5,735 additions and 348 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_token: aIOvFO7phTMcwHDyACgpQuHHlxu91Rxy7
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
coverage
reports
*.html
.nyc_output
8 changes: 0 additions & 8 deletions .jscsrc

This file was deleted.

15 changes: 0 additions & 15 deletions .jshintrc

This file was deleted.

6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
node_js:
- 4
- 5
- 6
- 8
- 10
- 11
branches:
only:
- master
Expand Down
3 changes: 1 addition & 2 deletions bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ new Benchmark.Suite()
.on('complete', () => {
console.log('benchmark finish')
})
.run({'async': true})
.run({ 'async': true })

//
//
Expand All @@ -52,4 +52,3 @@ new Benchmark.Suite()
function uuid () {
return (~~(Math.random() * 1e9)).toString(36)
}

88 changes: 44 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict'

const crypto = require('crypto')
const b64url = require('base64-url')
const inherits = require('util').inherits
const parse = require('json-parse-safe')
const extend = require('xtend')
const isObject = require('is.object')
const crypto = require('crypto')
const b64url = require('base64-url')
const inherits = require('util').inherits
const parse = require('json-parse-safe')
const extend = require('xtend')
const isObject = require('is.object')

//
// supported algorithms
//

const algorithms = {
HS256: {hash: 'sha256', type: 'hmac'},
HS384: {hash: 'sha384', type: 'hmac'},
HS512: {hash: 'sha512', type: 'hmac'},
RS256: {hash: 'RSA-SHA256', type: 'sign'}
HS256: { hash: 'sha256', type: 'hmac' },
HS384: { hash: 'sha384', type: 'hmac' },
HS512: { hash: 'sha512', type: 'hmac' },
RS256: { hash: 'RSA-SHA256', type: 'sign' }
}

//
Expand All @@ -24,30 +24,30 @@ const algorithms = {

const jwt = module.exports

jwt.JWTError = JWTError
jwt.JWTError = JWTError
jwt.getAlgorithms = getAlgorithms
jwt.encode = encode
jwt.decode = decode
jwt.encode = encode
jwt.decode = decode

function getAlgorithms () {
return Object.keys(algorithms)
}

function encode (key, data, algorithm, cb) {
if (paramIsValid(algorithm, 'function')) {
if (isFunction(algorithm, 'function')) {
cb = algorithm
algorithm = 'HS256'
}

var defaultHeader = {typ: 'JWT', alg: algorithm}
var defaultHeader = { typ: 'JWT', alg: algorithm }

var payload = isObject(data) && data.payload ?
data.payload :
data
var payload = isObject(data) && data.payload
? data.payload
: data

var header = isObject(data) && data.header ?
extend(data.header, defaultHeader) :
defaultHeader
var header = isObject(data) && data.header
? extend(data.header, defaultHeader)
: defaultHeader

const validationError = encodeValidations(key, payload, algorithm)

Expand Down Expand Up @@ -101,15 +101,15 @@ function decode (key, token, cb) {
parts[2]
)

return prcResult(!res && 'Invalid key!' || null, payload, header, cb)
return prcResult((!res && 'Invalid key!') || null, payload, header, cb)
}

function encodeValidations (key, payload, algorithm) {
return paramsAreFalsy(key, payload) ?
'The key and payload are mandatory!' :
!Object.keys(payload).length ?
'The payload is an empty object!' :
!algorithms[algorithm] && 'The algorithm is not supported!'
return paramsAreFalsy(key, payload)
? 'The key and payload are mandatory!'
: !Object.keys(payload).length
? 'The payload is an empty object!'
: !algorithms[algorithm] && 'The algorithm is not supported!'
}

//
Expand All @@ -130,41 +130,41 @@ inherits(JWTError, Error)
//

function sign (alg, key, input) {
return 'hmac' === alg.type ?
b64url.escape(crypto.createHmac(alg.hash, key)
return alg.type === 'hmac'
? b64url.escape(crypto.createHmac(alg.hash, key)
.update(input)
.digest('base64')) :
b64url.escape(crypto.createSign(alg.hash)
.digest('base64'))
: b64url.escape(crypto.createSign(alg.hash)
.update(input)
.sign(key, 'base64'))
}

function verify (alg, key, input, signVar) {
return 'hmac' === alg.type ?
signVar === sign(alg, key, input) :
crypto.createVerify(alg.hash)
return alg.type === 'hmac'
? signVar === sign(alg, key, input)
: crypto.createVerify(alg.hash)
.update(input)
.verify(key, b64url.unescape(signVar), 'base64')
}

function prcResult (err, payload, header, cb) {
if (paramIsValid(header, 'function')) {
if (isFunction(header, 'function')) {
cb = header
header = undefined
}

err = err && new JWTError(err)

return cb ?
cb(err, payload, header) :
(header ?
{error: err, value: payload, header: header} :
{error: err, value: payload}
)
return cb
? cb(err, payload, header)
: (header
? { error: err, value: payload, header: header }
: { error: err, value: payload }
)
}

function paramIsValid (param, type) {
return !param || typeof param === type
function isFunction (param) {
return !param || typeof param === 'function'
}

function paramsAreFalsy (param1, param2) {
Expand All @@ -174,5 +174,5 @@ function paramsAreFalsy (param1, param2) {
function JSONParse (str) {
const res = parse(str)

return res.error && '' || res.value
return res.error ? '' : res.value
}
Loading

0 comments on commit 277ae0a

Please sign in to comment.