Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
03c66f4223
|
|||
|
70488a6a48
|
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "2.3.0",
|
"version": "2.3.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "2.3.0",
|
"version": "2.3.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20231025.0",
|
"@cloudflare/workers-types": "^4.20231025.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "2.3.0",
|
"version": "2.3.1",
|
||||||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"exports": "./index.js",
|
"exports": "./index.js",
|
||||||
|
|||||||
34
src/index.ts
34
src/index.ts
@@ -10,7 +10,7 @@ export type JwtAlgorithm = 'ES256'|'ES384'|'ES512'|'HS256'|'HS384'|'HS512'|'RS25
|
|||||||
/**
|
/**
|
||||||
* @typedef JwtAlgorithms
|
* @typedef JwtAlgorithms
|
||||||
*/
|
*/
|
||||||
export interface JwtAlgorithms {
|
export type JwtAlgorithms = {
|
||||||
[key: string]: SubtleCryptoImportKeyAlgorithm
|
[key: string]: SubtleCryptoImportKeyAlgorithm
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,16 +18,14 @@ export interface JwtAlgorithms {
|
|||||||
* @typedef JwtHeader
|
* @typedef JwtHeader
|
||||||
* @prop {string} [typ] Type
|
* @prop {string} [typ] Type
|
||||||
*/
|
*/
|
||||||
export interface JwtHeader {
|
export type JwtHeader<T = {}> = {
|
||||||
/**
|
/**
|
||||||
* Type (default: `"JWT"`)
|
* Type (default: `"JWT"`)
|
||||||
*
|
*
|
||||||
* @default "JWT"
|
* @default "JWT"
|
||||||
*/
|
*/
|
||||||
typ?: string
|
typ?: string
|
||||||
|
} & T
|
||||||
[key: string]: any
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef JwtPayload
|
* @typedef JwtPayload
|
||||||
@@ -39,7 +37,7 @@ export interface JwtHeader {
|
|||||||
* @prop {string} [iat] Issued At
|
* @prop {string} [iat] Issued At
|
||||||
* @prop {string} [jti] JWT ID
|
* @prop {string} [jti] JWT ID
|
||||||
*/
|
*/
|
||||||
export interface JwtPayload {
|
export type JwtPayload<T = {}> = {
|
||||||
/** Issuer */
|
/** Issuer */
|
||||||
iss?: string
|
iss?: string
|
||||||
|
|
||||||
@@ -62,13 +60,13 @@ export interface JwtPayload {
|
|||||||
jti?: string
|
jti?: string
|
||||||
|
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
}
|
} & T
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef JwtOptions
|
* @typedef JwtOptions
|
||||||
* @prop {JwtAlgorithm | string} algorithm
|
* @prop {JwtAlgorithm | string} algorithm
|
||||||
*/
|
*/
|
||||||
export interface JwtOptions {
|
export type JwtOptions = {
|
||||||
algorithm?: JwtAlgorithm | string
|
algorithm?: JwtAlgorithm | string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,32 +75,32 @@ export interface JwtOptions {
|
|||||||
* @extends JwtOptions
|
* @extends JwtOptions
|
||||||
* @prop {JwtHeader} [header]
|
* @prop {JwtHeader} [header]
|
||||||
*/
|
*/
|
||||||
export interface JwtSignOptions extends JwtOptions {
|
export type JwtSignOptions = {
|
||||||
header?: JwtHeader
|
header?: JwtHeader
|
||||||
}
|
} & JwtOptions
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef JwtVerifyOptions
|
* @typedef JwtVerifyOptions
|
||||||
* @extends JwtOptions
|
* @extends JwtOptions
|
||||||
* @prop {boolean} [throwError=false] If `true` throw error if checks fail. (default: `false`)
|
* @prop {boolean} [throwError=false] If `true` throw error if checks fail. (default: `false`)
|
||||||
*/
|
*/
|
||||||
export interface JwtVerifyOptions extends JwtOptions {
|
export type JwtVerifyOptions = {
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
}
|
} & JwtOptions
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef JwtData
|
* @typedef JwtData
|
||||||
* @prop {JwtHeader} header
|
* @prop {JwtHeader} header
|
||||||
* @prop {JwtPayload} payload
|
* @prop {JwtPayload} payload
|
||||||
*/
|
*/
|
||||||
export interface JwtData {
|
export type JwtData<Payload = {}, Header = {}> = {
|
||||||
header?: JwtHeader
|
header?: JwtHeader<Header>
|
||||||
payload?: JwtPayload
|
payload?: JwtPayload<Payload>
|
||||||
}
|
}
|
||||||
|
|
||||||
const algorithms: JwtAlgorithms = {
|
const algorithms: JwtAlgorithms = {
|
||||||
@@ -309,10 +307,10 @@ export async function verify(token: string, secret: string | JsonWebKey, options
|
|||||||
* @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`.
|
||||||
*/
|
*/
|
||||||
export function decode(token: string): JwtData {
|
export function decode<Payload = {}, Header = {}>(token: string): JwtData<Payload, Header> {
|
||||||
return {
|
return {
|
||||||
header: decodePayload<JwtHeader>(token.split('.')[0].replace(/-/g, '+').replace(/_/g, '/')),
|
header: decodePayload<JwtHeader<Header>>(token.split('.')[0].replace(/-/g, '+').replace(/_/g, '/')),
|
||||||
payload: decodePayload<JwtPayload>(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'))
|
payload: decodePayload<JwtPayload<Payload>>(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user