@orangecheck/me-client 0.40.0 → 0.41.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
@@ -1,4 +1,4 @@
1
- import { verifyOcToken as verifyOcToken$1, getOcSession as getOcSession$1, SAFE_METHODS, integratorRevocationCheck, bondMeets, readBondClaim } from '@orangecheck/auth-core';
1
+ import { verifyOcToken as verifyOcToken$1, readOcSessionToken, checkOcToken, SAFE_METHODS, integratorRevocationCheck, bondMeets, readBondClaim } from '@orangecheck/auth-core';
2
2
  export { bondMeets, readBondClaim } from '@orangecheck/auth-core';
3
3
 
4
4
  var __defProp = Object.defineProperty;
@@ -732,6 +732,110 @@ function isClaimUrl(v) {
732
732
  var CONNECT_CONSENT_LABEL = "Also create my orangecheck account";
733
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
734
 
735
+ // src/tokens.ts
736
+ var DEFAULT_ISSUER2 = "https://ochk.io";
737
+ var DEFAULT_TIMEOUT_MS2 = 5e3;
738
+ var MAX_TIMEOUT_MS2 = 3e4;
739
+ var MAX_TOKENS = 8;
740
+ var MAX_TOKEN_LENGTH = 4096;
741
+ function fail2(message) {
742
+ throw new TypeError(`@orangecheck/me-client: ${message}`);
743
+ }
744
+ var nowSeconds = () => Math.floor(Date.now() / 1e3);
745
+ var MemoryJtiStore = class {
746
+ constructor() {
747
+ this.spent = /* @__PURE__ */ new Map();
748
+ }
749
+ add(jti, exp) {
750
+ const now = nowSeconds();
751
+ for (const [k, until] of this.spent) if (until <= now) this.spent.delete(k);
752
+ if (this.spent.has(jti)) return false;
753
+ this.spent.set(jti, exp);
754
+ return true;
755
+ }
756
+ };
757
+ function spendOnce(jti, exp, store) {
758
+ if (typeof jti !== "string" || jti === "") fail2("spendOnce: jti must be one non-empty string");
759
+ if (typeof exp !== "number" || !Number.isFinite(exp)) {
760
+ fail2("spendOnce: exp must be the token's expiry, in seconds");
761
+ }
762
+ if (store === null || typeof store !== "object" || typeof store.add !== "function") {
763
+ fail2("spendOnce: store must have add(jti, exp)");
764
+ }
765
+ if (exp <= nowSeconds()) return Promise.resolve(false);
766
+ return Promise.resolve().then(() => store.add(jti, exp)).then((spent) => {
767
+ if (typeof spent !== "boolean") fail2("spendOnce: store.add must answer true or false");
768
+ return spent;
769
+ });
770
+ }
771
+ var REVOKE_OPTIONS = /* @__PURE__ */ new Set(["issuer", "timeoutMs"]);
772
+ function issuerOrigin(issuer) {
773
+ if (typeof issuer !== "string") fail2("revokeToken: issuer must be a URL");
774
+ const refuse = () => fail2(`revokeToken: issuer must be one https origin (http only on this machine), such as https://ochk.io, got ${issuer}`);
775
+ if (/[?#@\\]/.test(issuer)) refuse();
776
+ let url;
777
+ try {
778
+ url = new URL(issuer);
779
+ } catch {
780
+ return refuse();
781
+ }
782
+ const local = ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
783
+ if (url.protocol !== "https:" && !(url.protocol === "http:" && local)) refuse();
784
+ if (url.pathname !== "/" || url.search !== "" || url.hash !== "" || url.username !== "" || url.password !== "") {
785
+ refuse();
786
+ }
787
+ return url.origin;
788
+ }
789
+ function revokeToken(token, options = {}) {
790
+ if (options === null || typeof options !== "object") fail2("revokeToken: options must be an object");
791
+ const unknown = Object.keys(options).filter((k) => !REVOKE_OPTIONS.has(k));
792
+ if (unknown.length > 0) {
793
+ fail2(`revokeToken: unknown option ${unknown.join(", ")}. It reads only ${[...REVOKE_OPTIONS].join(", ")}.`);
794
+ }
795
+ const tokens = typeof token === "string" ? [token] : Array.isArray(token) ? [...token] : null;
796
+ if (!tokens || tokens.length < 1 || tokens.length > MAX_TOKENS || !tokens.every((t) => typeof t === "string" && t.length > 0 && t.length <= MAX_TOKEN_LENGTH)) {
797
+ fail2(`revokeToken: pass one token, or 1 to ${MAX_TOKENS}, each a non-empty string of at most ${MAX_TOKEN_LENGTH} characters`);
798
+ }
799
+ const origin = issuerOrigin(options.issuer ?? DEFAULT_ISSUER2);
800
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS2;
801
+ if (typeof timeoutMs !== "number" || !(timeoutMs > 0 && timeoutMs <= MAX_TIMEOUT_MS2)) {
802
+ fail2(`revokeToken: timeoutMs must be above 0 and at most ${MAX_TIMEOUT_MS2}`);
803
+ }
804
+ return post2(`${origin}/api/auth/revoke`, tokens, timeoutMs);
805
+ }
806
+ async function post2(url, tokens, timeoutMs) {
807
+ const controller = new AbortController();
808
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
809
+ try {
810
+ const res = await fetch(url, {
811
+ method: "POST",
812
+ // The endpoint never redirects, so a redirect is an error: the tokens go
813
+ // to the auth host and nowhere else.
814
+ redirect: "error",
815
+ headers: { "content-type": "application/json" },
816
+ body: JSON.stringify({ tokens }),
817
+ signal: controller.signal
818
+ });
819
+ let body = null;
820
+ try {
821
+ body = await res.json();
822
+ } catch {
823
+ body = null;
824
+ }
825
+ const b = body !== null && typeof body === "object" ? body : null;
826
+ if (res.status === 200) {
827
+ return b?.ok === true && Number.isSafeInteger(b.revoked) && b.revoked >= 0 ? { ok: true, revoked: b.revoked } : { ok: false, reason: "unavailable", status: 200 };
828
+ }
829
+ if (res.status === 429) return { ok: false, reason: "rate_limited", status: 429 };
830
+ if (res.status === 400) return { ok: false, reason: "bad_request", status: 400 };
831
+ return { ok: false, reason: "unavailable", status: res.status };
832
+ } catch {
833
+ return { ok: false, reason: "unavailable", status: 0 };
834
+ } finally {
835
+ clearTimeout(timer);
836
+ }
837
+ }
838
+
735
839
  // src/server.ts
736
840
  function lacksHandle(session, requireHandle) {
737
841
  if (requireHandle === false) return false;
@@ -803,11 +907,15 @@ async function getOcSession(headers, options = {}) {
803
907
  async function checkOcSession(headers, options = {}) {
804
908
  checkOptions(options);
805
909
  const { issuer, jwksCacheTtlMs, jwksMaxStaleMs, audience } = options;
806
- const session = await getOcSession$1(headers, { issuer, jwksCacheTtlMs, jwksMaxStaleMs, audience });
807
- if (!session) {
910
+ const token = readOcSessionToken(headers);
911
+ if (!token) return { status: "signed_out" };
912
+ const verified = await checkOcToken(token, { issuer, jwksCacheTtlMs, jwksMaxStaleMs, audience });
913
+ if (verified.status === "unavailable") return { status: "unavailable", reason: "key_set_unavailable" };
914
+ if (verified.status === "invalid") {
808
915
  if (options.audience === void 0) warnIfAudienceMissing(headers);
809
916
  return { status: "signed_out" };
810
917
  }
918
+ const session = verified.session;
811
919
  if (lacksHandle(session, options.requireHandle)) {
812
920
  warnNoHandle(session);
813
921
  return { status: "signed_out", reason: "no_session_handle" };
@@ -817,12 +925,15 @@ async function checkOcSession(headers, options = {}) {
817
925
  const state = await check.status(session);
818
926
  if (state === "revoked") return { status: "signed_out" };
819
927
  if (state === "unverified" && !SAFE_METHODS.has((options.method ?? "").toUpperCase())) {
820
- return { status: "unavailable" };
928
+ return { status: "unavailable", reason: "revocations_unavailable" };
821
929
  }
822
930
  }
823
931
  return { status: "signed_in", session };
824
932
  }
825
- var UNAVAILABLE = { status: 503, body: { ok: false, reason: "revocations_unavailable" } };
933
+ var unavailable = (checked) => ({
934
+ status: 503,
935
+ body: { ok: false, reason: checked.reason ?? "revocations_unavailable" }
936
+ });
826
937
  var refusedFor = (checked) => checked.status === "signed_out" ? checked.reason : void 0;
827
938
  var warnedAudience = false;
828
939
  function warnIfAudienceMissing(headers) {
@@ -1002,7 +1113,7 @@ function withOcAuth(handler, options = {}) {
1002
1113
  const session = checked.status === "signed_in" ? checked.session : null;
1003
1114
  const withSession = req;
1004
1115
  withSession.ocSession = session;
1005
- const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options, refusedFor(checked));
1116
+ const refused2 = checked.status === "unavailable" ? unavailable(checked) : await refusal(session, options, refusedFor(checked));
1006
1117
  if (refused2) return res.status(refused2.status).json(refused2.body);
1007
1118
  return handler(withSession, res);
1008
1119
  };
@@ -1022,7 +1133,7 @@ function ocAuthExpress(options = {}) {
1022
1133
  );
1023
1134
  const session = checked.status === "signed_in" ? checked.session : null;
1024
1135
  req.ocSession = session;
1025
- const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options, refusedFor(checked));
1136
+ const refused2 = checked.status === "unavailable" ? unavailable(checked) : await refusal(session, options, refusedFor(checked));
1026
1137
  if (refused2) {
1027
1138
  res.status(refused2.status).json(refused2.body);
1028
1139
  return;
@@ -1044,12 +1155,12 @@ function ocAuthHono(options = {}) {
1044
1155
  });
1045
1156
  const session = checked.status === "signed_in" ? checked.session : null;
1046
1157
  c.set("ocSession", session);
1047
- const refused2 = checked.status === "unavailable" ? UNAVAILABLE : await refusal(session, options, refusedFor(checked));
1158
+ const refused2 = checked.status === "unavailable" ? unavailable(checked) : await refusal(session, options, refusedFor(checked));
1048
1159
  if (refused2) return c.json(refused2.body, refused2.status);
1049
1160
  await next();
1050
1161
  };
1051
1162
  }
1052
1163
 
1053
- export { BACK_ACCOUNT_URL, CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL, _resetSessionChecksForTests, checkOcSession, connectClaimLink, connectDelete, connectProvision, getOcSession, identity, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, verifyActivityAttestation, verifyOcToken, withOcAuth };
1164
+ export { BACK_ACCOUNT_URL, CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL, MemoryJtiStore, _resetSessionChecksForTests, checkOcSession, connectClaimLink, connectDelete, connectProvision, getOcSession, identity, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, revokeToken, spendOnce, verifyActivityAttestation, verifyOcToken, withOcAuth };
1054
1165
  //# sourceMappingURL=server.mjs.map
1055
1166
  //# sourceMappingURL=server.mjs.map