@orangecheck/me-client 0.37.0 → 0.38.1

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;
@@ -599,15 +599,26 @@ function _resetSessionChecksForTests() {
599
599
  warnedUnchecked = false;
600
600
  }
601
601
  async function getOcSession(headers, options = {}) {
602
+ const checked = await checkOcSession(headers, options);
603
+ return checked.status === "signed_in" ? checked.session : null;
604
+ }
605
+ async function checkOcSession(headers, options = {}) {
602
606
  const session = await getOcSession$1(headers, options);
603
607
  if (!session) {
604
608
  if (options.audience === void 0) warnIfAudienceMissing(headers);
605
- return null;
609
+ return { status: "signed_out" };
606
610
  }
607
611
  const check = revocationFor(options);
608
- if (check && await check.refuses(session, options.method)) return null;
609
- return session;
612
+ if (check) {
613
+ const state = await check.status(session);
614
+ if (state === "revoked") return { status: "signed_out" };
615
+ if (state === "unverified" && !SAFE_METHODS.has((options.method ?? "").toUpperCase())) {
616
+ return { status: "unavailable" };
617
+ }
618
+ }
619
+ return { status: "signed_in", session };
610
620
  }
621
+ var UNAVAILABLE = { status: 503, body: { ok: false, reason: "revocations_unavailable" } };
611
622
  var warnedAudience = false;
612
623
  function warnIfAudienceMissing(headers) {
613
624
  if (warnedAudience) return;
@@ -634,12 +645,15 @@ function base64UrlToString(s) {
634
645
  return Buffer.from(s, "base64url").toString("utf8");
635
646
  }
636
647
  var BACK_ACCOUNT_URL = "https://ochk.app/me/account#backing";
648
+ var stamp = () => ({ mono: performance.now(), wall: Date.now() });
649
+ var since = (s) => Math.max(performance.now() - s.mono, Date.now() - s.wall);
637
650
  var unknownKeys = /* @__PURE__ */ new Map();
638
651
  var UNKNOWN_PROJECT = /* @__PURE__ */ Symbol("unknown_project");
639
652
  var POLICY_TTL_MS = 6e4;
640
653
  var POLICY_RETRY_MS = 1e4;
641
654
  var POLICY_MAX_STALE_MS = 6e5;
642
655
  var policies = /* @__PURE__ */ new Map();
656
+ var policyReads = /* @__PURE__ */ new Map();
643
657
  var isFloor = (n) => Number.isSafeInteger(n) && n >= 0;
644
658
  function readRequirement(body) {
645
659
  const b = body;
@@ -649,16 +663,27 @@ function readRequirement(body) {
649
663
  if (!r || typeof r !== "object" || !isFloor(r.min_sats) || !isFloor(r.min_days)) return void 0;
650
664
  return { minSats: r.min_sats, minDays: r.min_days };
651
665
  }
666
+ function standing(slot) {
667
+ const hit = policies.get(slot);
668
+ return hit?.at && since(hit.at) < POLICY_MAX_STALE_MS ? hit.need : void 0;
669
+ }
652
670
  async function projectRequirement(options) {
653
671
  const base = (options.meOrigin ?? "https://me.ochk.io").replace(/\/$/, "");
654
672
  const key = options.projectKey;
655
673
  const slot = `${base} ${key}`;
674
+ const unknownAt = unknownKeys.get(slot);
675
+ if (unknownAt && since(unknownAt) < POLICY_TTL_MS) return UNKNOWN_PROJECT;
656
676
  const hit = policies.get(slot);
657
- const now = Date.now();
658
- const standing = () => hit?.at && now - hit.at < POLICY_MAX_STALE_MS ? hit.need : void 0;
659
- if (unknownKeys.has(slot) && now - unknownKeys.get(slot) < POLICY_TTL_MS) return UNKNOWN_PROJECT;
660
- if (hit && now - hit.at < POLICY_TTL_MS) return hit.need;
661
- if (hit?.failedAt && now - hit.failedAt < POLICY_RETRY_MS) return standing();
677
+ if (hit?.at && since(hit.at) < POLICY_TTL_MS) return hit.need;
678
+ if (hit?.failedAt && since(hit.failedAt) < POLICY_RETRY_MS) return standing(slot);
679
+ let read = policyReads.get(slot);
680
+ if (!read) {
681
+ read = readProjectRule(base, key, slot).finally(() => policyReads.delete(slot));
682
+ policyReads.set(slot, read);
683
+ }
684
+ return read;
685
+ }
686
+ async function readProjectRule(base, key, slot) {
662
687
  try {
663
688
  const r = await fetch(`${base}/api/integrator/${encodeURIComponent(key)}/sign-in`, {
664
689
  signal: AbortSignal.timeout(4e3)
@@ -669,18 +694,19 @@ async function projectRequirement(options) {
669
694
  `[@orangecheck/me-client] projectKey "${key}" is not a project. Copy yours from https://ochk.app/me/projects.`
670
695
  );
671
696
  }
672
- unknownKeys.set(slot, Date.now());
697
+ unknownKeys.set(slot, stamp());
673
698
  return UNKNOWN_PROJECT;
674
699
  }
675
700
  unknownKeys.delete(slot);
676
701
  if (!r.ok) throw new Error(`HTTP ${r.status}`);
677
702
  const need = readRequirement(await r.json());
678
703
  if (need === void 0) throw new Error("unexpected body");
679
- policies.set(slot, { at: Date.now(), need });
704
+ policies.set(slot, { at: stamp(), need });
680
705
  return need;
681
706
  } catch {
682
- policies.set(slot, { at: hit?.at ?? 0, need: hit?.need ?? null, failedAt: Date.now() });
683
- return standing();
707
+ const hit = policies.get(slot);
708
+ policies.set(slot, { at: hit?.at ?? null, need: hit?.need ?? null, failedAt: stamp() });
709
+ return standing(slot);
684
710
  }
685
711
  }
686
712
  async function bondNeed(options) {
@@ -726,16 +752,17 @@ function withOcAuth(handler, options = {}) {
726
752
  return async (req, res) => {
727
753
  res.setHeader("Cache-Control", "no-store, private");
728
754
  res.setHeader("Vary", "Cookie, Authorization");
729
- const session = await getOcSession(
755
+ const checked = await checkOcSession(
730
756
  {
731
757
  cookie: typeof req.headers.cookie === "string" ? req.headers.cookie : null,
732
758
  authorization: typeof req.headers.authorization === "string" ? req.headers.authorization : null
733
759
  },
734
760
  { ...options, method: req.method }
735
761
  );
762
+ const session = checked.status === "signed_in" ? checked.session : null;
736
763
  const withSession = req;
737
764
  withSession.ocSession = session;
738
- const refused = await refusal(session, options);
765
+ const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
739
766
  if (refused) return res.status(refused.status).json(refused.body);
740
767
  return handler(withSession, res);
741
768
  };
@@ -745,15 +772,16 @@ function ocAuthExpress(options = {}) {
745
772
  try {
746
773
  res.setHeader("Cache-Control", "no-store, private");
747
774
  res.setHeader("Vary", "Cookie, Authorization");
748
- const session = await getOcSession(
775
+ const checked = await checkOcSession(
749
776
  {
750
777
  cookie: typeof req.headers.cookie === "string" ? req.headers.cookie : null,
751
778
  authorization: typeof req.headers.authorization === "string" ? req.headers.authorization : null
752
779
  },
753
780
  { ...options, method: req.method }
754
781
  );
782
+ const session = checked.status === "signed_in" ? checked.session : null;
755
783
  req.ocSession = session;
756
- const refused = await refusal(session, options);
784
+ const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
757
785
  if (refused) {
758
786
  res.status(refused.status).json(refused.body);
759
787
  return;
@@ -768,17 +796,18 @@ function ocAuthHono(options = {}) {
768
796
  return async (c, next) => {
769
797
  c.header("Cache-Control", "no-store, private");
770
798
  c.header("Vary", "Cookie, Authorization");
771
- const session = await getOcSession(c.req.raw.headers, {
799
+ const checked = await checkOcSession(c.req.raw.headers, {
772
800
  ...options,
773
801
  method: c.req.raw.method
774
802
  });
803
+ const session = checked.status === "signed_in" ? checked.session : null;
775
804
  c.set("ocSession", session);
776
- const refused = await refusal(session, options);
805
+ const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
777
806
  if (refused) return c.json(refused.body, refused.status);
778
807
  await next();
779
808
  };
780
809
  }
781
810
 
782
- export { BACK_ACCOUNT_URL, _resetSessionChecksForTests, getOcSession, identity, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, verifyActivityAttestation, withOcAuth };
811
+ export { BACK_ACCOUNT_URL, _resetSessionChecksForTests, checkOcSession, getOcSession, identity, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, verifyActivityAttestation, withOcAuth };
783
812
  //# sourceMappingURL=server.mjs.map
784
813
  //# sourceMappingURL=server.mjs.map