@orangecheck/me-client 0.47.0 → 0.49.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
@@ -769,9 +769,9 @@ function spendOnce(jti, exp, store) {
769
769
  });
770
770
  }
771
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}`);
772
+ function issuerOrigin(issuer, where = "revokeToken") {
773
+ if (typeof issuer !== "string") fail2(`${where}: issuer must be a URL`);
774
+ const refuse = () => fail2(`${where}: issuer must be one https origin (http only on this machine), such as https://ochk.io, got ${issuer}`);
775
775
  if (/[?#@\\]/.test(issuer)) refuse();
776
776
  let url;
777
777
  try {
@@ -836,6 +836,82 @@ async function post2(url, tokens, timeoutMs) {
836
836
  }
837
837
  }
838
838
 
839
+ // src/native.ts
840
+ var DEFAULT_ISSUER3 = "https://ochk.io";
841
+ var DEFAULT_TIMEOUT_MS3 = 5e3;
842
+ var MAX_TIMEOUT_MS3 = 3e4;
843
+ var MAX_FIELD_LENGTH = 512;
844
+ var OPTIONS = /* @__PURE__ */ new Set(["issuer", "timeoutMs"]);
845
+ var FIELDS = ["code", "codeVerifier", "redirectUri", "projectKey", "audience"];
846
+ function fail3(message) {
847
+ throw new TypeError(`@orangecheck/me-client: exchangeNativeCode: ${message}`);
848
+ }
849
+ async function exchangeNativeCode(input, options = {}) {
850
+ if (input === null || typeof input !== "object") fail3("input must be an object");
851
+ for (const f of FIELDS) {
852
+ const v = input[f];
853
+ if (typeof v !== "string" || v.length === 0 || v.length > MAX_FIELD_LENGTH) {
854
+ fail3(`${f} must be a non-empty string of at most ${MAX_FIELD_LENGTH} characters`);
855
+ }
856
+ }
857
+ if (input.audience === "*") fail3("audience must be your site's origin, not '*'");
858
+ if (options === null || typeof options !== "object") fail3("options must be an object");
859
+ const unknown = Object.keys(options).filter((k) => !OPTIONS.has(k));
860
+ if (unknown.length > 0) fail3(`unknown option ${unknown.join(", ")}. It reads only ${[...OPTIONS].join(", ")}.`);
861
+ const issuer = issuerOrigin(options.issuer ?? DEFAULT_ISSUER3, "exchangeNativeCode");
862
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS3;
863
+ if (typeof timeoutMs !== "number" || !(timeoutMs > 0 && timeoutMs <= MAX_TIMEOUT_MS3)) {
864
+ fail3(`timeoutMs must be above 0 and at most ${MAX_TIMEOUT_MS3}`);
865
+ }
866
+ const answer = await post3(`${issuer}/api/auth/native/token`, input, timeoutMs);
867
+ if (answer.status !== "token") return answer;
868
+ const checked = await checkOcToken(answer.token, { audience: input.audience, issuer });
869
+ if (checked.status === "valid") return { status: "valid", token: answer.token, session: checked.session };
870
+ if (checked.status === "unavailable") return { status: "unavailable", reason: "key_set_unavailable" };
871
+ return { status: "invalid", reason: checked.reason ?? "invalid_token" };
872
+ }
873
+ async function post3(url, input, timeoutMs) {
874
+ const unavailable2 = { status: "unavailable", reason: "auth_host_unavailable" };
875
+ const controller = new AbortController();
876
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
877
+ try {
878
+ const res = await fetch(url, {
879
+ method: "POST",
880
+ // The endpoint never redirects: the code goes to the auth host and nowhere else.
881
+ redirect: "error",
882
+ headers: { "content-type": "application/json" },
883
+ body: JSON.stringify({
884
+ grant_type: "authorization_code",
885
+ code: input.code,
886
+ code_verifier: input.codeVerifier,
887
+ redirect_uri: input.redirectUri,
888
+ project: input.projectKey
889
+ }),
890
+ signal: controller.signal
891
+ });
892
+ let body = null;
893
+ try {
894
+ const json = await res.json();
895
+ if (json !== null && typeof json === "object") body = json;
896
+ } catch {
897
+ body = null;
898
+ }
899
+ if (res.status === 200) {
900
+ return body?.ok === true && typeof body.token === "string" && body.token.length > 0 ? { status: "token", token: body.token } : unavailable2;
901
+ }
902
+ if (res.status === 400) {
903
+ return { status: "invalid", reason: body?.reason === "invalid_grant" ? "invalid_grant" : "bad_request" };
904
+ }
905
+ if (res.status === 404) return { status: "invalid", reason: "not_offered" };
906
+ if (res.status === 429) return { status: "unavailable", reason: "rate_limited" };
907
+ return unavailable2;
908
+ } catch {
909
+ return unavailable2;
910
+ } finally {
911
+ clearTimeout(timer);
912
+ }
913
+ }
914
+
839
915
  // src/merged.ts
840
916
  var MERGED_HANDLES_MAX = 16;
841
917
  function mergedHandles(session) {
@@ -1239,6 +1315,6 @@ function ocAuthHono(options = {}) {
1239
1315
  };
1240
1316
  }
1241
1317
 
1242
- export { BACK_ACCOUNT_URL, CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL, MemoryJtiStore, _resetSessionChecksForTests, checkOcSession, checkOcToken, checkSignInRule, connectClaimLink, connectDelete, connectProvision, getOcSession, identity, matchMergedUser, mergedHandles, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, revokeToken, spendOnce, verifyActivityAttestation, verifyOcToken, withOcAuth };
1318
+ export { BACK_ACCOUNT_URL, CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL, MemoryJtiStore, _resetSessionChecksForTests, checkOcSession, checkOcToken, checkSignInRule, connectClaimLink, connectDelete, connectProvision, exchangeNativeCode, getOcSession, identity, matchMergedUser, mergedHandles, ocAuthExpress, ocAuthHono, refreshEnvelopeJwks, revokeToken, spendOnce, verifyActivityAttestation, verifyOcToken, withOcAuth };
1243
1319
  //# sourceMappingURL=server.mjs.map
1244
1320
  //# sourceMappingURL=server.mjs.map