@pithy-sh/auth 0.8.0 → 0.10.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 +12 -2
- package/dist/audit/actions.d.ts +21 -0
- package/dist/audit/actions.d.ts.map +1 -1
- package/dist/audit/actions.js +22 -1
- package/dist/audit/emit.js +1 -1
- package/dist/caller/certificate.d.ts +83 -0
- package/dist/caller/certificate.d.ts.map +1 -0
- package/dist/caller/certificate.js +92 -0
- package/dist/caller/request.d.ts +66 -0
- package/dist/caller/request.d.ts.map +1 -0
- package/dist/caller/request.js +71 -0
- package/dist/caller/token.d.ts +130 -0
- package/dist/caller/token.d.ts.map +1 -0
- package/dist/caller/token.js +207 -0
- package/dist/capability.d.ts +70 -2
- package/dist/capability.d.ts.map +1 -1
- package/dist/capability.js +28 -7
- package/dist/client/lifecycle.d.ts +19 -0
- package/dist/client/lifecycle.d.ts.map +1 -0
- package/dist/client/lifecycle.js +25 -0
- package/dist/client/projection.d.ts +4 -0
- package/dist/client/projection.d.ts.map +1 -1
- package/dist/data/betterAuth.d.ts +5 -1
- package/dist/data/betterAuth.d.ts.map +1 -1
- package/dist/data/betterAuth.js +7 -3
- package/dist/data/kitFields.d.ts +20 -25
- package/dist/data/kitFields.d.ts.map +1 -1
- package/dist/data/kitFields.js +28 -32
- package/dist/data/tables.d.ts +29 -3
- package/dist/data/tables.d.ts.map +1 -1
- package/dist/data/tables.js +5 -3
- package/dist/data/userLifecycle.d.ts +61 -0
- package/dist/data/userLifecycle.d.ts.map +1 -0
- package/dist/data/userLifecycle.js +50 -0
- package/dist/http/credentialResign.d.ts +46 -0
- package/dist/http/credentialResign.d.ts.map +1 -0
- package/dist/http/credentialResign.js +203 -0
- package/dist/http/errorCallbackUrl.d.ts.map +1 -1
- package/dist/http/errorCallbackUrl.js +15 -9
- package/dist/http/guards.d.ts +23 -1
- package/dist/http/guards.d.ts.map +1 -1
- package/dist/http/guards.js +28 -3
- package/dist/http/lifecycleGate.d.ts +18 -0
- package/dist/http/lifecycleGate.d.ts.map +1 -0
- package/dist/http/lifecycleGate.js +75 -0
- package/dist/http/middleware.d.ts +5 -0
- package/dist/http/middleware.d.ts.map +1 -1
- package/dist/http/middleware.js +113 -21
- package/dist/http/providerRefusal.d.ts +8 -7
- package/dist/http/providerRefusal.d.ts.map +1 -1
- package/dist/http/providerRefusal.js +37 -32
- package/dist/http/resolve.d.ts.map +1 -1
- package/dist/http/resolve.js +23 -5
- package/dist/http/routes.d.ts.map +1 -1
- package/dist/http/routes.js +20 -1
- package/dist/instance/auth.d.ts +1280 -5
- package/dist/instance/auth.d.ts.map +1 -1
- package/dist/instance/auth.js +59 -4
- package/dist/instance/jwksReencryption.d.ts +75 -0
- package/dist/instance/jwksReencryption.d.ts.map +1 -0
- package/dist/instance/jwksReencryption.js +160 -0
- package/dist/instance/microsoftEmail.d.ts +70 -0
- package/dist/instance/microsoftEmail.d.ts.map +1 -0
- package/dist/instance/microsoftEmail.js +96 -0
- package/dist/instance/providerSignInGate.d.ts +14 -12
- package/dist/instance/providerSignInGate.d.ts.map +1 -1
- package/dist/instance/providerSignInGate.js +23 -20
- package/dist/instance/providers.d.ts +7 -5
- package/dist/instance/providers.d.ts.map +1 -1
- package/dist/instance/providers.js +5 -3
- package/dist/instance/secrets.d.ts +232 -4
- package/dist/instance/secrets.d.ts.map +1 -1
- package/dist/instance/secrets.js +129 -7
- package/dist/instance/sessionSecrets.d.ts +45 -0
- package/dist/instance/sessionSecrets.d.ts.map +1 -0
- package/dist/instance/sessionSecrets.js +29 -0
- package/dist/lifecycle/enforce.d.ts +40 -0
- package/dist/lifecycle/enforce.d.ts.map +1 -0
- package/dist/lifecycle/enforce.js +70 -0
- package/dist/lifecycle/hooks.d.ts +53 -0
- package/dist/lifecycle/hooks.d.ts.map +1 -0
- package/dist/lifecycle/hooks.js +2 -0
- package/dist/lifecycle/request.d.ts +15 -0
- package/dist/lifecycle/request.d.ts.map +1 -0
- package/dist/lifecycle/request.js +27 -0
- package/dist/lifecycle/state.d.ts +64 -0
- package/dist/lifecycle/state.d.ts.map +1 -0
- package/dist/lifecycle/state.js +90 -0
- package/dist/lifecycle/transitions.d.ts +132 -0
- package/dist/lifecycle/transitions.d.ts.map +1 -0
- package/dist/lifecycle/transitions.js +349 -0
- package/dist/migrations/0001_init.d.ts.map +1 -1
- package/dist/migrations/0003_account_issuer_nullable.d.ts +5 -0
- package/dist/migrations/0003_account_issuer_nullable.d.ts.map +1 -0
- package/dist/migrations/0003_account_issuer_nullable.js +131 -0
- package/dist/migrations/0004_user_lifecycle.d.ts +28 -0
- package/dist/migrations/0004_user_lifecycle.d.ts.map +1 -0
- package/dist/migrations/0004_user_lifecycle.js +47 -0
- package/dist/migrations/set.d.ts +3 -1
- package/dist/migrations/set.d.ts.map +1 -1
- package/dist/migrations/set.js +6 -2
- package/dist/peer.d.ts +4 -0
- package/dist/peer.d.ts.map +1 -1
- package/dist/test-utils/liveApp.d.ts +26 -0
- package/dist/test-utils/liveApp.d.ts.map +1 -1
- package/dist/test-utils/liveApp.js +1 -0
- package/dist/test-utils/schemaDrift.d.ts +49 -0
- package/dist/test-utils/schemaDrift.d.ts.map +1 -0
- package/dist/test-utils/schemaDrift.js +35 -0
- package/dist/test-utils/scopedBackend.d.ts +204 -0
- package/dist/test-utils/scopedBackend.d.ts.map +1 -0
- package/dist/test-utils/scopedBackend.js +166 -0
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.d.ts.map +1 -1
- package/dist/version.generated.js +1 -1
- package/docs/account-lifecycle.md +105 -0
- package/docs/apple-signin.md +7 -1
- package/docs/facebook-oauth.md +7 -1
- package/docs/github-oauth.md +7 -1
- package/docs/google-oauth.md +7 -1
- package/docs/microsoft-oauth.md +177 -0
- package/docs/server-authoritative.md +182 -0
- package/package.json +11 -8
- package/pithy.manifest.json +17 -3
- package/src/audit/actions.ts +23 -0
- package/src/audit/emit.ts +1 -1
- package/src/caller/certificate.ts +133 -0
- package/src/caller/request.ts +84 -0
- package/src/caller/token.ts +276 -0
- package/src/capability.ts +170 -38
- package/src/client/lifecycle.ts +27 -0
- package/src/client/projection.ts +4 -0
- package/src/data/betterAuth.ts +23 -2
- package/src/data/kitFields.ts +27 -0
- package/src/data/tables.ts +4 -2
- package/src/data/userLifecycle.ts +80 -0
- package/src/http/credentialResign.ts +268 -0
- package/src/http/errorCallbackUrl.ts +21 -13
- package/src/http/guards.ts +29 -1
- package/src/http/lifecycleGate.ts +78 -0
- package/src/http/middleware.ts +161 -37
- package/src/http/providerRefusal.ts +40 -32
- package/src/http/resolve.ts +34 -5
- package/src/http/routes.ts +39 -2
- package/src/instance/auth.ts +91 -6
- package/src/instance/jwksReencryption.ts +206 -0
- package/src/instance/microsoftEmail.ts +111 -0
- package/src/instance/providerSignInGate.ts +30 -27
- package/src/instance/providers.ts +8 -4
- package/src/instance/secrets.ts +153 -6
- package/src/instance/sessionSecrets.ts +64 -0
- package/src/lifecycle/enforce.ts +89 -0
- package/src/lifecycle/hooks.ts +54 -0
- package/src/lifecycle/request.ts +31 -0
- package/src/lifecycle/state.ts +101 -0
- package/src/lifecycle/transitions.ts +483 -0
- package/src/migrations/0001_init.ts +8 -10
- package/src/migrations/0003_account_issuer_nullable.ts +199 -0
- package/src/migrations/0004_user_lifecycle.ts +68 -0
- package/src/migrations/set.ts +5 -1
- package/src/test-utils/liveApp.ts +2 -1
- package/src/test-utils/schemaDrift.ts +82 -0
- package/src/test-utils/scopedBackend.ts +137 -0
- package/src/version.generated.ts +1 -1
package/dist/http/resolve.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import { authDatabase } from "../data/tables.js";
|
|
4
4
|
import { baseURLResolver } from "./baseUrl.js";
|
|
5
|
-
import { resolveAppleCredentials, resolveFacebookCredentials, resolveGithubCredentials, resolveGoogleCredentials,
|
|
5
|
+
import { resolveAppleCredentials, resolveFacebookCredentials, resolveGithubCredentials, resolveGoogleCredentials, resolveMicrosoftCredentials, resolveSessionSecrets } from "../instance/secrets.js";
|
|
6
6
|
import { makeSendAuthEmail } from "../email/send.js";
|
|
7
7
|
import { resolveProvider } from "../instance/providers.js";
|
|
8
8
|
import { makeAuth } from "../instance/auth.js";
|
|
9
|
+
import { ensureJwksSealedUnderCurrent } from "../instance/jwksReencryption.js";
|
|
9
10
|
import { InternalError } from "@pithy-sh/core/src/error/pithyError";
|
|
11
|
+
import { createWorkerLogger } from "@pithy-sh/core/src/logger/worker";
|
|
10
12
|
//#region src/http/resolve.ts
|
|
11
13
|
/**
|
|
12
14
|
* Build the Better-Auth instance for one request, memoized on the request context so the
|
|
@@ -61,20 +63,34 @@ async function buildAuthInstance(c, wiring) {
|
|
|
61
63
|
detail: "auth.compose did not resolve the email enqueue seam; ensure email() is composed."
|
|
62
64
|
});
|
|
63
65
|
const env = c.env;
|
|
64
|
-
const [
|
|
65
|
-
|
|
66
|
+
const [sessionSecrets, google, apple, facebook, github, microsoft] = await Promise.all([
|
|
67
|
+
resolveSessionSecrets(env),
|
|
66
68
|
resolveProvider(cfg.google.enabled, () => resolveGoogleCredentials(env)),
|
|
67
69
|
resolveProvider(cfg.apple.enabled, () => resolveAppleCredentials(env)),
|
|
68
70
|
resolveProvider(cfg.facebook.enabled, () => resolveFacebookCredentials(env)),
|
|
69
|
-
resolveProvider(cfg.github.enabled, () => resolveGithubCredentials(env))
|
|
71
|
+
resolveProvider(cfg.github.enabled, () => resolveGithubCredentials(env)),
|
|
72
|
+
resolveProvider(cfg.microsoft.enabled, () => resolveMicrosoftCredentials(env))
|
|
70
73
|
]);
|
|
71
74
|
const expiresMinutes = Math.max(1, Math.round(cfg.verificationExpiresIn / 60));
|
|
72
75
|
const d1 = resolveDb(c.env, cfg.database);
|
|
76
|
+
const secret = sessionSecrets.versions[sessionSecrets.currentVersion];
|
|
77
|
+
if (secret === void 0) throw new InternalError({
|
|
78
|
+
message: "Authentication is misconfigured.",
|
|
79
|
+
detail: "auth-session-secret resolved with no value at its current version"
|
|
80
|
+
});
|
|
81
|
+
await ensureJwksSealedUnderCurrent({
|
|
82
|
+
db: authDatabase(d1),
|
|
83
|
+
holder: cfg.database,
|
|
84
|
+
secrets: env.SECRETS,
|
|
85
|
+
versions: sessionSecrets,
|
|
86
|
+
log: (c.get("log") ?? createWorkerLogger({ name: "auth" })).child("auth")
|
|
87
|
+
});
|
|
73
88
|
const listener = wiring.onSessionRevoked;
|
|
74
89
|
return makeAuth({
|
|
75
90
|
db: authDatabase(d1),
|
|
76
91
|
...listener ? { onSessionRevoked: (session) => listener(session, d1) } : {},
|
|
77
92
|
secret,
|
|
93
|
+
sessionSecrets,
|
|
78
94
|
baseURL: baseURLResolver(cfg.baseURL)(c.req.raw),
|
|
79
95
|
locale: c.var.locale?.catalogLocale ?? null,
|
|
80
96
|
basePath: cfg.basePath,
|
|
@@ -83,6 +99,7 @@ async function buildAuthInstance(c, wiring) {
|
|
|
83
99
|
apple,
|
|
84
100
|
facebook,
|
|
85
101
|
github,
|
|
102
|
+
microsoft,
|
|
86
103
|
sendEmail: makeSendAuthEmail((input) => enqueueEmail(env, input), expiresMinutes, recipientLocale(c, authDatabase(resolveDb(c.env, cfg.database)))),
|
|
87
104
|
sessionExpiresIn: cfg.sessionExpiresIn,
|
|
88
105
|
sessionUpdateAge: cfg.sessionUpdateAge,
|
|
@@ -93,7 +110,8 @@ async function buildAuthInstance(c, wiring) {
|
|
|
93
110
|
google: cfg.google.allowSignUp,
|
|
94
111
|
apple: cfg.apple.allowSignUp,
|
|
95
112
|
facebook: cfg.facebook.allowSignUp,
|
|
96
|
-
github: cfg.github.allowSignUp
|
|
113
|
+
github: cfg.github.allowSignUp,
|
|
114
|
+
microsoft: cfg.microsoft.allowSignUp
|
|
97
115
|
},
|
|
98
116
|
...wiring.resolveGithubUserInfo ? { resolveGithubUserInfo: wiring.resolveGithubUserInfo } : {},
|
|
99
117
|
emit: (event) => c.var.emit(event),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/http/routes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAQ7E,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":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAQ7E,OAAO,KAAK,EAAW,IAAI,EAAE,MAAM,MAAM,CAAC;AAE1C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAwChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,CAuHtF"}
|
package/dist/http/routes.js
CHANGED
|
@@ -4,6 +4,8 @@ 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
6
|
import { requireFreshAuthentication } from "./providerFreshness.js";
|
|
7
|
+
import { isLifecycleRefusal } from "../lifecycle/state.js";
|
|
8
|
+
import { noticedRefusal } from "../lifecycle/enforce.js";
|
|
7
9
|
import { PROFILE_IMAGE_PATH } from "../profile/profile.js";
|
|
8
10
|
import { collapseProviderRefusal } from "./providerRefusal.js";
|
|
9
11
|
import { getAuthInstance, resolveDb } from "./resolve.js";
|
|
@@ -14,6 +16,7 @@ import { RevokeDeviceBody } from "./schemas.js";
|
|
|
14
16
|
import { registerAuthAdminRoutes } from "./adminRoutes.js";
|
|
15
17
|
import { guardErrorCallbackURL } from "./errorCallbackUrl.js";
|
|
16
18
|
import { apiErrorToPithy } from "./errors.js";
|
|
19
|
+
import { admitsRefusedAccount, lifecycleRedirect } from "./lifecycleGate.js";
|
|
17
20
|
import { NotFoundError, UnauthorizedError } from "@pithy-sh/core/src/error/pithyError";
|
|
18
21
|
import { TURNSTILE_LOGIN_ACTION } from "@pithy-sh/turnstile/src/config/config";
|
|
19
22
|
import { describeRoute } from "@pithy-sh/core/src/http/routeSpec";
|
|
@@ -197,12 +200,26 @@ async function auditRefusal(c, response) {
|
|
|
197
200
|
*/
|
|
198
201
|
async function handleBetterAuth(c, wiring) {
|
|
199
202
|
const instance = await getAuthInstance(c, wiring);
|
|
203
|
+
const pathname = new URL(c.req.raw.url).pathname;
|
|
204
|
+
const refusal = noticedRefusal(c);
|
|
205
|
+
if (refusal && !admitsRefusedAccount(pathname.slice(wiring.config.basePath.length))) throw refusal;
|
|
200
206
|
const request = await guardErrorCallbackURL(c.req.raw, c.req.raw.url);
|
|
201
207
|
let response;
|
|
202
208
|
try {
|
|
203
209
|
response = await instance.handler(request);
|
|
204
210
|
} catch (error) {
|
|
205
|
-
|
|
211
|
+
const pithy = apiErrorToPithy(error);
|
|
212
|
+
if (isLifecycleRefusal(pithy)) {
|
|
213
|
+
if (isSignInAttempt(pathname)) await emitDenied(c.var.emit, {
|
|
214
|
+
...correlation(c.req.raw.headers),
|
|
215
|
+
detail: pithy.payload.code
|
|
216
|
+
});
|
|
217
|
+
if (c.req.method === "GET" && pathname === `${wiring.config.basePath}/magic-link/verify`) {
|
|
218
|
+
const redirect = lifecycleRedirect(new URL(request.url), wiring.config.baseURL, wiring.config.trustedOrigins, pithy);
|
|
219
|
+
if (redirect) return redirect;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
throw pithy;
|
|
206
223
|
}
|
|
207
224
|
const collapsed = collapseProviderRefusal(c.req.raw.url, response);
|
|
208
225
|
if (collapsed) {
|
|
@@ -272,6 +289,8 @@ async function rotateToken(c, wiring) {
|
|
|
272
289
|
action: "Present a valid session or bearer token to rotate."
|
|
273
290
|
});
|
|
274
291
|
}
|
|
292
|
+
const refusal = noticedRefusal(c);
|
|
293
|
+
if (refusal) throw refusal;
|
|
275
294
|
const ip = headers.get("cf-connecting-ip") ?? void 0;
|
|
276
295
|
const userAgent = headers.get("user-agent") ?? void 0;
|
|
277
296
|
const session = current.session;
|