@sourceregistry/node-jwt 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +128 -147
- package/dist/index-CSRWSLal.cjs +2 -0
- package/dist/index-CSRWSLal.cjs.map +1 -0
- package/dist/index-DyXdSqEc.js +515 -0
- package/dist/index-DyXdSqEc.js.map +1 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +2 -236
- package/dist/index.es.js +18 -366
- package/dist/index.es.js.map +1 -1
- package/dist/promises.cjs.js +1 -1
- package/dist/promises.cjs.js.map +1 -1
- package/dist/promises.d.ts +2 -115
- package/dist/promises.es.js +34 -15
- package/dist/promises.es.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/index.ts"],"sourcesContent":["import crypto, {\n createHmac,\n createSign,\n createVerify,\n sign as cryptoSign,\n verify as cryptoVerify,\n timingSafeEqual,\n type BinaryLike,\n type KeyLike\n} from 'crypto';\n\n// Base64URL helpers (padding-safe)\nexport const base64Url = {\n encode: (input: string | Buffer): string =>\n Buffer.from(input).toString('base64url'),\n\n decode: (input: string): string => {\n // Node.js Buffer handles unpadded base64url since v16, but we normalize for safety\n return Buffer.from(input, 'base64url').toString();\n }\n};\n\n// Timing-safe string comparison to prevent timing attacks\nconst timingSafeCompare = (a: string, b: string): boolean => {\n if (a.length !== b.length) {\n return false;\n }\n return timingSafeEqual(Buffer.from(a), Buffer.from(b));\n};\n\n// Standard JWT payload claims\nexport interface JWTPayload {\n /**\n * Issuer\n */\n iss?: string;\n /**\n * Subject\n */\n sub?: string;\n /**\n * Audience\n */\n aud?: string | string[];\n /**\n * Expiration Time (as UNIX timestamp)\n */\n exp?: number;\n /**\n * Not Before (as UNIX timestamp)\n */\n nbf?: number;\n /**\n * Issued At (as UNIX timestamp)\n */\n iat?: number;\n /**\n * JWT ID\n */\n jti?: string;\n /**\n * Session ID\n */\n sid?: string;\n\n /**\n * Custom claims\n */\n [key: string]: unknown;\n}\n\nexport interface JWTHeader {\n alg: string; // Allow unknown algs during decode\n typ?: string;\n kid?: string;\n}\n\nexport interface JWT {\n header: JWTHeader;\n payload: JWTPayload;\n signature: string;\n}\n\n// Signature algorithms\nexport const SignatureAlgorithm = {\n // HMAC\n HS256: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createHmac('sha256', secret).update(data).digest('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n const expected = createHmac('sha256', secret).update(data).digest('base64url');\n return timingSafeCompare(expected, signature);\n }\n },\n HS384: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createHmac('sha384', secret).update(data).digest('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n const expected = createHmac('sha384', secret).update(data).digest('base64url');\n return timingSafeCompare(expected, signature);\n }\n },\n HS512: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createHmac('sha512', secret).update(data).digest('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n const expected = createHmac('sha512', secret).update(data).digest('base64url');\n return timingSafeCompare(expected, signature);\n }\n },\n\n // RSA (DER-encoded signatures, base64url)\n RS256: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('RSA-SHA256').update(data).end().sign(secret).toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('RSA-SHA256')\n .update(data)\n .end()\n .verify(secret, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n RS384: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('RSA-SHA384').update(data).end().sign(secret).toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('RSA-SHA384')\n .update(data)\n .end()\n .verify(secret, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n RS512: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('RSA-SHA512').update(data).end().sign(secret).toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('RSA-SHA512')\n .update(data)\n .end()\n .verify(secret, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n\n // ECDSA (DER-encoded by default — no dsaEncoding!)\n ES256: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('SHA256').update(data).end().sign(secret).toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('SHA256')\n .update(data)\n .end()\n .verify(secret, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n ES384: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('SHA384').update(data).end().sign(secret).toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('SHA384')\n .update(data)\n .end()\n .verify(secret, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n ES512: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('SHA512').update(data).end().sign(secret).toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('SHA512')\n .update(data)\n .end()\n .verify(secret, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n ES256K: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('SHA256').update(data).end().sign(secret).toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('SHA256')\n .update(data)\n .end()\n .verify(secret, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n PS256: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('RSA-SHA256')\n .update(data)\n .end()\n .sign({\n //@ts-ignore\n key: secret,\n padding: crypto.constants.RSA_PKCS1_PSS_PADDING,\n saltLength: 32\n })\n .toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('RSA-SHA256')\n .update(data)\n .end()\n .verify({\n //@ts-ignore\n key: secret,\n padding: crypto.constants.RSA_PKCS1_PSS_PADDING,\n saltLength: 32\n }, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n PS384: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('RSA-SHA384')\n .update(data)\n .end()\n .sign({\n //@ts-ignore\n key: secret,\n padding: crypto.constants.RSA_PKCS1_PSS_PADDING,\n saltLength: 48\n })\n .toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('RSA-SHA384')\n .update(data)\n .end()\n .verify({\n //@ts-ignore\n key: secret,\n padding: crypto.constants.RSA_PKCS1_PSS_PADDING,\n saltLength: 48\n }, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n PS512: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n createSign('RSA-SHA512')\n .update(data)\n .end()\n .sign({\n //@ts-ignore\n key: secret,\n padding: crypto.constants.RSA_PKCS1_PSS_PADDING,\n saltLength: 64\n })\n .toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return createVerify('RSA-SHA512')\n .update(data)\n .end()\n .verify({\n //@ts-ignore\n key: secret,\n padding: crypto.constants.RSA_PKCS1_PSS_PADDING,\n saltLength: 64\n }, Buffer.from(signature, 'base64url'));\n } catch {\n return false;\n }\n }\n },\n EdDSA: {\n sign: (data: BinaryLike, secret: KeyLike) =>\n cryptoSign(null, typeof data === 'string' ? Buffer.from(data, 'utf8') : data, secret)\n .toString('base64url'),\n verify: (data: BinaryLike, secret: KeyLike, signature: string) => {\n try {\n return cryptoVerify(\n null,\n typeof data === 'string' ? Buffer.from(data, 'utf8') : data,\n secret,\n Buffer.from(signature, 'base64url')\n );\n } catch {\n return false;\n }\n }\n }\n} as const;\n\nexport type SupportedAlgorithm = keyof typeof SignatureAlgorithm;\n\nexport const SupportedAlgorithms = Object.keys(SignatureAlgorithm) as Array<SupportedAlgorithm>;\n\n/**\n * Decode a JWT string into its parts (without verification)\n */\nexport const decode = (token: string): JWT => {\n const parts = token.split('.');\n if (parts.length !== 3) {\n throw new Error('Invalid JWT: must contain exactly 3 parts separated by \".\"');\n }\n\n const [headerPart, payloadPart, signature] = parts;\n\n if (!headerPart || !payloadPart || !signature) {\n throw new Error('Invalid JWT: empty part detected');\n }\n\n try {\n const header = JSON.parse(base64Url.decode(headerPart)) as JWTHeader;\n const payload = JSON.parse(base64Url.decode(payloadPart)) as JWTPayload;\n return {header, payload, signature};\n } catch (err) {\n throw new Error(`Invalid JWT: malformed header or payload (${(err as Error).message})`);\n }\n};\n\n/**\n * Sign a JWT\n */\nexport const sign = (\n payload: JWTPayload,\n secret: KeyLike,\n options: {\n alg?: SupportedAlgorithm;\n kid?: string;\n typ?: string;\n } = {}\n): string => {\n const alg = options.alg ?? 'HS256';\n const typ = options.typ ?? 'JWT';\n\n if (!(alg in SignatureAlgorithm)) {\n throw new Error(`Unsupported algorithm: ${alg}`);\n }\n\n const header: JWTHeader = {alg, typ};\n if (options.kid) header.kid = options.kid;\n\n const headerEncoded = base64Url.encode(JSON.stringify(header));\n const payloadEncoded = base64Url.encode(JSON.stringify(payload));\n\n const signingInput = `${headerEncoded}.${payloadEncoded}`;\n const signature = SignatureAlgorithm[alg].sign(signingInput, secret);\n\n return `${headerEncoded}.${payloadEncoded}.${signature}`;\n};\n\n/**\n * Verify and validate a JWT\n */\nexport const verify = (\n token: string,\n secret: KeyLike,\n options: {\n algorithms?: SupportedAlgorithm[]; // Whitelist of allowed algorithms\n issuer?: string;\n subject?: string;\n audience?: string | string[];\n jwtId?: string;\n ignoreExpiration?: boolean;\n clockSkew?: number; // in seconds, default 0\n maxTokenAge?: number; // Maximum age in seconds\n } = {}\n):\n | { valid: true; header: JWTHeader; payload: JWTPayload; signature: string }\n | { valid: false; error: { reason: string; code: string } } => {\n let decoded: JWT;\n try {\n decoded = decode(token);\n } catch (err) {\n return {\n valid: false,\n error: {\n reason: (err as Error).message,\n code: 'INVALID_TOKEN'\n }\n };\n }\n\n const {header, payload, signature} = decoded;\n\n // Validate algorithm\n const alg = header.alg as SupportedAlgorithm;\n if (!(alg in SignatureAlgorithm)) {\n return {\n valid: false,\n error: {\n reason: `Unsupported or unknown algorithm: ${header.alg}`,\n code: 'INVALID_ALGORITHM'\n }\n };\n }\n\n // Algorithm whitelist validation (prevents algorithm confusion attacks)\n if (options.algorithms && options.algorithms.length > 0) {\n if (!options.algorithms.includes(alg)) {\n return {\n valid: false,\n error: {\n reason: `Algorithm \"${alg}\" is not in the allowed algorithms list`,\n code: 'ALGORITHM_NOT_ALLOWED'\n }\n };\n }\n }\n\n // Validate 'typ' header (must be 'JWT' if present)\n if (header.typ !== undefined && header.typ !== 'JWT') {\n return {\n valid: false,\n error: {\n reason: `Invalid token type: expected 'JWT', got '${header.typ}'`,\n code: 'INVALID_TYPE'\n }\n };\n }\n\n // Verify signature\n const signingInput = `${base64Url.encode(JSON.stringify(header))}.${base64Url.encode(JSON.stringify(payload))}`;\n const isValidSignature = SignatureAlgorithm[alg].verify(signingInput, secret, signature);\n\n if (!isValidSignature) {\n return {\n valid: false,\n error: {\n reason: \"Signature verification failed\",\n code: 'INVALID_SIGNATURE'\n }\n };\n }\n\n // Time validation\n const now = Math.floor(Date.now() / 1000);\n const skew = options.clockSkew ?? 0;\n\n if (!options.ignoreExpiration) {\n if (payload.exp !== undefined && now > payload.exp + skew) {\n return {\n valid: false,\n error: {\n reason: 'Token expired',\n code: 'TOKEN_EXPIRED'\n }\n };\n }\n }\n\n if (payload.nbf !== undefined && now + skew < payload.nbf) {\n return {\n valid: false,\n error: {\n reason: 'Token not yet valid',\n code: 'TOKEN_NOT_ACTIVE'\n }\n };\n }\n\n if (payload.iat !== undefined && now + skew < payload.iat) {\n return {\n valid: false,\n error: {\n reason: 'Token issued in the future',\n code: 'TOKEN_FUTURE_ISSUED'\n }\n };\n }\n\n // Maximum token age validation\n if (options.maxTokenAge !== undefined && payload.iat !== undefined) {\n const tokenAge = now - payload.iat;\n if (tokenAge > options.maxTokenAge) {\n return {\n valid: false,\n error: {\n reason: `Token age (${tokenAge}s) exceeds maximum allowed age (${options.maxTokenAge}s)`,\n code: 'TOKEN_TOO_OLD'\n }\n };\n }\n }\n\n // --- Claim validations (only if options provided) ---\n\n // Issuer (`iss`)\n if (options.issuer !== undefined) {\n if (payload.iss === undefined) {\n return {\n valid: false,\n error: {\n reason: 'Token missing required issuer claim (\"iss\")',\n code: 'MISSING_ISSUER'\n }\n };\n }\n if (options.issuer !== payload.iss) {\n return {\n valid: false,\n error: {\n reason: `Invalid token issuer: expected \"${options.issuer}\", got \"${payload.iss}\"`,\n code: 'INVALID_ISSUER'\n }\n };\n }\n }\n\n // Subject (`sub`)\n if (options.subject !== undefined) {\n if (payload.sub === undefined) {\n return {\n valid: false,\n error: {\n reason: 'Token missing required subject claim (\"sub\")',\n code: 'MISSING_SUBJECT'\n }\n };\n }\n if (options.subject !== payload.sub) {\n return {\n valid: false,\n error: {\n reason: `Invalid token subject: expected \"${options.subject}\", got \"${payload.sub}\"`,\n code: 'INVALID_SUBJECT'\n }\n };\n }\n }\n\n // Audience (`aud`)\n if (options.audience !== undefined) {\n const aud = payload.aud;\n if (aud === undefined) {\n return {\n valid: false,\n error: {\n reason: 'Token missing required audience claim (\"aud\")',\n code: 'MISSING_AUDIENCE'\n }\n };\n }\n\n const expectedAud = Array.isArray(options.audience) ? options.audience : [options.audience];\n const tokenAud = Array.isArray(aud) ? aud : [aud];\n\n const hasMatch = expectedAud.some(a => tokenAud.includes(a));\n if (!hasMatch) {\n return {\n valid: false,\n error: {\n reason: 'Audience claim mismatch',\n code: 'INVALID_AUDIENCE'\n }\n };\n }\n }\n\n // JWT ID (`jti`)\n if (options.jwtId !== undefined) {\n if (payload.jti === undefined) {\n return {\n valid: false,\n error: {\n reason: 'Token missing required JWT ID claim (\"jti\")',\n code: 'MISSING_JTI'\n }\n };\n }\n if (options.jwtId !== payload.jti) {\n return {\n valid: false,\n error: {\n reason: `Invalid JWT ID: expected \"${options.jwtId}\", got \"${payload.jti}\"`,\n code: 'INVALID_JTI'\n }\n };\n }\n }\n\n return {valid: true, header, payload, signature};\n};\n\n// Optional: namespace export\nexport const JWT = {\n sign,\n verify,\n decode,\n algorithms: SignatureAlgorithm\n};\n"],"names":["base64Url","input","timingSafeCompare","a","b","timingSafeEqual","SignatureAlgorithm","data","secret","createHmac","signature","expected","createSign","createVerify","crypto","cryptoSign","cryptoVerify","SupportedAlgorithms","decode","token","parts","headerPart","payloadPart","header","payload","err","sign","options","alg","typ","headerEncoded","payloadEncoded","signingInput","verify","decoded","now","skew","tokenAge","aud","expectedAud","tokenAud","JWT"],"mappings":"0GAYaA,EAAY,CACrB,OAASC,GACL,OAAO,KAAKA,CAAK,EAAE,SAAS,WAAW,EAE3C,OAASA,GAEE,OAAO,KAAKA,EAAO,WAAW,EAAE,SAAA,CAE/C,EAGMC,EAAoB,CAACC,EAAWC,IAC9BD,EAAE,SAAWC,EAAE,OACR,GAEJC,EAAAA,gBAAgB,OAAO,KAAKF,CAAC,EAAG,OAAO,KAAKC,CAAC,CAAC,EAyD5CE,EAAqB,CAE9B,MAAO,CACH,KAAM,CAACC,EAAkBC,IACrBC,EAAAA,WAAW,SAAUD,CAAM,EAAE,OAAOD,CAAI,EAAE,OAAO,WAAW,EAChE,OAAQ,CAACA,EAAkBC,EAAiBE,IAAsB,CAC9D,MAAMC,EAAWF,aAAW,SAAUD,CAAM,EAAE,OAAOD,CAAI,EAAE,OAAO,WAAW,EAC7E,OAAOL,EAAkBS,EAAUD,CAAS,CAChD,CAAA,EAEJ,MAAO,CACH,KAAM,CAACH,EAAkBC,IACrBC,EAAAA,WAAW,SAAUD,CAAM,EAAE,OAAOD,CAAI,EAAE,OAAO,WAAW,EAChE,OAAQ,CAACA,EAAkBC,EAAiBE,IAAs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,CAEA,MAAO,CAAC,MAAO,GAAM,OAAAD,EAAQ,QAAAC,EAAS,UAAAd,CAAA,CAC1C,EAGa+B,EAAM,CACf,KAAAf,EACA,OAAAO,EACA,OAAAf,EACA,WAAYZ,CAChB"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,236 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
encode: (input: string | Buffer) => string;
|
|
4
|
-
decode: (input: string) => string;
|
|
5
|
-
};
|
|
6
|
-
export interface JWTPayload {
|
|
7
|
-
/**
|
|
8
|
-
* Issuer
|
|
9
|
-
*/
|
|
10
|
-
iss?: string;
|
|
11
|
-
/**
|
|
12
|
-
* Subject
|
|
13
|
-
*/
|
|
14
|
-
sub?: string;
|
|
15
|
-
/**
|
|
16
|
-
* Audience
|
|
17
|
-
*/
|
|
18
|
-
aud?: string | string[];
|
|
19
|
-
/**
|
|
20
|
-
* Expiration Time (as UNIX timestamp)
|
|
21
|
-
*/
|
|
22
|
-
exp?: number;
|
|
23
|
-
/**
|
|
24
|
-
* Not Before (as UNIX timestamp)
|
|
25
|
-
*/
|
|
26
|
-
nbf?: number;
|
|
27
|
-
/**
|
|
28
|
-
* Issued At (as UNIX timestamp)
|
|
29
|
-
*/
|
|
30
|
-
iat?: number;
|
|
31
|
-
/**
|
|
32
|
-
* JWT ID
|
|
33
|
-
*/
|
|
34
|
-
jti?: string;
|
|
35
|
-
/**
|
|
36
|
-
* Session ID
|
|
37
|
-
*/
|
|
38
|
-
sid?: string;
|
|
39
|
-
/**
|
|
40
|
-
* Custom claims
|
|
41
|
-
*/
|
|
42
|
-
[key: string]: unknown;
|
|
43
|
-
}
|
|
44
|
-
export interface JWTHeader {
|
|
45
|
-
alg: string;
|
|
46
|
-
typ?: string;
|
|
47
|
-
kid?: string;
|
|
48
|
-
}
|
|
49
|
-
export interface JWT {
|
|
50
|
-
header: JWTHeader;
|
|
51
|
-
payload: JWTPayload;
|
|
52
|
-
signature: string;
|
|
53
|
-
}
|
|
54
|
-
export declare const SignatureAlgorithm: {
|
|
55
|
-
readonly HS256: {
|
|
56
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
57
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
58
|
-
};
|
|
59
|
-
readonly HS384: {
|
|
60
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
61
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
62
|
-
};
|
|
63
|
-
readonly HS512: {
|
|
64
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
65
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
66
|
-
};
|
|
67
|
-
readonly RS256: {
|
|
68
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
69
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
70
|
-
};
|
|
71
|
-
readonly RS384: {
|
|
72
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
73
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
74
|
-
};
|
|
75
|
-
readonly RS512: {
|
|
76
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
77
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
78
|
-
};
|
|
79
|
-
readonly ES256: {
|
|
80
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
81
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
82
|
-
};
|
|
83
|
-
readonly ES384: {
|
|
84
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
85
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
86
|
-
};
|
|
87
|
-
readonly ES512: {
|
|
88
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
89
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
90
|
-
};
|
|
91
|
-
readonly ES256K: {
|
|
92
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
93
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
94
|
-
};
|
|
95
|
-
readonly PS256: {
|
|
96
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
97
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
98
|
-
};
|
|
99
|
-
readonly PS384: {
|
|
100
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
101
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
102
|
-
};
|
|
103
|
-
readonly PS512: {
|
|
104
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
105
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
106
|
-
};
|
|
107
|
-
readonly EdDSA: {
|
|
108
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
109
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
export type SupportedAlgorithm = keyof typeof SignatureAlgorithm;
|
|
113
|
-
export declare const SupportedAlgorithms: Array<SupportedAlgorithm>;
|
|
114
|
-
/**
|
|
115
|
-
* Decode a JWT string into its parts (without verification)
|
|
116
|
-
*/
|
|
117
|
-
export declare const decode: (token: string) => JWT;
|
|
118
|
-
/**
|
|
119
|
-
* Sign a JWT
|
|
120
|
-
*/
|
|
121
|
-
export declare const sign: (payload: JWTPayload, secret: KeyLike, options?: {
|
|
122
|
-
alg?: SupportedAlgorithm;
|
|
123
|
-
kid?: string;
|
|
124
|
-
typ?: string;
|
|
125
|
-
}) => string;
|
|
126
|
-
/**
|
|
127
|
-
* Verify and validate a JWT
|
|
128
|
-
*/
|
|
129
|
-
export declare const verify: (token: string, secret: KeyLike, options?: {
|
|
130
|
-
algorithms?: SupportedAlgorithm[];
|
|
131
|
-
issuer?: string;
|
|
132
|
-
subject?: string;
|
|
133
|
-
audience?: string | string[];
|
|
134
|
-
jwtId?: string;
|
|
135
|
-
ignoreExpiration?: boolean;
|
|
136
|
-
clockSkew?: number;
|
|
137
|
-
maxTokenAge?: number;
|
|
138
|
-
}) => {
|
|
139
|
-
valid: true;
|
|
140
|
-
header: JWTHeader;
|
|
141
|
-
payload: JWTPayload;
|
|
142
|
-
signature: string;
|
|
143
|
-
} | {
|
|
144
|
-
valid: false;
|
|
145
|
-
error: {
|
|
146
|
-
reason: string;
|
|
147
|
-
code: string;
|
|
148
|
-
};
|
|
149
|
-
};
|
|
150
|
-
export declare const JWT: {
|
|
151
|
-
sign: (payload: JWTPayload, secret: KeyLike, options?: {
|
|
152
|
-
alg?: SupportedAlgorithm;
|
|
153
|
-
kid?: string;
|
|
154
|
-
typ?: string;
|
|
155
|
-
}) => string;
|
|
156
|
-
verify: (token: string, secret: KeyLike, options?: {
|
|
157
|
-
algorithms?: SupportedAlgorithm[];
|
|
158
|
-
issuer?: string;
|
|
159
|
-
subject?: string;
|
|
160
|
-
audience?: string | string[];
|
|
161
|
-
jwtId?: string;
|
|
162
|
-
ignoreExpiration?: boolean;
|
|
163
|
-
clockSkew?: number;
|
|
164
|
-
maxTokenAge?: number;
|
|
165
|
-
}) => {
|
|
166
|
-
valid: true;
|
|
167
|
-
header: JWTHeader;
|
|
168
|
-
payload: JWTPayload;
|
|
169
|
-
signature: string;
|
|
170
|
-
} | {
|
|
171
|
-
valid: false;
|
|
172
|
-
error: {
|
|
173
|
-
reason: string;
|
|
174
|
-
code: string;
|
|
175
|
-
};
|
|
176
|
-
};
|
|
177
|
-
decode: (token: string) => JWT;
|
|
178
|
-
algorithms: {
|
|
179
|
-
readonly HS256: {
|
|
180
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
181
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
182
|
-
};
|
|
183
|
-
readonly HS384: {
|
|
184
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
185
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
186
|
-
};
|
|
187
|
-
readonly HS512: {
|
|
188
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
189
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
190
|
-
};
|
|
191
|
-
readonly RS256: {
|
|
192
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
193
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
194
|
-
};
|
|
195
|
-
readonly RS384: {
|
|
196
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
197
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
198
|
-
};
|
|
199
|
-
readonly RS512: {
|
|
200
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
201
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
202
|
-
};
|
|
203
|
-
readonly ES256: {
|
|
204
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
205
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
206
|
-
};
|
|
207
|
-
readonly ES384: {
|
|
208
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
209
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
210
|
-
};
|
|
211
|
-
readonly ES512: {
|
|
212
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
213
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
214
|
-
};
|
|
215
|
-
readonly ES256K: {
|
|
216
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
217
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
218
|
-
};
|
|
219
|
-
readonly PS256: {
|
|
220
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
221
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
222
|
-
};
|
|
223
|
-
readonly PS384: {
|
|
224
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
225
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
226
|
-
};
|
|
227
|
-
readonly PS512: {
|
|
228
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
229
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
230
|
-
};
|
|
231
|
-
readonly EdDSA: {
|
|
232
|
-
readonly sign: (data: BinaryLike, secret: KeyLike) => string;
|
|
233
|
-
readonly verify: (data: BinaryLike, secret: KeyLike, signature: string) => boolean;
|
|
234
|
-
};
|
|
235
|
-
};
|
|
236
|
-
};
|
|
1
|
+
export * from './jwt';
|
|
2
|
+
export * from './jwks';
|
package/dist/index.es.js
CHANGED
|
@@ -1,369 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
const S = {
|
|
3
|
-
encode: (e) => Buffer.from(e).toString("base64url"),
|
|
4
|
-
decode: (e) => Buffer.from(e, "base64url").toString()
|
|
5
|
-
}, h = (e, r) => e.length !== r.length ? !1 : N(Buffer.from(e), Buffer.from(r)), g = {
|
|
6
|
-
// HMAC
|
|
7
|
-
HS256: {
|
|
8
|
-
sign: (e, r) => c("sha256", r).update(e).digest("base64url"),
|
|
9
|
-
verify: (e, r, t) => {
|
|
10
|
-
const n = c("sha256", r).update(e).digest("base64url");
|
|
11
|
-
return h(n, t);
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
HS384: {
|
|
15
|
-
sign: (e, r) => c("sha384", r).update(e).digest("base64url"),
|
|
16
|
-
verify: (e, r, t) => {
|
|
17
|
-
const n = c("sha384", r).update(e).digest("base64url");
|
|
18
|
-
return h(n, t);
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
HS512: {
|
|
22
|
-
sign: (e, r) => c("sha512", r).update(e).digest("base64url"),
|
|
23
|
-
verify: (e, r, t) => {
|
|
24
|
-
const n = c("sha512", r).update(e).digest("base64url");
|
|
25
|
-
return h(n, t);
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
// RSA (DER-encoded signatures, base64url)
|
|
29
|
-
RS256: {
|
|
30
|
-
sign: (e, r) => s("RSA-SHA256").update(e).end().sign(r).toString("base64url"),
|
|
31
|
-
verify: (e, r, t) => {
|
|
32
|
-
try {
|
|
33
|
-
return d("RSA-SHA256").update(e).end().verify(r, Buffer.from(t, "base64url"));
|
|
34
|
-
} catch {
|
|
35
|
-
return !1;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
RS384: {
|
|
40
|
-
sign: (e, r) => s("RSA-SHA384").update(e).end().sign(r).toString("base64url"),
|
|
41
|
-
verify: (e, r, t) => {
|
|
42
|
-
try {
|
|
43
|
-
return d("RSA-SHA384").update(e).end().verify(r, Buffer.from(t, "base64url"));
|
|
44
|
-
} catch {
|
|
45
|
-
return !1;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
RS512: {
|
|
50
|
-
sign: (e, r) => s("RSA-SHA512").update(e).end().sign(r).toString("base64url"),
|
|
51
|
-
verify: (e, r, t) => {
|
|
52
|
-
try {
|
|
53
|
-
return d("RSA-SHA512").update(e).end().verify(r, Buffer.from(t, "base64url"));
|
|
54
|
-
} catch {
|
|
55
|
-
return !1;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
},
|
|
59
|
-
// ECDSA (DER-encoded by default — no dsaEncoding!)
|
|
60
|
-
ES256: {
|
|
61
|
-
sign: (e, r) => s("SHA256").update(e).end().sign(r).toString("base64url"),
|
|
62
|
-
verify: (e, r, t) => {
|
|
63
|
-
try {
|
|
64
|
-
return d("SHA256").update(e).end().verify(r, Buffer.from(t, "base64url"));
|
|
65
|
-
} catch {
|
|
66
|
-
return !1;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
ES384: {
|
|
71
|
-
sign: (e, r) => s("SHA384").update(e).end().sign(r).toString("base64url"),
|
|
72
|
-
verify: (e, r, t) => {
|
|
73
|
-
try {
|
|
74
|
-
return d("SHA384").update(e).end().verify(r, Buffer.from(t, "base64url"));
|
|
75
|
-
} catch {
|
|
76
|
-
return !1;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
ES512: {
|
|
81
|
-
sign: (e, r) => s("SHA512").update(e).end().sign(r).toString("base64url"),
|
|
82
|
-
verify: (e, r, t) => {
|
|
83
|
-
try {
|
|
84
|
-
return d("SHA512").update(e).end().verify(r, Buffer.from(t, "base64url"));
|
|
85
|
-
} catch {
|
|
86
|
-
return !1;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
},
|
|
90
|
-
ES256K: {
|
|
91
|
-
sign: (e, r) => s("SHA256").update(e).end().sign(r).toString("base64url"),
|
|
92
|
-
verify: (e, r, t) => {
|
|
93
|
-
try {
|
|
94
|
-
return d("SHA256").update(e).end().verify(r, Buffer.from(t, "base64url"));
|
|
95
|
-
} catch {
|
|
96
|
-
return !1;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
PS256: {
|
|
101
|
-
sign: (e, r) => s("RSA-SHA256").update(e).end().sign({
|
|
102
|
-
//@ts-ignore
|
|
103
|
-
key: r,
|
|
104
|
-
padding: l.constants.RSA_PKCS1_PSS_PADDING,
|
|
105
|
-
saltLength: 32
|
|
106
|
-
}).toString("base64url"),
|
|
107
|
-
verify: (e, r, t) => {
|
|
108
|
-
try {
|
|
109
|
-
return d("RSA-SHA256").update(e).end().verify({
|
|
110
|
-
//@ts-ignore
|
|
111
|
-
key: r,
|
|
112
|
-
padding: l.constants.RSA_PKCS1_PSS_PADDING,
|
|
113
|
-
saltLength: 32
|
|
114
|
-
}, Buffer.from(t, "base64url"));
|
|
115
|
-
} catch {
|
|
116
|
-
return !1;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
PS384: {
|
|
121
|
-
sign: (e, r) => s("RSA-SHA384").update(e).end().sign({
|
|
122
|
-
//@ts-ignore
|
|
123
|
-
key: r,
|
|
124
|
-
padding: l.constants.RSA_PKCS1_PSS_PADDING,
|
|
125
|
-
saltLength: 48
|
|
126
|
-
}).toString("base64url"),
|
|
127
|
-
verify: (e, r, t) => {
|
|
128
|
-
try {
|
|
129
|
-
return d("RSA-SHA384").update(e).end().verify({
|
|
130
|
-
//@ts-ignore
|
|
131
|
-
key: r,
|
|
132
|
-
padding: l.constants.RSA_PKCS1_PSS_PADDING,
|
|
133
|
-
saltLength: 48
|
|
134
|
-
}, Buffer.from(t, "base64url"));
|
|
135
|
-
} catch {
|
|
136
|
-
return !1;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
PS512: {
|
|
141
|
-
sign: (e, r) => s("RSA-SHA512").update(e).end().sign({
|
|
142
|
-
//@ts-ignore
|
|
143
|
-
key: r,
|
|
144
|
-
padding: l.constants.RSA_PKCS1_PSS_PADDING,
|
|
145
|
-
saltLength: 64
|
|
146
|
-
}).toString("base64url"),
|
|
147
|
-
verify: (e, r, t) => {
|
|
148
|
-
try {
|
|
149
|
-
return d("RSA-SHA512").update(e).end().verify({
|
|
150
|
-
//@ts-ignore
|
|
151
|
-
key: r,
|
|
152
|
-
padding: l.constants.RSA_PKCS1_PSS_PADDING,
|
|
153
|
-
saltLength: 64
|
|
154
|
-
}, Buffer.from(t, "base64url"));
|
|
155
|
-
} catch {
|
|
156
|
-
return !1;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
EdDSA: {
|
|
161
|
-
sign: (e, r) => T(null, typeof e == "string" ? Buffer.from(e, "utf8") : e, r).toString("base64url"),
|
|
162
|
-
verify: (e, r, t) => {
|
|
163
|
-
try {
|
|
164
|
-
return E(
|
|
165
|
-
null,
|
|
166
|
-
typeof e == "string" ? Buffer.from(e, "utf8") : e,
|
|
167
|
-
r,
|
|
168
|
-
Buffer.from(t, "base64url")
|
|
169
|
-
);
|
|
170
|
-
} catch {
|
|
171
|
-
return !1;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}, P = Object.keys(g), I = (e) => {
|
|
176
|
-
const r = e.split(".");
|
|
177
|
-
if (r.length !== 3)
|
|
178
|
-
throw new Error('Invalid JWT: must contain exactly 3 parts separated by "."');
|
|
179
|
-
const [t, n, i] = r;
|
|
180
|
-
if (!t || !n || !i)
|
|
181
|
-
throw new Error("Invalid JWT: empty part detected");
|
|
182
|
-
try {
|
|
183
|
-
const a = JSON.parse(S.decode(t)), o = JSON.parse(S.decode(n));
|
|
184
|
-
return { header: a, payload: o, signature: i };
|
|
185
|
-
} catch (a) {
|
|
186
|
-
throw new Error(`Invalid JWT: malformed header or payload (${a.message})`);
|
|
187
|
-
}
|
|
188
|
-
}, k = (e, r, t = {}) => {
|
|
189
|
-
const n = t.alg ?? "HS256", i = t.typ ?? "JWT";
|
|
190
|
-
if (!(n in g))
|
|
191
|
-
throw new Error(`Unsupported algorithm: ${n}`);
|
|
192
|
-
const a = { alg: n, typ: i };
|
|
193
|
-
t.kid && (a.kid = t.kid);
|
|
194
|
-
const o = S.encode(JSON.stringify(a)), f = S.encode(JSON.stringify(e)), A = `${o}.${f}`, m = g[n].sign(A, r);
|
|
195
|
-
return `${o}.${f}.${m}`;
|
|
196
|
-
}, D = (e, r, t = {}) => {
|
|
197
|
-
let n;
|
|
198
|
-
try {
|
|
199
|
-
n = I(e);
|
|
200
|
-
} catch (u) {
|
|
201
|
-
return {
|
|
202
|
-
valid: !1,
|
|
203
|
-
error: {
|
|
204
|
-
reason: u.message,
|
|
205
|
-
code: "INVALID_TOKEN"
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
const { header: i, payload: a, signature: o } = n, f = i.alg;
|
|
210
|
-
if (!(f in g))
|
|
211
|
-
return {
|
|
212
|
-
valid: !1,
|
|
213
|
-
error: {
|
|
214
|
-
reason: `Unsupported or unknown algorithm: ${i.alg}`,
|
|
215
|
-
code: "INVALID_ALGORITHM"
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
if (t.algorithms && t.algorithms.length > 0 && !t.algorithms.includes(f))
|
|
219
|
-
return {
|
|
220
|
-
valid: !1,
|
|
221
|
-
error: {
|
|
222
|
-
reason: `Algorithm "${f}" is not in the allowed algorithms list`,
|
|
223
|
-
code: "ALGORITHM_NOT_ALLOWED"
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
if (i.typ !== void 0 && i.typ !== "JWT")
|
|
227
|
-
return {
|
|
228
|
-
valid: !1,
|
|
229
|
-
error: {
|
|
230
|
-
reason: `Invalid token type: expected 'JWT', got '${i.typ}'`,
|
|
231
|
-
code: "INVALID_TYPE"
|
|
232
|
-
}
|
|
233
|
-
};
|
|
234
|
-
const A = `${S.encode(JSON.stringify(i))}.${S.encode(JSON.stringify(a))}`;
|
|
235
|
-
if (!g[f].verify(A, r, o))
|
|
236
|
-
return {
|
|
237
|
-
valid: !1,
|
|
238
|
-
error: {
|
|
239
|
-
reason: "Signature verification failed",
|
|
240
|
-
code: "INVALID_SIGNATURE"
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
const y = Math.floor(Date.now() / 1e3), v = t.clockSkew ?? 0;
|
|
244
|
-
if (!t.ignoreExpiration && a.exp !== void 0 && y > a.exp + v)
|
|
245
|
-
return {
|
|
246
|
-
valid: !1,
|
|
247
|
-
error: {
|
|
248
|
-
reason: "Token expired",
|
|
249
|
-
code: "TOKEN_EXPIRED"
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
if (a.nbf !== void 0 && y + v < a.nbf)
|
|
253
|
-
return {
|
|
254
|
-
valid: !1,
|
|
255
|
-
error: {
|
|
256
|
-
reason: "Token not yet valid",
|
|
257
|
-
code: "TOKEN_NOT_ACTIVE"
|
|
258
|
-
}
|
|
259
|
-
};
|
|
260
|
-
if (a.iat !== void 0 && y + v < a.iat)
|
|
261
|
-
return {
|
|
262
|
-
valid: !1,
|
|
263
|
-
error: {
|
|
264
|
-
reason: "Token issued in the future",
|
|
265
|
-
code: "TOKEN_FUTURE_ISSUED"
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
if (t.maxTokenAge !== void 0 && a.iat !== void 0) {
|
|
269
|
-
const u = y - a.iat;
|
|
270
|
-
if (u > t.maxTokenAge)
|
|
271
|
-
return {
|
|
272
|
-
valid: !1,
|
|
273
|
-
error: {
|
|
274
|
-
reason: `Token age (${u}s) exceeds maximum allowed age (${t.maxTokenAge}s)`,
|
|
275
|
-
code: "TOKEN_TOO_OLD"
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
if (t.issuer !== void 0) {
|
|
280
|
-
if (a.iss === void 0)
|
|
281
|
-
return {
|
|
282
|
-
valid: !1,
|
|
283
|
-
error: {
|
|
284
|
-
reason: 'Token missing required issuer claim ("iss")',
|
|
285
|
-
code: "MISSING_ISSUER"
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
if (t.issuer !== a.iss)
|
|
289
|
-
return {
|
|
290
|
-
valid: !1,
|
|
291
|
-
error: {
|
|
292
|
-
reason: `Invalid token issuer: expected "${t.issuer}", got "${a.iss}"`,
|
|
293
|
-
code: "INVALID_ISSUER"
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
if (t.subject !== void 0) {
|
|
298
|
-
if (a.sub === void 0)
|
|
299
|
-
return {
|
|
300
|
-
valid: !1,
|
|
301
|
-
error: {
|
|
302
|
-
reason: 'Token missing required subject claim ("sub")',
|
|
303
|
-
code: "MISSING_SUBJECT"
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
if (t.subject !== a.sub)
|
|
307
|
-
return {
|
|
308
|
-
valid: !1,
|
|
309
|
-
error: {
|
|
310
|
-
reason: `Invalid token subject: expected "${t.subject}", got "${a.sub}"`,
|
|
311
|
-
code: "INVALID_SUBJECT"
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
if (t.audience !== void 0) {
|
|
316
|
-
const u = a.aud;
|
|
317
|
-
if (u === void 0)
|
|
318
|
-
return {
|
|
319
|
-
valid: !1,
|
|
320
|
-
error: {
|
|
321
|
-
reason: 'Token missing required audience claim ("aud")',
|
|
322
|
-
code: "MISSING_AUDIENCE"
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
const p = Array.isArray(t.audience) ? t.audience : [t.audience], b = Array.isArray(u) ? u : [u];
|
|
326
|
-
if (!p.some((_) => b.includes(_)))
|
|
327
|
-
return {
|
|
328
|
-
valid: !1,
|
|
329
|
-
error: {
|
|
330
|
-
reason: "Audience claim mismatch",
|
|
331
|
-
code: "INVALID_AUDIENCE"
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
if (t.jwtId !== void 0) {
|
|
336
|
-
if (a.jti === void 0)
|
|
337
|
-
return {
|
|
338
|
-
valid: !1,
|
|
339
|
-
error: {
|
|
340
|
-
reason: 'Token missing required JWT ID claim ("jti")',
|
|
341
|
-
code: "MISSING_JTI"
|
|
342
|
-
}
|
|
343
|
-
};
|
|
344
|
-
if (t.jwtId !== a.jti)
|
|
345
|
-
return {
|
|
346
|
-
valid: !1,
|
|
347
|
-
error: {
|
|
348
|
-
reason: `Invalid JWT ID: expected "${t.jwtId}", got "${a.jti}"`,
|
|
349
|
-
code: "INVALID_JTI"
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
return { valid: !0, header: i, payload: a, signature: o };
|
|
354
|
-
}, $ = {
|
|
355
|
-
sign: k,
|
|
356
|
-
verify: D,
|
|
357
|
-
decode: I,
|
|
358
|
-
algorithms: g
|
|
359
|
-
};
|
|
1
|
+
import { A as t, f as e, j as o, h as r, J as i, S as J, a as K, b as W, c as m, d as g, e as p, g as c, i as l, n as u, s as S, t as b, v as d } from "./index-DyXdSqEc.js";
|
|
360
2
|
export {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
3
|
+
t as AutodetectAlgorithm,
|
|
4
|
+
e as JWK,
|
|
5
|
+
o as JWKS,
|
|
6
|
+
r as JWKSToKeyObject,
|
|
7
|
+
i as JWT,
|
|
8
|
+
J as SignatureAlgorithm,
|
|
9
|
+
K as SupportedAlgorithms,
|
|
10
|
+
W as base64Url,
|
|
11
|
+
m as computeX5T,
|
|
12
|
+
g as decode,
|
|
13
|
+
p as exportJWK,
|
|
14
|
+
c as getJWKThumbprint,
|
|
15
|
+
l as importJWK,
|
|
16
|
+
u as normalizeJWKS,
|
|
17
|
+
S as sign,
|
|
18
|
+
b as toPublicJWK,
|
|
19
|
+
d as verify
|
|
368
20
|
};
|
|
369
21
|
//# sourceMappingURL=index.es.js.map
|