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

update dev dependencies, add ts types #23

Merged
merged 1 commit into from
Sep 30, 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
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