@orangecheck/me-client 0.49.0 → 0.50.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/README.md CHANGED
@@ -281,24 +281,28 @@ Set `OC_PROJECT_KEY` and `OC_SIGNING_SECRET` (from ochk.app/me/projects) and `ge
281
281
 
282
282
  ### Sign in from an app on the person's device
283
283
 
284
- A desktop build or a game can't open the popup, and must not carry a secret. It signs the person in through their browser instead (0.49.0, test projects only for now): the app listens on `127.0.0.1`, opens ochk.io's `/native/authorize` with a PKCE challenge, and its listener receives a one-time `code`. The app hands the code, its verifier and its redirect to your server, which trades them:
284
+ A desktop build or a game can't open the popup, and must not carry a secret. It signs the person in through their browser instead (0.50.0, test projects only for now): the app listens on `127.0.0.1`, opens ochk.io's `/native/authorize` with a PKCE challenge, and its listener receives a one-time `code`. The app hands the code, its verifier and its redirect to your server, which trades them:
285
285
 
286
286
  ```ts
287
287
  import { exchangeNativeCode, spendOnce } from '@orangecheck/me-client/server';
288
288
 
289
- const out = await exchangeNativeCode({
290
- code,
291
- codeVerifier,
292
- redirectUri, // exactly as the app sent it: http://127.0.0.1:<port>/<path>
293
- projectKey: 'pk_test_…',
294
- audience: 'https://your-site.example', // your verified domain, or your test project's dev origin
295
- });
289
+ const out = await exchangeNativeCode(
290
+ {
291
+ code,
292
+ codeVerifier,
293
+ redirectUri, // exactly as the app sent it: http://127.0.0.1:<port>/<path>
294
+ audience: 'https://your-site.example', // your verified domain, or your test project's dev origin
295
+ },
296
+ { projectKey: process.env.OC_PROJECT_KEY } // your own key, from your config, never from the request
297
+ );
296
298
  if (out.status === 'valid' && (await spendOnce(out.session.jti, out.session.exp ?? 0, store))) {
297
299
  // Start your session for out.session.did_oc.
298
300
  }
299
301
  ```
300
302
 
301
- The token is the popup's, verified for your `audience`, with `azp: "native"`. `invalid` names why (`invalid_grant`: start again); `unavailable` is an outage, so answer it 503. Each code works once, whatever the answer. Turn native sign-in on for a test project on ochk.app; the app's half, with a Godot example, is at ochk.io/docs/integration/native-sign-in.
303
+ Start a session only from `valid`. It means what a request through `withOcAuth` means: the token is the popup's, verified for your `audience`, with `azp: "native"`; the person has not ended it everywhere; and the account meets your project's "who can sign in" rule. The key decides whose rule and list of ended sessions apply, so it comes from your config (`projectKey`, default `OC_PROJECT_KEY`), and a `projectKey` in the input throws. The list of ended sessions is read with your project key and `OC_SIGNING_SECRET`, as the adapters read it, or pass `revocation`; the trade counts as a write, so a list that can't be read answers `unavailable`.
304
+
305
+ `invalid` names why: `invalid_grant` (start again), `session_ended`, or `bond_required` with `need`, `have` and `back_url`, as the adapters' 403 carries them. `unavailable` is an outage (`revocations_unavailable` and `bond_policy_unavailable` among them), so answer it 503. Each code works once, whatever the answer. Turn native sign-in on for a test project on ochk.app; the app's half, with a Godot example, is at ochk.io/docs/integration/native-sign-in.
302
306
 
303
307
  ## Keep your own login: OC Connect
304
308
 
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
  });