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

Support to add additional JWT headers #12

Merged
merged 2 commits into from
Jan 19, 2017
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
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

var xtend = require('xtend')

const crypto = require('crypto')
const b64url = require('base64-url')
const inherits = require('util').inherits
Expand Down Expand Up @@ -43,7 +45,10 @@ function encode (key, payload, algorithm, cb) {
return prcResult(validationError, null, cb)
}

var parts = b64url.encode(JSON.stringify({typ: 'JWT', alg: algorithm})) +
var header = xtend({typ: 'JWT', alg: algorithm}, payload.header);
delete payload.header;

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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"homepage": "https://github.com/joaquimserafim/json-web-token",
"dependencies": {
"base64-url": "^1.2.2",
"json-parse-safe": "^1.0.3"
"json-parse-safe": "^1.0.3",
"xtend": "^4.0.1"
},
"devDependencies": {
"istanbul": "^0.4.3",
Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,33 @@ var read = require('fs').readFileSync
var test = require('tape')
var b64url = require('base64-url')
var jwt = require('../.')
var xtend = require('xtend')

var payload = {
iss: 'my_issurer',
aud: 'World',
iat: 1400062400223,
typ: '/online/transactionstatus/v2',
header: {
kid: 'TestKeyId'
},
request: {
myTransactionId: '[myTransactionId]',
merchantTransactionId: '[merchantTransactionId]',
status: 'SUCCESS'
}
}

var extraHeaders = {
header: {kid: 'TestKeyId'}
};

var payloadWithHeaders = xtend(payload, extraHeaders);

var secret = 'TOPSECRETTTTT'
var theToken = null
var theTokenSign = null
var theTokenSignWithHeaders = null
var algorithms

test('get the error class', function(assert) {
Expand Down Expand Up @@ -57,6 +68,17 @@ test('jwt - encode with callback / sign', function(assert) {
})
})

test('jwt + custom headers - encode with callback / sign', function(assert) {
var pem = read(__dirname + '/fixtures/test.pem').toString('ascii')
jwt.encode(pem, payloadWithHeaders, 'RS256', function(err, token) {
assert.deepEqual(err, null)
assert.ok(token)
theTokenSignWithHeaders = token
assert.deepEqual(token.split('.').length, 3)
assert.end()
})
})

test('jwt - encode with callback / bad algorithm', function(assert) {
jwt.encode(secret, payload, 'wow', function(err) {
assert.deepEqual(err.message, 'The algorithm is not supported!')
Expand All @@ -81,6 +103,15 @@ test('jwt - decode with callback / sign', function(assert) {
})
})

test('jwt + custom headers - decode with callback / sign', function(assert) {
var crt = read(__dirname + '/fixtures/test.crt').toString('ascii')
jwt.decode(crt, theTokenSignWithHeaders, function(err, decodePayload) {
assert.deepEqual(err, null)
assert.deepEqual(decodePayload, payloadWithHeaders)
assert.end()
})
})

test('jwt - decode with callback / bad algorithm', function(assert) {
var t = theToken.split('.').slice(1, 3)
var badHeader = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJ3b3cifQ'
Expand Down