Skip to content

Commit

Permalink
update dev dependencies, add ts types
Browse files Browse the repository at this point in the history
update dependencies

fix package-lock.json issue
  • Loading branch information
joaquimserafim committed Sep 30, 2019
1 parent e5b67c7 commit 57a31e7
Show file tree
Hide file tree
Showing 6 changed files with 1,031 additions and 1,080 deletions.
22 changes: 11 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
language: node_js
node_js:
- 8
- 10
- 12
branches:
only:
- master
notifications:
language: node_js
node_js:
- "8"
- "10"
- "12"
branches:
only:
- master
notifications:
email:
- joaquim.serafim@gmail.com
script: npm test
- joaquim.serafim@gmail.com
script: npm test
2 changes: 1 addition & 1 deletion 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 Down
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// methods
//

export function encode(
key: string,
data: any,
algorithm: string,
cb: (err?: Error, token?: string) => any
): any;

export function decode(
key: string,
token: string,
cb: (err?: Error, token?: string) => any
): any;

export function getAlgorithms(): string[];

export function JWTError(message: string): Error;
51 changes: 24 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ function encode (key, data, algorithm, cb) {

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)

if (validationError) {
return prcResult(validationError, null, cb)
}

const parts = b64url.encode(JSON.stringify(header)) +
const parts =
b64url.encode(JSON.stringify(header)) +
'.' +
b64url.encode(JSON.stringify(payload))

Expand All @@ -75,11 +75,7 @@ function decode (key, token, cb) {

// check all parts're present
if (parts.length !== 3) {
return prcResult(
'The JWT should consist of three parts!',
null,
cb
)
return prcResult('The JWT should consist of three parts!', null, cb)
}

// base64 decode and parse JSON
Expand All @@ -94,12 +90,7 @@ function decode (key, token, cb) {
}

// verify the signature
const res = verify(
algorithm,
key,
parts.slice(0, 2).join('.'),
parts[2]
)
const res = verify(algorithm, key, parts.slice(0, 2).join('.'), parts[2])

return prcResult((!res && 'Invalid key!') || null, payload, header, cb)
}
Expand Down Expand Up @@ -131,18 +122,25 @@ inherits(JWTError, Error)

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

function verify (alg, key, input, signVar) {
return alg.type === 'hmac'
? signVar === sign(alg, key, input)
: crypto.createVerify(alg.hash)
: crypto
.createVerify(alg.hash)
.update(input)
.verify(key, b64url.unescape(signVar), 'base64')
}
Expand All @@ -157,10 +155,9 @@ function prcResult (err, payload, header, cb) {

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

function isFunction (param) {
Expand Down
Loading

0 comments on commit 57a31e7

Please sign in to comment.