@orangecheck/me-client 0.49.0 → 0.51.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
@@ -841,13 +841,25 @@ var DEFAULT_ISSUER3 = "https://ochk.io";
841
841
  var DEFAULT_TIMEOUT_MS3 = 5e3;
842
842
  var MAX_TIMEOUT_MS3 = 3e4;
843
843
  var MAX_FIELD_LENGTH = 512;
844
- var OPTIONS = /* @__PURE__ */ new Set(["issuer", "timeoutMs"]);
845
- var FIELDS = ["code", "codeVerifier", "redirectUri", "projectKey", "audience"];
844
+ var OPTIONS = /* @__PURE__ */ new Set([
845
+ "projectKey",
846
+ "issuer",
847
+ "meOrigin",
848
+ "revocation",
849
+ "requireBond",
850
+ "timeoutMs"
851
+ ]);
852
+ var FIELDS = ["code", "codeVerifier", "redirectUri", "audience"];
846
853
  function fail3(message) {
847
854
  throw new TypeError(`@orangecheck/me-client: exchangeNativeCode: ${message}`);
848
855
  }
849
856
  async function exchangeNativeCode(input, options = {}) {
850
857
  if (input === null || typeof input !== "object") fail3("input must be an object");
858
+ if ("projectKey" in input) {
859
+ fail3("projectKey is your own key: pass it in options, or set OC_PROJECT_KEY, never from the request");
860
+ }
861
+ const extra = Object.keys(input).filter((k) => !FIELDS.includes(k));
862
+ if (extra.length > 0) fail3(`unknown input ${extra.join(", ")}. It reads only ${FIELDS.join(", ")}.`);
851
863
  for (const f of FIELDS) {
852
864
  const v = input[f];
853
865
  if (typeof v !== "string" || v.length === 0 || v.length > MAX_FIELD_LENGTH) {
@@ -858,19 +870,53 @@ async function exchangeNativeCode(input, options = {}) {
858
870
  if (options === null || typeof options !== "object") fail3("options must be an object");
859
871
  const unknown = Object.keys(options).filter((k) => !OPTIONS.has(k));
860
872
  if (unknown.length > 0) fail3(`unknown option ${unknown.join(", ")}. It reads only ${[...OPTIONS].join(", ")}.`);
873
+ const projectKey = options.projectKey ?? (typeof process !== "undefined" ? process.env?.OC_PROJECT_KEY?.trim() : void 0);
874
+ if (typeof projectKey !== "string" || projectKey.length === 0 || projectKey.length > MAX_FIELD_LENGTH) {
875
+ fail3("needs your project key: options.projectKey, or OC_PROJECT_KEY");
876
+ }
861
877
  const issuer = issuerOrigin(options.issuer ?? DEFAULT_ISSUER3, "exchangeNativeCode");
862
878
  const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS3;
863
879
  if (typeof timeoutMs !== "number" || !(timeoutMs > 0 && timeoutMs <= MAX_TIMEOUT_MS3)) {
864
880
  fail3(`timeoutMs must be above 0 and at most ${MAX_TIMEOUT_MS3}`);
865
881
  }
866
- const answer = await post3(`${issuer}/api/auth/native/token`, input, timeoutMs);
882
+ const answer = await post3(`${issuer}/api/auth/native/token`, input, projectKey, timeoutMs);
867
883
  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" };
884
+ const token = answer.token;
885
+ const verified = await checkOcToken(token, { audience: input.audience, issuer });
886
+ if (verified.status === "unavailable") return { status: "unavailable", reason: "key_set_unavailable" };
887
+ if (verified.status === "invalid") return { status: "invalid", reason: verified.reason ?? "invalid_token" };
888
+ const where = options.meOrigin ? { meOrigin: options.meOrigin } : {};
889
+ const checked = await checkOcSession(
890
+ { authorization: `Bearer ${token}` },
891
+ {
892
+ audience: input.audience,
893
+ issuer,
894
+ projectKey,
895
+ method: "POST",
896
+ ...where,
897
+ ...options.revocation !== void 0 ? { revocation: options.revocation } : {}
898
+ }
899
+ );
900
+ if (checked.status === "unavailable") {
901
+ return { status: "unavailable", reason: checked.reason ?? "revocations_unavailable" };
902
+ }
903
+ if (checked.status === "signed_out") return { status: "invalid", reason: checked.reason ?? "session_ended" };
904
+ const rule = await checkSignInRule(checked.session, {
905
+ projectKey,
906
+ ...where,
907
+ ...options.requireBond ? { requireBond: options.requireBond } : {}
908
+ });
909
+ if (!rule.ok) {
910
+ const b = rule.body;
911
+ if (b.reason === "bond_required" && b.need && b.back_url) {
912
+ return { status: "invalid", reason: "bond_required", need: b.need, have: b.have ?? null, back_url: b.back_url };
913
+ }
914
+ if (b.reason === "unknown_project_key") return { status: "invalid", reason: "unknown_project_key" };
915
+ return { status: "unavailable", reason: "bond_policy_unavailable" };
916
+ }
917
+ return { status: "valid", token, session: checked.session };
872
918
  }
873
- async function post3(url, input, timeoutMs) {
919
+ async function post3(url, input, projectKey, timeoutMs) {
874
920
  const unavailable2 = { status: "unavailable", reason: "auth_host_unavailable" };
875
921
  const controller = new AbortController();
876
922
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -885,7 +931,7 @@ async function post3(url, input, timeoutMs) {
885
931
  code: input.code,
886
932
  code_verifier: input.codeVerifier,
887
933
  redirect_uri: input.redirectUri,
888
- project: input.projectKey
934
+ project: projectKey
889
935
  }),
890
936
  signal: controller.signal
891
937
  });