unjwt 0.6.1 → 0.7.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 +1 -2
- package/dist/THIRD-PARTY-LICENSES.md +63 -0
- package/dist/_chunks/_internal.mjs +88 -0
- package/dist/_chunks/index.d.mts +401 -41
- package/dist/_chunks/jwe.d.mts +82 -0
- package/dist/_chunks/jwe.mjs +407 -0
- package/dist/_chunks/{_crypto.mjs → jwk.mjs} +637 -26
- package/dist/_chunks/jws.d.mts +111 -0
- package/dist/_chunks/jws.mjs +264 -2031
- package/dist/_chunks/libs/rou3.mjs +5 -0
- package/dist/_chunks/libs/unsecure.d.mts +94 -0
- package/dist/_chunks/libs/unsecure.mjs +144 -0
- package/dist/_chunks/utils.mjs +222 -211
- package/dist/adapters/h3v1/index.d.mts +9 -9
- package/dist/adapters/h3v1/index.mjs +13 -75
- package/dist/adapters/h3v2/index.d.mts +9 -9
- package/dist/adapters/h3v2/index.mjs +14 -38
- package/dist/core/jwe.d.mts +3 -40
- package/dist/core/jwe.mjs +3 -161
- package/dist/core/jwk.d.mts +202 -46
- package/dist/core/jwk.mjs +3 -686
- package/dist/core/jws.d.mts +3 -31
- package/dist/core/jws.mjs +3 -104
- package/dist/core/utils/index.d.mts +74 -60
- package/dist/core/utils/index.mjs +3 -2
- package/dist/index.d.mts +6 -7
- package/dist/index.mjs +5 -5
- package/package.json +24 -22
- package/dist/_chunks/error.d.mts +0 -67
- package/dist/_chunks/jwk.d.mts +0 -708
- package/dist/core/_crypto/index.d.mts +0 -71
- package/dist/core/_crypto/index.mjs +0 -2
package/dist/core/jws.d.mts
CHANGED
|
@@ -1,31 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { i as
|
|
3
|
-
|
|
4
|
-
//#region src/core/jws.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Creates a JWS (JSON Web Signature) in Compact Serialization format.
|
|
7
|
-
*
|
|
8
|
-
* @param payload The payload to sign. Can be a string, Uint8Array, or an object (which will be JSON stringified).
|
|
9
|
-
* @param key The signing key (CryptoKey, JWK, or raw symmetric key as Uint8Array).
|
|
10
|
-
* @param options Signing options, including the algorithm (`alg`) and protected header parameters.
|
|
11
|
-
* @returns A Promise resolving to the JWS Compact Serialization string.
|
|
12
|
-
*/
|
|
13
|
-
declare function sign(payload: JOSEPayload, key: JWK_Symmetric | JWK_Private, options?: JWSSignOptions): Promise<string>;
|
|
14
|
-
declare function sign(payload: JOSEPayload, key: CryptoKey | Uint8Array<ArrayBuffer>, options: JWSSignOptions & {
|
|
15
|
-
alg: JWSAlgorithm;
|
|
16
|
-
}): Promise<string>;
|
|
17
|
-
/**
|
|
18
|
-
* Verifies a JWS (JSON Web Signature) in Compact Serialization format.
|
|
19
|
-
*
|
|
20
|
-
* @param jws The JWS Compact Serialization string.
|
|
21
|
-
* @param key The verification key (CryptoKey, JWK, JWKSet, raw symmetric key as Uint8Array, or a function resolving the key or set).
|
|
22
|
-
* @param options Verification options, such as allowed algorithms.
|
|
23
|
-
* @returns A Promise resolving to an object containing the verified payload and protected header.
|
|
24
|
-
* @throws If the JWS is invalid, signature verification fails, or options are not met.
|
|
25
|
-
*/
|
|
26
|
-
declare function verify<T extends JOSEPayload>(jws: string, key: CryptoKey | JWKSet | JWK_Public | JWK_Symmetric | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSVerifyOptions): Promise<JWSVerifyResult<T>>;
|
|
27
|
-
declare function verify(jws: string, key: CryptoKey | JWKSet | JWK_Public | JWK_Symmetric | Uint8Array<ArrayBuffer> | JWKLookupFunction, options: JWSVerifyOptions & {
|
|
28
|
-
forceUint8Array: true;
|
|
29
|
-
}): Promise<JWSVerifyResult<Uint8Array<ArrayBuffer>>>;
|
|
30
|
-
//#endregion
|
|
31
|
-
export { JWSAlgorithm, JWSHeaderParameters, JWSProtectedHeader, JWSSignOptions, JWSVerifyOptions, JWSVerifyResult, JWTError, type JWTErrorCauseMap, type JWTErrorCode, isJWTError, sign, verify };
|
|
1
|
+
import { A as JWSSignOptions, C as JWSMultiSignOptions, D as JWSMultiVerifyOutcome, E as JWSMultiVerifyOptions, F as JWTErrorCauseMap, I as JWTErrorCode, L as isJWTError, M as JWSVerifyResult, O as JWSMultiVerifyResult, P as JWTError, S as JWSHeaderParameters, T as JWSMultiVerifyAllOptions, b as JWSGeneralSerialization, j as JWSVerifyOptions, k as JWSProtectedHeader, v as JWSAlgorithm, w as JWSMultiSigner, x as JWSGeneralSignature, y as JWSFlattenedSerialization } from "../_chunks/index.mjs";
|
|
2
|
+
import { a as verifyMulti, i as signMulti, n as verify, o as verifyMultiAll, r as generalToFlattenedJWS, t as sign } from "../_chunks/jws.mjs";
|
|
3
|
+
export { JWSAlgorithm, JWSFlattenedSerialization, JWSGeneralSerialization, JWSGeneralSignature, JWSHeaderParameters, JWSMultiSignOptions, JWSMultiSigner, JWSMultiVerifyAllOptions, JWSMultiVerifyOptions, JWSMultiVerifyOutcome, JWSMultiVerifyResult, JWSProtectedHeader, JWSSignOptions, JWSVerifyOptions, JWSVerifyResult, JWTError, JWTErrorCauseMap, JWTErrorCode, generalToFlattenedJWS, isJWTError, sign, signMulti, verify, verifyMulti, verifyMultiAll };
|
package/dist/core/jws.mjs
CHANGED
|
@@ -1,104 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
//#region src/core/jws.ts
|
|
5
|
-
async function sign(payload, key, options = {}) {
|
|
6
|
-
const { protectedHeader: userHeader } = options;
|
|
7
|
-
let { alg } = options;
|
|
8
|
-
if (!alg) if (isJWK(key) && key.alg) alg = key.alg;
|
|
9
|
-
else throw new TypeError("JWS \"alg\" (Algorithm) must be provided in options");
|
|
10
|
-
if (alg === "none") throw new JWTError("\"none\" is not a valid signing algorithm", "ERR_JWS_ALG_NOT_ALLOWED");
|
|
11
|
-
const signingKey = await _resolveSigningKey(alg, await importKey(key, alg), "sign");
|
|
12
|
-
const protectedHeader = _buildJWSHeader(alg, key, userHeader, payload);
|
|
13
|
-
const computedPayload = computeJwtTimeClaims(payload, protectedHeader.typ, options.expiresIn, options.currentDate);
|
|
14
|
-
const protectedHeaderEncoded = base64UrlEncode(JSON.stringify(protectedHeader));
|
|
15
|
-
let payloadBytes;
|
|
16
|
-
if (payload instanceof Uint8Array) payloadBytes = payload;
|
|
17
|
-
else if (typeof payload === "string") payloadBytes = textEncoder.encode(payload);
|
|
18
|
-
else if (typeof payload === "object" && payload !== null) payloadBytes = textEncoder.encode(JSON.stringify(computedPayload || payload));
|
|
19
|
-
else throw new TypeError("Payload must be a string, Uint8Array, or a JSON-serializable object.");
|
|
20
|
-
const signingInputString = `${protectedHeaderEncoded}.${protectedHeader.b64 !== false ? base64UrlEncode(payloadBytes) : textDecoder.decode(payloadBytes)}`;
|
|
21
|
-
const signingInputBytes = textEncoder.encode(signingInputString);
|
|
22
|
-
return `${signingInputString}.${base64UrlEncode(await sign$1(alg, signingKey, signingInputBytes))}`;
|
|
23
|
-
}
|
|
24
|
-
async function verify(jws, key, options = {}) {
|
|
25
|
-
const parts = jws.split(".");
|
|
26
|
-
if (parts.length !== 3) throw new JWTError("Invalid JWS: Must contain three parts separated by dots.", "ERR_JWS_INVALID");
|
|
27
|
-
const [protectedHeaderEncoded, payloadEncoded, signatureEncoded] = parts;
|
|
28
|
-
let protectedHeader;
|
|
29
|
-
try {
|
|
30
|
-
const protectedHeaderString = base64UrlDecode(protectedHeaderEncoded);
|
|
31
|
-
protectedHeader = sanitizeObject(JSON.parse(protectedHeaderString));
|
|
32
|
-
} catch {
|
|
33
|
-
throw new JWTError("Invalid JWS: Protected header could not be decoded.", "ERR_JWS_INVALID");
|
|
34
|
-
}
|
|
35
|
-
if (!protectedHeader || typeof protectedHeader !== "object" || !protectedHeader.alg) throw new JWTError("Invalid JWS: Protected header must be an object with an \"alg\" property.", "ERR_JWS_INVALID");
|
|
36
|
-
const alg = protectedHeader.alg;
|
|
37
|
-
if (options.algorithms && !options.algorithms.includes(alg)) throw new JWTError(`Algorithm not allowed: ${alg}`, "ERR_JWS_ALG_NOT_ALLOWED");
|
|
38
|
-
if (options.typ && protectedHeader.typ !== options.typ) throw new JWTError(`Invalid JWS: "typ" (Type) Header Parameter mismatch. Expected "${options.typ}", got "${protectedHeader.typ}".`, "ERR_JWS_INVALID");
|
|
39
|
-
let signatureBytes;
|
|
40
|
-
try {
|
|
41
|
-
signatureBytes = base64UrlDecode(signatureEncoded, false);
|
|
42
|
-
} catch {
|
|
43
|
-
throw new JWTError("Invalid JWS: Signature could not be decoded.", "ERR_JWS_INVALID");
|
|
44
|
-
}
|
|
45
|
-
const keyInput = typeof key === "function" ? await key(protectedHeader, jws) : key;
|
|
46
|
-
const signingInputBytes = textEncoder.encode(`${protectedHeaderEncoded}.${payloadEncoded}`);
|
|
47
|
-
if (isJWKSet(keyInput)) {
|
|
48
|
-
const candidates = getJWKsFromSet(keyInput, _buildJWSSetFilter(protectedHeader));
|
|
49
|
-
if (candidates.length === 0) throw new JWTError(`No key found in JWK Set${protectedHeader.kid ? ` with kid "${protectedHeader.kid}"` : ""}.`, "ERR_JWK_KEY_NOT_FOUND");
|
|
50
|
-
let verified = false;
|
|
51
|
-
for (const candidate of candidates) try {
|
|
52
|
-
if (await verify$1(alg, await _resolveSigningKey(alg, await importKey(candidate, alg), "verify"), signatureBytes, signingInputBytes)) {
|
|
53
|
-
verified = true;
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
} catch {}
|
|
57
|
-
if (!verified) throw new JWTError("JWS signature verification failed.", "ERR_JWS_SIGNATURE_INVALID");
|
|
58
|
-
} else if (!await verify$1(alg, await _resolveSigningKey(alg, await importKey(keyInput, alg), "verify"), signatureBytes, signingInputBytes)) throw new JWTError("JWS signature verification failed.", "ERR_JWS_SIGNATURE_INVALID");
|
|
59
|
-
const useB64 = protectedHeader.b64 !== false;
|
|
60
|
-
let payload;
|
|
61
|
-
try {
|
|
62
|
-
payload = useB64 ? decodePayloadFromB64UrlSegment(payloadEncoded, protectedHeader, options.forceUint8Array) : options.forceUint8Array ? textEncoder.encode(payloadEncoded) : payloadEncoded;
|
|
63
|
-
} catch (error_) {
|
|
64
|
-
throw new JWTError(`Invalid JWS: Payload decoding failed (${error_ instanceof Error ? error_.message : error_})`, "ERR_JWS_INVALID");
|
|
65
|
-
}
|
|
66
|
-
validateCriticalHeadersJWS(protectedHeader, options.recognizedHeaders);
|
|
67
|
-
if (payload && typeof payload === "object" && options.validateClaims !== false && (options.validateClaims === true || protectedHeader.typ?.toLowerCase().includes("jwt")) && !options.forceUint8Array && !(payload instanceof Uint8Array)) validateJwtClaims(payload, options);
|
|
68
|
-
return {
|
|
69
|
-
payload,
|
|
70
|
-
protectedHeader
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
async function _resolveSigningKey(alg, key, usage) {
|
|
74
|
-
if (key instanceof Uint8Array) {
|
|
75
|
-
const minBytes = Number.parseInt(alg.slice(2), 10) / 8;
|
|
76
|
-
if (key.length < minBytes) throw new JWTError(`${alg} requires a key of at least ${minBytes} bytes`, "ERR_JWK_INVALID");
|
|
77
|
-
return crypto.subtle.importKey("raw", key, {
|
|
78
|
-
name: "HMAC",
|
|
79
|
-
hash: `SHA-${alg.slice(-3)}`
|
|
80
|
-
}, false, [usage]);
|
|
81
|
-
}
|
|
82
|
-
if (alg.startsWith("RS") || alg.startsWith("PS")) {
|
|
83
|
-
const { modulusLength } = key.algorithm;
|
|
84
|
-
if (typeof modulusLength !== "number" || modulusLength < 2048) throw new JWTError(`${alg} requires a key modulusLength of at least 2048 bits`, "ERR_JWK_INVALID");
|
|
85
|
-
}
|
|
86
|
-
return key;
|
|
87
|
-
}
|
|
88
|
-
function _buildJWSSetFilter(header) {
|
|
89
|
-
const { kid, alg } = header;
|
|
90
|
-
return (k) => (!kid || k.kid === kid) && (!k.alg || k.alg === alg);
|
|
91
|
-
}
|
|
92
|
-
function _buildJWSHeader(alg, key, userHeader, payload) {
|
|
93
|
-
const safeHeader = sanitizeObject(userHeader);
|
|
94
|
-
const header = {
|
|
95
|
-
...isJWK(key) && key.kid ? { kid: key.kid } : {},
|
|
96
|
-
...safeHeader,
|
|
97
|
-
alg
|
|
98
|
-
};
|
|
99
|
-
applyTypCtyDefaults(header, payload);
|
|
100
|
-
if (header.b64 === true) delete header.b64;
|
|
101
|
-
return header;
|
|
102
|
-
}
|
|
103
|
-
//#endregion
|
|
104
|
-
export { JWTError, isJWTError, sign, verify };
|
|
1
|
+
import { D as JWTError, O as isJWTError } from "../_chunks/utils.mjs";
|
|
2
|
+
import { a as verifyMulti, i as signMulti, n as verify, o as verifyMultiAll, r as generalToFlattenedJWS, t as sign } from "../_chunks/jws.mjs";
|
|
3
|
+
export { JWTError, generalToFlattenedJWS, isJWTError, sign, signMulti, verify, verifyMulti, verifyMultiAll };
|
|
@@ -1,6 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { B as JWTClaimValidationOptions, Ft as KeyManagementAlgorithm, N as StrictOmit, Pt as JWK_oct, Q as JWK, Tt as JWK_Public, V as JWTClaims, at as JWKSet, n as ExpiresIn, t as Duration, v as JWSAlgorithm, wt as JWK_Private } from "../../_chunks/index.mjs";
|
|
2
|
+
import { a as textDecoder, c as sanitizeObject, i as base64UrlEncode, l as sanitizeObjectCopy, n as base64Encode, o as textEncoder, r as base64UrlDecode, s as safeJsonParse, t as base64Decode, u as secureRandomBytes } from "../../_chunks/libs/unsecure.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Infer the set of JWS signing algorithms a key unambiguously supports.
|
|
5
|
+
* Returns `undefined` when inference is not possible — callers must pass
|
|
6
|
+
* `options.algorithms` explicitly (raw bytes, ambiguous JWK, lookup functions, ...).
|
|
7
|
+
*/
|
|
8
|
+
declare function inferJWSAllowedAlgorithms(key: unknown): JWSAlgorithm[] | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Infer the set of JWE key-management algorithms a key unambiguously supports.
|
|
11
|
+
* Returns `undefined` when inference is not possible — callers must pass
|
|
12
|
+
* `options.algorithms` explicitly.
|
|
13
|
+
*
|
|
14
|
+
* Passwords (`string` / `Uint8Array`) infer to the three PBES2 variants.
|
|
15
|
+
*/
|
|
16
|
+
declare function inferJWEAllowedAlgorithms(key: unknown): KeyManagementAlgorithm[] | undefined;
|
|
4
17
|
/**
|
|
5
18
|
* Apply default typ/cty semantics shared by JWS & JWE.
|
|
6
19
|
* - If typ is undefined and payload is a non-Uint8Array object, set typ="JWT".
|
|
@@ -15,33 +28,57 @@ declare function isJWTContent(header: {
|
|
|
15
28
|
typ?: string;
|
|
16
29
|
cty?: string;
|
|
17
30
|
} | undefined): boolean;
|
|
31
|
+
/** Parse a decoded string as JSON when it looks like a JSON object or array, otherwise return the string verbatim. */
|
|
32
|
+
declare function decodeMaybeJWTString<T = unknown>(decodedString: string): T | string;
|
|
33
|
+
/** Decode raw payload bytes honoring `forceUint8Array`. */
|
|
34
|
+
declare function decodePayloadFromBytes<T = unknown>(bytes: Uint8Array<ArrayBuffer>, forceUint8Array?: boolean): T | Uint8Array<ArrayBuffer> | string;
|
|
35
|
+
/** Decode a Base64URL-encoded payload segment honoring `forceUint8Array`. */
|
|
36
|
+
declare function decodePayloadFromB64UrlSegment<T = unknown>(payloadEncoded: string, forceUint8Array?: boolean): T | Uint8Array<ArrayBuffer> | string;
|
|
37
|
+
/** Convert plaintext input to bytes, shared by JWS & JWE when preparing payload. */
|
|
38
|
+
declare function getPlaintextBytes(payload: string | Uint8Array<ArrayBuffer> | Record<string, unknown>): Uint8Array<ArrayBuffer>;
|
|
18
39
|
/**
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Decode a payload that is represented as raw bytes into T | string, honoring forceUint8Array and headers.
|
|
40
|
+
* Parse a {@link Duration} (integer seconds or shorthand string like `"10m"`)
|
|
41
|
+
* into a positive integer number of seconds. Used for `expiresIn`-style inputs
|
|
42
|
+
* where `0` / negatives are invalid.
|
|
43
|
+
*
|
|
44
|
+
* For inputs that legitimately permit `0` (e.g. `notBeforeIn: 0`), compute the
|
|
45
|
+
* bounds at the call site instead.
|
|
27
46
|
*/
|
|
28
|
-
declare function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
47
|
+
declare function computeDurationInSeconds(duration: Duration): number;
|
|
48
|
+
/** @deprecated Renamed to {@link computeDurationInSeconds} in v0.7.0. */
|
|
49
|
+
declare const computeExpiresInSeconds: (expiresIn: ExpiresIn) => number;
|
|
50
|
+
/** @deprecated Renamed to {@link computeDurationInSeconds} in v0.7.0. */
|
|
51
|
+
declare const computeMaxTokenAgeSeconds: (expiresIn: ExpiresIn) => number;
|
|
52
|
+
/** Options for {@link computeJwtTimeClaims}. */
|
|
53
|
+
interface ComputeJwtTimeClaimsOptions {
|
|
54
|
+
/** Reference moment for `iat` (and for `expiresIn` / `notBeforeIn` math). Defaults to `new Date()`. */
|
|
55
|
+
currentDate?: Date;
|
|
56
|
+
/** Duration until `exp`, relative to `currentDate`. Mutually exclusive with `expiresAt`. */
|
|
57
|
+
expiresIn?: ExpiresIn;
|
|
58
|
+
/** Absolute moment for `exp`. Mutually exclusive with `expiresIn`. */
|
|
59
|
+
expiresAt?: Date;
|
|
60
|
+
/**
|
|
61
|
+
* Duration from `iat` before which the JWT must not be accepted.
|
|
62
|
+
* `0` is allowed and means `nbf = iat` (explicit temporal floor at sign time).
|
|
63
|
+
* Mutually exclusive with `notBeforeAt`.
|
|
64
|
+
*/
|
|
65
|
+
notBeforeIn?: ExpiresIn;
|
|
66
|
+
/** Absolute moment for `nbf` (RFC 7519 §4.1.5). Mutually exclusive with `notBeforeIn`. */
|
|
67
|
+
notBeforeAt?: Date;
|
|
68
|
+
}
|
|
32
69
|
/**
|
|
33
|
-
*
|
|
70
|
+
* Compute `iat` + optional `exp` / `nbf` for any JSON-object payload.
|
|
71
|
+
*
|
|
72
|
+
* Returns a shallow-cloned payload with the time claims added, or `undefined`
|
|
73
|
+
* when no hint applies (no expiry/nbf provided, non-object payload, or the
|
|
74
|
+
* payload already carries the corresponding claim).
|
|
75
|
+
*
|
|
76
|
+
* `expiresIn` / `expiresAt` and `notBeforeIn` / `notBeforeAt` are each mutually
|
|
77
|
+
* exclusive pairs — passing both within a pair throws `ERR_JWT_CLAIM_INVALID`.
|
|
78
|
+
* Invalid `Date` inputs (NaN `.getTime()`) and negative `notBeforeIn` durations
|
|
79
|
+
* also throw.
|
|
34
80
|
*/
|
|
35
|
-
declare function
|
|
36
|
-
typ?: string;
|
|
37
|
-
cty?: string;
|
|
38
|
-
} | undefined, forceUint8Array?: boolean): T | Uint8Array<ArrayBuffer> | string;
|
|
39
|
-
/** Convert plaintext input to bytes, shared by JWS & JWE when preparing payload. */
|
|
40
|
-
declare function getPlaintextBytes(payload: string | Uint8Array<ArrayBuffer> | Record<string, unknown>): Uint8Array<ArrayBuffer>;
|
|
41
|
-
declare function computeExpiresInSeconds(expiresIn: ExpiresIn): number;
|
|
42
|
-
declare const computeMaxTokenAgeSeconds: (expiresIn: ExpiresIn) => number;
|
|
43
|
-
/** Optionally compute iat/exp when signing JWTs. */
|
|
44
|
-
declare function computeJwtTimeClaims(payload: unknown, headerTyp: string | undefined, expiresIn?: ExpiresIn, currentDate?: Date): JWTClaims | undefined;
|
|
81
|
+
declare function computeJwtTimeClaims(payload: unknown, options?: ComputeJwtTimeClaimsOptions): JWTClaims | undefined;
|
|
45
82
|
/** Validate JWT claims under JWS verification rules (or custom JWE implementations). */
|
|
46
83
|
declare function validateJwtClaims(jwtClaims: JWTClaims, options?: JWTClaimValidationOptions): void;
|
|
47
84
|
/** Validate critical headers in JWS semantics. */
|
|
@@ -52,44 +89,22 @@ declare function validateCriticalHeadersJWS(protectedHeader: {
|
|
|
52
89
|
declare function validateCriticalHeadersJWE(protectedHeader: {
|
|
53
90
|
crit?: string[];
|
|
54
91
|
} & Record<string, any>, recognizedHeaders?: string[]): void;
|
|
55
|
-
|
|
56
|
-
//#region src/core/utils/sanitize.d.ts
|
|
57
|
-
/**
|
|
58
|
-
* Originally derived from unsecure@0.0.4
|
|
59
|
-
* Licensed under the MIT License.
|
|
60
|
-
*
|
|
61
|
-
* Source: https://github.com/sandros94/unsecure/blob/913ed5580c551153a19154b4916c67a8efb2be85/src/sanitize.ts
|
|
62
|
-
*/
|
|
63
|
-
/**
|
|
64
|
-
* Returns a deep structural copy of `obj` with prototype-pollution vectors
|
|
65
|
-
* stripped at every level (`__proto__`, `prototype`, `constructor`).
|
|
66
|
-
*
|
|
67
|
-
* The input is never modified. Arrays are copied element-by-element;
|
|
68
|
-
* nested objects are recursed with a WeakSet cycle guard.
|
|
69
|
-
*/
|
|
70
|
-
declare function sanitizeObject<T extends Record<string, unknown> | undefined>(obj: T): T;
|
|
71
|
-
//#endregion
|
|
72
|
-
//#region src/core/utils/index.d.ts
|
|
73
|
-
declare const textEncoder: TextEncoder;
|
|
74
|
-
declare const textDecoder: TextDecoder;
|
|
75
|
-
declare function base64Encode(data: Uint8Array<ArrayBuffer> | string): string;
|
|
76
|
-
declare function base64UrlEncode(data: Uint8Array<ArrayBuffer> | string): string;
|
|
77
|
-
declare function base64Decode(str: string | undefined): string;
|
|
78
|
-
declare function base64Decode<T extends boolean | undefined>(str?: string | undefined, toString?: T): T extends false ? Uint8Array<ArrayBuffer> : string;
|
|
79
|
-
declare function base64UrlDecode(str: string | undefined): string;
|
|
80
|
-
declare function base64UrlDecode<T extends boolean | undefined>(str?: string | undefined, toString?: T): T extends false ? Uint8Array<ArrayBuffer> : string;
|
|
81
|
-
declare function randomBytes(length: Readonly<number>): Uint8Array<ArrayBuffer>;
|
|
82
|
-
/**
|
|
83
|
-
* Concatenates multiple Uint8Arrays
|
|
84
|
-
* @param arrays Arrays to concatenate
|
|
85
|
-
* @returns Concatenated array
|
|
86
|
-
*/
|
|
92
|
+
/** Concatenate multiple `Uint8Array`s into a single contiguous buffer. */
|
|
87
93
|
declare function concatUint8Arrays(...arrays: Readonly<Uint8Array<ArrayBuffer>[]>): Uint8Array<ArrayBuffer>;
|
|
94
|
+
/** Wrap a value in an array; returns the array unchanged if already an array. */
|
|
88
95
|
declare function maybeArray<T>(item: T | T[]): T[];
|
|
96
|
+
/** Type guard for {@link JWK} — checks for an object with a string `kty`. */
|
|
89
97
|
declare function isJWK(key: any): key is JWK;
|
|
98
|
+
/** Type guard for {@link JWKSet} — checks for an object with a `keys` array. */
|
|
90
99
|
declare function isJWKSet(key: any): key is JWKSet;
|
|
100
|
+
/** Asserts that `key` is a `CryptoKey`; throws `TypeError` otherwise. */
|
|
91
101
|
declare function assertCryptoKey(key: unknown): asserts key is CryptoKey;
|
|
102
|
+
/**
|
|
103
|
+
* Type guard for `CryptoKey`. Uses `Symbol.toStringTag` instead of `instanceof`
|
|
104
|
+
* so it works across worker / realm boundaries where `CryptoKey` identities differ.
|
|
105
|
+
*/
|
|
92
106
|
declare function isCryptoKey(key: unknown): key is CryptoKey;
|
|
107
|
+
/** Type guard for `CryptoKeyPair` — both `publicKey` and `privateKey` must be CryptoKeys. */
|
|
93
108
|
declare const isCryptoKeyPair: (key: any) => key is CryptoKeyPair;
|
|
94
109
|
/** Returns true if the JWK is a symmetric (oct) key. */
|
|
95
110
|
declare function isSymmetricJWK(key: unknown): key is Extract<JWK, JWK_oct>;
|
|
@@ -105,5 +120,4 @@ declare function isPrivateJWK(key: unknown): key is JWK_Private;
|
|
|
105
120
|
* This checks for the presence of public components and absence of private material.
|
|
106
121
|
*/
|
|
107
122
|
declare function isPublicJWK(key: unknown): key is JWK_Public;
|
|
108
|
-
|
|
109
|
-
export { StrictOmit, applyTypCtyDefaults, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, computeExpiresInSeconds, computeJwtTimeClaims, computeMaxTokenAgeSeconds, concatUint8Arrays, decodeMaybeJWTString, decodePayloadFromB64UrlSegment, decodePayloadFromBytes, getPlaintextBytes, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTContent, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, randomBytes, sanitizeObject, textDecoder, textEncoder, validateCriticalHeadersJWE, validateCriticalHeadersJWS, validateJwtClaims };
|
|
123
|
+
export { ComputeJwtTimeClaimsOptions, StrictOmit, applyTypCtyDefaults, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, computeDurationInSeconds, computeExpiresInSeconds, computeJwtTimeClaims, computeMaxTokenAgeSeconds, concatUint8Arrays, decodeMaybeJWTString, decodePayloadFromB64UrlSegment, decodePayloadFromBytes, getPlaintextBytes, inferJWEAllowedAlgorithms, inferJWSAllowedAlgorithms, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTContent, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, safeJsonParse, sanitizeObject, sanitizeObjectCopy, secureRandomBytes, textDecoder, textEncoder, validateCriticalHeadersJWE, validateCriticalHeadersJWS, validateJwtClaims };
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { C as validateCriticalHeadersJWS, E as inferJWSAllowedAlgorithms, S as validateCriticalHeadersJWE, T as inferJWEAllowedAlgorithms, _ as decodeMaybeJWTString, a as isCryptoKeyPair, b as getPlaintextBytes, c as isPrivateJWK, d as maybeArray, f as applyTypCtyDefaults, g as computeMaxTokenAgeSeconds, h as computeJwtTimeClaims, i as isCryptoKey, l as isPublicJWK, m as computeExpiresInSeconds, n as concatUint8Arrays, o as isJWK, p as computeDurationInSeconds, r as isAsymmetricJWK, s as isJWKSet, t as assertCryptoKey, u as isSymmetricJWK, v as decodePayloadFromB64UrlSegment, w as validateJwtClaims, x as isJWTContent, y as decodePayloadFromBytes } from "../../_chunks/utils.mjs";
|
|
2
|
+
import { a as base64UrlEncode, c as safeJsonParse, i as base64UrlDecode, l as sanitizeObject, n as base64Decode, o as textDecoder, r as base64Encode, s as textEncoder, t as secureRandomBytes, u as sanitizeObjectCopy } from "../../_chunks/libs/unsecure.mjs";
|
|
3
|
+
export { applyTypCtyDefaults, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, computeDurationInSeconds, computeExpiresInSeconds, computeJwtTimeClaims, computeMaxTokenAgeSeconds, concatUint8Arrays, decodeMaybeJWTString, decodePayloadFromB64UrlSegment, decodePayloadFromBytes, getPlaintextBytes, inferJWEAllowedAlgorithms, inferJWSAllowedAlgorithms, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTContent, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, safeJsonParse, sanitizeObject, sanitizeObjectCopy, secureRandomBytes, textDecoder, textEncoder, validateCriticalHeadersJWE, validateCriticalHeadersJWS, validateJwtClaims };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { $ as
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
export { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, ExpiresIn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, JOSEPayload, JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEHeaderParameters, JWEKeyManagementHeaderParameters, JWEProtectedHeader, JWK, JWKAlgorithm, JWKCacheAdapter, JWKLookupFunction, JWKLookupFunctionHeader, JWKPEMAlgorithm, JWKParameters, JWKSet, JWK_AES_CBC_HMAC, JWK_AES_GCM, JWK_AES_GCM_KW, JWK_AES_KW, JWK_Asymmetric, JWK_Asymmetric_Algorithm, JWK_EC, JWK_ECDH_ES, JWK_ECDSA, JWK_EC_Private, JWK_EC_Public, JWK_HMAC, JWK_OKP, JWK_OKP_Private, JWK_OKP_Public, JWK_OKP_SIGN, JWK_PBES2, JWK_Pair, JWK_Private, JWK_Public, JWK_RSA, JWK_RSA_ENC, JWK_RSA_PSS, JWK_RSA_Private, JWK_RSA_Public, JWK_RSA_SIGN, JWK_Symmetric, JWK_Symmetric_Algorithm, JWK_oct, JWSAlgorithm, JWSHeaderParameters, JWSProtectedHeader, JWSSignOptions, JWSVerifyOptions, JWSVerifyResult, JWT, JWTClaimValidationOptions, JWTClaims, JWTError, type JWTErrorCauseMap, type JWTErrorCode, JoseHeaderParameters, KeyManagementAlgorithm, MaxTokenAge, UnwrapKeyOptions, WeakMapJWKCache, WrapKeyOptions, WrapKeyResult, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, clearJWKCache, computeExpiresInSeconds, computeMaxTokenAgeSeconds, concatUint8Arrays, configureJWKCache, decrypt, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, encrypt, exportKey, exportToPEM, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTError, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, randomBytes, sanitizeObject, sign, textDecoder, textEncoder, unwrapKey, validateJwtClaims, verify, wrapKey };
|
|
1
|
+
import { $ as JWKAlgorithm, A as JWSSignOptions, At as JWK_RSA_Public, B as JWTClaimValidationOptions, C as JWSMultiSignOptions, Ct as JWK_Pair, D as JWSMultiVerifyOutcome, Dt as JWK_RSA_ENC, E as JWSMultiVerifyOptions, Et as JWK_RSA, F as JWTErrorCauseMap, Ft as KeyManagementAlgorithm, G as DeriveKeyOptions, H as JoseHeaderParameters, I as JWTErrorCode, It as UnwrapKeyOptions, J as GenerateJWKReturn, K as DeriveKeyReturn, L as isJWTError, Lt as WrapKeyOptions, M as JWSVerifyResult, Mt as JWK_Symmetric, Nt as JWK_Symmetric_Algorithm, O as JWSMultiVerifyResult, Ot as JWK_RSA_PSS, P as JWTError, Pt as JWK_oct, Q as JWK, R as JOSEPayload, Rt as WrapKeyResult, S as JWSHeaderParameters, St as JWK_PBES2, T as JWSMultiVerifyAllOptions, Tt as JWK_Public, U as CompositeKey, V as JWTClaims, W as ContentEncryptionAlgorithm, X as GenerateKeyOptions, Y as GenerateKeyAlgorithm, Z as GenerateKeyReturn, _ as JWEProtectedHeader, _t as JWK_HMAC, a as JWEDecryptOptions, at as JWKSet, b as JWSGeneralSerialization, bt as JWK_OKP_Public, c as JWEFlattenedSerialization, ct as JWK_AES_GCM_KW, d as JWEHeaderParameters, dt as JWK_Asymmetric_Algorithm, et as JWKCacheAdapter, f as JWEKeyManagementHeaderParameters, ft as JWK_EC, g as JWEMultiRecipient, gt as JWK_EC_Public, h as JWEMultiEncryptOptions, ht as JWK_EC_Private, i as NotBeforeIn, it as JWKParameters, j as JWSVerifyOptions, jt as JWK_RSA_SIGN, k as JWSProtectedHeader, kt as JWK_RSA_Private, l as JWEGeneralRecipient, lt as JWK_AES_KW, m as JWEMultiDecryptResult, mt as JWK_ECDSA, n as ExpiresIn, nt as JWKLookupFunctionHeader, o as JWEDecryptResult, ot as JWK_AES_CBC_HMAC, p as JWEMultiDecryptOptions, pt as JWK_ECDH_ES, q as GenerateJWKOptions, r as MaxTokenAge, rt as JWKPEMAlgorithm, s as JWEEncryptOptions, st as JWK_AES_GCM, t as Duration, tt as JWKLookupFunction, u as JWEGeneralSerialization, ut as JWK_Asymmetric, v as JWSAlgorithm, vt as JWK_OKP, w as JWSMultiSigner, wt as JWK_Private, x as JWSGeneralSignature, xt as JWK_OKP_SIGN, y as JWSFlattenedSerialization, yt as JWK_OKP_Private, z as JWT } from "./_chunks/index.mjs";
|
|
2
|
+
import { WeakMapJWKCache, clearJWKCache, configureJWKCache, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, exportKey, exportPEM, exportToPEM, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, importPEM, unwrapKey, wrapKey } from "./core/jwk.mjs";
|
|
3
|
+
import { a as generalToFlattened, i as encryptMulti, n as encrypt, r as decryptMulti, t as decrypt } from "./_chunks/jwe.mjs";
|
|
4
|
+
import { a as verifyMulti, i as signMulti, n as verify, o as verifyMultiAll, r as generalToFlattenedJWS, t as sign } from "./_chunks/jws.mjs";
|
|
5
|
+
import { assertCryptoKey, computeDurationInSeconds, computeExpiresInSeconds, computeMaxTokenAgeSeconds, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isPrivateJWK, isPublicJWK, isSymmetricJWK, validateJwtClaims } from "./core/utils/index.mjs";
|
|
6
|
+
export { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, Duration, ExpiresIn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, JOSEPayload, JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEFlattenedSerialization, JWEGeneralRecipient, JWEGeneralSerialization, JWEHeaderParameters, JWEKeyManagementHeaderParameters, JWEMultiDecryptOptions, JWEMultiDecryptResult, JWEMultiEncryptOptions, JWEMultiRecipient, JWEProtectedHeader, JWK, JWKAlgorithm, JWKCacheAdapter, JWKLookupFunction, JWKLookupFunctionHeader, JWKPEMAlgorithm, JWKParameters, JWKSet, JWK_AES_CBC_HMAC, JWK_AES_GCM, JWK_AES_GCM_KW, JWK_AES_KW, JWK_Asymmetric, JWK_Asymmetric_Algorithm, JWK_EC, JWK_ECDH_ES, JWK_ECDSA, JWK_EC_Private, JWK_EC_Public, JWK_HMAC, JWK_OKP, JWK_OKP_Private, JWK_OKP_Public, JWK_OKP_SIGN, JWK_PBES2, JWK_Pair, JWK_Private, JWK_Public, JWK_RSA, JWK_RSA_ENC, JWK_RSA_PSS, JWK_RSA_Private, JWK_RSA_Public, JWK_RSA_SIGN, JWK_Symmetric, JWK_Symmetric_Algorithm, JWK_oct, JWSAlgorithm, JWSFlattenedSerialization, JWSGeneralSerialization, JWSGeneralSignature, JWSHeaderParameters, JWSMultiSignOptions, JWSMultiSigner, JWSMultiVerifyAllOptions, JWSMultiVerifyOptions, JWSMultiVerifyOutcome, JWSMultiVerifyResult, JWSProtectedHeader, JWSSignOptions, JWSVerifyOptions, JWSVerifyResult, JWT, JWTClaimValidationOptions, JWTClaims, JWTError, type JWTErrorCauseMap, type JWTErrorCode, JoseHeaderParameters, KeyManagementAlgorithm, MaxTokenAge, NotBeforeIn, UnwrapKeyOptions, WeakMapJWKCache, WrapKeyOptions, WrapKeyResult, assertCryptoKey, clearJWKCache, computeDurationInSeconds, computeExpiresInSeconds, computeMaxTokenAgeSeconds, configureJWKCache, decrypt, decryptMulti, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, encrypt, encryptMulti, exportKey, exportPEM, exportToPEM, generalToFlattened, generalToFlattenedJWS, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, importPEM, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTError, isPrivateJWK, isPublicJWK, isSymmetricJWK, sign, signMulti, unwrapKey, validateJwtClaims, verify, verifyMulti, verifyMultiAll, wrapKey };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
export { JWTError, WeakMapJWKCache, assertCryptoKey,
|
|
1
|
+
import { D as JWTError, O as isJWTError, a as isCryptoKeyPair, c as isPrivateJWK, g as computeMaxTokenAgeSeconds, i as isCryptoKey, l as isPublicJWK, m as computeExpiresInSeconds, o as isJWK, p as computeDurationInSeconds, r as isAsymmetricJWK, s as isJWKSet, t as assertCryptoKey, u as isSymmetricJWK, w as validateJwtClaims } from "./_chunks/utils.mjs";
|
|
2
|
+
import { _ as unwrapKey, a as deriveKeyFromPassword, c as exportPEM, d as generateKey, f as getJWKFromSet, g as importPEM, h as importKey, i as deriveJWKFromPassword, l as exportToPEM, m as importFromPEM, n as clearJWKCache, o as deriveSharedSecret, p as getJWKsFromSet, r as configureJWKCache, s as exportKey, t as WeakMapJWKCache, u as generateJWK, v as wrapKey } from "./_chunks/jwk.mjs";
|
|
3
|
+
import { a as verifyMulti, i as signMulti, n as verify, o as verifyMultiAll, r as generalToFlattenedJWS, t as sign } from "./_chunks/jws.mjs";
|
|
4
|
+
import { a as generalToFlattened, i as encryptMulti, n as encrypt, r as decryptMulti, t as decrypt } from "./_chunks/jwe.mjs";
|
|
5
|
+
export { JWTError, WeakMapJWKCache, assertCryptoKey, clearJWKCache, computeDurationInSeconds, computeExpiresInSeconds, computeMaxTokenAgeSeconds, configureJWKCache, decrypt, decryptMulti, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, encrypt, encryptMulti, exportKey, exportPEM, exportToPEM, generalToFlattened, generalToFlattenedJWS, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, importPEM, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTError, isPrivateJWK, isPublicJWK, isSymmetricJWK, sign, signMulti, unwrapKey, validateJwtClaims, verify, verifyMulti, verifyMultiAll, wrapKey };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unjwt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Low-level JWT utilities",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -55,30 +55,31 @@
|
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@types/node": "^24",
|
|
58
|
-
"@types/supertest": "^
|
|
59
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
60
|
-
"@vitest/coverage-v8": "^4.
|
|
58
|
+
"@types/supertest": "^7.2.0",
|
|
59
|
+
"@typescript/native-preview": "7.0.0-dev.20260419.1",
|
|
60
|
+
"@vitest/coverage-v8": "^4.1.4",
|
|
61
61
|
"automd": "^0.4.3",
|
|
62
62
|
"changelogen": "^0.6.2",
|
|
63
|
-
"cookie-esv1": "npm:cookie-es@^1.2.
|
|
64
|
-
"cookie-
|
|
65
|
-
"h3v1": "npm:h3@^1.15.
|
|
66
|
-
"h3v2": "npm:h3@^2.0.1-rc.
|
|
67
|
-
"jose": "^6.2.
|
|
68
|
-
"nano-staged": "^0.
|
|
69
|
-
"obuild": "^0.4.
|
|
70
|
-
"oxfmt": "^0.
|
|
71
|
-
"oxlint": "^1.
|
|
72
|
-
"rolldown": "1.0.0-rc.
|
|
63
|
+
"cookie-esv1": "npm:cookie-es@^1.2.3",
|
|
64
|
+
"cookie-esv3": "npm:cookie-es@^3.1.1",
|
|
65
|
+
"h3v1": "npm:h3@^1.15.11",
|
|
66
|
+
"h3v2": "npm:h3@^2.0.1-rc.20",
|
|
67
|
+
"jose": "^6.2.2",
|
|
68
|
+
"nano-staged": "^1.0.2",
|
|
69
|
+
"obuild": "^0.4.33",
|
|
70
|
+
"oxfmt": "^0.45.0",
|
|
71
|
+
"oxlint": "^1.60.0",
|
|
72
|
+
"rolldown": "1.0.0-rc.16",
|
|
73
|
+
"rou3": "^0.8.1",
|
|
73
74
|
"simple-git-hooks": "^2.13.1",
|
|
74
75
|
"supertest": "^7.2.2",
|
|
75
|
-
"typescript": "^
|
|
76
|
-
"
|
|
76
|
+
"typescript": "^6.0.3",
|
|
77
|
+
"unsecure": "^0.2.1",
|
|
78
|
+
"vitest": "^4.1.4"
|
|
77
79
|
},
|
|
78
80
|
"peerDependencies": {
|
|
79
|
-
"cookie-es": "^1.2.
|
|
80
|
-
"h3": "^1.15.4 || >=2.0.1-rc.
|
|
81
|
-
"rou3": "^0.7.10"
|
|
81
|
+
"cookie-es": "^1.2.3 || ^3.1.1",
|
|
82
|
+
"h3": "^1.15.4 || >=2.0.1-rc.20 || ^2.0.1"
|
|
82
83
|
},
|
|
83
84
|
"peerDependenciesMeta": {
|
|
84
85
|
"cookie-es": {
|
|
@@ -86,11 +87,11 @@
|
|
|
86
87
|
},
|
|
87
88
|
"h3": {
|
|
88
89
|
"optional": true
|
|
89
|
-
},
|
|
90
|
-
"rou3": {
|
|
91
|
-
"optional": true
|
|
92
90
|
}
|
|
93
91
|
},
|
|
92
|
+
"resolutions": {
|
|
93
|
+
"unjwt": "link:."
|
|
94
|
+
},
|
|
94
95
|
"simple-git-hooks": {
|
|
95
96
|
"pre-commit": "./node_modules/.bin/nano-staged"
|
|
96
97
|
},
|
|
@@ -106,6 +107,7 @@
|
|
|
106
107
|
"lint": "oxlint . && oxfmt --check .",
|
|
107
108
|
"fmt": "automd && oxlint --fix . && oxfmt .",
|
|
108
109
|
"release": "pnpm test && pnpm prepack && changelogen --release --push",
|
|
110
|
+
"stub": "obuild --stub",
|
|
109
111
|
"test": "vitest run --slowTestThreshold=1000",
|
|
110
112
|
"test:coverage": "vitest run --coverage --slowTestThreshold=1000",
|
|
111
113
|
"typecheck": "tsgo --noEmit",
|
package/dist/_chunks/error.d.mts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
//#region src/core/error.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* All known error codes thrown by unjwt.
|
|
4
|
-
*
|
|
5
|
-
* Code naming follows the JOSE spec hierarchy:
|
|
6
|
-
* ERR_JWT_* — RFC 7519 JSON Web Token claim validation
|
|
7
|
-
* ERR_JWS_* — RFC 7515 JSON Web Signature
|
|
8
|
-
* ERR_JWE_* — RFC 7516 JSON Web Encryption
|
|
9
|
-
* ERR_JWK_* — RFC 7517 JSON Web Key
|
|
10
|
-
*/
|
|
11
|
-
type JWTErrorCode = "ERR_JWT_EXPIRED" | "ERR_JWT_NBF" | "ERR_JWT_CLAIM_INVALID" | "ERR_JWT_CLAIM_MISSING" | "ERR_JWS_INVALID" | "ERR_JWS_SIGNATURE_INVALID" | "ERR_JWS_ALG_NOT_ALLOWED" | "ERR_JWE_INVALID" | "ERR_JWE_DECRYPTION_FAILED" | "ERR_JWE_ALG_NOT_ALLOWED" | "ERR_JWK_INVALID" | "ERR_JWK_KEY_NOT_FOUND" | (string & {});
|
|
12
|
-
/**
|
|
13
|
-
* Maps known error codes to their structured `cause` shapes.
|
|
14
|
-
* Only codes that carry decoded context beyond the message appear here.
|
|
15
|
-
* Extend this interface when new structured causes are introduced.
|
|
16
|
-
*/
|
|
17
|
-
interface JWTErrorCauseMap {
|
|
18
|
-
/** Decoded claims available at the point the lifetime check failed. */
|
|
19
|
-
ERR_JWT_EXPIRED: {
|
|
20
|
-
jti?: string;
|
|
21
|
-
iat?: number;
|
|
22
|
-
exp?: number;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Base error class for all unjwt errors. Every error thrown by the library is
|
|
27
|
-
* an instance of `JWTError`, so a single `instanceof` check is sufficient to
|
|
28
|
-
* distinguish library errors from other thrown values. Use the `code` property
|
|
29
|
-
* (or the `isJWTError` type guard) to narrow to a specific error condition.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* import { JWTError, isJWTError } from "unjwt/jws";
|
|
33
|
-
*
|
|
34
|
-
* try {
|
|
35
|
-
* await verify(token, key);
|
|
36
|
-
* } catch (err) {
|
|
37
|
-
* if (isJWTError(err, "ERR_JWT_EXPIRED")) {
|
|
38
|
-
* console.log(err.cause.jti); // string | undefined
|
|
39
|
-
* }
|
|
40
|
-
* }
|
|
41
|
-
*/
|
|
42
|
-
declare class JWTError<TCode extends JWTErrorCode = JWTErrorCode> extends Error {
|
|
43
|
-
readonly code: TCode;
|
|
44
|
-
override readonly cause?: unknown;
|
|
45
|
-
constructor(message: string, code: TCode, cause?: unknown);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Type guard that narrows `error` to `JWTError`.
|
|
49
|
-
*
|
|
50
|
-
* When called with a code that is a key of `JWTErrorCauseMap`, the return
|
|
51
|
-
* type is additionally narrowed to include the structured `cause` shape for
|
|
52
|
-
* that code. When called with any other `JWTErrorCode`, `code` is narrowed
|
|
53
|
-
* but `cause` remains `unknown`. When called without a code, narrows only to
|
|
54
|
-
* `JWTError`.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* if (isJWTError(err, "ERR_JWT_EXPIRED")) {
|
|
58
|
-
* err.cause.jti // string | undefined ✓
|
|
59
|
-
* }
|
|
60
|
-
*/
|
|
61
|
-
declare function isJWTError<T extends keyof JWTErrorCauseMap>(error: unknown, code: T): error is JWTError<T> & {
|
|
62
|
-
cause: JWTErrorCauseMap[T];
|
|
63
|
-
};
|
|
64
|
-
declare function isJWTError<T extends JWTErrorCode>(error: unknown, code: T): error is JWTError<T>;
|
|
65
|
-
declare function isJWTError(error: unknown): error is JWTError;
|
|
66
|
-
//#endregion
|
|
67
|
-
export { isJWTError as i, JWTErrorCauseMap as n, JWTErrorCode as r, JWTError as t };
|