@orkestrel/server 0.0.15 → 0.0.17
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/dist/src/server/index.cjs +19 -57
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +10 -39
- package/dist/src/server/index.d.ts +10 -39
- package/dist/src/server/index.js +20 -56
- package/dist/src/server/index.js.map +1 -1
- package/package.json +17 -11
|
@@ -2,6 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let _orkestrel_contract = require("@orkestrel/contract");
|
|
3
3
|
let node_events = require("node:events");
|
|
4
4
|
let node_net = require("node:net");
|
|
5
|
+
let _orkestrel_codec = require("@orkestrel/codec");
|
|
5
6
|
let node_http = require("node:http");
|
|
6
7
|
let _orkestrel_abort = require("@orkestrel/abort");
|
|
7
8
|
let _orkestrel_timeout = require("@orkestrel/timeout");
|
|
@@ -546,49 +547,6 @@ function clearCookie(headers, name, options) {
|
|
|
546
547
|
}));
|
|
547
548
|
}
|
|
548
549
|
/**
|
|
549
|
-
* Base64url-encode a byte sequence — the payload encoding {@link signToken} /
|
|
550
|
-
* {@link verifyToken} use, and the signature encoding for the HMAC.
|
|
551
|
-
*
|
|
552
|
-
* @param bytes - The bytes to encode
|
|
553
|
-
* @returns The base64url string (no padding)
|
|
554
|
-
*
|
|
555
|
-
* @example
|
|
556
|
-
* ```ts
|
|
557
|
-
* encodeBase64Url(new TextEncoder().encode('hi')) // 'aGk'
|
|
558
|
-
* ```
|
|
559
|
-
*/
|
|
560
|
-
function encodeBase64Url(bytes) {
|
|
561
|
-
let binary = "";
|
|
562
|
-
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
563
|
-
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* Decode a base64url string back into its bytes — the inverse of
|
|
567
|
-
* {@link encodeBase64Url}.
|
|
568
|
-
*
|
|
569
|
-
* @remarks
|
|
570
|
-
* Restores the standard base64 alphabet + padding before decoding with
|
|
571
|
-
* `atob`. THROWS `DOMException` on a malformed input — callers on the
|
|
572
|
-
* untrusted-token path (`verifyToken`) catch it to stay total (AGENTS §14).
|
|
573
|
-
*
|
|
574
|
-
* @param value - The base64url string to decode
|
|
575
|
-
* @returns The decoded bytes
|
|
576
|
-
* @throws {DOMException} When `value` is not valid base64url
|
|
577
|
-
*
|
|
578
|
-
* @example
|
|
579
|
-
* ```ts
|
|
580
|
-
* new TextDecoder().decode(decodeBase64Url('aGk')) // 'hi'
|
|
581
|
-
* ```
|
|
582
|
-
*/
|
|
583
|
-
function decodeBase64Url(value) {
|
|
584
|
-
const restored = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
585
|
-
const padding = restored.length % 4 === 0 ? "" : "=".repeat(4 - restored.length % 4);
|
|
586
|
-
const binary = atob(restored + padding);
|
|
587
|
-
const bytes = new Uint8Array(binary.length);
|
|
588
|
-
for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index);
|
|
589
|
-
return bytes;
|
|
590
|
-
}
|
|
591
|
-
/**
|
|
592
550
|
* Sign a value into a stateless, HMAC-SHA256 token — `<payload>.<signature>`.
|
|
593
551
|
*
|
|
594
552
|
* @remarks
|
|
@@ -617,7 +575,7 @@ async function signToken(value, options) {
|
|
|
617
575
|
const secret = normalizeSecret(options.secret)[0];
|
|
618
576
|
if (secret === void 0) throw new HTTPError(500, "signToken requires at least one secret");
|
|
619
577
|
const exp = options.ttl !== void 0 ? Date.now() + options.ttl : void 0;
|
|
620
|
-
const encoded =
|
|
578
|
+
const encoded = (0, _orkestrel_codec.encodeBase64URL)(new TextEncoder().encode(JSON.stringify({
|
|
621
579
|
value,
|
|
622
580
|
exp
|
|
623
581
|
})));
|
|
@@ -626,15 +584,17 @@ async function signToken(value, options) {
|
|
|
626
584
|
hash: "SHA-256"
|
|
627
585
|
}, false, ["sign"]);
|
|
628
586
|
const signature = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(encoded));
|
|
629
|
-
return `${encoded}.${
|
|
587
|
+
return `${encoded}.${(0, _orkestrel_codec.encodeBase64URL)(new Uint8Array(signature))}`;
|
|
630
588
|
}
|
|
631
589
|
/**
|
|
632
590
|
* Verify a stateless token and return its embedded value — TOTAL, never throws.
|
|
633
591
|
*
|
|
634
592
|
* @remarks
|
|
635
593
|
* The inverse of {@link signToken}: splits the token on its LAST `.` (so a
|
|
636
|
-
* value containing dots survives),
|
|
637
|
-
*
|
|
594
|
+
* value containing dots survives), decodes the signature with
|
|
595
|
+
* `@orkestrel/codec`'s `decodeBase64URL` — a signature that is not canonical
|
|
596
|
+
* base64url is refused there, before any key import — and checks the payload's
|
|
597
|
+
* HMAC-SHA256 signature against EACH {@link TokenSecret} candidate via
|
|
638
598
|
* `crypto.subtle.verify` (constant-time internally — the old `safeCompare` is
|
|
639
599
|
* retired, §3 of the proposal), accepting the token on the first match (the
|
|
640
600
|
* rotation path). It then decodes + narrows the payload (`isRecord` +
|
|
@@ -658,10 +618,10 @@ async function verifyToken(token, secret) {
|
|
|
658
618
|
const dot = token.lastIndexOf(".");
|
|
659
619
|
if (dot < 0) return void 0;
|
|
660
620
|
const encoded = token.slice(0, dot);
|
|
661
|
-
const
|
|
621
|
+
const signature = (0, _orkestrel_codec.decodeBase64URL)(token.slice(dot + 1));
|
|
622
|
+
if (signature === void 0) return void 0;
|
|
623
|
+
const data = new TextEncoder().encode(encoded);
|
|
662
624
|
try {
|
|
663
|
-
const signature = decodeBase64Url(signatureText);
|
|
664
|
-
const data = new TextEncoder().encode(encoded);
|
|
665
625
|
for (const candidate of secrets) {
|
|
666
626
|
const key = await crypto.subtle.importKey("raw", new TextEncoder().encode(candidate), {
|
|
667
627
|
name: "HMAC",
|
|
@@ -680,17 +640,21 @@ async function verifyToken(token, secret) {
|
|
|
680
640
|
* signature match.
|
|
681
641
|
*
|
|
682
642
|
* @remarks
|
|
683
|
-
* Decodes the
|
|
684
|
-
*
|
|
685
|
-
*
|
|
643
|
+
* Decodes the payload with `@orkestrel/codec`'s `decodeBase64URL` (total — a
|
|
644
|
+
* non-canonical base64url segment answers `undefined` rather than throwing),
|
|
645
|
+
* reads it as UTF-8 JSON, narrows it to a record with a string `value`
|
|
646
|
+
* (AGENTS §14 — never `as`), and rejects an expired `exp`. TOTAL — any
|
|
647
|
+
* decode/shape/expiry failure yields `undefined`; only `JSON.parse` still
|
|
648
|
+
* throws, and its `catch` answers `undefined` too.
|
|
686
649
|
*
|
|
687
650
|
* @param encoded - The base64url-encoded payload segment (before the last `.`)
|
|
688
651
|
* @returns The embedded value when the payload is well-shaped + unexpired, else `undefined`
|
|
689
652
|
*/
|
|
690
653
|
function decodeTokenPayload(encoded) {
|
|
654
|
+
const bytes = (0, _orkestrel_codec.decodeBase64URL)(encoded);
|
|
655
|
+
if (bytes === void 0) return void 0;
|
|
691
656
|
try {
|
|
692
|
-
const
|
|
693
|
-
const payload = JSON.parse(text);
|
|
657
|
+
const payload = JSON.parse(new TextDecoder().decode(bytes));
|
|
694
658
|
if (!(0, _orkestrel_contract.isRecord)(payload)) return void 0;
|
|
695
659
|
if (typeof payload.value !== "string") return void 0;
|
|
696
660
|
if (payload.exp !== void 0 && typeof payload.exp !== "number") return void 0;
|
|
@@ -2201,12 +2165,10 @@ exports.compose = compose;
|
|
|
2201
2165
|
exports.computeBodyETag = computeBodyETag;
|
|
2202
2166
|
exports.createNegotiator = createNegotiator;
|
|
2203
2167
|
exports.createServer = createServer;
|
|
2204
|
-
exports.decodeBase64Url = decodeBase64Url;
|
|
2205
2168
|
exports.decodeCookieValue = decodeCookieValue;
|
|
2206
2169
|
exports.decodeTokenPayload = decodeTokenPayload;
|
|
2207
2170
|
exports.decompressRequestBody = decompressRequestBody;
|
|
2208
2171
|
exports.discoverPort = discoverPort;
|
|
2209
|
-
exports.encodeBase64Url = encodeBase64Url;
|
|
2210
2172
|
exports.enqueueStreamText = enqueueStreamText;
|
|
2211
2173
|
exports.ipv6Network = ipv6Network;
|
|
2212
2174
|
exports.isAddressInfo = isAddressInfo;
|