|
|
|
@@ -21,7 +21,7 @@ export interface JwtAlgorithms {
|
|
|
|
export interface JwtHeader {
|
|
|
|
export interface JwtHeader {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Type (default: `"JWT"`)
|
|
|
|
* Type (default: `"JWT"`)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @default "JWT"
|
|
|
|
* @default "JWT"
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
typ?: string
|
|
|
|
typ?: string
|
|
|
|
@@ -89,7 +89,7 @@ export interface JwtSignOptions extends JwtOptions {
|
|
|
|
export interface JwtVerifyOptions extends JwtOptions {
|
|
|
|
export interface JwtVerifyOptions extends JwtOptions {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* If `true` throw error if checks fail. (default: `false`)
|
|
|
|
* If `true` throw error if checks fail. (default: `false`)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @default false
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
throwError?: boolean
|
|
|
|
throwError?: boolean
|
|
|
|
@@ -169,14 +169,14 @@ function _decodePayload(raw: string): JwtHeader | JwtPayload | null {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Signs a payload and returns the token
|
|
|
|
* Signs a payload and returns the token
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {JwtPayload} payload The payload object. To use `nbf` (Not Before) and/or `exp` (Expiration Time) add `nbf` and/or `exp` to the payload.
|
|
|
|
* @param {JwtPayload} payload The payload object. To use `nbf` (Not Before) and/or `exp` (Expiration Time) add `nbf` and/or `exp` to the payload.
|
|
|
|
* @param {string} secret A string which is used to sign the payload.
|
|
|
|
* @param {string | JsonWebKey} secret A string which is used to sign the payload.
|
|
|
|
* @param {JwtSignOptions | JwtAlgorithm | string} [options={ algorithm: 'HS256', header: { typ: 'JWT' } }] The options object or the algorithm.
|
|
|
|
* @param {JwtSignOptions | JwtAlgorithm | string} [options={ algorithm: 'HS256', header: { typ: 'JWT' } }] The options object or the algorithm.
|
|
|
|
* @throws {Error} If there's a validation issue.
|
|
|
|
* @throws {Error} If there's a validation issue.
|
|
|
|
* @returns {Promise<string>} Returns token as a `string`.
|
|
|
|
* @returns {Promise<string>} Returns token as a `string`.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export async function sign(payload: JwtPayload, secret: string, options: JwtSignOptions | JwtAlgorithm = { algorithm: 'HS256', header: { typ: 'JWT' } }): Promise<string> {
|
|
|
|
export async function sign(payload: JwtPayload, secret: string | JsonWebKey, options: JwtSignOptions | JwtAlgorithm = { algorithm: 'HS256', header: { typ: 'JWT' } }): Promise<string> {
|
|
|
|
if (typeof options === 'string')
|
|
|
|
if (typeof options === 'string')
|
|
|
|
options = { algorithm: options, header: { typ: 'JWT' } }
|
|
|
|
options = { algorithm: options, header: { typ: 'JWT' } }
|
|
|
|
|
|
|
|
|
|
|
|
@@ -185,8 +185,8 @@ export async function sign(payload: JwtPayload, secret: string, options: JwtSign
|
|
|
|
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' && typeof secret !== 'object')
|
|
|
|
throw new Error('secret must be a string')
|
|
|
|
throw new Error('secret must be a string or a JWK object')
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof options.algorithm !== 'string')
|
|
|
|
if (typeof options.algorithm !== 'string')
|
|
|
|
throw new Error('options.algorithm must be a string')
|
|
|
|
throw new Error('options.algorithm must be a string')
|
|
|
|
@@ -196,17 +196,24 @@ export async function sign(payload: JwtPayload, secret: string, options: JwtSign
|
|
|
|
if (!algorithm)
|
|
|
|
if (!algorithm)
|
|
|
|
throw new Error('algorithm not found')
|
|
|
|
throw new Error('algorithm not found')
|
|
|
|
|
|
|
|
|
|
|
|
payload.iat = Math.floor(Date.now() / 1000)
|
|
|
|
if (!payload.iat)
|
|
|
|
|
|
|
|
payload.iat = Math.floor(Date.now() / 1000)
|
|
|
|
|
|
|
|
|
|
|
|
const payloadAsJSON = JSON.stringify(payload)
|
|
|
|
const payloadAsJSON = JSON.stringify(payload)
|
|
|
|
const partialToken = `${base64UrlStringify(_utf8ToUint8Array(JSON.stringify({ ...options.header, alg: options.algorithm })))}.${base64UrlStringify(_utf8ToUint8Array(payloadAsJSON))}`
|
|
|
|
const partialToken = `${base64UrlStringify(_utf8ToUint8Array(JSON.stringify({ ...options.header, alg: options.algorithm })))}.${base64UrlStringify(_utf8ToUint8Array(payloadAsJSON))}`
|
|
|
|
|
|
|
|
|
|
|
|
let keyFormat = 'raw'
|
|
|
|
let keyFormat = 'raw'
|
|
|
|
let keyData
|
|
|
|
let keyData
|
|
|
|
if (secret.startsWith('-----BEGIN')) {
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof secret === 'object') {
|
|
|
|
|
|
|
|
keyFormat = 'jwk'
|
|
|
|
|
|
|
|
keyData = secret
|
|
|
|
|
|
|
|
} else if (typeof secret === 'string' && secret.startsWith('-----BEGIN')) {
|
|
|
|
keyFormat = 'pkcs8'
|
|
|
|
keyFormat = 'pkcs8'
|
|
|
|
keyData = _str2ab(secret.replace(/-----BEGIN.*?-----/g, '').replace(/-----END.*?-----/g, '').replace(/\s/g, ''))
|
|
|
|
keyData = _str2ab(secret.replace(/-----BEGIN.*?-----/g, '').replace(/-----END.*?-----/g, '').replace(/\s/g, ''))
|
|
|
|
} else
|
|
|
|
} else
|
|
|
|
keyData = _utf8ToUint8Array(secret)
|
|
|
|
keyData = _utf8ToUint8Array(secret)
|
|
|
|
|
|
|
|
|
|
|
|
const key = await crypto.subtle.importKey(keyFormat, keyData, algorithm, false, ['sign'])
|
|
|
|
const key = await crypto.subtle.importKey(keyFormat, keyData, algorithm, false, ['sign'])
|
|
|
|
const signature = await crypto.subtle.sign(algorithm, key, _utf8ToUint8Array(partialToken))
|
|
|
|
const signature = await crypto.subtle.sign(algorithm, key, _utf8ToUint8Array(partialToken))
|
|
|
|
|
|
|
|
|
|
|
|
@@ -215,14 +222,14 @@ export async function sign(payload: JwtPayload, secret: string, options: JwtSign
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Verifies the integrity of the token and returns a boolean value.
|
|
|
|
* Verifies the integrity of the token and returns a boolean value.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {string} token The token string generated by `jwt.sign()`.
|
|
|
|
* @param {string} token The token string generated by `jwt.sign()`.
|
|
|
|
* @param {string} secret The string which was used to sign the payload.
|
|
|
|
* @param {string | JsonWebKey} secret The string which was used to sign the payload.
|
|
|
|
* @param {JWTVerifyOptions | JWTAlgorithm} options The options object or the algorithm.
|
|
|
|
* @param {JWTVerifyOptions | JWTAlgorithm} options The options object or the algorithm.
|
|
|
|
* @throws {Error | string} Throws an error `string` if the token is invalid or an `Error-Object` if there's a validation issue.
|
|
|
|
* @throws {Error | string} Throws an error `string` if the token is invalid or an `Error-Object` if there's a validation issue.
|
|
|
|
* @returns {Promise<boolean>} Returns `true` if signature, `nbf` (if set) and `exp` (if set) are valid, otherwise returns `false`.
|
|
|
|
* @returns {Promise<boolean>} Returns `true` if signature, `nbf` (if set) and `exp` (if set) are valid, otherwise returns `false`.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export async function verify(token: string, secret: string, options: JwtVerifyOptions | JwtAlgorithm = { algorithm: 'HS256', throwError: false }): Promise<boolean> {
|
|
|
|
export async function verify(token: string, secret: string | JsonWebKey, options: JwtVerifyOptions | JwtAlgorithm = { algorithm: 'HS256', throwError: false }): Promise<boolean> {
|
|
|
|
if (typeof options === 'string')
|
|
|
|
if (typeof options === 'string')
|
|
|
|
options = { algorithm: options, throwError: false }
|
|
|
|
options = { algorithm: options, throwError: false }
|
|
|
|
|
|
|
|
|
|
|
|
@@ -231,8 +238,8 @@ export async function verify(token: string, secret: string, options: JwtVerifyOp
|
|
|
|
if (typeof token !== 'string')
|
|
|
|
if (typeof token !== 'string')
|
|
|
|
throw new Error('token must be a string')
|
|
|
|
throw new Error('token must be a string')
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof secret !== 'string')
|
|
|
|
if (typeof secret !== 'string' && typeof secret !== 'object')
|
|
|
|
throw new Error('secret must be a string')
|
|
|
|
throw new Error('secret must be a string or a JWK object')
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof options.algorithm !== 'string')
|
|
|
|
if (typeof options.algorithm !== 'string')
|
|
|
|
throw new Error('options.algorithm must be a string')
|
|
|
|
throw new Error('options.algorithm must be a string')
|
|
|
|
@@ -271,18 +278,24 @@ export async function verify(token: string, secret: string, options: JwtVerifyOp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let keyFormat = 'raw'
|
|
|
|
let keyFormat = 'raw'
|
|
|
|
let keyData
|
|
|
|
let keyData
|
|
|
|
if (secret.startsWith('-----BEGIN')) {
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof secret === 'object') {
|
|
|
|
|
|
|
|
keyFormat = 'jwk';
|
|
|
|
|
|
|
|
keyData = secret;
|
|
|
|
|
|
|
|
} else if (typeof secret === 'string' && secret.startsWith('-----BEGIN')) {
|
|
|
|
keyFormat = 'spki'
|
|
|
|
keyFormat = 'spki'
|
|
|
|
keyData = _str2ab(secret.replace(/-----BEGIN.*?-----/g, '').replace(/-----END.*?-----/g, '').replace(/\s/g, ''))
|
|
|
|
keyData = _str2ab(secret.replace(/-----BEGIN.*?-----/g, '').replace(/-----END.*?-----/g, '').replace(/\s/g, ''))
|
|
|
|
} else
|
|
|
|
} else
|
|
|
|
keyData = _utf8ToUint8Array(secret)
|
|
|
|
keyData = _utf8ToUint8Array(secret)
|
|
|
|
|
|
|
|
|
|
|
|
const key = await crypto.subtle.importKey(keyFormat, keyData, algorithm, false, ['verify'])
|
|
|
|
const key = await crypto.subtle.importKey(keyFormat, keyData, algorithm, false, ['verify'])
|
|
|
|
|
|
|
|
|
|
|
|
return await crypto.subtle.verify(algorithm, key, base64UrlParse(tokenParts[2]), _utf8ToUint8Array(`${tokenParts[0]}.${tokenParts[1]}`))
|
|
|
|
return await crypto.subtle.verify(algorithm, key, base64UrlParse(tokenParts[2]), _utf8ToUint8Array(`${tokenParts[0]}.${tokenParts[1]}`))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Returns the payload **without** verifying the integrity of the token. Please use `jwt.verify()` first to keep your application secure!
|
|
|
|
* Returns the payload **without** verifying the integrity of the token. Please use `jwt.verify()` first to keep your application secure!
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {string} token The token string generated by `jwt.sign()`.
|
|
|
|
* @param {string} token The token string generated by `jwt.sign()`.
|
|
|
|
* @returns {JwtData} Returns an `object` containing `header` and `payload`.
|
|
|
|
* @returns {JwtData} Returns an `object` containing `header` and `payload`.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|