@orangecheck/me-client 0.42.1 → 0.44.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
@@ -99,6 +99,19 @@ export default withOcAuth(handler, {
99
99
 
100
100
  Or set it in code with `requireBond: { minSats: 100_000, minDays: 30, maxAgeSecs: 3600 }`. ochk.io re-checks the coin when its last check is over ten minutes old; `maxAgeSecs` refuses a claim checked longer ago than you accept. From 0.28.0, an account below the floor gets `403 { ok: false, reason: 'bond_required', need: { min_sats, min_days }, have: { sats_floor, days_floor } | null, back_url }`: link the person to `back_url` to back their account. If the project's setting can't be read, the last one read stands for ten minutes; with none read in that time, a signed-in request gets `503 { reason: 'bond_policy_unavailable' }` rather than being let through unchecked. A request with no session is not held to the project's setting and never waits on it (0.37.0); pass `required: true` to answer it 401. `readBondClaim(session)` and `bondMeets(session, { minSats, minDays, maxAgeSecs })` are exported for your own checks.
101
101
 
102
+ `verifyOcToken` and `checkOcToken` check the token alone, not the rule. Where you verify it yourself, apply the rule with one call in the route that starts your own session (0.43.0). It reads and holds the rule as `withOcAuth` does and answers with the same status and body:
103
+
104
+ ```ts
105
+ import { checkSignInRule, verifyOcToken } from '@orangecheck/me-client/server';
106
+
107
+ const session = await verifyOcToken(token, { audience: 'https://your-site.example' });
108
+ if (!session) return res.status(401).json({ ok: false });
109
+ const checked = await checkSignInRule(session, { projectKey: process.env.OC_PROJECT_KEY! });
110
+ if (!checked.ok) return res.status(checked.status).json(checked.body);
111
+ ```
112
+
113
+ It takes only the session object `verifyOcToken`, `checkOcToken`, `getOcSession` or an adapter returned, and throws for anything else: claims read back from your own session store, or a copy, could name anyone. A floor with `maxAgeSecs` refuses a backing checked too long ago, so apply it where your session starts, not later.
114
+
102
115
  ## Pay your users, when you choose to
103
116
 
104
117
  Paying the people who use your app is optional. In your project's dashboard you turn it on, schedule promos ("Launch week", Oct 20 to 27), or leave it off. While a pay period runs, each event you send earns the person their share and takes its fee from your balance. Outside every period an event earns and costs nothing, and me.ochk.io records nothing, so you can fire events from code that runs all the time:
@@ -258,6 +271,8 @@ app.get('/api/me', (c) => {
258
271
 
259
272
  ### Sessions ended everywhere
260
273
 
274
+ A *.ochk.io site that reads the family cookie (no `audience`) sees family sessions, which ochk.io ends on its own feed. Set `OC_REVOCATION_FEED_TOKEN` (the site's feed token from ochk.io) and the helpers check every family session against it, once a minute per process; while the feed can't be read, reads go on and writes answer 503. With it unset, a family session makes them throw rather than accept it by signature alone (0.44.0); `revocation: false` reads it by signature alone on purpose. The feed is read from `OC_AUTH_ISSUER` (default https://ochk.io), so set it too if you verify another issuer.
275
+
261
276
  Set `OC_PROJECT_KEY` and `OC_SIGNING_SECRET` (from ochk.app/me/projects) and `getOcSession`, `withOcAuth`, `ocAuthExpress` and `ocAuthHono` refuse a session the person ended everywhere: sign out everywhere, a passkey removed, the account deleted. A sign-in token carries `rh`, its revocation handle for your origin; they look it up in me.ochk.io's list of ended handles for that origin, read once a minute per origin with a request signed by your secret, so a refusal lands within about two minutes. The handles name nobody, and another site's handles for the same person never match yours. If the list can't be read, or is more than ten minutes old by its own clock, reads go on and writes are refused until it can: the adapters answer such a write `503 { reason: 'revocations_unavailable' }` (0.38.0), not 401, so a client retries instead of signing the person out, and `checkOcSession(headers, options)` tells the same apart for your own code (`signed_in`, `signed_out` or `unavailable`). Every token while the two variables are unset is checked by its signature alone and stays valid until it expires. A token bound to your site without `rh` is refused (`no_session_handle`, 0.40.0 or later), since sign out everywhere can't reach it; every token from the sign-in popup carries one, and `requireHandle: false` accepts one minted some other way. Needs 0.35.0 or later.
262
277
 
263
278
  ## Keep your own login: OC Connect
package/dist/server.d.mts CHANGED
@@ -126,6 +126,19 @@ interface WithOcAuthOptions extends OcSessionOptions {
126
126
  projectKey?: string;
127
127
  }
128
128
  declare const BACK_ACCOUNT_URL = "https://ochk.app/me/account#backing";
129
+ interface SignInRuleOptions {
130
+ projectKey?: string;
131
+ meOrigin?: string;
132
+ requireBond?: WithOcAuthOptions['requireBond'];
133
+ }
134
+ type SignInRuleCheck = {
135
+ ok: true;
136
+ } | {
137
+ ok: false;
138
+ status: number;
139
+ body: Record<string, unknown>;
140
+ };
141
+ declare function checkSignInRule(session: SessionPayload, options: SignInRuleOptions): Promise<SignInRuleCheck>;
129
142
  declare function withOcAuth<Req extends NextApiRequestLike = NextApiRequestLike, Res extends NextApiResponseLike = NextApiResponseLike>(handler: (req: WithOcSession<Req>, res: Res) => unknown, options?: WithOcAuthOptions): (req: Req, res: Res) => Promise<unknown>;
130
143
  declare global {
131
144
  namespace Express {
@@ -169,4 +182,4 @@ interface HonoContextLike {
169
182
  type HonoNextLike = () => Promise<void>;
170
183
  declare function ocAuthHono(options?: WithOcAuthOptions): (c: HonoContextLike, next: HonoNextLike) => Promise<unknown>;
171
184
 
172
- export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type JtiStore, MemoryJtiStore, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type OcSessionRefusal, type OcSessionUnavailable, type OcTokenCheck, type RevokeTokenOptions, type RevokeTokenRefusal, type RevokeTokenResult, type VerifyOcTokenOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, checkOcToken, connectClaimLink, connectDelete, connectProvision, getOcSession, ocAuthExpress, ocAuthHono, revokeToken, spendOnce, verifyOcToken, withOcAuth };
185
+ export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type JtiStore, MemoryJtiStore, 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, getOcSession, ocAuthExpress, ocAuthHono, revokeToken, spendOnce, verifyOcToken, withOcAuth };
package/dist/server.d.ts CHANGED
@@ -126,6 +126,19 @@ interface WithOcAuthOptions extends OcSessionOptions {
126
126
  projectKey?: string;
127
127
  }
128
128
  declare const BACK_ACCOUNT_URL = "https://ochk.app/me/account#backing";
129
+ interface SignInRuleOptions {
130
+ projectKey?: string;
131
+ meOrigin?: string;
132
+ requireBond?: WithOcAuthOptions['requireBond'];
133
+ }
134
+ type SignInRuleCheck = {
135
+ ok: true;
136
+ } | {
137
+ ok: false;
138
+ status: number;
139
+ body: Record<string, unknown>;
140
+ };
141
+ declare function checkSignInRule(session: SessionPayload, options: SignInRuleOptions): Promise<SignInRuleCheck>;
129
142
  declare function withOcAuth<Req extends NextApiRequestLike = NextApiRequestLike, Res extends NextApiResponseLike = NextApiResponseLike>(handler: (req: WithOcSession<Req>, res: Res) => unknown, options?: WithOcAuthOptions): (req: Req, res: Res) => Promise<unknown>;
130
143
  declare global {
131
144
  namespace Express {
@@ -169,4 +182,4 @@ interface HonoContextLike {
169
182
  type HonoNextLike = () => Promise<void>;
170
183
  declare function ocAuthHono(options?: WithOcAuthOptions): (c: HonoContextLike, next: HonoNextLike) => Promise<unknown>;
171
184
 
172
- export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type JtiStore, MemoryJtiStore, type OcHonoEnv, type OcSessionCheck, type OcSessionOptions, type OcSessionRefusal, type OcSessionUnavailable, type OcTokenCheck, type RevokeTokenOptions, type RevokeTokenRefusal, type RevokeTokenResult, type VerifyOcTokenOptions, type WithOcAuthOptions, type WithOcSession, _resetSessionChecksForTests, checkOcSession, checkOcToken, connectClaimLink, connectDelete, connectProvision, getOcSession, ocAuthExpress, ocAuthHono, revokeToken, spendOnce, verifyOcToken, withOcAuth };
185
+ export { BACK_ACCOUNT_URL, type ConnectClaimLinkRefusal, type ConnectClaimLinkResult, type ConnectDeleteRefusal, type ConnectDeleteResult, type ConnectOptions, type ConnectProvisionOptions, type ConnectProvisionRefusal, type ConnectProvisionResult, type ConnectRefused, type JtiStore, MemoryJtiStore, 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, getOcSession, ocAuthExpress, ocAuthHono, revokeToken, spendOnce, verifyOcToken, withOcAuth };
package/dist/server.js CHANGED
@@ -838,10 +838,18 @@ async function post2(url, tokens, timeoutMs) {
838
838
  }
839
839
 
840
840
  // src/server.ts
841
+ var VERIFIED = /* @__PURE__ */ new WeakSet();
842
+ function vouch(session) {
843
+ VERIFIED.add(session);
844
+ return session;
845
+ }
846
+ function isFamily(session) {
847
+ const aud = session.aud;
848
+ return aud === void 0 || Array.isArray(aud) && aud.length === 0;
849
+ }
841
850
  function lacksHandle(session, requireHandle) {
842
- if (requireHandle === false) return false;
843
- const { aud, rh } = session;
844
- if (aud === void 0) return false;
851
+ if (requireHandle === false || isFamily(session)) return false;
852
+ const rh = session.rh;
845
853
  return typeof rh !== "string" || rh.length === 0;
846
854
  }
847
855
  function checkRequireHandle(requireHandle) {
@@ -869,7 +877,7 @@ async function verifyOcToken(token, options = {}) {
869
877
  warnNoHandle(session);
870
878
  return null;
871
879
  }
872
- return session;
880
+ return vouch(session);
873
881
  }
874
882
  async function checkOcToken(token, options = {}) {
875
883
  if (options === null || typeof options !== "object") {
@@ -884,13 +892,22 @@ async function checkOcToken(token, options = {}) {
884
892
  warnNoHandle(checked.session);
885
893
  return { status: "invalid", reason: "no_session_handle" };
886
894
  }
887
- return { status: "valid", session: checked.session };
895
+ return { status: "valid", session: vouch(checked.session) };
888
896
  }
889
897
  var sessionChecks = /* @__PURE__ */ new Map();
890
898
  var warnedUnchecked = false;
891
- function revocationFor(options) {
899
+ function revocationFor(options, session) {
892
900
  if (options.revocation === false) return null;
893
901
  if (options.revocation) return options.revocation;
902
+ if (isFamily(session)) {
903
+ const token = typeof process !== "undefined" ? process.env?.OC_REVOCATION_FEED_TOKEN?.trim() : void 0;
904
+ if (!token) {
905
+ throw new Error(
906
+ "@orangecheck/me-client: a family session (no aud) is checked against ochk.io's revocation feed, and OC_REVOCATION_FEED_TOKEN is not set. Set it, or pass `revocation: false` to read family sessions by signature alone."
907
+ );
908
+ }
909
+ return authCore.familyRevocationCheck();
910
+ }
894
911
  const key = `${options.meOrigin ?? ""} ${options.projectKey ?? ""}`;
895
912
  if (!sessionChecks.has(key)) {
896
913
  sessionChecks.set(
@@ -936,7 +953,7 @@ async function checkOcSession(headers, options = {}) {
936
953
  warnNoHandle(session);
937
954
  return { status: "signed_out", reason: "no_session_handle" };
938
955
  }
939
- const check = revocationFor(options);
956
+ const check = revocationFor(options, session);
940
957
  if (check) {
941
958
  const state = await check.status(session);
942
959
  if (state === "revoked") return { status: "signed_out" };
@@ -944,7 +961,7 @@ async function checkOcSession(headers, options = {}) {
944
961
  return { status: "unavailable", reason: "revocations_unavailable" };
945
962
  }
946
963
  }
947
- return { status: "signed_in", session };
964
+ return { status: "signed_in", session: vouch(session) };
948
965
  }
949
966
  var unavailable = (checked) => ({
950
967
  status: 503,
@@ -1114,6 +1131,33 @@ async function refusal(session, options, why) {
1114
1131
  }
1115
1132
  return null;
1116
1133
  }
1134
+ var SIGN_IN_RULE_OPTIONS = /* @__PURE__ */ new Set([
1135
+ "projectKey",
1136
+ "meOrigin",
1137
+ "requireBond"
1138
+ ]);
1139
+ async function checkSignInRule(session, options) {
1140
+ if (options === null || typeof options !== "object") {
1141
+ throw new TypeError("@orangecheck/me-client: options must be an object");
1142
+ }
1143
+ const unknown = Object.keys(options).filter((k) => !SIGN_IN_RULE_OPTIONS.has(k));
1144
+ if (unknown.length > 0) {
1145
+ throw new TypeError(
1146
+ `@orangecheck/me-client: unknown option ${unknown.join(", ")}. It reads only ${[...SIGN_IN_RULE_OPTIONS].join(", ")}.`
1147
+ );
1148
+ }
1149
+ checkOptions(options);
1150
+ if (!options.projectKey && !options.requireBond) {
1151
+ throw new TypeError("@orangecheck/me-client: checkSignInRule needs projectKey or requireBond");
1152
+ }
1153
+ if (session === null || typeof session !== "object" || !VERIFIED.has(session)) {
1154
+ throw new TypeError(
1155
+ "@orangecheck/me-client: checkSignInRule takes the session verifyOcToken, checkOcToken or getOcSession returned, as is, not claims read some other way"
1156
+ );
1157
+ }
1158
+ const refused2 = await refusal(session, options);
1159
+ return refused2 ? { ok: false, status: refused2.status, body: refused2.body } : { ok: true };
1160
+ }
1117
1161
  function withOcAuth(handler, options = {}) {
1118
1162
  checkOptions(options);
1119
1163
  return async (req, res) => {
@@ -1192,6 +1236,7 @@ exports.MemoryJtiStore = MemoryJtiStore;
1192
1236
  exports._resetSessionChecksForTests = _resetSessionChecksForTests;
1193
1237
  exports.checkOcSession = checkOcSession;
1194
1238
  exports.checkOcToken = checkOcToken;
1239
+ exports.checkSignInRule = checkSignInRule;
1195
1240
  exports.connectClaimLink = connectClaimLink;
1196
1241
  exports.connectDelete = connectDelete;
1197
1242
  exports.connectProvision = connectProvision;