@orangecheck/me-client 0.42.1 → 0.43.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:
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,6 +838,11 @@ 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
+ }
841
846
  function lacksHandle(session, requireHandle) {
842
847
  if (requireHandle === false) return false;
843
848
  const { aud, rh } = session;
@@ -869,7 +874,7 @@ async function verifyOcToken(token, options = {}) {
869
874
  warnNoHandle(session);
870
875
  return null;
871
876
  }
872
- return session;
877
+ return vouch(session);
873
878
  }
874
879
  async function checkOcToken(token, options = {}) {
875
880
  if (options === null || typeof options !== "object") {
@@ -884,7 +889,7 @@ async function checkOcToken(token, options = {}) {
884
889
  warnNoHandle(checked.session);
885
890
  return { status: "invalid", reason: "no_session_handle" };
886
891
  }
887
- return { status: "valid", session: checked.session };
892
+ return { status: "valid", session: vouch(checked.session) };
888
893
  }
889
894
  var sessionChecks = /* @__PURE__ */ new Map();
890
895
  var warnedUnchecked = false;
@@ -944,7 +949,7 @@ async function checkOcSession(headers, options = {}) {
944
949
  return { status: "unavailable", reason: "revocations_unavailable" };
945
950
  }
946
951
  }
947
- return { status: "signed_in", session };
952
+ return { status: "signed_in", session: vouch(session) };
948
953
  }
949
954
  var unavailable = (checked) => ({
950
955
  status: 503,
@@ -1114,6 +1119,33 @@ async function refusal(session, options, why) {
1114
1119
  }
1115
1120
  return null;
1116
1121
  }
1122
+ var SIGN_IN_RULE_OPTIONS = /* @__PURE__ */ new Set([
1123
+ "projectKey",
1124
+ "meOrigin",
1125
+ "requireBond"
1126
+ ]);
1127
+ async function checkSignInRule(session, options) {
1128
+ if (options === null || typeof options !== "object") {
1129
+ throw new TypeError("@orangecheck/me-client: options must be an object");
1130
+ }
1131
+ const unknown = Object.keys(options).filter((k) => !SIGN_IN_RULE_OPTIONS.has(k));
1132
+ if (unknown.length > 0) {
1133
+ throw new TypeError(
1134
+ `@orangecheck/me-client: unknown option ${unknown.join(", ")}. It reads only ${[...SIGN_IN_RULE_OPTIONS].join(", ")}.`
1135
+ );
1136
+ }
1137
+ checkOptions(options);
1138
+ if (!options.projectKey && !options.requireBond) {
1139
+ throw new TypeError("@orangecheck/me-client: checkSignInRule needs projectKey or requireBond");
1140
+ }
1141
+ if (session === null || typeof session !== "object" || !VERIFIED.has(session)) {
1142
+ throw new TypeError(
1143
+ "@orangecheck/me-client: checkSignInRule takes the session verifyOcToken, checkOcToken or getOcSession returned, as is, not claims read some other way"
1144
+ );
1145
+ }
1146
+ const refused2 = await refusal(session, options);
1147
+ return refused2 ? { ok: false, status: refused2.status, body: refused2.body } : { ok: true };
1148
+ }
1117
1149
  function withOcAuth(handler, options = {}) {
1118
1150
  checkOptions(options);
1119
1151
  return async (req, res) => {
@@ -1192,6 +1224,7 @@ exports.MemoryJtiStore = MemoryJtiStore;
1192
1224
  exports._resetSessionChecksForTests = _resetSessionChecksForTests;
1193
1225
  exports.checkOcSession = checkOcSession;
1194
1226
  exports.checkOcToken = checkOcToken;
1227
+ exports.checkSignInRule = checkSignInRule;
1195
1228
  exports.connectClaimLink = connectClaimLink;
1196
1229
  exports.connectDelete = connectDelete;
1197
1230
  exports.connectProvision = connectProvision;