@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.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { SessionPayload, VerifyOcOptions, RevocationCheck, SessionRequestHeaders } from '@orangecheck/auth-core';
1
+ import { RevocationCheck, SessionPayload, VerifyOcOptions, SessionRequestHeaders } from '@orangecheck/auth-core';
2
2
  export { BondClaim, SessionPayload, VerifyOcOptions, bondMeets, readBondClaim } from '@orangecheck/auth-core';
3
3
  export { A as ActivityAttestation, a as ActivityAttestationBundle, b as ActivityVerifyCheck, c as ActivityVerifyResult, V as VerifyActivityOptions, i as identity, r as refreshEnvelopeJwks, v as verifyActivityAttestation } from './identity-DfxCvxuH.mjs';
4
4
  export { CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL } from './connect-consent.mjs';
@@ -66,14 +66,18 @@ interface ExchangeNativeCodeInput {
66
66
  code: string;
67
67
  codeVerifier: string;
68
68
  redirectUri: string;
69
- projectKey: string;
70
69
  audience: string;
71
70
  }
72
71
  interface ExchangeNativeCodeOptions {
72
+ projectKey?: string;
73
73
  issuer?: string;
74
+ meOrigin?: string;
75
+ revocation?: RevocationCheck | false;
76
+ requireBond?: WithOcAuthOptions['requireBond'];
74
77
  timeoutMs?: number;
75
78
  }
76
- type ExchangeNativeCodeRefusal = 'invalid_grant' | 'not_offered' | 'bad_request' | 'invalid_token' | 'no_session_handle';
79
+ type ExchangeNativeCodeRefusal = 'invalid_grant' | 'not_offered' | 'bad_request' | 'invalid_token' | 'no_session_handle' | 'session_ended' | 'unknown_project_key';
80
+ type ExchangeNativeCodeUnavailable = 'auth_host_unavailable' | 'rate_limited' | 'key_set_unavailable' | 'revocations_unavailable' | 'bond_policy_unavailable';
77
81
  type ExchangeNativeCodeResult = {
78
82
  status: 'valid';
79
83
  token: string;
@@ -81,9 +85,22 @@ type ExchangeNativeCodeResult = {
81
85
  } | {
82
86
  status: 'invalid';
83
87
  reason: ExchangeNativeCodeRefusal;
88
+ } | {
89
+ status: 'invalid';
90
+ reason: 'bond_required';
91
+ need: {
92
+ min_sats: number;
93
+ min_days: number;
94
+ max_age_secs?: number;
95
+ };
96
+ have: {
97
+ sats_floor: number;
98
+ days_floor: number;
99
+ } | null;
100
+ back_url: string;
84
101
  } | {
85
102
  status: 'unavailable';
86
- reason: 'auth_host_unavailable' | 'rate_limited' | 'key_set_unavailable';
103
+ reason: ExchangeNativeCodeUnavailable;
87
104
  };
88
105
  declare function exchangeNativeCode(input: ExchangeNativeCodeInput, options?: ExchangeNativeCodeOptions): Promise<ExchangeNativeCodeResult>;
89
106
 
@@ -223,4 +240,4 @@ interface HonoContextLike {
223
240
  type HonoNextLike = () => Promise<void>;
224
241
  declare function ocAuthHono(options?: WithOcAuthOptions): (c: HonoContextLike, next: HonoNextLike) => Promise<unknown>;
225
242
 
226
- export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type ExchangeNativeCodeInput, type ExchangeNativeCodeOptions, type ExchangeNativeCodeRefusal, type ExchangeNativeCodeResult, type JtiStore, MemoryJtiStore, type MergedUserMatch, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type OcSessionRefusal, type OcSessionUnavailable, type OcTokenCheck, type RevokeTokenOptions, type RevokeTokenRefusal, type RevokeTokenResult, type SignInRuleCheck, type SignInRuleOptions, type VerifyOcTokenOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, checkOcToken, checkSignInRule, connectClaimLink, connectDelete, connectProvision, exchangeNativeCode, getOcSession, matchMergedUser, mergedHandles, ocAuthExpress, ocAuthHono, revokeToken, spendOnce, verifyOcToken, withOcAuth };
243
+ export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type ExchangeNativeCodeInput, type ExchangeNativeCodeOptions, type ExchangeNativeCodeRefusal, type ExchangeNativeCodeResult, type ExchangeNativeCodeUnavailable, type JtiStore, MemoryJtiStore, type MergedUserMatch, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type OcSessionRefusal, type OcSessionUnavailable, type OcTokenCheck, type RevokeTokenOptions, type RevokeTokenRefusal, type RevokeTokenResult, type SignInRuleCheck, type SignInRuleOptions, type VerifyOcTokenOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, checkOcToken, checkSignInRule, connectClaimLink, connectDelete, connectProvision, exchangeNativeCode, getOcSession, matchMergedUser, mergedHandles, ocAuthExpress, ocAuthHono, revokeToken, spendOnce, verifyOcToken, withOcAuth };
package/dist/server.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SessionPayload, VerifyOcOptions, RevocationCheck, SessionRequestHeaders } from '@orangecheck/auth-core';
1
+ import { RevocationCheck, SessionPayload, VerifyOcOptions, SessionRequestHeaders } from '@orangecheck/auth-core';
2
2
  export { BondClaim, SessionPayload, VerifyOcOptions, bondMeets, readBondClaim } from '@orangecheck/auth-core';
3
3
  export { A as ActivityAttestation, a as ActivityAttestationBundle, b as ActivityVerifyCheck, c as ActivityVerifyResult, V as VerifyActivityOptions, i as identity, r as refreshEnvelopeJwks, v as verifyActivityAttestation } from './identity-DfxCvxuH.js';
4
4
  export { CONNECT_CONSENT_BLURB, CONNECT_CONSENT_LABEL } from './connect-consent.js';
@@ -66,14 +66,18 @@ interface ExchangeNativeCodeInput {
66
66
  code: string;
67
67
  codeVerifier: string;
68
68
  redirectUri: string;
69
- projectKey: string;
70
69
  audience: string;
71
70
  }
72
71
  interface ExchangeNativeCodeOptions {
72
+ projectKey?: string;
73
73
  issuer?: string;
74
+ meOrigin?: string;
75
+ revocation?: RevocationCheck | false;
76
+ requireBond?: WithOcAuthOptions['requireBond'];
74
77
  timeoutMs?: number;
75
78
  }
76
- type ExchangeNativeCodeRefusal = 'invalid_grant' | 'not_offered' | 'bad_request' | 'invalid_token' | 'no_session_handle';
79
+ type ExchangeNativeCodeRefusal = 'invalid_grant' | 'not_offered' | 'bad_request' | 'invalid_token' | 'no_session_handle' | 'session_ended' | 'unknown_project_key';
80
+ type ExchangeNativeCodeUnavailable = 'auth_host_unavailable' | 'rate_limited' | 'key_set_unavailable' | 'revocations_unavailable' | 'bond_policy_unavailable';
77
81
  type ExchangeNativeCodeResult = {
78
82
  status: 'valid';
79
83
  token: string;
@@ -81,9 +85,22 @@ type ExchangeNativeCodeResult = {
81
85
  } | {
82
86
  status: 'invalid';
83
87
  reason: ExchangeNativeCodeRefusal;
88
+ } | {
89
+ status: 'invalid';
90
+ reason: 'bond_required';
91
+ need: {
92
+ min_sats: number;
93
+ min_days: number;
94
+ max_age_secs?: number;
95
+ };
96
+ have: {
97
+ sats_floor: number;
98
+ days_floor: number;
99
+ } | null;
100
+ back_url: string;
84
101
  } | {
85
102
  status: 'unavailable';
86
- reason: 'auth_host_unavailable' | 'rate_limited' | 'key_set_unavailable';
103
+ reason: ExchangeNativeCodeUnavailable;
87
104
  };
88
105
  declare function exchangeNativeCode(input: ExchangeNativeCodeInput, options?: ExchangeNativeCodeOptions): Promise<ExchangeNativeCodeResult>;
89
106
 
@@ -223,4 +240,4 @@ interface HonoContextLike {
223
240
  type HonoNextLike = () => Promise<void>;
224
241
  declare function ocAuthHono(options?: WithOcAuthOptions): (c: HonoContextLike, next: HonoNextLike) => Promise<unknown>;
225
242
 
226
- export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type ExchangeNativeCodeInput, type ExchangeNativeCodeOptions, type ExchangeNativeCodeRefusal, type ExchangeNativeCodeResult, type JtiStore, MemoryJtiStore, type MergedUserMatch, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type OcSessionRefusal, type OcSessionUnavailable, type OcTokenCheck, type RevokeTokenOptions, type RevokeTokenRefusal, type RevokeTokenResult, type SignInRuleCheck, type SignInRuleOptions, type VerifyOcTokenOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, checkOcToken, checkSignInRule, connectClaimLink, connectDelete, connectProvision, exchangeNativeCode, getOcSession, matchMergedUser, mergedHandles, ocAuthExpress, ocAuthHono, revokeToken, spendOnce, verifyOcToken, withOcAuth };
243
+ export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type ExchangeNativeCodeInput, type ExchangeNativeCodeOptions, type ExchangeNativeCodeRefusal, type ExchangeNativeCodeResult, type ExchangeNativeCodeUnavailable, type JtiStore, MemoryJtiStore, type MergedUserMatch, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type OcSessionRefusal, type OcSessionUnavailable, type OcTokenCheck, type RevokeTokenOptions, type RevokeTokenRefusal, type RevokeTokenResult, type SignInRuleCheck, type SignInRuleOptions, type VerifyOcTokenOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, checkOcToken, checkSignInRule, connectClaimLink, connectDelete, connectProvision, exchangeNativeCode, getOcSession, matchMergedUser, mergedHandles, ocAuthExpress, ocAuthHono, revokeToken, spendOnce, verifyOcToken, withOcAuth };
package/dist/server.js CHANGED
@@ -842,13 +842,25 @@ var DEFAULT_ISSUER3 = "https://ochk.io";
842
842
  var DEFAULT_TIMEOUT_MS3 = 5e3;
843
843
  var MAX_TIMEOUT_MS3 = 3e4;
844
844
  var MAX_FIELD_LENGTH = 512;
845
- var OPTIONS = /* @__PURE__ */ new Set(["issuer", "timeoutMs"]);
846
- var FIELDS = ["code", "codeVerifier", "redirectUri", "projectKey", "audience"];
845
+ var OPTIONS = /* @__PURE__ */ new Set([
846
+ "projectKey",
847
+ "issuer",
848
+ "meOrigin",
849
+ "revocation",
850
+ "requireBond",
851
+ "timeoutMs"
852
+ ]);
853
+ var FIELDS = ["code", "codeVerifier", "redirectUri", "audience"];
847
854
  function fail3(message) {
848
855
  throw new TypeError(`@orangecheck/me-client: exchangeNativeCode: ${message}`);
849
856
  }
850
857
  async function exchangeNativeCode(input, options = {}) {
851
858
  if (input === null || typeof input !== "object") fail3("input must be an object");
859
+ if ("projectKey" in input) {
860
+ fail3("projectKey is your own key: pass it in options, or set OC_PROJECT_KEY, never from the request");
861
+ }
862
+ const extra = Object.keys(input).filter((k) => !FIELDS.includes(k));
863
+ if (extra.length > 0) fail3(`unknown input ${extra.join(", ")}. It reads only ${FIELDS.join(", ")}.`);
852
864
  for (const f of FIELDS) {
853
865
  const v = input[f];
854
866
  if (typeof v !== "string" || v.length === 0 || v.length > MAX_FIELD_LENGTH) {
@@ -859,19 +871,53 @@ async function exchangeNativeCode(input, options = {}) {
859
871
  if (options === null || typeof options !== "object") fail3("options must be an object");
860
872
  const unknown = Object.keys(options).filter((k) => !OPTIONS.has(k));
861
873
  if (unknown.length > 0) fail3(`unknown option ${unknown.join(", ")}. It reads only ${[...OPTIONS].join(", ")}.`);
874
+ const projectKey = options.projectKey ?? (typeof process !== "undefined" ? process.env?.OC_PROJECT_KEY?.trim() : void 0);
875
+ if (typeof projectKey !== "string" || projectKey.length === 0 || projectKey.length > MAX_FIELD_LENGTH) {
876
+ fail3("needs your project key: options.projectKey, or OC_PROJECT_KEY");
877
+ }
862
878
  const issuer = issuerOrigin(options.issuer ?? DEFAULT_ISSUER3, "exchangeNativeCode");
863
879
  const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS3;
864
880
  if (typeof timeoutMs !== "number" || !(timeoutMs > 0 && timeoutMs <= MAX_TIMEOUT_MS3)) {
865
881
  fail3(`timeoutMs must be above 0 and at most ${MAX_TIMEOUT_MS3}`);
866
882
  }
867
- const answer = await post3(`${issuer}/api/auth/native/token`, input, timeoutMs);
883
+ const answer = await post3(`${issuer}/api/auth/native/token`, input, projectKey, timeoutMs);
868
884
  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" };
885
+ const token = answer.token;
886
+ const verified = await checkOcToken(token, { audience: input.audience, issuer });
887
+ if (verified.status === "unavailable") return { status: "unavailable", reason: "key_set_unavailable" };
888
+ if (verified.status === "invalid") return { status: "invalid", reason: verified.reason ?? "invalid_token" };
889
+ const where = options.meOrigin ? { meOrigin: options.meOrigin } : {};
890
+ const checked = await checkOcSession(
891
+ { authorization: `Bearer ${token}` },
892
+ {
893
+ audience: input.audience,
894
+ issuer,
895
+ projectKey,
896
+ method: "POST",
897
+ ...where,
898
+ ...options.revocation !== void 0 ? { revocation: options.revocation } : {}
899
+ }
900
+ );
901
+ if (checked.status === "unavailable") {
902
+ return { status: "unavailable", reason: checked.reason ?? "revocations_unavailable" };
903
+ }
904
+ if (checked.status === "signed_out") return { status: "invalid", reason: checked.reason ?? "session_ended" };
905
+ const rule = await checkSignInRule(checked.session, {
906
+ projectKey,
907
+ ...where,
908
+ ...options.requireBond ? { requireBond: options.requireBond } : {}
909
+ });
910
+ if (!rule.ok) {
911
+ const b = rule.body;
912
+ if (b.reason === "bond_required" && b.need && b.back_url) {
913
+ return { status: "invalid", reason: "bond_required", need: b.need, have: b.have ?? null, back_url: b.back_url };
914
+ }
915
+ if (b.reason === "unknown_project_key") return { status: "invalid", reason: "unknown_project_key" };
916
+ return { status: "unavailable", reason: "bond_policy_unavailable" };
917
+ }
918
+ return { status: "valid", token, session: checked.session };
873
919
  }
874
- async function post3(url, input, timeoutMs) {
920
+ async function post3(url, input, projectKey, timeoutMs) {
875
921
  const unavailable2 = { status: "unavailable", reason: "auth_host_unavailable" };
876
922
  const controller = new AbortController();
877
923
  const timer = setTimeout(() => controller.abort(), timeoutMs);
@@ -886,7 +932,7 @@ async function post3(url, input, timeoutMs) {
886
932
  code: input.code,
887
933
  code_verifier: input.codeVerifier,
888
934
  redirect_uri: input.redirectUri,
889
- project: input.projectKey
935
+ project: projectKey
890
936
  }),
891
937
  signal: controller.signal
892
938
  });