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
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a Uint8Array of cryptographically secure random bytes.
|
|
3
|
+
*
|
|
4
|
+
* @param length Number of random bytes to generate.
|
|
5
|
+
* @returns A Uint8Array filled with random bytes.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const key = secureRandomBytes(32); // 256-bit key material
|
|
9
|
+
*/
|
|
10
|
+
declare function secureRandomBytes(length: number): Uint8Array<ArrayBuffer>;
|
|
11
|
+
/**
|
|
12
|
+
* Shuffles an array in-place using the Fisher-Yates algorithm with a
|
|
13
|
+
* cryptographically secure random number generator.
|
|
14
|
+
* @template T
|
|
15
|
+
* @param {Array<T>} array The array to shuffle.
|
|
16
|
+
* @param {SecureRandomGenerator} [generator] - An optional random number generator to reuse.
|
|
17
|
+
*
|
|
18
|
+
* @description If you require to shuffle multiple times it is adviced to pass the `generator` parameter to avoid creating a new generator for each shuffle. (refer to `createSecureRandomGenerator()` factory)
|
|
19
|
+
*
|
|
20
|
+
* @returns {Array<T>} The shuffled array.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Parse JSON while stripping prototype-pollution vectors during parsing.
|
|
24
|
+
*
|
|
25
|
+
* Uses a reviver to drop own properties named `__proto__`, `prototype`, or
|
|
26
|
+
* `constructor` before they can be assigned. For nested payloads this is
|
|
27
|
+
* cheaper than parsing first and sanitizing after, and it guarantees no
|
|
28
|
+
* unsanitized object ever exists.
|
|
29
|
+
*
|
|
30
|
+
* @param json The JSON text to parse.
|
|
31
|
+
* @returns The parsed value with dangerous keys stripped.
|
|
32
|
+
* @throws {SyntaxError} If `json` is not valid JSON.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const payload = safeJsonParse<{ user: { name: string } }>(untrustedInput);
|
|
36
|
+
*/
|
|
37
|
+
declare function safeJsonParse<T = any>(json: string): T;
|
|
38
|
+
/**
|
|
39
|
+
* Remove prototype-pollution vectors from a plain record in-place.
|
|
40
|
+
*
|
|
41
|
+
* Strips own properties named `__proto__`, `prototype`, and `constructor`
|
|
42
|
+
* recursively through nested objects and arrays. Cycle-safe.
|
|
43
|
+
*
|
|
44
|
+
* Returns the same reference for convenience. Use {@link sanitizeObjectCopy}
|
|
45
|
+
* if you need a deep copy with the original preserved.
|
|
46
|
+
*/
|
|
47
|
+
declare function sanitizeObject<T extends Record<string, unknown> | undefined>(obj: T): T;
|
|
48
|
+
/**
|
|
49
|
+
* Return a sanitized deep copy of `obj`. The input is never mutated.
|
|
50
|
+
*
|
|
51
|
+
* Dangerous keys (`__proto__`, `prototype`, `constructor`) are stripped
|
|
52
|
+
* recursively. The returned structure consists of plain objects (rooted on
|
|
53
|
+
* `Object.prototype`) and arrays. Cycle-safe: circular references in the
|
|
54
|
+
* input are preserved in the output (pointing at the copied node, not the
|
|
55
|
+
* original reference).
|
|
56
|
+
*
|
|
57
|
+
* Non-object / undefined inputs are returned unchanged.
|
|
58
|
+
*/
|
|
59
|
+
declare function sanitizeObjectCopy<T extends Record<string, unknown> | undefined>(obj: T): T;
|
|
60
|
+
/**
|
|
61
|
+
* Base64 encoding. Accepts `string` or raw bytes; throws `TypeError` on
|
|
62
|
+
* `null`/`undefined`. Empty strings and empty `Uint8Array` return `""`.
|
|
63
|
+
*/
|
|
64
|
+
declare function base64Encode(data: Uint8Array<ArrayBuffer> | string): string;
|
|
65
|
+
/**
|
|
66
|
+
* Base64 URL-safe encoding (no padding). Accepts `string` or raw bytes;
|
|
67
|
+
* throws `TypeError` on `null`/`undefined`. Empty inputs return `""`.
|
|
68
|
+
*/
|
|
69
|
+
declare function base64UrlEncode(data: Uint8Array<ArrayBuffer> | string): string;
|
|
70
|
+
declare function base64Decode<T extends DecodeReturnAs>(data: string | Uint8Array<ArrayBuffer>, options: {
|
|
71
|
+
returnAs: T;
|
|
72
|
+
}): T extends "string" ? string : Uint8Array<ArrayBuffer>;
|
|
73
|
+
declare function base64Decode(data: string): string;
|
|
74
|
+
declare function base64Decode(data: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
|
|
75
|
+
declare function base64UrlDecode<T extends DecodeReturnAs>(data: string | Uint8Array<ArrayBuffer>, options: {
|
|
76
|
+
returnAs: T;
|
|
77
|
+
}): T extends "string" ? string : Uint8Array<ArrayBuffer>;
|
|
78
|
+
declare function base64UrlDecode(data: string): string;
|
|
79
|
+
declare function base64UrlDecode(data: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
|
|
80
|
+
/**
|
|
81
|
+
* Hex encoding. Accepts `string` or raw bytes; throws `TypeError` on
|
|
82
|
+
* `null`/`undefined`. Empty strings and empty `Uint8Array` return `""`.
|
|
83
|
+
*/
|
|
84
|
+
/**
|
|
85
|
+
* Output-shape option shared by `hexDecode`, `base64Decode`, `base64UrlDecode`,
|
|
86
|
+
* and `base32Decode`. Decoders mirror their input type when this is omitted:
|
|
87
|
+
* `string` input decodes to `string`, `Uint8Array` input decodes to `Uint8Array`.
|
|
88
|
+
*
|
|
89
|
+
* `"bytes"` is an alias for `"uint8array"`.
|
|
90
|
+
*/
|
|
91
|
+
type DecodeReturnAs = "string" | "uint8array" | "bytes";
|
|
92
|
+
declare const textEncoder: TextEncoder;
|
|
93
|
+
declare const textDecoder: TextDecoder;
|
|
94
|
+
export { textDecoder as a, sanitizeObject as c, base64UrlEncode as i, sanitizeObjectCopy as l, base64Encode as n, textEncoder as o, base64UrlDecode as r, safeJsonParse as s, base64Decode as t, secureRandomBytes as u };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
function safeJsonParse(json) {
|
|
2
|
+
return JSON.parse(json, _jsonReviver);
|
|
3
|
+
}
|
|
4
|
+
function sanitizeObject(obj) {
|
|
5
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
6
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
7
|
+
seen.add(obj);
|
|
8
|
+
_sanitizeInPlace(obj, seen);
|
|
9
|
+
return obj;
|
|
10
|
+
}
|
|
11
|
+
function sanitizeObjectCopy(obj) {
|
|
12
|
+
if (!obj || typeof obj !== "object") return obj;
|
|
13
|
+
return _sanitizeCopy(obj, /* @__PURE__ */ new WeakMap());
|
|
14
|
+
}
|
|
15
|
+
const _jsonReviver = (key, value) => _isDangerousKey(key) ? void 0 : value;
|
|
16
|
+
function _isDangerousKey(key) {
|
|
17
|
+
return key === "__proto__" || key === "prototype" || key === "constructor";
|
|
18
|
+
}
|
|
19
|
+
function _sanitizeInPlace(current, seen) {
|
|
20
|
+
if (Array.isArray(current)) {
|
|
21
|
+
for (let i = 0; i < current.length; i++) {
|
|
22
|
+
const v = current[i];
|
|
23
|
+
if (v !== null && typeof v === "object" && !seen.has(v)) {
|
|
24
|
+
seen.add(v);
|
|
25
|
+
_sanitizeInPlace(v, seen);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const keys = Object.keys(current);
|
|
31
|
+
for (let i = 0; i < keys.length; i++) {
|
|
32
|
+
const key = keys[i];
|
|
33
|
+
if (_isDangerousKey(key)) {
|
|
34
|
+
delete current[key];
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const v = current[key];
|
|
38
|
+
if (v !== null && typeof v === "object" && !seen.has(v)) {
|
|
39
|
+
seen.add(v);
|
|
40
|
+
_sanitizeInPlace(v, seen);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function _sanitizeCopy(current, seen) {
|
|
45
|
+
const existing = seen.get(current);
|
|
46
|
+
if (existing !== void 0) return existing;
|
|
47
|
+
if (Array.isArray(current)) {
|
|
48
|
+
const out = [];
|
|
49
|
+
seen.set(current, out);
|
|
50
|
+
for (let i = 0; i < current.length; i++) {
|
|
51
|
+
const v = current[i];
|
|
52
|
+
out.push(v !== null && typeof v === "object" ? _sanitizeCopy(v, seen) : v);
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
const out = {};
|
|
57
|
+
seen.set(current, out);
|
|
58
|
+
const keys = Object.keys(current);
|
|
59
|
+
for (let i = 0; i < keys.length; i++) {
|
|
60
|
+
const key = keys[i];
|
|
61
|
+
if (_isDangerousKey(key)) continue;
|
|
62
|
+
const v = current[key];
|
|
63
|
+
out[key] = v !== null && typeof v === "object" ? _sanitizeCopy(v, seen) : v;
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
const _Buffer = /* @__PURE__ */ (() => {
|
|
68
|
+
try {
|
|
69
|
+
return globalThis.Buffer;
|
|
70
|
+
} catch {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
})();
|
|
74
|
+
const _hasBuffer = typeof _Buffer?.from === "function";
|
|
75
|
+
function _toBuffer(data) {
|
|
76
|
+
return data instanceof Uint8Array ? _Buffer.from(data.buffer, data.byteOffset, data.byteLength) : _Buffer.from(data);
|
|
77
|
+
}
|
|
78
|
+
const _hasToBase64 = !_hasBuffer && typeof Uint8Array.prototype.toBase64 === "function";
|
|
79
|
+
const _hasFromBase64 = !_hasBuffer && typeof Uint8Array.fromBase64 === "function";
|
|
80
|
+
const _b64UrlEncOpts = /* @__PURE__ */ Object.freeze({
|
|
81
|
+
alphabet: "base64url",
|
|
82
|
+
omitPadding: true
|
|
83
|
+
});
|
|
84
|
+
const _b64UrlDecOpts = /* @__PURE__ */ Object.freeze({ alphabet: "base64url" });
|
|
85
|
+
function base64Encode(data) {
|
|
86
|
+
if (data == null) throw new TypeError("base64Encode: data must be a string or Uint8Array.");
|
|
87
|
+
if (_hasBuffer) return _toBuffer(data).toString("base64");
|
|
88
|
+
const encodedData = data instanceof Uint8Array ? data : textEncoder.encode(data);
|
|
89
|
+
if (_hasToBase64) return encodedData.toBase64();
|
|
90
|
+
return btoa(String.fromCodePoint(...encodedData));
|
|
91
|
+
}
|
|
92
|
+
function base64UrlEncode(data) {
|
|
93
|
+
if (data == null) throw new TypeError("base64UrlEncode: data must be a string or Uint8Array.");
|
|
94
|
+
if (_hasBuffer) return _toBuffer(data).toString("base64url");
|
|
95
|
+
const encodedData = data instanceof Uint8Array ? data : textEncoder.encode(data);
|
|
96
|
+
if (_hasToBase64) return encodedData.toBase64(_b64UrlEncOpts);
|
|
97
|
+
return btoa(String.fromCodePoint(...encodedData)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
98
|
+
}
|
|
99
|
+
function base64Decode(data, options) {
|
|
100
|
+
if (data == null) throw new TypeError("base64Decode: data must be a string or Uint8Array.");
|
|
101
|
+
const isBufferInput = data instanceof Uint8Array;
|
|
102
|
+
const str = isBufferInput ? textDecoder.decode(data) : data;
|
|
103
|
+
const decodeToString = (options?.returnAs ?? (isBufferInput ? "uint8array" : "string")) === "string";
|
|
104
|
+
if (!str) return decodeToString ? "" : new Uint8Array(0);
|
|
105
|
+
if (_hasBuffer) {
|
|
106
|
+
const buf = _Buffer.from(str, "base64");
|
|
107
|
+
return decodeToString ? buf.toString("utf8") : new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
108
|
+
}
|
|
109
|
+
const decoded = _hasFromBase64 ? Uint8Array.fromBase64(str) : Uint8Array.from(atob(str), (b) => b.codePointAt(0));
|
|
110
|
+
return decodeToString ? textDecoder.decode(decoded) : decoded;
|
|
111
|
+
}
|
|
112
|
+
function base64UrlDecode(data, options) {
|
|
113
|
+
if (data == null) throw new TypeError("base64UrlDecode: data must be a string or Uint8Array.");
|
|
114
|
+
const isBufferInput = data instanceof Uint8Array;
|
|
115
|
+
let str = isBufferInput ? textDecoder.decode(data) : data;
|
|
116
|
+
const decodeToString = (options?.returnAs ?? (isBufferInput ? "uint8array" : "string")) === "string";
|
|
117
|
+
if (!str) return decodeToString ? "" : new Uint8Array(0);
|
|
118
|
+
if (_hasBuffer) {
|
|
119
|
+
const buf = _Buffer.from(str, "base64url");
|
|
120
|
+
return decodeToString ? buf.toString("utf8") : new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
121
|
+
}
|
|
122
|
+
let decoded;
|
|
123
|
+
if (_hasFromBase64) decoded = Uint8Array.fromBase64(str, _b64UrlDecOpts);
|
|
124
|
+
else {
|
|
125
|
+
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
126
|
+
while (str.length % 4) str += "=";
|
|
127
|
+
decoded = Uint8Array.from(atob(str), (b) => b.codePointAt(0));
|
|
128
|
+
}
|
|
129
|
+
return decodeToString ? textDecoder.decode(decoded) : decoded;
|
|
130
|
+
}
|
|
131
|
+
!_hasBuffer && Uint8Array.prototype.toHex;
|
|
132
|
+
!_hasBuffer && Uint8Array.fromHex;
|
|
133
|
+
const textEncoder = /* @__PURE__ */ new TextEncoder();
|
|
134
|
+
const textDecoder = /* @__PURE__ */ new TextDecoder();
|
|
135
|
+
function secureRandomBytes(length) {
|
|
136
|
+
if (!Number.isInteger(length) || length < 0) throw new RangeError("length must be a non-negative integer.");
|
|
137
|
+
const bytes = new Uint8Array(length);
|
|
138
|
+
if (length > 0) for (let offset = 0; offset < length; offset += 65536) {
|
|
139
|
+
const chunk = bytes.subarray(offset, Math.min(offset + 65536, length));
|
|
140
|
+
crypto.getRandomValues(chunk);
|
|
141
|
+
}
|
|
142
|
+
return bytes;
|
|
143
|
+
}
|
|
144
|
+
export { base64UrlEncode as a, safeJsonParse as c, base64UrlDecode as i, sanitizeObject as l, base64Decode as n, textDecoder as o, base64Encode as r, textEncoder as s, secureRandomBytes as t, sanitizeObjectCopy as u };
|