|
|
|
@@ -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,7 +169,7 @@ 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} 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.
|
|
|
|
@@ -196,7 +196,8 @@ 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))}`
|
|
|
|
@@ -215,12 +216,12 @@ 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} 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, options: JwtVerifyOptions | JwtAlgorithm = { algorithm: 'HS256', throwError: false }): Promise<boolean> {
|
|
|
|
if (typeof options === 'string')
|
|
|
|
if (typeof options === 'string')
|
|
|
|
@@ -282,7 +283,7 @@ export async function verify(token: string, secret: string, options: JwtVerifyOp
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 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`.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|