@pithy-sh/auth 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/audit/actions.d.ts +12 -0
- package/dist/audit/actions.d.ts.map +1 -1
- package/dist/audit/actions.js +12 -0
- package/dist/audit/emit.d.ts +12 -0
- package/dist/audit/emit.d.ts.map +1 -1
- package/dist/audit/emit.js +19 -1
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +9 -2
- package/dist/client/projection.d.ts +22 -0
- package/dist/client/projection.d.ts.map +1 -1
- package/dist/data/betterAuth.d.ts +1 -0
- package/dist/data/betterAuth.d.ts.map +1 -1
- package/dist/data/betterAuth.js +2 -1
- package/dist/data/kitFields.d.ts +23 -1
- package/dist/data/kitFields.d.ts.map +1 -1
- package/dist/data/kitFields.js +23 -1
- package/dist/data/tables.d.ts +1 -0
- package/dist/data/tables.d.ts.map +1 -1
- package/dist/http/linkFreshness.d.ts +88 -0
- package/dist/http/linkFreshness.d.ts.map +1 -0
- package/dist/http/linkFreshness.js +91 -0
- package/dist/http/middleware.d.ts +0 -7
- package/dist/http/middleware.d.ts.map +1 -1
- package/dist/http/middleware.js +7 -1
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +4 -0
- package/dist/instance/auth.d.ts +56 -1
- package/dist/instance/auth.d.ts.map +1 -1
- package/dist/instance/auth.js +47 -3
- package/dist/migrations/0002_session_authenticated_at.d.ts +39 -0
- package/dist/migrations/0002_session_authenticated_at.d.ts.map +1 -0
- package/dist/migrations/0002_session_authenticated_at.js +47 -0
- package/dist/migrations/set.d.ts +20 -0
- package/dist/migrations/set.d.ts.map +1 -0
- package/dist/migrations/set.js +24 -0
- package/dist/seeds/devSession.d.ts.map +1 -1
- package/dist/seeds/devSession.js +2 -1
- package/dist/test-utils/liveApp.d.ts.map +1 -1
- package/dist/test-utils/liveApp.js +3 -2
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/docs/github-oauth.md +5 -3
- package/package.json +5 -5
- package/src/audit/actions.ts +12 -0
- package/src/audit/emit.ts +24 -0
- package/src/capability.ts +14 -2
- package/src/client/projection.ts +22 -0
- package/src/data/betterAuth.ts +3 -0
- package/src/data/kitFields.ts +19 -1
- package/src/http/linkFreshness.ts +130 -0
- package/src/http/middleware.ts +13 -0
- package/src/http/routes.ts +28 -1
- package/src/instance/auth.ts +46 -5
- package/src/migrations/0002_session_authenticated_at.ts +50 -0
- package/src/migrations/set.ts +23 -0
- package/src/seeds/devSession.ts +2 -0
- package/src/test-utils/liveApp.ts +3 -2
- package/src/version.generated.ts +1 -1
package/dist/audit/actions.d.ts
CHANGED
|
@@ -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,MAA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D,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"}
|
package/dist/audit/actions.js
CHANGED
|
@@ -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'.
|
package/dist/audit/emit.d.ts
CHANGED
|
@@ -94,6 +94,18 @@ export declare function emitProviderUnavailable(emit: AuditEmit, context: {
|
|
|
94
94
|
provider: string;
|
|
95
95
|
headers: Headers | undefined;
|
|
96
96
|
}): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Emit a `session_not_fresh` event — an attempt to attach a provider on an authentic but stale session.
|
|
99
|
+
*
|
|
100
|
+
* `actorType: "user"` rather than `anonymous`, unlike `emitProviderUnavailable`: this caller is
|
|
101
|
+
* authenticated, and the user id is the whole value of the row. Counting these per user is how a stolen
|
|
102
|
+
* credential being walked toward a link becomes visible.
|
|
103
|
+
*/
|
|
104
|
+
export declare function emitLinkSessionNotFresh(emit: AuditEmit, context: {
|
|
105
|
+
userId: string;
|
|
106
|
+
sessionId: string;
|
|
107
|
+
headers: Headers | undefined;
|
|
108
|
+
}): Promise<void>;
|
|
97
109
|
/** A blocked/failed auth attempt — recorded as `denied` (first-class). */
|
|
98
110
|
export declare function emitDenied(emit: AuditEmit, context: {
|
|
99
111
|
ip?: string;
|
package/dist/audit/emit.d.ts.map
CHANGED
|
@@ -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,qFAAqF;AACrF,wBAAsB,gBAAgB,CACpC,IAAI,EAAE,SAAS,EACf,OAAO,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,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,uBAAuB,CAC3C,IAAI,EAAE,SAAS,EACf,OAAO,EAAE;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,GAC3E,OAAO,CAAC,IAAI,CAAC,CAYf;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"}
|
package/dist/audit/emit.js
CHANGED
|
@@ -157,6 +157,24 @@ 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 attach a provider on an authentic but stale session.
|
|
162
|
+
*
|
|
163
|
+
* `actorType: "user"` rather than `anonymous`, unlike `emitProviderUnavailable`: this caller is
|
|
164
|
+
* authenticated, and the user id is the whole value of the row. Counting these per user is how a stolen
|
|
165
|
+
* credential being walked toward a link becomes visible.
|
|
166
|
+
*/
|
|
167
|
+
async function emitLinkSessionNotFresh(emit, context) {
|
|
168
|
+
await safeEmit(emit, {
|
|
169
|
+
action: AuthAuditActions.sessionNotFresh,
|
|
170
|
+
outcome: "denied",
|
|
171
|
+
severity: "warning",
|
|
172
|
+
actorType: "user",
|
|
173
|
+
actorId: context.userId,
|
|
174
|
+
sessionId: context.sessionId,
|
|
175
|
+
...correlation(context.headers)
|
|
176
|
+
});
|
|
177
|
+
}
|
|
160
178
|
/** A blocked/failed auth attempt — recorded as `denied` (first-class). */
|
|
161
179
|
async function emitDenied(emit, context) {
|
|
162
180
|
await safeEmit(emit, {
|
|
@@ -169,4 +187,4 @@ async function emitDenied(emit, context) {
|
|
|
169
187
|
});
|
|
170
188
|
}
|
|
171
189
|
//#endregion
|
|
172
|
-
export { correlation, emitAfterRequest, emitControlPlaneAction, emitDenied, emitDeviceRevoked, emitProviderUnavailable, emitTokenRefresh, emitTokenReuseDetected };
|
|
190
|
+
export { correlation, emitAfterRequest, emitControlPlaneAction, emitDenied, emitDeviceRevoked, emitLinkSessionNotFresh, emitProviderUnavailable, emitTokenRefresh, emitTokenReuseDetected };
|
package/dist/capability.d.ts.map
CHANGED
|
@@ -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;
|
|
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;AAmCxE,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,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"}
|
package/dist/capability.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
|
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"}
|
package/dist/data/betterAuth.js
CHANGED
|
@@ -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({
|
package/dist/data/kitFields.d.ts
CHANGED
|
@@ -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
|
|
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
|
|
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"}
|
package/dist/data/kitFields.js
CHANGED
|
@@ -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
|
|
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
|
package/dist/data/tables.d.ts
CHANGED
|
@@ -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
|
|
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"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* **What pays for `allowDifferentEmails`.**
|
|
5
|
+
*
|
|
6
|
+
* A signed-in user may attach a provider whose email differs from their own. That is the right behavior
|
|
7
|
+
* and it widens something: before it, a provider could only be linked to an account holding the same
|
|
8
|
+
* address, so a link was self-limiting. After it, a link is a decision the session alone authorizes.
|
|
9
|
+
*
|
|
10
|
+
* And Better Auth guards that decision **less** than it guards undoing it. Read the two endpoints beside
|
|
11
|
+
* each other in `better-auth/dist/api/routes/account.mjs`: `/link-social` takes `use: [sessionMiddleware]`
|
|
12
|
+
* — any session, any age — while `/unlink-account` takes `use: [freshSessionMiddleware]`. Attaching an
|
|
13
|
+
* identity grants *permanent* access, because once linked a sign-in resolves by account id and the email
|
|
14
|
+
* stops mattering; detaching one does not. The cheaper-guarded operation is the more powerful one.
|
|
15
|
+
*
|
|
16
|
+
* **It reads `authenticatedAt`, not the session's `createdAt`, and that distinction is the whole of #558.**
|
|
17
|
+
* The first version of this gate read session age. `/token/rotate` stamps a successor's `createdAt` with
|
|
18
|
+
* the moment of the call, so session age is reset by every rotation — ordinary on the bearer path, and
|
|
19
|
+
* available on demand to anyone holding a stolen refresh token. Two requests and the gate was open. The
|
|
20
|
+
* question it means to ask is how recently the *person* authenticated, which is a different fact, stamped
|
|
21
|
+
* once at sign-in and carried across rotations verbatim.
|
|
22
|
+
*
|
|
23
|
+
* **Not a Better Auth hook, and that is a framework constraint rather than a preference.** A `before` hook
|
|
24
|
+
* runs ahead of the endpoint's own `sessionMiddleware`, so `ctx.context.session` is null at hook time; and
|
|
25
|
+
* `runBeforeHooks` accumulates each hook's context changes and applies them only after *every* hook, so
|
|
26
|
+
* `bearer()`'s rewrite of `Authorization` into the session cookie never reaches a sibling hook either. A
|
|
27
|
+
* hook therefore cannot resolve a mobile caller's session at all, and a gate that cannot read a session
|
|
28
|
+
* refuses everyone — indistinguishable from the feature being broken, which invites deleting the gate.
|
|
29
|
+
*
|
|
30
|
+
* Provider-agnostic on purpose. Google and Apple are trusted providers, which makes them *more* exposed
|
|
31
|
+
* to this, not less: the question is how recently the caller proved they hold the account.
|
|
32
|
+
*/
|
|
33
|
+
import type { PithyHonoEnv } from "@pithy-sh/core/src/capability/capability";
|
|
34
|
+
import { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
35
|
+
import type { MiddlewareHandler } from "hono";
|
|
36
|
+
/**
|
|
37
|
+
* How recently the caller must have authenticated to attach a provider: **fifteen minutes.**
|
|
38
|
+
*
|
|
39
|
+
* Stated rather than inherited. Better Auth's `freshAge` default is 24 hours, which is the wrong order of
|
|
40
|
+
* magnitude here — a day-old authentication is not evidence that the person at the keyboard holds the
|
|
41
|
+
* account, and the whole exposure is a credential valid for days. Fifteen minutes covers an OAuth round
|
|
42
|
+
* trip that involves signing in to the provider first, and is short enough that a stolen credential is
|
|
43
|
+
* unlikely to still be inside the window.
|
|
44
|
+
*
|
|
45
|
+
* The cost of being wrong is one re-authentication — exactly what `/unlink-account` already charges for
|
|
46
|
+
* the smaller decision.
|
|
47
|
+
*/
|
|
48
|
+
export declare const LINK_FRESH_AGE_SECONDS: number;
|
|
49
|
+
/**
|
|
50
|
+
* Whether an authentication is recent enough to authorize attaching a provider.
|
|
51
|
+
*
|
|
52
|
+
* **Fails closed on anything it cannot read.** An absent, unparseable or future timestamp is not evidence
|
|
53
|
+
* of a recent authentication — it is the absence of evidence — and a gate that reads "I cannot tell" as
|
|
54
|
+
* "fresh" is one a row written before the column walks straight through. The future case matters as much
|
|
55
|
+
* as the past: a window checked on one side only would pass any future value forever.
|
|
56
|
+
*/
|
|
57
|
+
export declare function authenticationIsFresh(authenticatedAt: Date | string | null | undefined, now: Date): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* The gate, as Hono middleware on the kit's own route tree.
|
|
60
|
+
*
|
|
61
|
+
* **A pure read of `c.var.auth`.** `createSessionMiddleware` has already resolved this request's
|
|
62
|
+
* credential — core registers every capability's middleware before any capability's routes — so resolving
|
|
63
|
+
* it again would be a second D1 round trip for a fact the seam is holding. That is the argument
|
|
64
|
+
* `AuthContext` already makes for `locale`.
|
|
65
|
+
*
|
|
66
|
+
* **No credential at all is 401, not this 403.** `auth/session_not_fresh` means "signed in, but not
|
|
67
|
+
* recently enough"; a caller holding nothing cannot tell that from "not signed in" and will not know
|
|
68
|
+
* whether to re-authenticate or to retry. The refusal is `requireAuth()`'s, word for word, so the two
|
|
69
|
+
* surfaces answer an absent credential identically.
|
|
70
|
+
*
|
|
71
|
+
* **Recorded before the throw**, like `emitProviderUnavailable`: one refusal is somebody who left a tab
|
|
72
|
+
* open, and a run of them against one user id is the incident. The trail has to hold it whether or not
|
|
73
|
+
* anything logs the refusal.
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
76
|
+
* The refusal for attaching a provider without having authenticated recently.
|
|
77
|
+
*
|
|
78
|
+
* **Not `auth/forbidden`.** A client has to render this differently from every other 403, because the
|
|
79
|
+
* remedy is specific and cheap — sign in again, then retry — and a generic "you are not allowed" sends
|
|
80
|
+
* somebody to ask for a permission they already hold. `http/linkFreshness.ts` carries the argument for
|
|
81
|
+
* the gate itself.
|
|
82
|
+
*
|
|
83
|
+
* `message` says nothing was changed, because the caller is mid-flow and cannot otherwise tell whether
|
|
84
|
+
* the link half-happened.
|
|
85
|
+
*/
|
|
86
|
+
export declare function sessionNotFresh(): PithyError;
|
|
87
|
+
export declare function requireFreshAuthenticationToLink(now?: () => Date): MiddlewareHandler<PithyHonoEnv>;
|
|
88
|
+
//# sourceMappingURL=linkFreshness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"linkFreshness.d.ts","sourceRoot":"","sources":["../../src/http/linkFreshness.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;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;AAG9C;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,sBAAsB,QAAU,CAAC;AAE9C;;;;;;;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,wBAAgB,gCAAgC,CAAC,GAAG,GAAE,MAAM,IAAuB,GAAG,iBAAiB,CAAC,YAAY,CAAC,CAmBpH"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { emitLinkSessionNotFresh } from "../audit/emit.js";
|
|
4
|
+
import { PithyError, UnauthorizedError } from "@pithy-sh/core/src/error/pithyError";
|
|
5
|
+
//#region src/http/linkFreshness.ts
|
|
6
|
+
/**
|
|
7
|
+
* How recently the caller must have authenticated to attach a provider: **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
|
+
* The cost of being wrong is one re-authentication — exactly what `/unlink-account` already charges for
|
|
16
|
+
* the smaller decision.
|
|
17
|
+
*/
|
|
18
|
+
const LINK_FRESH_AGE_SECONDS = 900;
|
|
19
|
+
/**
|
|
20
|
+
* Whether an authentication is recent enough to authorize attaching a provider.
|
|
21
|
+
*
|
|
22
|
+
* **Fails closed on anything it cannot read.** An absent, unparseable or future timestamp is not evidence
|
|
23
|
+
* of a recent authentication — it is the absence of evidence — and a gate that reads "I cannot tell" as
|
|
24
|
+
* "fresh" is one a row written before the column walks straight through. The future case matters as much
|
|
25
|
+
* as the past: a window checked on one side only would pass any future value forever.
|
|
26
|
+
*/
|
|
27
|
+
function authenticationIsFresh(authenticatedAt, now) {
|
|
28
|
+
if (authenticatedAt === null || authenticatedAt === void 0) return false;
|
|
29
|
+
const millis = (authenticatedAt instanceof Date ? authenticatedAt : new Date(authenticatedAt)).getTime();
|
|
30
|
+
if (Number.isNaN(millis)) return false;
|
|
31
|
+
const age = (now.getTime() - millis) / 1e3;
|
|
32
|
+
if (age < 0) return false;
|
|
33
|
+
return age <= 900;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The gate, as Hono middleware on the kit's own route tree.
|
|
37
|
+
*
|
|
38
|
+
* **A pure read of `c.var.auth`.** `createSessionMiddleware` has already resolved this request's
|
|
39
|
+
* credential — core registers every capability's middleware before any capability's routes — so resolving
|
|
40
|
+
* it again would be a second D1 round trip for a fact the seam is holding. That is the argument
|
|
41
|
+
* `AuthContext` already makes for `locale`.
|
|
42
|
+
*
|
|
43
|
+
* **No credential at all is 401, not this 403.** `auth/session_not_fresh` means "signed in, but not
|
|
44
|
+
* recently enough"; a caller holding nothing cannot tell that from "not signed in" and will not know
|
|
45
|
+
* whether to re-authenticate or to retry. The refusal is `requireAuth()`'s, word for word, so the two
|
|
46
|
+
* surfaces answer an absent credential identically.
|
|
47
|
+
*
|
|
48
|
+
* **Recorded before the throw**, like `emitProviderUnavailable`: one refusal is somebody who left a tab
|
|
49
|
+
* open, and a run of them against one user id is the incident. The trail has to hold it whether or not
|
|
50
|
+
* anything logs the refusal.
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* The refusal for attaching a provider without having authenticated recently.
|
|
54
|
+
*
|
|
55
|
+
* **Not `auth/forbidden`.** A client has to render this differently from every other 403, because the
|
|
56
|
+
* remedy is specific and cheap — sign in again, then retry — and a generic "you are not allowed" sends
|
|
57
|
+
* somebody to ask for a permission they already hold. `http/linkFreshness.ts` carries the argument for
|
|
58
|
+
* the gate itself.
|
|
59
|
+
*
|
|
60
|
+
* `message` says nothing was changed, because the caller is mid-flow and cannot otherwise tell whether
|
|
61
|
+
* the link half-happened.
|
|
62
|
+
*/
|
|
63
|
+
function sessionNotFresh() {
|
|
64
|
+
return new PithyError({
|
|
65
|
+
code: "auth/session_not_fresh",
|
|
66
|
+
status: 403,
|
|
67
|
+
message: "Sign in again to connect an account. Nothing was changed.",
|
|
68
|
+
action: "A deliberate gate: connecting a provider grants permanent access, so it requires a recent sign-in the way disconnecting one already does. See `http/linkFreshness.ts`.",
|
|
69
|
+
detail: "session authenticatedAt is older than LINK_FRESH_AGE_SECONDS, absent, or unreadable"
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function requireFreshAuthenticationToLink(now = () => /* @__PURE__ */ new Date()) {
|
|
73
|
+
return async (c, next) => {
|
|
74
|
+
const auth = c.var.auth;
|
|
75
|
+
if (!auth) throw new UnauthorizedError({
|
|
76
|
+
message: "Authentication required.",
|
|
77
|
+
action: "Sign in and retry with a valid session or bearer token."
|
|
78
|
+
});
|
|
79
|
+
if (!authenticationIsFresh(auth.authenticatedAt, now())) {
|
|
80
|
+
await emitLinkSessionNotFresh(c.var.emit, {
|
|
81
|
+
userId: auth.userId,
|
|
82
|
+
sessionId: auth.sessionId,
|
|
83
|
+
headers: c.req.raw.headers
|
|
84
|
+
});
|
|
85
|
+
throw sessionNotFresh();
|
|
86
|
+
}
|
|
87
|
+
await next();
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
export { LINK_FRESH_AGE_SECONDS, authenticationIsFresh, requireFreshAuthenticationToLink, sessionNotFresh };
|
|
@@ -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;
|
|
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"}
|
package/dist/http/middleware.js
CHANGED
|
@@ -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 {}
|
|
@@ -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;
|
|
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,CAuCtF"}
|
package/dist/http/routes.js
CHANGED
|
@@ -9,6 +9,7 @@ import { consumeSession, findConsumedToken, pruneConsumedTokens, recordConsumedT
|
|
|
9
9
|
import { RevokeDeviceBody } from "./schemas.js";
|
|
10
10
|
import { registerAuthAdminRoutes } from "./adminRoutes.js";
|
|
11
11
|
import { apiErrorToPithy } from "./errors.js";
|
|
12
|
+
import { requireFreshAuthenticationToLink } from "./linkFreshness.js";
|
|
12
13
|
import { UnauthorizedError } from "@pithy-sh/core/src/error/pithyError";
|
|
13
14
|
import { TURNSTILE_LOGIN_ACTION } from "@pithy-sh/turnstile/src/config/config";
|
|
14
15
|
import { zValidator } from "@hono/zod-validator";
|
|
@@ -63,6 +64,7 @@ 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`, requireFreshAuthenticationToLink(), (c) => handleBetterAuth(c, wiring));
|
|
66
68
|
registerAuthAdminRoutes(wiring)(app);
|
|
67
69
|
app.all(`${base}/*`, (c) => handleBetterAuth(c, wiring));
|
|
68
70
|
};
|
|
@@ -209,10 +211,12 @@ async function rotateToken(c, wiring) {
|
|
|
209
211
|
const session = current.session;
|
|
210
212
|
const deviceId = session.deviceId ?? void 0;
|
|
211
213
|
const familyId = session.familyId ?? crypto.randomUUID();
|
|
214
|
+
const authenticatedAt = session.authenticatedAt ?? null;
|
|
212
215
|
const next = await ctx.internalAdapter.createSession(current.user.id, void 0, {
|
|
213
216
|
ipAddress: ip,
|
|
214
217
|
userAgent,
|
|
215
218
|
familyId,
|
|
219
|
+
authenticatedAt,
|
|
216
220
|
...deviceId ? { deviceId } : {}
|
|
217
221
|
});
|
|
218
222
|
const access = await instance.api.getToken({ headers: new Headers({ authorization: `Bearer ${next.token}` }) });
|