Add support for JWT header
This commit is contained in:
1
index.d.ts
vendored
1
index.d.ts
vendored
@@ -42,6 +42,7 @@ type JWTAlgorithm = 'ES256' | 'ES384' | 'ES512' | 'HS256' | 'HS384' | 'HS512' |
|
|||||||
type JWTSignOptions = {
|
type JWTSignOptions = {
|
||||||
algorithm?: JWTAlgorithm,
|
algorithm?: JWTAlgorithm,
|
||||||
keyid?: string
|
keyid?: string
|
||||||
|
header?: object
|
||||||
}
|
}
|
||||||
|
|
||||||
type JWTVerifyOptions = {
|
type JWTVerifyOptions = {
|
||||||
|
|||||||
6
index.js
6
index.js
@@ -53,9 +53,9 @@ class JWT {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async sign(payload, secret, options = { algorithm: 'HS256' }) {
|
async sign(payload, secret, options = { algorithm: 'HS256', header: { typ: 'JWT' } }) {
|
||||||
if (typeof options === 'string')
|
if (typeof options === 'string')
|
||||||
options = { algorithm: options }
|
options = { algorithm: options, header: { typ: 'JWT' } }
|
||||||
if (payload === null || typeof payload !== 'object')
|
if (payload === null || typeof payload !== 'object')
|
||||||
throw new Error('payload must be an object')
|
throw new Error('payload must be an object')
|
||||||
if (typeof secret !== 'string')
|
if (typeof secret !== 'string')
|
||||||
@@ -67,7 +67,7 @@ class JWT {
|
|||||||
throw new Error('algorithm not found')
|
throw new Error('algorithm not found')
|
||||||
payload.iat = Math.floor(Date.now() / 1000)
|
payload.iat = Math.floor(Date.now() / 1000)
|
||||||
const payloadAsJSON = JSON.stringify(payload)
|
const payloadAsJSON = JSON.stringify(payload)
|
||||||
const partialToken = `${Base64URL.stringify(this._utf8ToUint8Array(JSON.stringify({ alg: options.algorithm, kid: options.keyid })))}.${Base64URL.stringify(this._utf8ToUint8Array(payloadAsJSON))}`
|
const partialToken = `${Base64URL.stringify(this._utf8ToUint8Array(JSON.stringify({ ...options.header, alg: options.algorithm, kid: options.keyid })))}.${Base64URL.stringify(this._utf8ToUint8Array(payloadAsJSON))}`
|
||||||
let keyFormat = 'raw'
|
let keyFormat = 'raw'
|
||||||
let keyData
|
let keyData
|
||||||
if (secret.startsWith('-----BEGIN')) {
|
if (secret.startsWith('-----BEGIN')) {
|
||||||
|
|||||||
Reference in New Issue
Block a user