unjwt 0.8.4 → 0.8.5

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.
Files changed (53) hide show
  1. package/README.md +3 -9
  2. package/dist/_chunks/{_import-key.mjs → algorithms.mjs} +194 -399
  3. package/dist/_chunks/{_codec-jwe.d.mts → codec-jwe.d.mts} +40 -9
  4. package/dist/_chunks/{_codec-jws.mjs → codec-jws.mjs} +133 -80
  5. package/dist/_chunks/cookie.mjs +50 -0
  6. package/dist/_chunks/digest.mjs +10 -3
  7. package/dist/_chunks/error.d.mts +103 -0
  8. package/dist/_chunks/export.mjs +97 -0
  9. package/dist/_chunks/index.d.mts +5 -103
  10. package/dist/_chunks/index2.d.mts +206 -12
  11. package/dist/_chunks/index3.d.mts +12 -109
  12. package/dist/_chunks/{jwe.d.mts → index4.d.mts} +2 -1
  13. package/dist/_chunks/{jws.d.mts → index5.d.mts} +2 -1
  14. package/dist/_chunks/index6.d.mts +113 -0
  15. package/dist/_chunks/jwe.mjs +205 -72
  16. package/dist/{core → _chunks}/jwk.mjs +210 -495
  17. package/dist/_chunks/jws.mjs +280 -193
  18. package/dist/_chunks/jwt.d.mts +2 -1
  19. package/dist/_chunks/{_jwt-internal.mjs → jwt.mjs} +110 -54
  20. package/dist/_chunks/libs/unsecure.d.mts +35 -25
  21. package/dist/_chunks/libs/unsecure.mjs +101 -268
  22. package/dist/_chunks/{_internal.d.mts → limits.d.mts} +1 -1
  23. package/dist/_chunks/payload.mjs +23 -0
  24. package/dist/_chunks/wrap.mjs +209 -0
  25. package/dist/adapters/elysia/index.d.mts +4 -4
  26. package/dist/adapters/elysia/index.mjs +4 -3
  27. package/dist/adapters/h3/index.d.mts +13 -13
  28. package/dist/adapters/h3/index.mjs +8 -11
  29. package/dist/adapters/h3v1/index.d.mts +5 -5
  30. package/dist/adapters/h3v1/index.mjs +6 -9
  31. package/dist/core/internal.mjs +1 -1
  32. package/dist/core/{jwe.d.mts → jwe/index.d.mts} +5 -4
  33. package/dist/core/jwe/index.mjs +5 -0
  34. package/dist/core/{jwk.d.mts → jwk/index.d.mts} +5 -4
  35. package/dist/core/jwk/index.mjs +7 -0
  36. package/dist/core/{jws.d.mts → jws/index.d.mts} +5 -4
  37. package/dist/core/jws/index.mjs +5 -0
  38. package/dist/dpop/index.d.mts +73 -0
  39. package/dist/dpop/index.mjs +296 -0
  40. package/dist/error.d.mts +3 -0
  41. package/dist/error.mjs +20 -0
  42. package/dist/index.d.mts +10 -9
  43. package/dist/index.mjs +7 -4
  44. package/dist/session/index.d.mts +4 -4
  45. package/dist/session/index.mjs +6 -47
  46. package/dist/utils/index.d.mts +7 -0
  47. package/dist/{core/utils → utils}/index.mjs +4 -3
  48. package/package.json +21 -10
  49. package/dist/_chunks/jwk.d.mts +0 -205
  50. package/dist/core/jwe.mjs +0 -5
  51. package/dist/core/jws.mjs +0 -5
  52. package/dist/core/utils/index.d.mts +0 -6
  53. /package/dist/_chunks/{_cookie-attributes.d.mts → cookie-attributes.d.mts} +0 -0
@@ -1,205 +0,0 @@
1
- import { ContentEncryptionAlgorithm, DeriveJWKOptions, DeriveJWKReturn, DeriveKeyOptions, DeriveKeyReturn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, ImportedKey, ImportedPEMJWK, JWK, JWKAlgorithm, JWKCacheAdapter, JWKMetadata, JWKPEMAlgorithm, JWKSet, JWK_Asymmetric, JWK_ECDH_ES, JWK_EC_Private, JWK_EC_Public, JWK_PBES2, JWK_oct, KeyManagementAlgorithm, PEMType, ToPublicJWKReturn, UnwrapKeyOptions, UnwrapKeyResult, UnwrappingKeyFor, WrapKeyOptions, WrapKeyResult, WrappingKeyFor } from "./index.mjs";
2
- import "./index2.mjs";
3
- /**
4
- * Default JWK import cache, keyed by JWK object identity: a copied JWK misses it, and a JWK
5
- * mutated in place keeps serving the old `CryptoKey`. Rotate by replacing the object.
6
- */
7
- export declare class WeakMapJWKCache implements JWKCacheAdapter {
8
- private readonly _map;
9
- get(jwk: JWK, alg: JWKAlgorithm): CryptoKey | undefined;
10
- set(jwk: JWK, alg: JWKAlgorithm, key: CryptoKey): void;
11
- }
12
- /**
13
- * Replaces the JWK import cache used by `importKey`, or disables it with `false`.
14
- *
15
- * A cache shared across JWK objects keys on the RFC 7638 thumbprint, the pair half (`d` present)
16
- * and `alg`: the thumbprint covers public members only, and a `kid` may name different keys.
17
- * `get` and `set` may return promises.
18
- *
19
- * @example
20
- * ```ts
21
- * const cacheKey = async (jwk: JWK, alg: JWKAlgorithm) =>
22
- * `${await thumbprint(jwk)}:${"d" in jwk ? "private" : "public"}:${alg}`;
23
- * const map = new Map<string, CryptoKey>();
24
- * configureJWKCache({
25
- * get: async (jwk, alg) => map.get(await cacheKey(jwk, alg)),
26
- * set: async (jwk, alg, key) => {
27
- * map.set(await cacheKey(jwk, alg), key);
28
- * },
29
- * });
30
- * ```
31
- */
32
- export declare function configureJWKCache(cache: JWKCacheAdapter | false): void;
33
- /** Resets the JWK import cache to a fresh `WeakMapJWKCache`. */
34
- export declare function clearJWKCache(): void;
35
- /** Imports the secret of an `oct` JWK as a `CryptoKey` under `algorithm` and `usage`. */
36
- export declare function importKey(key: JWK_oct, options: {
37
- asCryptoKey: true;
38
- /** Algorithm to import the key as (e.g. `{ name: "AES-GCM", length: 256 }`). */
39
- algorithm: Parameters<typeof crypto.subtle.importKey>[2];
40
- usage: KeyUsage[];
41
- /** Mark the key as extractable. Defaults to `false`. */
42
- extractable?: boolean;
43
- }): Promise<CryptoKey>;
44
- /**
45
- * Imports key material: a `CryptoKey` or `Uint8Array` is returned as is, an `oct` JWK as its raw
46
- * bytes, an asymmetric JWK as a `CryptoKey` under its own `alg` or the hint. A string throws
47
- * `ERR_JWK_INVALID`.
48
- *
49
- * `expect` and `extractable` apply to asymmetric keys only; `expect` rejects the other pair half
50
- * with `ERR_JWK_INVALID`. A JWK's `alg` is authoritative (RFC 7517 §4.4): a different requested
51
- * `alg` throws `ERR_JWK_INVALID`, except an `oct` JWK declaring a content encryption `alg` under
52
- * `"dir"`.
53
- */
54
- export declare function importKey<K extends CryptoKey | JWK | Uint8Array<ArrayBuffer>>(key: K, options?: JWKAlgorithm | {
55
- /** Algorithm hint. Required when `key.alg` is absent on an asymmetric JWK. */
56
- alg?: JWKAlgorithm;
57
- /** Reject on shape mismatch instead of silently importing. */
58
- expect?: "public" | "private";
59
- /** Defaults to `false` for private, `true` for public material; JWK `ext: false` wins. */
60
- extractable?: boolean;
61
- }): Promise<ImportedKey<K>>;
62
- /**
63
- * Returns the JWKs of a JWK Set, dropping non-JWK entries, optionally narrowed by `filter`.
64
- * An invalid set throws `ERR_JWK_INVALID`.
65
- */
66
- export declare function getJWKsFromSet(jwkSet: JWKSet, filter?: (jwk: JWK) => boolean): JWK[];
67
- /**
68
- * Generates a key for a JWA algorithm: a `CryptoKey` for symmetric algorithms, raw bytes for
69
- * the `A*CBC-HS*` composites, a `CryptoKeyPair` for asymmetric ones; with `toJWK: true`, a JWK
70
- * (or JWK pair) named by the `kid` option or its RFC 7638 thumbprint. PBES2, `"dir"` and `"none"` are refused.
71
- *
72
- * @param options
73
- * - `extractable?` — defaults to `true`; ignored when `toJWK` is set.
74
- * - `kid?` — with `toJWK`, a string set verbatim or `false` for no `kid`, either skipping the
75
- * thumbprint digest; anything else throws `ERR_INVALID_OPTION`.
76
- * - `keyUsage?` — narrows the usages derived from `alg`; leaving either half of a pair with
77
- * no usage throws `ERR_INVALID_OPTION`.
78
- * - `modulusLength?` — RSA only; defaults to `2048`.
79
- * - `publicExponent?` — RSA only; big-endian bytes, defaults to `0x010001`.
80
- * - `namedCurve?` — EC/OKP only; defaults to `"P-256"` / `"Ed25519"`. One that contradicts
81
- * the curve `ES*` or `Ed25519` names throws `ERR_INVALID_OPTION`.
82
- */
83
- export declare function generateKey<TAlg extends GenerateKeyAlgorithm, ToJWK extends boolean = false, Kid extends string | false = string>(alg: TAlg, options?: GenerateKeyOptions<ToJWK, Kid>): Promise<GenerateKeyReturn<TAlg, GenerateKeyOptions<ToJWK, Kid>>>;
84
- /**
85
- * Generates a JWK (or JWK pair) for a JWA algorithm, its `kid` the caller's, none with
86
- * `kid: false`, or else its RFC 7638 thumbprint. PBES2, `"dir"` and `"none"` are refused.
87
- *
88
- * @param options
89
- * - `keyUsage?`, `modulusLength?` (default `2048`), `publicExponent?` (default `0x010001`),
90
- * `namedCurve?` (default `"P-256"` / `"Ed25519"`); a narrowing that empties a pair half or
91
- * a curve that contradicts `alg` throws `ERR_INVALID_OPTION`.
92
- * - `kid?` — a string set verbatim or `false` for no `kid`, either skipping the thumbprint
93
- * digest; anything else throws `ERR_INVALID_OPTION`.
94
- * - JWK metadata (`use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is merged onto the result;
95
- * `alg`, `kty`, `key_ops`, `ext` and key-material members are reserved (`ERR_INVALID_OPTION`).
96
- */
97
- export declare function generateJWK<TAlg extends GenerateKeyAlgorithm, Kid extends string | false = string>(alg: TAlg, options?: GenerateJWKOptions<Kid> & Omit<JWKMetadata, "kid">): Promise<GenerateJWKReturn<TAlg, Kid>>;
98
- /**
99
- * Derives an AES-KW key from a password with PBKDF2 per PBES2 (RFC 7518 §4.8).
100
- *
101
- * @param options
102
- * - `alg` — a PBES2 algorithm; any other throws `ERR_JWK_UNSUPPORTED`.
103
- * - `salt` — PBKDF2 salt (`p2s`), at least 8 octets (RFC 7518 §4.8.1.1).
104
- * - `iterations` — PBKDF2 count (`p2c`), an integer of at least 1000 (RFC 7518 §4.8.1.2);
105
- * anything else throws `ERR_INVALID_OPTION`.
106
- * - `keyUsage?` — defaults to `["wrapKey", "unwrapKey"]`.
107
- * - `extractable?` — defaults to `false`; ignored when `toJWK` is set.
108
- * - `toJWK?` — return a `JWK_oct` carrying the inner AES-KW `alg` instead of a `CryptoKey`.
109
- */
110
- export declare function deriveKeyFromPassword<TOptions extends DeriveKeyOptions>(password: string | Uint8Array<ArrayBuffer>, options: TOptions): Promise<DeriveKeyReturn<TOptions, TOptions["alg"]>>;
111
- /**
112
- * Derives a `JWK_oct` from a password per PBES2 (RFC 7518 §4.8); its `alg` is the inner AES-KW
113
- * variant and its `kid` the caller's, none with `kid: false`, or else its RFC 7638 thumbprint.
114
- *
115
- * @param options
116
- * - `alg` (PBES2 only, else `ERR_JWK_UNSUPPORTED`), `salt` (at least 8 octets), `iterations`
117
- * (integer of at least 1000, else `ERR_INVALID_OPTION`), `keyUsage?` (default
118
- * `["wrapKey", "unwrapKey"]`).
119
- * - `kid?` — a string set verbatim or `false` for no `kid`, either skipping the thumbprint
120
- * digest; anything else throws `ERR_INVALID_OPTION`.
121
- * - JWK metadata (`use`, `x5c`, `x5t`, `x5t#S256`, `x5u`) is merged onto the result;
122
- * `alg`, `kty`, `key_ops`, `ext` and key-material members are reserved (`ERR_INVALID_OPTION`).
123
- */
124
- export declare function deriveJWKFromPassword<TAlg extends JWK_PBES2, Kid extends string | false = string>(password: string | Uint8Array<ArrayBuffer>, options: DeriveJWKOptions<TAlg, Kid> & Omit<JWKMetadata, "kid">): Promise<DeriveJWKReturn<TAlg, Kid>>;
125
- /**
126
- * Exports an extractable `CryptoKey` as a JWK, merging optional `jwkParams` metadata.
127
- *
128
- * The `alg` is WebCrypto's, which knows no JOSE roles: an AES-GCM key minted for `A128GCMKW`
129
- * exports as `A128GCM`, so key-wrapping keys are minted with `generateJWK` instead.
130
- *
131
- * @throws `ERR_INVALID_OPTION` if `key` is not a `CryptoKey` or `jwkParams` sets a reserved member
132
- * (`alg`, `kty`, `key_ops`, `ext`, key material); `ERR_JWK_INVALID` for a non-extractable key.
133
- */
134
- export declare function exportJWK<T extends JWK = JWK>(key: CryptoKey, jwkParams?: JWKMetadata): Promise<T>;
135
- /** @deprecated Use `exportJWK`; removed in 0.9.0. */
136
- export declare function exportKey<T extends JWK = JWK>(key: CryptoKey, jwkParams?: JWKMetadata): Promise<T>;
137
- /**
138
- * Utility to derive the public half of an asymmetric JWK, or throw if the input is not asymmetric.
139
- *
140
- * @param jwk An asymmetric JWK (EC, OKP, RSA) with a private scalar (`d`).
141
- * @throws `ERR_JWK_INVALID` if the input is not an asymmetric JWK.
142
- * @returns A JWK with the same public members, without the private scalar.
143
- */
144
- export declare function toPublicJWK<T extends JWK_Asymmetric>(jwk: T): ToPublicJWKReturn<T>;
145
- /**
146
- * Wraps a CEK under a JWA key management algorithm; the result shape follows `alg`.
147
- *
148
- * @param wrappingKey A `CryptoKey`, a JWK, or for PBES2 a password string or bytes.
149
- * @param options `alg` plus its parameters (`p2s`/`p2c` for PBES2, `ecdh` for ECDH-ES, …).
150
- */
151
- export declare function wrapKey<TAlg extends KeyManagementAlgorithm>(keyToWrap: CryptoKey | Uint8Array<ArrayBuffer>, wrappingKey: WrappingKeyFor<TAlg>, options: WrapKeyOptions<TAlg>): Promise<WrapKeyResult<TAlg>>;
152
- /**
153
- * Unwraps a CEK under a JWA key management algorithm.
154
- *
155
- * @param unwrappingKey A `CryptoKey`, a JWK, or for PBES2 a password string or bytes.
156
- * @param options `alg`, `format?` (`"cryptokey"` by default, or `"raw"`), plus the algorithm's
157
- * parameters (`iv`/`tag`, `p2s`/`p2c`, `epk`, …).
158
- * @throws `ERR_JWE_DECRYPTION_FAILED` for any failed unwrap, its cause undisclosed (RFC 7516
159
- * §11.5); `ERR_INVALID_OPTION` for a malformed `wrappedKey` or `options`; `ERR_JWE_INVALID` for
160
- * missing or malformed algorithm parameters.
161
- */
162
- export declare function unwrapKey<TOptions extends UnwrapKeyOptions>(wrappedKey: Uint8Array<ArrayBuffer>, unwrappingKey: UnwrappingKeyFor<TOptions["alg"]>, options: TOptions): Promise<UnwrapKeyResult<TOptions>>;
163
- /**
164
- * Imports a PEM key as a JWK carrying `alg`. The type follows the label — `PRIVATE KEY`
165
- * (PKCS#8), `PUBLIC KEY` (SPKI), `CERTIFICATE` (X.509, only its SPKI kept) — unless
166
- * `options.pemType` overrides it. The JWK holds the key material in plain JavaScript.
167
- *
168
- * @throws `ERR_JWK_INVALID` if the type cannot be inferred or does not match the label;
169
- * `ERR_JWK_UNSUPPORTED` for an unknown `pemType`; `ERR_INVALID_OPTION` if `jwkParams` sets
170
- * `alg`, `kty`, `key_ops`, `ext` or a key-material member.
171
- */
172
- export declare function importPEM<A extends JWKPEMAlgorithm, Type extends PEMType | undefined = undefined>(pem: string, alg: A, options?: {
173
- /** PEM type. Inferred from the PEM label when omitted, which leaves the result public or private. */
174
- pemType?: Type;
175
- /** Additional JWK properties merged into the exported key (e.g. `kid`). */
176
- jwkParams?: JWKMetadata;
177
- }): Promise<ImportedPEMJWK<A, Type>>;
178
- /**
179
- * Exports an asymmetric JWK as PEM: PKCS#8 when it carries `d`, SPKI otherwise.
180
- *
181
- * @throws `ERR_JWK_UNSUPPORTED` for `oct` JWKs or an unknown format or algorithm;
182
- * `ERR_JWK_INVALID` for a missing `alg` or a `pemFormat` that contradicts the JWK's half.
183
- */
184
- export declare function exportPEM(jwk: JWK, options?: {
185
- /** PEM format. Inferred from the JWK shape when omitted. */
186
- pemFormat?: "pkcs8" | "spki";
187
- /** Algorithm hint used when `jwk.alg` is absent. */
188
- alg?: JWKPEMAlgorithm;
189
- }): Promise<string>;
190
- /**
191
- * Derives ECDH-ES key material (RFC 7518 §4.6): ECDH followed by the Concat KDF.
192
- *
193
- * @param publicKey The recipient's static public key (the ephemeral one when decrypting).
194
- * @param privateKey The ephemeral private key (the recipient's static one when decrypting).
195
- */
196
- export declare function deriveSharedSecret(publicKey: CryptoKey | JWK_EC_Public, privateKey: CryptoKey | JWK_EC_Private, options: {
197
- /** Algorithm identifier fed to the concat KDF as `AlgorithmID`. */
198
- enc: JWK_ECDH_ES | ContentEncryptionAlgorithm;
199
- /** Derived length in bits. Defaults to `enc`'s length; required for `"ECDH-ES"`. */
200
- keyLength?: number;
201
- /** Agreement PartyUInfo (apu). */
202
- partyUInfo?: Uint8Array<ArrayBuffer>;
203
- /** Agreement PartyVInfo (apv). */
204
- partyVInfo?: Uint8Array<ArrayBuffer>;
205
- }): Promise<Uint8Array<ArrayBuffer>>;
package/dist/core/jwe.mjs DELETED
@@ -1,5 +0,0 @@
1
- import { UnsecureError } from "../_chunks/libs/unsecure.mjs";
2
- import { JWTError, isJWTError, isUnsecureError } from "../_chunks/_jwt-internal.mjs";
3
- import { DEFAULT_JOSE_LIMITS } from "../_chunks/_import-key.mjs";
4
- import { decrypt, decryptMulti, encrypt, encryptMulti, generalToFlattened } from "../_chunks/jwe.mjs";
5
- export { DEFAULT_JOSE_LIMITS, JWTError, UnsecureError, decrypt, decryptMulti, encrypt, encryptMulti, generalToFlattened, isJWTError, isUnsecureError };
package/dist/core/jws.mjs DELETED
@@ -1,5 +0,0 @@
1
- import { UnsecureError } from "../_chunks/libs/unsecure.mjs";
2
- import { JWTError, isJWTError, isUnsecureError } from "../_chunks/_jwt-internal.mjs";
3
- import { DEFAULT_JOSE_LIMITS } from "../_chunks/_import-key.mjs";
4
- import { generalToFlattenedJWS, sign, signMulti, verify, verifyMulti, verifyMultiAll } from "../_chunks/jws.mjs";
5
- export { DEFAULT_JOSE_LIMITS, JWTError, UnsecureError, generalToFlattenedJWS, isJWTError, isUnsecureError, sign, signMulti, verify, verifyMulti, verifyMultiAll };
@@ -1,6 +0,0 @@
1
- import { BytesSource, DecodeOptions, DecodeReturnAs, UnsecureError, UnsecureErrorCode, safeJsonParse, sanitizeObject, sanitizeObjectCopy, secureRandomBytes, textDecoder, textEncoder } from "../../_chunks/libs/unsecure.mjs";
2
- import { StrictOmit, isUnsecureError } from "../../_chunks/index.mjs";
3
- import { DigestHash, digestUrl } from "../../_chunks/digest.mjs";
4
- import { computeDurationInSeconds, isJWTContent, validateJwtClaims } from "../../_chunks/jwt.mjs";
5
- import { ComputeJwtTimeClaimsOptions, DecodePayloadOptions, InferAlgorithmsOptions, applyTypCtyDefaults, assertCritShape, assertCryptoKey, base64Decode, base64Encode, base64Parse, base64Stringify, base64UrlDecode, base64UrlEncode, base64UrlParse, base64UrlStringify, computeJwtTimeClaims, concatUint8Arrays, decodePayloadFromB64UrlSegment, decodePayloadFromBytes, getPlaintextBytes, inferJWEAllowedAlgorithms, inferJWSAllowedAlgorithms, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, validateCriticalHeadersJWE, validateCriticalHeadersJWS } from "../../_chunks/index3.mjs";
6
- export { type BytesSource, ComputeJwtTimeClaimsOptions, type DecodeOptions, DecodePayloadOptions, type DecodeReturnAs, type DigestHash, type InferAlgorithmsOptions, type StrictOmit, UnsecureError, type UnsecureErrorCode, applyTypCtyDefaults, assertCritShape, assertCryptoKey, base64Decode, base64Encode, base64Parse, base64Stringify, base64UrlDecode, base64UrlEncode, base64UrlParse, base64UrlStringify, computeDurationInSeconds, computeJwtTimeClaims, concatUint8Arrays, decodePayloadFromB64UrlSegment, decodePayloadFromBytes, digestUrl, getPlaintextBytes, inferJWEAllowedAlgorithms, inferJWSAllowedAlgorithms, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTContent, isPrivateJWK, isPublicJWK, isSymmetricJWK, isUnsecureError, maybeArray, safeJsonParse, sanitizeObject, sanitizeObjectCopy, secureRandomBytes, textDecoder, textEncoder, validateCriticalHeadersJWE, validateCriticalHeadersJWS, validateJwtClaims };