@orangecheck/me-client 0.38.0 → 0.38.2

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 CHANGED
@@ -195,6 +195,8 @@ These mirror what the me.ochk.io API accepts and returns. A change to the API sh
195
195
 
196
196
  Verify the OC session on your backend without writing a JWK fetcher, an env-var loader, or a cache. The SDK does that internally.
197
197
 
198
+ The adapters and `getOcSession` / `checkOcSession` throw on an option they do not read, so a misspelt `projectKey`, `audience` or bond threshold is never ignored.
199
+
198
200
  ### Next.js Pages Router
199
201
 
200
202
  ```ts
package/dist/server.js CHANGED
@@ -604,14 +604,19 @@ async function getOcSession(headers, options = {}) {
604
604
  return checked.status === "signed_in" ? checked.session : null;
605
605
  }
606
606
  async function checkOcSession(headers, options = {}) {
607
+ checkOptions(options);
607
608
  const session = await authCore.getOcSession(headers, options);
608
609
  if (!session) {
609
610
  if (options.audience === void 0) warnIfAudienceMissing(headers);
610
611
  return { status: "signed_out" };
611
612
  }
612
613
  const check = revocationFor(options);
613
- if (check && await check.refuses(session, options.method)) {
614
- return await check.status(session) === "revoked" ? { status: "signed_out" } : { status: "unavailable" };
614
+ if (check) {
615
+ const state = await check.status(session);
616
+ if (state === "revoked") return { status: "signed_out" };
617
+ if (state === "unverified" && !authCore.SAFE_METHODS.has((options.method ?? "").toUpperCase())) {
618
+ return { status: "unavailable" };
619
+ }
615
620
  }
616
621
  return { status: "signed_in", session };
617
622
  }
@@ -641,13 +646,51 @@ function base64UrlToString(s) {
641
646
  if (typeof atob === "function") return atob(b64 + "=".repeat((4 - b64.length % 4) % 4));
642
647
  return Buffer.from(s, "base64url").toString("utf8");
643
648
  }
649
+ var KNOWN_OPTIONS = /* @__PURE__ */ new Set([
650
+ "issuer",
651
+ "jwksCacheTtlMs",
652
+ "jwksMaxStaleMs",
653
+ "audience",
654
+ "method",
655
+ "projectKey",
656
+ "meOrigin",
657
+ "revocation",
658
+ "required",
659
+ "requireBond"
660
+ ]);
661
+ var BOND_OPTIONS = /* @__PURE__ */ new Set(["minSats", "minDays", "maxAgeSecs"]);
662
+ function checkOptions(options) {
663
+ if (options === null || typeof options !== "object") {
664
+ throw new TypeError("@orangecheck/me-client: options must be an object");
665
+ }
666
+ const unknown = Object.keys(options).filter((k) => !KNOWN_OPTIONS.has(k));
667
+ if (unknown.length > 0) {
668
+ throw new TypeError(
669
+ `@orangecheck/me-client: unknown option ${unknown.join(", ")}. It reads only ${[...KNOWN_OPTIONS].join(", ")}.`
670
+ );
671
+ }
672
+ const bond = options.requireBond;
673
+ if (bond === void 0) return;
674
+ if (bond === null || typeof bond !== "object") {
675
+ throw new TypeError("@orangecheck/me-client: requireBond must be an object");
676
+ }
677
+ const extra = Object.keys(bond).filter((k) => !BOND_OPTIONS.has(k));
678
+ if (extra.length > 0) {
679
+ throw new TypeError(
680
+ `@orangecheck/me-client: requireBond: unknown option ${extra.join(", ")}. It reads only ${[...BOND_OPTIONS].join(", ")}.`
681
+ );
682
+ }
683
+ }
644
684
  var BACK_ACCOUNT_URL = "https://ochk.app/me/account#backing";
685
+ var stamp = () => ({ mono: performance.now(), wall: Date.now() });
686
+ var since = (s) => Math.max(performance.now() - s.mono, Date.now() - s.wall);
645
687
  var unknownKeys = /* @__PURE__ */ new Map();
646
688
  var UNKNOWN_PROJECT = /* @__PURE__ */ Symbol("unknown_project");
647
689
  var POLICY_TTL_MS = 6e4;
648
690
  var POLICY_RETRY_MS = 1e4;
649
691
  var POLICY_MAX_STALE_MS = 6e5;
650
692
  var policies = /* @__PURE__ */ new Map();
693
+ var policyReads = /* @__PURE__ */ new Map();
651
694
  var isFloor = (n) => Number.isSafeInteger(n) && n >= 0;
652
695
  function readRequirement(body) {
653
696
  const b = body;
@@ -657,16 +700,27 @@ function readRequirement(body) {
657
700
  if (!r || typeof r !== "object" || !isFloor(r.min_sats) || !isFloor(r.min_days)) return void 0;
658
701
  return { minSats: r.min_sats, minDays: r.min_days };
659
702
  }
703
+ function standing(slot) {
704
+ const hit = policies.get(slot);
705
+ return hit?.at && since(hit.at) < POLICY_MAX_STALE_MS ? hit.need : void 0;
706
+ }
660
707
  async function projectRequirement(options) {
661
708
  const base = (options.meOrigin ?? "https://me.ochk.io").replace(/\/$/, "");
662
709
  const key = options.projectKey;
663
710
  const slot = `${base} ${key}`;
711
+ const unknownAt = unknownKeys.get(slot);
712
+ if (unknownAt && since(unknownAt) < POLICY_TTL_MS) return UNKNOWN_PROJECT;
664
713
  const hit = policies.get(slot);
665
- const now = Date.now();
666
- const standing = () => hit?.at && now - hit.at < POLICY_MAX_STALE_MS ? hit.need : void 0;
667
- if (unknownKeys.has(slot) && now - unknownKeys.get(slot) < POLICY_TTL_MS) return UNKNOWN_PROJECT;
668
- if (hit && now - hit.at < POLICY_TTL_MS) return hit.need;
669
- if (hit?.failedAt && now - hit.failedAt < POLICY_RETRY_MS) return standing();
714
+ if (hit?.at && since(hit.at) < POLICY_TTL_MS) return hit.need;
715
+ if (hit?.failedAt && since(hit.failedAt) < POLICY_RETRY_MS) return standing(slot);
716
+ let read = policyReads.get(slot);
717
+ if (!read) {
718
+ read = readProjectRule(base, key, slot).finally(() => policyReads.delete(slot));
719
+ policyReads.set(slot, read);
720
+ }
721
+ return read;
722
+ }
723
+ async function readProjectRule(base, key, slot) {
670
724
  try {
671
725
  const r = await fetch(`${base}/api/integrator/${encodeURIComponent(key)}/sign-in`, {
672
726
  signal: AbortSignal.timeout(4e3)
@@ -677,18 +731,19 @@ async function projectRequirement(options) {
677
731
  `[@orangecheck/me-client] projectKey "${key}" is not a project. Copy yours from https://ochk.app/me/projects.`
678
732
  );
679
733
  }
680
- unknownKeys.set(slot, Date.now());
734
+ unknownKeys.set(slot, stamp());
681
735
  return UNKNOWN_PROJECT;
682
736
  }
683
737
  unknownKeys.delete(slot);
684
738
  if (!r.ok) throw new Error(`HTTP ${r.status}`);
685
739
  const need = readRequirement(await r.json());
686
740
  if (need === void 0) throw new Error("unexpected body");
687
- policies.set(slot, { at: Date.now(), need });
741
+ policies.set(slot, { at: stamp(), need });
688
742
  return need;
689
743
  } catch {
690
- policies.set(slot, { at: hit?.at ?? 0, need: hit?.need ?? null, failedAt: Date.now() });
691
- return standing();
744
+ const hit = policies.get(slot);
745
+ policies.set(slot, { at: hit?.at ?? null, need: hit?.need ?? null, failedAt: stamp() });
746
+ return standing(slot);
692
747
  }
693
748
  }
694
749
  async function bondNeed(options) {
@@ -731,6 +786,7 @@ async function refusal(session, options) {
731
786
  return null;
732
787
  }
733
788
  function withOcAuth(handler, options = {}) {
789
+ checkOptions(options);
734
790
  return async (req, res) => {
735
791
  res.setHeader("Cache-Control", "no-store, private");
736
792
  res.setHeader("Vary", "Cookie, Authorization");
@@ -750,6 +806,7 @@ function withOcAuth(handler, options = {}) {
750
806
  };
751
807
  }
752
808
  function ocAuthExpress(options = {}) {
809
+ checkOptions(options);
753
810
  return async (req, res, next) => {
754
811
  try {
755
812
  res.setHeader("Cache-Control", "no-store, private");
@@ -775,6 +832,7 @@ function ocAuthExpress(options = {}) {
775
832
  };
776
833
  }
777
834
  function ocAuthHono(options = {}) {
835
+ checkOptions(options);
778
836
  return async (c, next) => {
779
837
  c.header("Cache-Control", "no-store, private");
780
838
  c.header("Vary", "Cookie, Authorization");