@pithy-sh/auth 0.3.0 → 0.5.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.
Files changed (58) hide show
  1. package/dist/audit/actions.d.ts +12 -0
  2. package/dist/audit/actions.d.ts.map +1 -1
  3. package/dist/audit/actions.js +12 -0
  4. package/dist/audit/emit.d.ts +16 -0
  5. package/dist/audit/emit.d.ts.map +1 -1
  6. package/dist/audit/emit.js +21 -1
  7. package/dist/capability.d.ts.map +1 -1
  8. package/dist/capability.js +9 -2
  9. package/dist/client/projection.d.ts +22 -0
  10. package/dist/client/projection.d.ts.map +1 -1
  11. package/dist/data/betterAuth.d.ts +1 -0
  12. package/dist/data/betterAuth.d.ts.map +1 -1
  13. package/dist/data/betterAuth.js +2 -1
  14. package/dist/data/kitFields.d.ts +23 -1
  15. package/dist/data/kitFields.d.ts.map +1 -1
  16. package/dist/data/kitFields.js +23 -1
  17. package/dist/data/tables.d.ts +1 -0
  18. package/dist/data/tables.d.ts.map +1 -1
  19. package/dist/http/middleware.d.ts +0 -7
  20. package/dist/http/middleware.d.ts.map +1 -1
  21. package/dist/http/middleware.js +7 -1
  22. package/dist/http/providerFreshness.d.ts +100 -0
  23. package/dist/http/providerFreshness.d.ts.map +1 -0
  24. package/dist/http/providerFreshness.js +98 -0
  25. package/dist/http/routes.d.ts.map +1 -1
  26. package/dist/http/routes.js +5 -0
  27. package/dist/instance/auth.d.ts +70 -1
  28. package/dist/instance/auth.d.ts.map +1 -1
  29. package/dist/instance/auth.js +62 -3
  30. package/dist/migrations/0002_session_authenticated_at.d.ts +39 -0
  31. package/dist/migrations/0002_session_authenticated_at.d.ts.map +1 -0
  32. package/dist/migrations/0002_session_authenticated_at.js +47 -0
  33. package/dist/migrations/set.d.ts +20 -0
  34. package/dist/migrations/set.d.ts.map +1 -0
  35. package/dist/migrations/set.js +24 -0
  36. package/dist/seeds/devSession.d.ts.map +1 -1
  37. package/dist/seeds/devSession.js +2 -1
  38. package/dist/test-utils/liveApp.d.ts.map +1 -1
  39. package/dist/test-utils/liveApp.js +3 -2
  40. package/dist/version.generated.d.ts +1 -1
  41. package/dist/version.generated.js +1 -1
  42. package/docs/github-oauth.md +5 -3
  43. package/package.json +5 -5
  44. package/src/audit/actions.ts +12 -0
  45. package/src/audit/emit.ts +30 -0
  46. package/src/capability.ts +14 -2
  47. package/src/client/projection.ts +22 -0
  48. package/src/data/betterAuth.ts +3 -0
  49. package/src/data/kitFields.ts +19 -1
  50. package/src/http/middleware.ts +13 -0
  51. package/src/http/providerFreshness.ts +146 -0
  52. package/src/http/routes.ts +29 -1
  53. package/src/instance/auth.ts +61 -5
  54. package/src/migrations/0002_session_authenticated_at.ts +50 -0
  55. package/src/migrations/set.ts +23 -0
  56. package/src/seeds/devSession.ts +2 -0
  57. package/src/test-utils/liveApp.ts +3 -2
  58. package/src/version.generated.ts +1 -1
@@ -39,6 +39,18 @@ export declare const AuthAuditActions: {
39
39
  * cost" is answerable from the same place every other auth question is.
40
40
  */
41
41
  readonly providerUnavailable: "auth/provider_unavailable";
42
+ /**
43
+ * Somebody signed in tried to attach a social provider without having authenticated recently enough.
44
+ * Outcome `denied`, severity `warning`.
45
+ *
46
+ * Attaching an identity grants permanent access — after it, sign-in resolves by account id and the email
47
+ * stops mattering — so this is the row that answers "was somebody walking a stolen credential at the
48
+ * account-takeover step". One is a person who left a tab open; a run of them against one user id is the
49
+ * incident, and without the row there is nothing to count.
50
+ *
51
+ * Mirrors the error code, which is this file's one precedent for doing so.
52
+ */
53
+ readonly sessionNotFresh: "auth/session_not_fresh";
42
54
  /**
43
55
  * The admin actions, emitted only from the control-plane surface (`http/adminRoutes.ts`) and always
44
56
  * with `actorType: "control-plane"` — a management client is not a user of the adopter's app, so its
@@ -1 +1 @@
1
- {"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../../src/audit/actions.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB;IAC3B,+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ChC,gEAAgE;aAChE,gBAAgB,EAAE,wBAAwB;IAC1C,wDAAwD;aACxD,aAAa,EAAE,qBAAqB;IACpC;;;;;;;;;;OAUG;aACH,mBAAmB,EAAE,2BAA2B;IAChD;;;;;;;;;;OAUG;aACH,eAAe,EAAE,wBAAwB;IAEzC;;;;;;;;;;OAUG;IAEH,wGAAwG;aACxG,gBAAgB,EAAE,yBAAyB;IAC3C,sFAAsF;aACtF,aAAa,EAAE,sBAAsB;IACrC,kEAAkE;aAClE,kBAAkB,EAAE,2BAA2B;IAC/C,iEAAiE;aACjE,mBAAmB,EAAE,4BAA4B;IACjD,iGAAiG;aACjG,wBAAwB,EAAE,kCAAkC;IAC5D,wGAAwG;aACxG,kBAAkB,EAAE,2BAA2B;CACvC,CAAC;AAEX,0CAA0C;AAC1C,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,OAAO,gBAAgB,CAAC,CAAC"}
@@ -41,6 +41,18 @@ const AuthAuditActions = {
41
41
  */
42
42
  providerUnavailable: "auth/provider_unavailable",
43
43
  /**
44
+ * Somebody signed in tried to attach a social provider without having authenticated recently enough.
45
+ * Outcome `denied`, severity `warning`.
46
+ *
47
+ * Attaching an identity grants permanent access — after it, sign-in resolves by account id and the email
48
+ * stops mattering — so this is the row that answers "was somebody walking a stolen credential at the
49
+ * account-takeover step". One is a person who left a tab open; a run of them against one user id is the
50
+ * incident, and without the row there is nothing to count.
51
+ *
52
+ * Mirrors the error code, which is this file's one precedent for doing so.
53
+ */
54
+ sessionNotFresh: "auth/session_not_fresh",
55
+ /**
44
56
  * The admin actions, emitted only from the control-plane surface (`http/adminRoutes.ts`) and always
45
57
  * with `actorType: "control-plane"` — a management client is not a user of the adopter's app, so its
46
58
  * actions must be answerable separately from their users'.
@@ -94,6 +94,22 @@ export declare function emitProviderUnavailable(emit: AuditEmit, context: {
94
94
  provider: string;
95
95
  headers: Headers | undefined;
96
96
  }): Promise<void>;
97
+ /** Which direction of provider change was refused — the two read very differently in a trail. */
98
+ export type ProviderChange = "link" | "unlink";
99
+ /**
100
+ * Emit a `session_not_fresh` event — an attempt to change connected accounts on an authentic but stale
101
+ * credential.
102
+ *
103
+ * `actorType: "user"` rather than `anonymous`, unlike `emitProviderUnavailable`: this caller is
104
+ * authenticated, and the user id is the whole value of the row. Counting these per user is how a stolen
105
+ * credential being walked toward an account change becomes visible.
106
+ */
107
+ export declare function emitProviderChangeNotFresh(emit: AuditEmit, context: {
108
+ change: ProviderChange;
109
+ userId: string;
110
+ sessionId: string;
111
+ headers: Headers | undefined;
112
+ }): Promise<void>;
97
113
  /** A blocked/failed auth attempt — recorded as `denied` (first-class). */
98
114
  export declare function emitDenied(emit: AuditEmit, context: {
99
115
  ip?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"emit.d.ts","sourceRoot":"","sources":["../../src/audit/emit.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AAGnE;;;;;;;GAOG;AAEH,gFAAgF;AAChF,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG;IAAE,EAAE,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,CAK7F;AAUD,uGAAuG;AACvG,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,GAAG,SAAS,CAAC;IAC7B,UAAU,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,GAAG,IAAI,CAAC;IAClF,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAcD,qHAAqH;AACrH,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAiCxF;AAED,qF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AC,OAAO,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,GAC1D,OAAO,CAAC,IAAI,CAAC,CASf;AAED,iGAAiG;AACjG,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,QAAQ,CAAC;AAE/C;;;;;;;GAOG;AACH,wBAAsB,0BAA0B,CAC9C,IAAI,EAAE,SAAS,EACf,OAAO,EAAE;IAAE,MAAM,EAAE,cAAc,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,GACnG,OAAO,CAAC,IAAI,CAAC,CAcf;AAED,0EAA0E;AAC1E,wBAAsB,UAAU,CAC9B,IAAI,EAAE,SAAS,EACf,OAAO,EAAE;IAAE,EAAE,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAC5D,OAAO,CAAC,IAAI,CAAC,CASf"}
@@ -157,6 +157,26 @@ async function emitProviderUnavailable(emit, context) {
157
157
  ...correlation(context.headers)
158
158
  });
159
159
  }
160
+ /**
161
+ * Emit a `session_not_fresh` event — an attempt to change connected accounts on an authentic but stale
162
+ * credential.
163
+ *
164
+ * `actorType: "user"` rather than `anonymous`, unlike `emitProviderUnavailable`: this caller is
165
+ * authenticated, and the user id is the whole value of the row. Counting these per user is how a stolen
166
+ * credential being walked toward an account change becomes visible.
167
+ */
168
+ async function emitProviderChangeNotFresh(emit, context) {
169
+ await safeEmit(emit, {
170
+ action: AuthAuditActions.sessionNotFresh,
171
+ outcome: "denied",
172
+ severity: "warning",
173
+ actorType: "user",
174
+ actorId: context.userId,
175
+ sessionId: context.sessionId,
176
+ metadata: { change: context.change },
177
+ ...correlation(context.headers)
178
+ });
179
+ }
160
180
  /** A blocked/failed auth attempt — recorded as `denied` (first-class). */
161
181
  async function emitDenied(emit, context) {
162
182
  await safeEmit(emit, {
@@ -169,4 +189,4 @@ async function emitDenied(emit, context) {
169
189
  });
170
190
  }
171
191
  //#endregion
172
- export { correlation, emitAfterRequest, emitControlPlaneAction, emitDenied, emitDeviceRevoked, emitProviderUnavailable, emitTokenRefresh, emitTokenReuseDetected };
192
+ export { correlation, emitAfterRequest, emitControlPlaneAction, emitDenied, emitDeviceRevoked, emitProviderChangeNotFresh, emitProviderUnavailable, emitTokenRefresh, emitTokenReuseDetected };
@@ -1 +1 @@
1
- {"version":3,"file":"capability.d.ts","sourceRoot":"","sources":["../src/capability.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAmB,MAAM,0CAA0C,CAAC;AAG5F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAGtE,OAAO,EAA0B,KAAK,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACnG,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAkCxE,mGAAmG;AACnG,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAsE8B,CAAC;AACtD,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC;AACrD,MAAM,MAAM,eAAe,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,UAAU,CAAC,CAAC;AAEzD;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,UAAU,CAAC;IACnB,8GAA8G;IAC9G,qBAAqB,EAAE,sBAAsB,GAAG,SAAS,CAAC;IAC1D,sGAAsG;IACtG,YAAY,EAAE,eAAe,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;IACrD,6FAA6F;IAC7F,SAAS,EAAE;QAAE,IAAI,EAAE,aAAa,CAAA;KAAE,GAAG,SAAS,CAAC;CAChD;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GAAG,e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,SAAS,CAAC;CAChD;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GAAG,eAAe,GAAG;IAC1C;;;;;;;;;;;;;OAaG;IACH,qBAAqB,CAAC,EAAE,sBAAsB,CAAC;CAChD,CAAC;AAEF,6EAA6E;AAC7E,MAAM,WAAW,cAAe,SAAQ,UAAU;IAChD,UAAU,EAAE,UAAU,CAAC;CACxB;AAiCD;;;;;GAKG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,cAAc,CAyJzD;AAED,0DAA0D;AAC1D,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,UAAU,GAAG,UAAU,IAAI,cAAc,CAErF"}
@@ -10,8 +10,9 @@ import { authAdminRoutes } from "./http/guards.js";
10
10
  import { createSessionMiddleware } from "./http/middleware.js";
11
11
  import { createRateLimitMiddleware } from "./http/rateLimit.js";
12
12
  import { createAuthRoutes } from "./http/routes.js";
13
- import { auth_0001_init } from "./migrations/0001_init.js";
13
+ import "./migrations/0001_init.js";
14
14
  import { authPluginPlan } from "./migrations/pluginTables.js";
15
+ import { AUTH_MIGRATIONS } from "./migrations/set.js";
15
16
  import { authExampleSeed } from "./seeds/example.js";
16
17
  import { PACKAGE_VERSION } from "./version.generated.js";
17
18
  import { defineCapability } from "@pithy-sh/core/src/capability/capability";
@@ -121,7 +122,7 @@ function auth(options) {
121
122
  tables: authTables,
122
123
  migrationOrder: 300,
123
124
  migrations: {
124
- "0001_init": auth_0001_init,
125
+ ...AUTH_MIGRATIONS,
125
126
  ...pluginPlan.migrations
126
127
  }
127
128
  } },
@@ -161,6 +162,12 @@ function auth(options) {
161
162
  facebook: resolved.facebook.enabled,
162
163
  github: resolved.github.enabled
163
164
  },
165
+ providerSignUp: {
166
+ google: resolved.google.allowSignUp,
167
+ apple: resolved.apple.allowSignUp,
168
+ facebook: resolved.facebook.allowSignUp,
169
+ github: resolved.github.allowSignUp
170
+ },
164
171
  otpLength: resolved.otpLength,
165
172
  signUpEnabled: !resolved.disableSignUp
166
173
  }),
@@ -45,6 +45,28 @@ export type AuthClientProjection = {
45
45
  /** Whether Sign in with GitHub is offered. */
46
46
  github: boolean;
47
47
  };
48
+ /**
49
+ * Whether each offered provider may create an account, from its `allowSignUp` toggle.
50
+ *
51
+ * **A second fact from {@link signUpEnabled}, and the screen needs both.** That one answers for
52
+ * magic link and OTP; this answers per provider. A project can let email create accounts while
53
+ * GitHub only signs existing ones in — which is the configuration #554 exists for — and with only
54
+ * the global flag a screen renders "Signing in creates one." directly beneath a button that will
55
+ * refuse (#559).
56
+ *
57
+ * Optional, so a screen copied before this landed keeps compiling and reads `undefined` as "no
58
+ * provider objects" — the state every project was in until now.
59
+ */
60
+ providerSignUp?: {
61
+ /** Whether a Google sign-in may create an account. */
62
+ google: boolean;
63
+ /** Whether an Apple sign-in may create an account. */
64
+ apple: boolean;
65
+ /** Whether a Facebook sign-in may create an account. */
66
+ facebook: boolean;
67
+ /** Whether a GitHub sign-in may create an account. */
68
+ github: boolean;
69
+ };
48
70
  /** How many digits an email OTP carries. */
49
71
  otpLength: number;
50
72
  /** Whether signing in may provision a new user. Drives the sign-up copy. */
@@ -1 +1 @@
1
- {"version":3,"file":"projection.d.ts","sourceRoot":"","sources":["../../src/client/projection.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,oBAAoB,GAC5B;IACE,mGAAmG;IACnG,OAAO,EAAE,KAAK,CAAC;CAChB,GACD;IACE,uCAAuC;IACvC,OAAO,EAAE,IAAI,CAAC;IACd,4DAA4D;IAC5D,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;;;OAOG;IACH,SAAS,EAAE;QACT,8CAA8C;QAC9C,MAAM,EAAE,OAAO,CAAC;QAChB,6CAA6C;QAC7C,KAAK,EAAE,OAAO,CAAC;QACf,gDAAgD;QAChD,QAAQ,EAAE,OAAO,CAAC;QAClB,8CAA8C;QAC9C,MAAM,EAAE,OAAO,CAAC;KACjB,CAAC;IACF,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,aAAa,EAAE,OAAO,CAAC;CACxB,CAAC"}
1
+ {"version":3,"file":"projection.d.ts","sourceRoot":"","sources":["../../src/client/projection.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,oBAAoB,GAC5B;IACE,mGAAmG;IACnG,OAAO,EAAE,KAAK,CAAC;CAChB,GACD;IACE,uCAAuC;IACvC,OAAO,EAAE,IAAI,CAAC;IACd,4DAA4D;IAC5D,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;;;OAOG;IACH,SAAS,EAAE;QACT,8CAA8C;QAC9C,MAAM,EAAE,OAAO,CAAC;QAChB,6CAA6C;QAC7C,KAAK,EAAE,OAAO,CAAC;QACf,gDAAgD;QAChD,QAAQ,EAAE,OAAO,CAAC;QAClB,8CAA8C;QAC9C,MAAM,EAAE,OAAO,CAAC;KACjB,CAAC;IACF;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE;QACf,sDAAsD;QACtD,MAAM,EAAE,OAAO,CAAC;QAChB,sDAAsD;QACtD,KAAK,EAAE,OAAO,CAAC;QACf,wDAAwD;QACxD,QAAQ,EAAE,OAAO,CAAC;QAClB,sDAAsD;QACtD,MAAM,EAAE,OAAO,CAAC;KACjB,CAAC;IACF,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,aAAa,EAAE,OAAO,CAAC;CACxB,CAAC"}
@@ -25,6 +25,7 @@ export declare const Session: z.ZodObject<{
25
25
  userId: z.ZodString;
26
26
  deviceId: z.ZodNullable<z.ZodString>;
27
27
  familyId: z.ZodNullable<z.ZodString>;
28
+ authenticatedAt: z.ZodNullable<z.ZodCodec<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodDate]>, z.ZodDate>>;
28
29
  }, z.core.$strip>;
29
30
  export type Session = z.output<typeof Session>;
30
31
  /** `pithy_auth_accounts` — a credential/identity link for a user (one row per social provider). */
@@ -1 +1 @@
1
- {"version":3,"file":"betterAuth.d.ts","sourceRoot":"","sources":["../../src/data/betterAuth.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAuBxB,2FAA2F;AAC3F,eAAO,MAAM,IAAI;;;;;;;;;iBAqBwF,CAAC;AAC1G,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;AAEzC,6FAA6F;AAC7F,eAAO,MAAM,OAAO;;;;;;;;;;;iBAkCqF,CAAC;AAC1G,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,OAAO,CAAC,CAAC;AAE/C,mGAAmG;AACnG,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;iBAyCqF,CAAC;AAC1G,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,OAAO,CAAC,CAAC;AAE/C,8FAA8F;AAC9F,eAAO,MAAM,YAAY;;;;;;;iBAmBtB,CAAC;AACJ,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,YAAY,CAAC,CAAC;AAEzD,gGAAgG;AAChG,eAAO,MAAM,IAAI;;;;;;;;iBA8Bd,CAAC;AACJ,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;AAEzC,sHAAsH;AACtH,eAAO,MAAM,SAAS;;;;;iBAcnB,CAAC;AACJ,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,SAAS,CAAC,CAAC"}
1
+ {"version":3,"file":"betterAuth.d.ts","sourceRoot":"","sources":["../../src/data/betterAuth.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAuBxB,2FAA2F;AAC3F,eAAO,MAAM,IAAI;;;;;;;;;iBAqBwF,CAAC;AAC1G,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;AAEzC,6FAA6F;AAC7F,eAAO,MAAM,OAAO;;;;;;;;;;;;iBAqCqF,CAAC;AAC1G,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,OAAO,CAAC,CAAC;AAE/C,mGAAmG;AACnG,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;iBAyCqF,CAAC;AAC1G,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,OAAO,CAAC,CAAC;AAE/C,8FAA8F;AAC9F,eAAO,MAAM,YAAY;;;;;;;iBAmBtB,CAAC;AACJ,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,YAAY,CAAC,CAAC;AAEzD,gGAAgG;AAChG,eAAO,MAAM,IAAI;;;;;;;;iBA8Bd,CAAC;AACJ,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;AAEzC,sHAAsH;AACtH,eAAO,MAAM,SAAS;;;;;iBAcnB,CAAC;AACJ,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,SAAS,CAAC,CAAC"}
@@ -45,7 +45,8 @@ const Session = z.object({
45
45
  userAgent: z.string().nullable().describe("The client user-agent at sign-in. Nullable."),
46
46
  userId: z.string().describe("The owning user's id. Foreign key to `pithy_auth_users(id)`, ON DELETE CASCADE."),
47
47
  deviceId: z.string().nullable().describe("The id of the `pithy_auth_devices` row this session is bound to (per-device sessions), or null for a device-less web login."),
48
- familyId: z.string().nullable().describe("The refresh-token family this session belongs to, carried across rotations so a replayed refresh token can revoke the whole chain. Server-set; null for a session created before rotation or one never rotated.")
48
+ familyId: z.string().nullable().describe("The refresh-token family this session belongs to, carried across rotations so a replayed refresh token can revoke the whole chain. Server-set; null for a session created before rotation or one never rotated."),
49
+ authenticatedAt: BetterAuthDate.nullable().describe("When the holder of this session last actually authenticated — a sign-in, never a rotation, which carries the value forward unchanged. This is what the provider-linking freshness gate reads; the session's own `createdAt` cannot be, because `/token/rotate` stamps a fresh one on every successor and is therefore resettable by anyone holding a stolen refresh token (#558). ISO-8601 text in SQLite. Null only for a session written before the column existed.")
49
50
  }).describe("A session in `pithy_auth_sessions` — a signed-in credential, optionally bound to a device.");
50
51
  /** `pithy_auth_accounts` — a credential/identity link for a user (one row per social provider). */
51
52
  const Account = z.object({
@@ -59,10 +59,27 @@ export declare const KIT_USER_FIELDS: {
59
59
  };
60
60
  };
61
61
  /**
62
+ * **`authenticatedAt` carries three decisions a future author must not undo.**
63
+ *
64
+ * `type: "date"` rather than `"string"`: the adapter's `Date` ↔ ISO-8601 conversion is keyed on that
65
+ * literal (`@better-auth/core`'s `factory.mjs`, on write and on read), and the kysely adapter reports
66
+ * `supportsDates: false` for SQLite. `deviceId`/`familyId` are `"string"` because they are opaque ids; a
67
+ * timestamp is not.
68
+ *
69
+ * **No `defaultValue`, and this is the one that bites.** A function default would stamp every sign-in
70
+ * with no hook at all — but `getSessionDefaultFields` is spread into the row *after* the caller's
71
+ * override in `createSession`, so a fresh default would overwrite the value `rotateToken` carries
72
+ * forward, silently restoring the exact defect this column exists to fix (#558). It is stamped by the
73
+ * `session.create.before` hook instead.
74
+ *
75
+ * `input: false`, like its siblings: a client able to name its own authentication time would defeat the
76
+ * link-freshness gate outright. `required: false` because the column is nullable for rows written before
77
+ * it existed.
78
+ *
62
79
  * The extra `pithy_auth_sessions` columns — the bound device, and the refresh-token family carried
63
80
  * across rotations.
64
81
  *
65
- * `input: false` on both: these are server-set facts about a session, and a client able to name its own
82
+ * `input: false` on all three: these are server-set facts about a session, and a client able to name its own
66
83
  * device id or token family could name somebody else's.
67
84
  */
68
85
  export declare const KIT_SESSION_FIELDS: {
@@ -76,5 +93,10 @@ export declare const KIT_SESSION_FIELDS: {
76
93
  readonly required: false;
77
94
  readonly input: false;
78
95
  };
96
+ readonly authenticatedAt: {
97
+ readonly type: "date";
98
+ readonly required: false;
99
+ readonly input: false;
100
+ };
79
101
  };
80
102
  //# sourceMappingURL=kitFields.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"kitFields.d.ts","sourceRoot":"","sources":["../../src/data/kitFields.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,eAAe;aAC1B,MAAM;iBAAI,IAAI,EAAE,QAAQ;iBAAE,QAAQ;iBAAS,KAAK;iBAAQ,SAAS;qBAAI,KAAK;;;CAClE,CAAC;AAEX;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB;aAC7B,QAAQ;iBAAI,IAAI,EAAE,QAAQ;iBAAE,QAAQ;iBAAS,KAAK;;aAClD,QAAQ;iBAAI,IAAI,EAAE,QAAQ;iBAAE,QAAQ;iBAAS,KAAK;;CAC1C,CAAC"}
1
+ {"version":3,"file":"kitFields.d.ts","sourceRoot":"","sources":["../../src/data/kitFields.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,eAAe;aAC1B,MAAM;iBAAI,IAAI,EAAE,QAAQ;iBAAE,QAAQ;iBAAS,KAAK;iBAAQ,SAAS;qBAAI,KAAK;;;CAClE,CAAC;AAEX;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,kBAAkB;aAC7B,QAAQ;iBAAI,IAAI,EAAE,QAAQ;iBAAE,QAAQ;iBAAS,KAAK;;aAClD,QAAQ;iBAAI,IAAI,EAAE,QAAQ;iBAAE,QAAQ;iBAAS,KAAK;;aAClD,eAAe;iBAAI,IAAI,EAAE,MAAM;iBAAE,QAAQ;iBAAS,KAAK;;CAC/C,CAAC"}
@@ -57,10 +57,27 @@ const KIT_USER_FIELDS = { locale: {
57
57
  validator: { input: Locale.nullable() }
58
58
  } };
59
59
  /**
60
+ * **`authenticatedAt` carries three decisions a future author must not undo.**
61
+ *
62
+ * `type: "date"` rather than `"string"`: the adapter's `Date` ↔ ISO-8601 conversion is keyed on that
63
+ * literal (`@better-auth/core`'s `factory.mjs`, on write and on read), and the kysely adapter reports
64
+ * `supportsDates: false` for SQLite. `deviceId`/`familyId` are `"string"` because they are opaque ids; a
65
+ * timestamp is not.
66
+ *
67
+ * **No `defaultValue`, and this is the one that bites.** A function default would stamp every sign-in
68
+ * with no hook at all — but `getSessionDefaultFields` is spread into the row *after* the caller's
69
+ * override in `createSession`, so a fresh default would overwrite the value `rotateToken` carries
70
+ * forward, silently restoring the exact defect this column exists to fix (#558). It is stamped by the
71
+ * `session.create.before` hook instead.
72
+ *
73
+ * `input: false`, like its siblings: a client able to name its own authentication time would defeat the
74
+ * link-freshness gate outright. `required: false` because the column is nullable for rows written before
75
+ * it existed.
76
+ *
60
77
  * The extra `pithy_auth_sessions` columns — the bound device, and the refresh-token family carried
61
78
  * across rotations.
62
79
  *
63
- * `input: false` on both: these are server-set facts about a session, and a client able to name its own
80
+ * `input: false` on all three: these are server-set facts about a session, and a client able to name its own
64
81
  * device id or token family could name somebody else's.
65
82
  */
66
83
  const KIT_SESSION_FIELDS = {
@@ -73,6 +90,11 @@ const KIT_SESSION_FIELDS = {
73
90
  type: "string",
74
91
  required: false,
75
92
  input: false
93
+ },
94
+ authenticatedAt: {
95
+ type: "date",
96
+ required: false,
97
+ input: false
76
98
  }
77
99
  };
78
100
  //#endregion
@@ -34,6 +34,7 @@ export declare const authTables: {
34
34
  userId: import("zod").ZodString;
35
35
  deviceId: import("zod").ZodNullable<import("zod").ZodString>;
36
36
  familyId: import("zod").ZodNullable<import("zod").ZodString>;
37
+ authenticatedAt: import("zod").ZodNullable<import("zod").ZodCodec<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodDate]>, import("zod").ZodDate>>;
37
38
  }, import("zod/v4/core").$strip>;
38
39
  pithyAuthAccounts: import("zod").ZodObject<{
39
40
  id: import("zod").ZodString;
@@ -1 +1 @@
1
- {"version":3,"file":"tables.d.ts","sourceRoot":"","sources":["../../src/data/tables.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAKrC;;;;;;;;GAQG;AACH,eAAO,MAAM,UAAU;IACrB,cAAc;;;;;;;;;;IACd,iBAAiB;;;;;;;;;;;;IACjB,iBAAiB;;;;;;;;;;;;;;;;IACjB,sBAAsB;;;;;;;;IACtB,aAAa;;;;;;;;;IACb,kBAAkB;;;;;;IAClB,gBAAgB;;;;;;;;;;;;;;;;;IAChB,sBAAsB;;;;;;CACvB,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,OAAO,UAAU,CAAC;AAE3C,8GAA8G;AAC9G,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,CAAC;AAE9D,yGAAyG;AACzG,wBAAgB,YAAY,CAAC,EAAE,EAAE,UAAU,GAAG,YAAY,CAEzD"}
1
+ {"version":3,"file":"tables.d.ts","sourceRoot":"","sources":["../../src/data/tables.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAKrC;;;;;;;;GAQG;AACH,eAAO,MAAM,UAAU;IACrB,cAAc;;;;;;;;;;IACd,iBAAiB;;;;;;;;;;;;;IACjB,iBAAiB;;;;;;;;;;;;;;;;IACjB,sBAAsB;;;;;;;;IACtB,aAAa;;;;;;;;;IACb,kBAAkB;;;;;;IAClB,gBAAgB;;;;;;;;;;;;;;;;;IAChB,sBAAsB;;;;;;CACvB,CAAC;AACF,MAAM,MAAM,UAAU,GAAG,OAAO,UAAU,CAAC;AAE3C,8GAA8G;AAC9G,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC,CAAC;AAE9D,yGAAyG;AACzG,wBAAgB,YAAY,CAAC,EAAE,EAAE,UAAU,GAAG,YAAY,CAEzD"}
@@ -3,13 +3,6 @@
3
3
  import type { PithyHonoEnv, PithyMiddleware } from "@pithy-sh/core/src/capability/capability";
4
4
  import type { MiddlewareHandler } from "hono";
5
5
  import type { AuthWiring } from "../capability";
6
- /**
7
- * The session-resolution middleware: it fills core's `AuthContext` seam (`c.var.auth`) for every
8
- * request that carries a credential — a bearer token or a session cookie. Better Auth resolves both
9
- * transparently (the bearer plugin rewrites the header into the session cookie), so one `getSession`
10
- * call covers mobile and web. A credential-less request stays anonymous (no instance build, no D1 hit).
11
- * Resolution never throws — an invalid credential simply leaves `auth` null for `requireAuth` to reject.
12
- */
13
6
  export declare function createSessionMiddleware(wiring: AuthWiring): PithyMiddleware;
14
7
  /**
15
8
  * The gate other capabilities stack on a protected route. Rejects a request whose `AuthContext` was
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/http/middleware.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,0CAA0C,CAAC;AAI9F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,UAAU,GAAG,eAAe,CAgC3E;AAED;;;GAGG;AACH,wBAAgB,WAAW,IAAI,iBAAiB,CAAC,YAAY,CAAC,CAU7D"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/http/middleware.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,0CAA0C,CAAC;AAI9F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAgBhD,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,UAAU,GAAG,eAAe,CAuC3E;AAED;;;GAGG;AACH,wBAAgB,WAAW,IAAI,iBAAiB,CAAC,YAAY,CAAC,CAU7D"}
@@ -12,6 +12,11 @@ import { AuthContext } from "@pithy-sh/core/src/http/authContext";
12
12
  * call covers mobile and web. A credential-less request stays anonymous (no instance build, no D1 hit).
13
13
  * Resolution never throws — an invalid credential simply leaves `auth` null for `requireAuth` to reject.
14
14
  */
15
+ /** The session's authentication instant when it is a real `Date`, else `null`. Never anything else. */
16
+ function authenticatedDate(session) {
17
+ const value = session.authenticatedAt;
18
+ return value instanceof Date ? value : null;
19
+ }
15
20
  function createSessionMiddleware(wiring) {
16
21
  return (app) => {
17
22
  app.use("*", async (c, next) => {
@@ -24,7 +29,8 @@ function createSessionMiddleware(wiring) {
24
29
  userId: session.user.id,
25
30
  sessionId: session.session.id,
26
31
  scopes: [],
27
- locale: typeof stored === "string" && isLocale(stored) ? stored : null
32
+ locale: typeof stored === "string" && isLocale(stored) ? stored : null,
33
+ authenticatedAt: authenticatedDate(session.session)
28
34
  }));
29
35
  }
30
36
  } catch {}
@@ -0,0 +1,100 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ /**
4
+ * **Changing which providers can sign you in requires a recent authentication.** Both directions.
5
+ *
6
+ * Attaching one grants permanent access — after it a sign-in resolves by account id and the email stops
7
+ * mattering — and it is what `allowDifferentEmails: true` widened: before that, a provider could only be
8
+ * linked to an account holding the same address, so a link was self-limiting. Detaching one is the
9
+ * smaller decision, but `allowUnlinkingAll: true` means an attacker inside the window can strip *every*
10
+ * provider from an account. Neither belongs behind a credential whose age nobody checked.
11
+ *
12
+ * **Better Auth guards the two unevenly, and neither guard is one the kit can use.** In
13
+ * `better-auth/dist/api/routes/account.mjs`, `/link-social` takes `use: [sessionMiddleware]` — any
14
+ * session, any age — while `/unlink-account` takes `use: [freshSessionMiddleware]`, which reads
15
+ * `session.createdAt` against a `freshAge` defaulting to 24 hours. So the more powerful operation was
16
+ * the cheaper-guarded one, and the guarded one was checked against a value `/token/rotate` resets:
17
+ * `internal-adapter.mjs:275` stamps `createdAt` after the caller's override, so a row's `created_at` is
18
+ * the moment of its most recent rotation. Ordinary refreshing resets it; so does anyone holding a stolen
19
+ * refresh token.
20
+ *
21
+ * **It reads `authenticatedAt`, not the session's `createdAt`, and that distinction is the whole of #558.**
22
+ * The first version of this gate read session age. `/token/rotate` stamps a successor's `createdAt` with
23
+ * the moment of the call, so session age is reset by every rotation — ordinary on the bearer path, and
24
+ * available on demand to anyone holding a stolen refresh token. Two requests and the gate was open. The
25
+ * question it means to ask is how recently the *person* authenticated, which is a different fact, stamped
26
+ * once at sign-in and carried across rotations verbatim.
27
+ *
28
+ * **Not a Better Auth hook, and that is a framework constraint rather than a preference.** A `before` hook
29
+ * runs ahead of the endpoint's own `sessionMiddleware`, so `ctx.context.session` is null at hook time; and
30
+ * `runBeforeHooks` accumulates each hook's context changes and applies them only after *every* hook, so
31
+ * `bearer()`'s rewrite of `Authorization` into the session cookie never reaches a sibling hook either. A
32
+ * hook therefore cannot resolve a mobile caller's session at all, and a gate that cannot read a session
33
+ * refuses everyone — indistinguishable from the feature being broken, which invites deleting the gate.
34
+ *
35
+ * Provider-agnostic on purpose. Google and Apple are trusted providers, which makes them *more* exposed
36
+ * to this, not less: the question is how recently the caller proved they hold the account.
37
+ */
38
+ import type { PithyHonoEnv } from "@pithy-sh/core/src/capability/capability";
39
+ import { PithyError } from "@pithy-sh/core/src/error/pithyError";
40
+ import type { MiddlewareHandler } from "hono";
41
+ import type { ProviderChange } from "../audit/emit";
42
+ /**
43
+ * How recently the caller must have authenticated to change which providers sign them in: **fifteen minutes.**
44
+ *
45
+ * Stated rather than inherited. Better Auth's `freshAge` default is 24 hours, which is the wrong order of
46
+ * magnitude here — a day-old authentication is not evidence that the person at the keyboard holds the
47
+ * account, and the whole exposure is a credential valid for days. Fifteen minutes covers an OAuth round
48
+ * trip that involves signing in to the provider first, and is short enough that a stolen credential is
49
+ * unlikely to still be inside the window.
50
+ *
51
+ * **One number for both operations.** Asymmetry is arguable — linking grants access and unlinking
52
+ * removes it — but the argument cuts both ways once `allowUnlinkingAll` is on, and a second constant
53
+ * would be a second thing to keep true. The cost of being wrong in either direction is one
54
+ * re-authentication.
55
+ */
56
+ export declare const PROVIDER_CHANGE_FRESH_AGE_SECONDS: number;
57
+ /**
58
+ * Whether an authentication is recent enough to authorize attaching a provider.
59
+ *
60
+ * **Fails closed on anything it cannot read.** An absent, unparseable or future timestamp is not evidence
61
+ * of a recent authentication — it is the absence of evidence — and a gate that reads "I cannot tell" as
62
+ * "fresh" is one a row written before the column walks straight through. The future case matters as much
63
+ * as the past: a window checked on one side only would pass any future value forever.
64
+ */
65
+ export declare function authenticationIsFresh(authenticatedAt: Date | string | null | undefined, now: Date): boolean;
66
+ /**
67
+ * The gate, as Hono middleware on the kit's own route tree.
68
+ *
69
+ * **A pure read of `c.var.auth`.** `createSessionMiddleware` has already resolved this request's
70
+ * credential — core registers every capability's middleware before any capability's routes — so resolving
71
+ * it again would be a second D1 round trip for a fact the seam is holding. That is the argument
72
+ * `AuthContext` already makes for `locale`.
73
+ *
74
+ * **No credential at all is 401, not this 403.** `auth/session_not_fresh` means "signed in, but not
75
+ * recently enough"; a caller holding nothing cannot tell that from "not signed in" and will not know
76
+ * whether to re-authenticate or to retry. The refusal is `requireAuth()`'s, word for word, so the two
77
+ * surfaces answer an absent credential identically.
78
+ *
79
+ * **Recorded before the throw**, like `emitProviderUnavailable`: one refusal is somebody who left a tab
80
+ * open, and a run of them against one user id is the incident. The trail has to hold it whether or not
81
+ * anything logs the refusal.
82
+ */
83
+ /**
84
+ * The refusal for attaching a provider without having authenticated recently.
85
+ *
86
+ * **Not `auth/forbidden`.** A client has to render this differently from every other 403, because the
87
+ * remedy is specific and cheap — sign in again, then retry — and a generic "you are not allowed" sends
88
+ * somebody to ask for a permission they already hold. `http/linkFreshness.ts` carries the argument for
89
+ * the gate itself.
90
+ *
91
+ * `message` says nothing was changed, because the caller is mid-flow and cannot otherwise tell whether
92
+ * the link half-happened.
93
+ */
94
+ export declare function sessionNotFresh(): PithyError;
95
+ /**
96
+ * Which direction is being refused. Carried into the audit row, because "somebody tried to attach an
97
+ * identity on a stale credential" and "somebody tried to strip one" are different things to read later.
98
+ */
99
+ export declare function requireFreshAuthentication(change: ProviderChange, now?: () => Date): MiddlewareHandler<PithyHonoEnv>;
100
+ //# sourceMappingURL=providerFreshness.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providerFreshness.d.ts","sourceRoot":"","sources":["../../src/http/providerFreshness.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAqB,MAAM,qCAAqC,CAAC;AACpF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAGpD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,iCAAiC,QAAU,CAAC;AAEzD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,eAAe,EAAE,IAAI,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,GAAG,EAAE,IAAI,GAAG,OAAO,CAQ3G;AAED;;;;;;;;;;;;;;;;GAgBG;AACH;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,IAAI,UAAU,CAS5C;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,cAAc,EACtB,GAAG,GAAE,MAAM,IAAuB,GACjC,iBAAiB,CAAC,YAAY,CAAC,CAoBjC"}
@@ -0,0 +1,98 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+ import { emitProviderChangeNotFresh } from "../audit/emit.js";
4
+ import { PithyError, UnauthorizedError } from "@pithy-sh/core/src/error/pithyError";
5
+ //#region src/http/providerFreshness.ts
6
+ /**
7
+ * How recently the caller must have authenticated to change which providers sign them in: **fifteen minutes.**
8
+ *
9
+ * Stated rather than inherited. Better Auth's `freshAge` default is 24 hours, which is the wrong order of
10
+ * magnitude here — a day-old authentication is not evidence that the person at the keyboard holds the
11
+ * account, and the whole exposure is a credential valid for days. Fifteen minutes covers an OAuth round
12
+ * trip that involves signing in to the provider first, and is short enough that a stolen credential is
13
+ * unlikely to still be inside the window.
14
+ *
15
+ * **One number for both operations.** Asymmetry is arguable — linking grants access and unlinking
16
+ * removes it — but the argument cuts both ways once `allowUnlinkingAll` is on, and a second constant
17
+ * would be a second thing to keep true. The cost of being wrong in either direction is one
18
+ * re-authentication.
19
+ */
20
+ const PROVIDER_CHANGE_FRESH_AGE_SECONDS = 900;
21
+ /**
22
+ * Whether an authentication is recent enough to authorize attaching a provider.
23
+ *
24
+ * **Fails closed on anything it cannot read.** An absent, unparseable or future timestamp is not evidence
25
+ * of a recent authentication — it is the absence of evidence — and a gate that reads "I cannot tell" as
26
+ * "fresh" is one a row written before the column walks straight through. The future case matters as much
27
+ * as the past: a window checked on one side only would pass any future value forever.
28
+ */
29
+ function authenticationIsFresh(authenticatedAt, now) {
30
+ if (authenticatedAt === null || authenticatedAt === void 0) return false;
31
+ const millis = (authenticatedAt instanceof Date ? authenticatedAt : new Date(authenticatedAt)).getTime();
32
+ if (Number.isNaN(millis)) return false;
33
+ const age = (now.getTime() - millis) / 1e3;
34
+ if (age < 0) return false;
35
+ return age <= 900;
36
+ }
37
+ /**
38
+ * The gate, as Hono middleware on the kit's own route tree.
39
+ *
40
+ * **A pure read of `c.var.auth`.** `createSessionMiddleware` has already resolved this request's
41
+ * credential — core registers every capability's middleware before any capability's routes — so resolving
42
+ * it again would be a second D1 round trip for a fact the seam is holding. That is the argument
43
+ * `AuthContext` already makes for `locale`.
44
+ *
45
+ * **No credential at all is 401, not this 403.** `auth/session_not_fresh` means "signed in, but not
46
+ * recently enough"; a caller holding nothing cannot tell that from "not signed in" and will not know
47
+ * whether to re-authenticate or to retry. The refusal is `requireAuth()`'s, word for word, so the two
48
+ * surfaces answer an absent credential identically.
49
+ *
50
+ * **Recorded before the throw**, like `emitProviderUnavailable`: one refusal is somebody who left a tab
51
+ * open, and a run of them against one user id is the incident. The trail has to hold it whether or not
52
+ * anything logs the refusal.
53
+ */
54
+ /**
55
+ * The refusal for attaching a provider without having authenticated recently.
56
+ *
57
+ * **Not `auth/forbidden`.** A client has to render this differently from every other 403, because the
58
+ * remedy is specific and cheap — sign in again, then retry — and a generic "you are not allowed" sends
59
+ * somebody to ask for a permission they already hold. `http/linkFreshness.ts` carries the argument for
60
+ * the gate itself.
61
+ *
62
+ * `message` says nothing was changed, because the caller is mid-flow and cannot otherwise tell whether
63
+ * the link half-happened.
64
+ */
65
+ function sessionNotFresh() {
66
+ return new PithyError({
67
+ code: "auth/session_not_fresh",
68
+ status: 403,
69
+ message: "Sign in again to change your connected accounts. Nothing was changed.",
70
+ action: "A deliberate gate: changing which providers can sign somebody in requires a recent authentication, in both directions. See `http/providerFreshness.ts`.",
71
+ detail: "session authenticatedAt is older than PROVIDER_CHANGE_FRESH_AGE_SECONDS, absent, or unreadable"
72
+ });
73
+ }
74
+ /**
75
+ * Which direction is being refused. Carried into the audit row, because "somebody tried to attach an
76
+ * identity on a stale credential" and "somebody tried to strip one" are different things to read later.
77
+ */
78
+ function requireFreshAuthentication(change, now = () => /* @__PURE__ */ new Date()) {
79
+ return async (c, next) => {
80
+ const auth = c.var.auth;
81
+ if (!auth) throw new UnauthorizedError({
82
+ message: "Authentication required.",
83
+ action: "Sign in and retry with a valid session or bearer token."
84
+ });
85
+ if (!authenticationIsFresh(auth.authenticatedAt, now())) {
86
+ await emitProviderChangeNotFresh(c.var.emit, {
87
+ change,
88
+ userId: auth.userId,
89
+ sessionId: auth.sessionId,
90
+ headers: c.req.raw.headers
91
+ });
92
+ throw sessionNotFresh();
93
+ }
94
+ await next();
95
+ };
96
+ }
97
+ //#endregion
98
+ export { PROVIDER_CHANGE_FRESH_AGE_SECONDS, authenticationIsFresh, requireFreshAuthentication, sessionNotFresh };
@@ -1 +1 @@
1
- {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/http/routes.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAM7E,OAAO,KAAK,EAAW,IAAI,EAAE,MAAM,MAAM,CAAC;AAE1C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAsBhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,CAgCtF"}
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/http/routes.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAM7E,OAAO,KAAK,EAAW,IAAI,EAAE,MAAM,MAAM,CAAC;AAE1C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAuBhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,CAwCtF"}
@@ -3,6 +3,7 @@
3
3
  import { authDatabase } from "../data/tables.js";
4
4
  import { correlation, emitDenied, emitDeviceRevoked, emitTokenRefresh, emitTokenReuseDetected } from "../audit/emit.js";
5
5
  import { deleteDevice, deviceSessionTokens, listDevices } from "../device/registry.js";
6
+ import { requireFreshAuthentication } from "./providerFreshness.js";
6
7
  import { getAuthInstance, resolveDb } from "./resolve.js";
7
8
  import { requireAuth } from "./middleware.js";
8
9
  import { consumeSession, findConsumedToken, pruneConsumedTokens, recordConsumedToken, revokeFamily } from "../token/rotation.js";
@@ -63,6 +64,8 @@ function createAuthRoutes(wiring) {
63
64
  app.post(`${base}/token/rotate`, csrf, (c) => rotateToken(c, wiring));
64
65
  app.get(`${base}/devices`, requireAuth(), (c) => listMyDevices(c, wiring));
65
66
  app.post(`${base}/devices/revoke`, requireAuth(), csrf, zValidator("json", RevokeDeviceBody, validationHook), (c) => revokeMyDevice(c, wiring, c.req.valid("json")));
67
+ app.post(`${base}/link-social`, requireFreshAuthentication("link"), (c) => handleBetterAuth(c, wiring));
68
+ app.post(`${base}/unlink-account`, requireFreshAuthentication("unlink"), (c) => handleBetterAuth(c, wiring));
66
69
  registerAuthAdminRoutes(wiring)(app);
67
70
  app.all(`${base}/*`, (c) => handleBetterAuth(c, wiring));
68
71
  };
@@ -209,10 +212,12 @@ async function rotateToken(c, wiring) {
209
212
  const session = current.session;
210
213
  const deviceId = session.deviceId ?? void 0;
211
214
  const familyId = session.familyId ?? crypto.randomUUID();
215
+ const authenticatedAt = session.authenticatedAt ?? null;
212
216
  const next = await ctx.internalAdapter.createSession(current.user.id, void 0, {
213
217
  ipAddress: ip,
214
218
  userAgent,
215
219
  familyId,
220
+ authenticatedAt,
216
221
  ...deviceId ? { deviceId } : {}
217
222
  });
218
223
  const access = await instance.api.getToken({ headers: new Headers({ authorization: `Bearer ${next.token}` }) });