unjwt 0.6.0 → 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 +25 -24
- 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/README.md
CHANGED
|
@@ -409,8 +409,7 @@ const accessConfig = {
|
|
|
409
409
|
|
|
410
410
|
## Credits
|
|
411
411
|
|
|
412
|
-
Originally
|
|
413
|
-
Heavily inspired by [Filip Skokan's work](https://github.com/panva/jose).
|
|
412
|
+
Originally visioned by [Johann Schopplich](https://github.com/johannschopplich/unjwt), heavily inspired by [Filip Skokan's `jose`](https://github.com/panva/jose) internal cryptographic primitives, and initially sponsored by [JAMflow](https://jamflow.cloud) — thanks to all three for making this project possible.
|
|
414
413
|
|
|
415
414
|
## License
|
|
416
415
|
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Licenses of Bundled Dependencies
|
|
2
|
+
|
|
3
|
+
The published artifact additionally contains code with the following licenses:
|
|
4
|
+
MIT
|
|
5
|
+
|
|
6
|
+
# Bundled Dependencies
|
|
7
|
+
|
|
8
|
+
## rou3
|
|
9
|
+
|
|
10
|
+
License: MIT
|
|
11
|
+
Repository: https://github.com/h3js/rou3
|
|
12
|
+
|
|
13
|
+
> MIT License
|
|
14
|
+
>
|
|
15
|
+
> Copyright (c) Pooya Parsa <pooya@pi0.io>
|
|
16
|
+
>
|
|
17
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
18
|
+
> of this software and associated documentation files (the "Software"), to deal
|
|
19
|
+
> in the Software without restriction, including without limitation the rights
|
|
20
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
21
|
+
> copies of the Software, and to permit persons to whom the Software is
|
|
22
|
+
> furnished to do so, subject to the following conditions:
|
|
23
|
+
>
|
|
24
|
+
> The above copyright notice and this permission notice shall be included in all
|
|
25
|
+
> copies or substantial portions of the Software.
|
|
26
|
+
>
|
|
27
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
28
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
29
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
30
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
31
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
32
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
33
|
+
> SOFTWARE.
|
|
34
|
+
|
|
35
|
+
---------------------------------------
|
|
36
|
+
|
|
37
|
+
## unsecure
|
|
38
|
+
|
|
39
|
+
License: MIT
|
|
40
|
+
By: Sandro Circi
|
|
41
|
+
Repository: https://github.com/sandros94/unsecure
|
|
42
|
+
|
|
43
|
+
> MIT License
|
|
44
|
+
>
|
|
45
|
+
> Copyright (c) 2025 Sandro Circi
|
|
46
|
+
>
|
|
47
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
48
|
+
> of this software and associated documentation files (the "Software"), to deal
|
|
49
|
+
> in the Software without restriction, including without limitation the rights
|
|
50
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
51
|
+
> copies of the Software, and to permit persons to whom the Software is
|
|
52
|
+
> furnished to do so, subject to the following conditions:
|
|
53
|
+
>
|
|
54
|
+
> The above copyright notice and this permission notice shall be included in all
|
|
55
|
+
> copies or substantial portions of the Software.
|
|
56
|
+
>
|
|
57
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
58
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
59
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
60
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
61
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
62
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
63
|
+
> SOFTWARE.
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { D as JWTError, E as inferJWSAllowedAlgorithms, T as inferJWEAllowedAlgorithms, a as isCryptoKeyPair, i as isCryptoKey, o as isJWK, v as decodePayloadFromB64UrlSegment, w as validateJwtClaims } from "./utils.mjs";
|
|
2
|
+
import { c as safeJsonParse, i as base64UrlDecode, s as textEncoder } from "./libs/unsecure.mjs";
|
|
3
|
+
function buildJWKSetFilter(header) {
|
|
4
|
+
const kid = header?.kid;
|
|
5
|
+
const alg = header?.alg;
|
|
6
|
+
return (k) => (!kid || k.kid === kid) && (!k.alg || k.alg === alg);
|
|
7
|
+
}
|
|
8
|
+
function parseEphemeralKey(ephemeralKey) {
|
|
9
|
+
if (isCryptoKeyPair(ephemeralKey)) return {
|
|
10
|
+
epk: ephemeralKey.publicKey,
|
|
11
|
+
epkPrivateKey: ephemeralKey.privateKey
|
|
12
|
+
};
|
|
13
|
+
if (typeof ephemeralKey === "object" && ephemeralKey !== null && "publicKey" in ephemeralKey && "privateKey" in ephemeralKey) {
|
|
14
|
+
if (!ephemeralKey.publicKey || !ephemeralKey.privateKey) throw new JWTError("ECDH-ES custom ephemeral key must include both publicKey and privateKey.", "ERR_JWK_INVALID");
|
|
15
|
+
return {
|
|
16
|
+
epk: ephemeralKey.publicKey,
|
|
17
|
+
epkPrivateKey: ephemeralKey.privateKey
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if (isCryptoKey(ephemeralKey)) {
|
|
21
|
+
if (ephemeralKey.type !== "private") throw new JWTError("ECDH-ES custom ephemeral CryptoKey must include private key material.", "ERR_JWK_INVALID");
|
|
22
|
+
return {
|
|
23
|
+
epk: ephemeralKey,
|
|
24
|
+
epkPrivateKey: ephemeralKey
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (isJWK(ephemeralKey)) {
|
|
28
|
+
if (!("d" in ephemeralKey) || typeof ephemeralKey.d !== "string") throw new JWTError("ECDH-ES custom ephemeral JWK must include private parameter \"d\".", "ERR_JWK_INVALID");
|
|
29
|
+
return {
|
|
30
|
+
epk: ephemeralKey,
|
|
31
|
+
epkPrivateKey: ephemeralKey
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
throw new JWTError("Unsupported ECDH-ES ephemeral key material provided.", "ERR_JWK_INVALID");
|
|
35
|
+
}
|
|
36
|
+
async function resolveSigningKey(alg, key, usage) {
|
|
37
|
+
if (key instanceof Uint8Array) {
|
|
38
|
+
const minBytes = Number.parseInt(alg.slice(2), 10) / 8;
|
|
39
|
+
if (key.length < minBytes) throw new JWTError(`${alg} requires a key of at least ${minBytes} bytes`, "ERR_JWK_INVALID");
|
|
40
|
+
return crypto.subtle.importKey("raw", key, {
|
|
41
|
+
name: "HMAC",
|
|
42
|
+
hash: `SHA-${alg.slice(-3)}`
|
|
43
|
+
}, false, [usage]);
|
|
44
|
+
}
|
|
45
|
+
if (alg.startsWith("RS") || alg.startsWith("PS")) {
|
|
46
|
+
const { modulusLength } = key.algorithm;
|
|
47
|
+
if (typeof modulusLength !== "number" || modulusLength < 2048) throw new JWTError(`${alg} requires a key modulusLength of at least 2048 bits`, "ERR_JWK_INVALID");
|
|
48
|
+
}
|
|
49
|
+
return key;
|
|
50
|
+
}
|
|
51
|
+
function validateJwtClaimsIfJsonPayload(payload, options) {
|
|
52
|
+
if (payload && typeof payload === "object" && !(payload instanceof Uint8Array) && !options.forceUint8Array && options.validateClaims !== false) validateJwtClaims(payload, options);
|
|
53
|
+
}
|
|
54
|
+
const JWS_ALG_CTX = /* @__PURE__ */ Object.freeze({
|
|
55
|
+
infer: inferJWSAllowedAlgorithms,
|
|
56
|
+
octAliasing: false,
|
|
57
|
+
errorCode: "ERR_JWS_ALG_NOT_ALLOWED",
|
|
58
|
+
label: "Algorithm"
|
|
59
|
+
});
|
|
60
|
+
const JWE_ALG_CTX = /* @__PURE__ */ Object.freeze({
|
|
61
|
+
infer: inferJWEAllowedAlgorithms,
|
|
62
|
+
octAliasing: true,
|
|
63
|
+
errorCode: "ERR_JWE_ALG_NOT_ALLOWED",
|
|
64
|
+
label: "Key management algorithm"
|
|
65
|
+
});
|
|
66
|
+
function checkAlgAllowed(alg, key, explicit, ctx) {
|
|
67
|
+
if (explicit) return explicit.includes(alg) ? void 0 : notAllowed(alg, ctx);
|
|
68
|
+
if (isJWK(key) && typeof key.alg === "string" && (!ctx.octAliasing || key.kty !== "oct")) return key.alg === alg ? void 0 : notAllowed(alg, ctx);
|
|
69
|
+
const inferred = ctx.infer(key);
|
|
70
|
+
if (!inferred) return new JWTError(`Cannot infer allowed ${ctx.label.toLowerCase()}s from this key; pass "options.algorithms" explicitly.`, ctx.errorCode);
|
|
71
|
+
return inferred.includes(alg) ? void 0 : notAllowed(alg, ctx);
|
|
72
|
+
}
|
|
73
|
+
function notAllowed(alg, ctx) {
|
|
74
|
+
return new JWTError(`${ctx.label} not allowed: ${alg}`, ctx.errorCode);
|
|
75
|
+
}
|
|
76
|
+
function decodeProtectedHeader(encoded, family) {
|
|
77
|
+
if (!encoded) return {};
|
|
78
|
+
try {
|
|
79
|
+
return safeJsonParse(base64UrlDecode(encoded));
|
|
80
|
+
} catch {
|
|
81
|
+
throw new JWTError(`Invalid ${family}: Protected header could not be decoded.`, `ERR_${family}_INVALID`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function decodeJWSPayload(payloadSegment, useB64, forceUint8Array) {
|
|
85
|
+
if (useB64) return decodePayloadFromB64UrlSegment(payloadSegment, forceUint8Array);
|
|
86
|
+
return forceUint8Array ? textEncoder.encode(payloadSegment) : payloadSegment;
|
|
87
|
+
}
|
|
88
|
+
export { decodeJWSPayload as a, resolveSigningKey as c, checkAlgAllowed as i, validateJwtClaimsIfJsonPayload as l, JWS_ALG_CTX as n, decodeProtectedHeader as o, buildJWKSetFilter as r, parseEphemeralKey as s, JWE_ALG_CTX as t };
|