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