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