@orangecheck/me-client 0.38.2 → 0.39.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/dist/server.mjs CHANGED
@@ -568,6 +568,170 @@ var identity = {
568
568
  refreshEnvelopeJwks
569
569
  };
570
570
 
571
+ // src/connect.ts
572
+ var ORIGIN = "https://ochk.io";
573
+ var DEFAULT_TIMEOUT_MS = 5e3;
574
+ var MAX_TIMEOUT_MS = 3e4;
575
+ var DID_OC = /^did:oc:[0-9a-f]{32}$/;
576
+ var BASE_KEYS = ["apiKey", "userId", "timeoutMs"];
577
+ var PROVISION_KEYS = /* @__PURE__ */ new Set([...BASE_KEYS, "consent", "email", "emailVerified"]);
578
+ var USER_KEYS = new Set(BASE_KEYS);
579
+ var PROVISION_REASONS = /* @__PURE__ */ new Set([
580
+ "account_deleted",
581
+ "daily_limit",
582
+ "rate_limited",
583
+ "invalid_api_key",
584
+ "app_suspended",
585
+ "bad_request"
586
+ ]);
587
+ var DELETE_REASONS = /* @__PURE__ */ new Set([
588
+ "unknown_user",
589
+ "holds_earnings",
590
+ "not_deletable",
591
+ "rate_limited",
592
+ "invalid_api_key",
593
+ "bad_request"
594
+ ]);
595
+ var CLAIM_LINK_REASONS = /* @__PURE__ */ new Set([
596
+ "unknown_user",
597
+ "already_claimed",
598
+ "rate_limited",
599
+ "invalid_api_key",
600
+ "app_suspended",
601
+ "bad_request"
602
+ ]);
603
+ var DELETE_RESULTS = /* @__PURE__ */ new Set(["deleted", "detached", "already_deleted"]);
604
+ function fail(message) {
605
+ throw new TypeError(`@orangecheck/me-client: ${message}`);
606
+ }
607
+ function checkCall(name, options, known) {
608
+ if (typeof document !== "undefined") {
609
+ fail(`${name} runs on your server only: in a browser your API key would be exposed`);
610
+ }
611
+ if (options === null || typeof options !== "object") fail(`${name}: options must be an object`);
612
+ const unknown = Object.keys(options).filter((k) => !known.has(k));
613
+ if (unknown.length > 0) {
614
+ const only = [...known].join(", ");
615
+ fail(`${name}: unknown option ${unknown.join(", ")}. It reads only ${only}.`);
616
+ }
617
+ const o = Object.assign(/* @__PURE__ */ Object.create(null), options);
618
+ if (typeof o.apiKey !== "string" || o.apiKey.trim() === "") {
619
+ fail(`${name}: apiKey is required (your Connect API key)`);
620
+ }
621
+ const id = o.userId;
622
+ if (typeof id !== "string" || id.length < 1 || id.length > 256) {
623
+ fail(`${name}: userId must be your user's id, 1 to 256 characters`);
624
+ }
625
+ const t = o.timeoutMs;
626
+ if (t !== void 0 && !(typeof t === "number" && t > 0 && t <= MAX_TIMEOUT_MS)) {
627
+ fail(`${name}: timeoutMs must be above 0 and at most ${MAX_TIMEOUT_MS}`);
628
+ }
629
+ return o;
630
+ }
631
+ async function post(path, apiKey, body, timeoutMs) {
632
+ const controller = new AbortController();
633
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
634
+ try {
635
+ const res = await fetch(`${ORIGIN}${path}`, {
636
+ method: "POST",
637
+ // These endpoints never redirect, so a redirect is an error: the key and
638
+ // the body go to ochk.io and nowhere else.
639
+ redirect: "error",
640
+ headers: {
641
+ "content-type": "application/json",
642
+ authorization: `Bearer ${apiKey}`
643
+ },
644
+ body: JSON.stringify(body),
645
+ signal: controller.signal
646
+ });
647
+ let parsed = null;
648
+ try {
649
+ parsed = await res.json();
650
+ } catch {
651
+ parsed = null;
652
+ }
653
+ const ok = parsed !== null && typeof parsed === "object" && !Array.isArray(parsed);
654
+ return { status: res.status, body: ok ? parsed : null };
655
+ } catch {
656
+ return { status: 0, body: null };
657
+ } finally {
658
+ clearTimeout(timer);
659
+ }
660
+ }
661
+ function refused(answer, known) {
662
+ const raw = answer.body?.reason;
663
+ const said = raw === "missing_api_key" ? "invalid_api_key" : raw;
664
+ const reason = typeof said === "string" && known.has(said) ? said : "unavailable";
665
+ return { ok: false, reason, status: answer.status };
666
+ }
667
+ function connectProvision(options) {
668
+ const o = checkCall("connectProvision", options, PROVISION_KEYS);
669
+ if (o.consent !== void 0 && o.consent !== true) {
670
+ fail(
671
+ "connectProvision: consent must be true or left out. An unticked box means no call at all; a call without consent makes an account nobody asked for"
672
+ );
673
+ }
674
+ const { email, emailVerified } = o;
675
+ if (email !== void 0 && (typeof email !== "string" || email === "")) {
676
+ fail("connectProvision: email must be a non-empty string");
677
+ }
678
+ if (emailVerified !== void 0 && typeof emailVerified !== "boolean") {
679
+ fail("connectProvision: emailVerified must be a boolean");
680
+ }
681
+ if (emailVerified !== void 0 && email === void 0) {
682
+ fail("connectProvision: emailVerified describes email, so pass email too");
683
+ }
684
+ const body = { user_id: o.userId };
685
+ if (o.consent === true) body.consent = true;
686
+ if (email !== void 0) body.email = email;
687
+ if (emailVerified !== void 0) body.email_verified = emailVerified;
688
+ return call("/api/connect/provision", o, body).then(
689
+ (answer) => {
690
+ const b = answer.body;
691
+ if (answer.status !== 200) return refused(answer, PROVISION_REASONS);
692
+ const good = b?.ok === true && typeof b.did_oc === "string" && DID_OC.test(b.did_oc) && typeof b.is_new === "boolean";
693
+ return good ? { ok: true, did_oc: b.did_oc, is_new: b.is_new } : { ok: false, reason: "unavailable", status: 200 };
694
+ }
695
+ );
696
+ }
697
+ function connectClaimLink(options) {
698
+ const o = checkCall("connectClaimLink", options, USER_KEYS);
699
+ return call("/api/connect/claim-link", o, { user_id: o.userId }).then(
700
+ (answer) => {
701
+ const b = answer.body;
702
+ if (answer.status !== 200) return refused(answer, CLAIM_LINK_REASONS);
703
+ const good = b?.ok === true && isClaimUrl(b.url) && typeof b.expires_at === "string" && b.expires_at !== "";
704
+ return good ? { ok: true, url: b.url, expires_at: b.expires_at } : { ok: false, reason: "unavailable", status: 200 };
705
+ }
706
+ );
707
+ }
708
+ function connectDelete(options) {
709
+ const o = checkCall("connectDelete", options, USER_KEYS);
710
+ return call("/api/connect/delete", o, { user_id: o.userId }).then(
711
+ (answer) => {
712
+ const b = answer.body;
713
+ if (answer.status !== 200) return refused(answer, DELETE_REASONS);
714
+ return b?.ok === true && typeof b.result === "string" && DELETE_RESULTS.has(b.result) ? { ok: true, result: b.result } : { ok: false, reason: "unavailable", status: 200 };
715
+ }
716
+ );
717
+ }
718
+ function call(path, o, body) {
719
+ return post(path, o.apiKey, body, o.timeoutMs ?? DEFAULT_TIMEOUT_MS);
720
+ }
721
+ function isClaimUrl(v) {
722
+ if (typeof v !== "string") return false;
723
+ try {
724
+ const url = new URL(v);
725
+ return url.protocol === "https:" && url.hostname === "ochk.app" && url.port === "" && url.username === "" && url.password === "";
726
+ } catch {
727
+ return false;
728
+ }
729
+ }
730
+
731
+ // src/connect-consent.ts
732
+ var CONNECT_CONSENT_LABEL = "Also create my orangecheck account";
733
+ var CONNECT_CONSENT_BLURB = "It stays with orangecheck until you claim it at ochk.app. Then you can use it on other sites, or delete it.";
734
+
571
735
  // src/server.ts
572
736
  var sessionChecks = /* @__PURE__ */ new Map();
573
737
  var warnedUnchecked = false;
@@ -604,7 +768,8 @@ async function getOcSession(headers, options = {}) {
604
768
  }
605
769
  async function checkOcSession(headers, options = {}) {
606
770
  checkOptions(options);
607
- const session = await getOcSession$1(headers, options);
771
+ const { issuer, jwksCacheTtlMs, jwksMaxStaleMs, audience } = options;
772
+ const session = await getOcSession$1(headers, { issuer, jwksCacheTtlMs, jwksMaxStaleMs, audience });
608
773
  if (!session) {
609
774
  if (options.audience === void 0) warnIfAudienceMissing(headers);
610
775
  return { status: "signed_out" };
@@ -799,8 +964,8 @@ function withOcAuth(handler, options = {}) {
799
964
  const session = checked.status === "signed_in" ? checked.session : null;
800
965
  const withSession = req;
801
966
  withSession.ocSession = session;
802
- const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
803
- if (refused) return res.status(refused.status).json(refused.body);
967
+ const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
968
+ if (refused2) return res.status(refused2.status).json(refused2.body);
804
969
  return handler(withSession, res);
805
970
  };
806
971
  }
@@ -819,9 +984,9 @@ function ocAuthExpress(options = {}) {
819
984
  );
820
985
  const session = checked.status === "signed_in" ? checked.session : null;
821
986
  req.ocSession = session;
822
- const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
823
- if (refused) {
824
- res.status(refused.status).json(refused.body);
987
+ const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
988
+ if (refused2) {
989
+ res.status(refused2.status).json(refused2.body);
825
990
  return;
826
991
  }
827
992
  next();
@@ -841,12 +1006,12 @@ function ocAuthHono(options = {}) {
841
1006
  });
842
1007
  const session = checked.status === "signed_in" ? checked.session : null;
843
1008
  c.set("ocSession", session);
844
- const refused = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
845
- if (refused) return c.json(refused.body, refused.status);
1009
+ const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options);
1010
+ if (refused2) return c.json(refused2.body, refused2.status);
846
1011
  await next();
847
1012
  };
848
1013
  }
849
1014
 
850
- export { BACK_ACCOUNT_URL, _resetSessionChecksForTests, checkOcSession, getOcSession, identity, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, verifyActivityAttestation, withOcAuth };
1015
+ export { BACK_ACCOUNT_URL, CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL, _resetSessionChecksForTests, checkOcSession, connectClaimLink, connectDelete, connectProvision, getOcSession, identity, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, verifyActivityAttestation, withOcAuth };
851
1016
  //# sourceMappingURL=server.mjs.map
852
1017
  //# sourceMappingURL=server.mjs.map