@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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/instance/auth.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/instance/auth.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CA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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAUtD"}
|
package/dist/instance/auth.js
CHANGED
|
@@ -5,10 +5,13 @@ import { claimedDelete, markSessionEnded, messageDeliveryDuring, sessionEndedDur
|
|
|
5
5
|
import { KIT_SESSION_FIELDS, KIT_USER_FIELDS } from "../data/kitFields.js";
|
|
6
6
|
import { parseDeviceMeta, registerDevice } from "../device/registry.js";
|
|
7
7
|
import "../http/providerFreshness.js";
|
|
8
|
+
import { lifecycleRefusalFor } from "../lifecycle/enforce.js";
|
|
8
9
|
import { refuseUnsafeProfile, sanitizeProfile } from "../profile/profile.js";
|
|
9
10
|
import { defaultGithubUserInfo } from "./githubUserInfo.js";
|
|
11
|
+
import { microsoftProfileToUser } from "./microsoftEmail.js";
|
|
10
12
|
import { kitPlugins } from "./plugins.js";
|
|
11
13
|
import { providerUnavailable, unavailableProviderFor } from "./providers.js";
|
|
14
|
+
import { betterAuthSecrets } from "./sessionSecrets.js";
|
|
12
15
|
import { betterAuth } from "better-auth";
|
|
13
16
|
import { APIError, createAuthMiddleware } from "better-auth/api";
|
|
14
17
|
//#region src/instance/auth.ts
|
|
@@ -70,9 +73,30 @@ function socialProviders(deps) {
|
|
|
70
73
|
getUserInfo: deps.resolveGithubUserInfo ?? defaultGithubUserInfo(),
|
|
71
74
|
...signUpOption(deps.providerSignUp.github)
|
|
72
75
|
};
|
|
76
|
+
if (deps.microsoft.state === "ready") {
|
|
77
|
+
const microsoft = deps.microsoft.credentials;
|
|
78
|
+
providers.microsoft = {
|
|
79
|
+
clientId: microsoft.clientId,
|
|
80
|
+
clientSecret: microsoft.clientSecret,
|
|
81
|
+
tenantId: microsoft.tenantId,
|
|
82
|
+
disableProfilePhoto: true,
|
|
83
|
+
mapProfileToUser: microsoftProfileToUser,
|
|
84
|
+
...signUpOption(deps.providerSignUp.microsoft)
|
|
85
|
+
};
|
|
86
|
+
}
|
|
73
87
|
return Object.keys(providers).length > 0 ? providers : void 0;
|
|
74
88
|
}
|
|
75
89
|
/**
|
|
90
|
+
* The providers whose email links into an existing account on the strength of their name alone.
|
|
91
|
+
*
|
|
92
|
+
* Google and Apple, and nobody else, permanently. Facebook's verified flag is asserted in its block with
|
|
93
|
+
* a reason; GitHub's comes from its emails API; Microsoft's is decided per sign-in by
|
|
94
|
+
* `./microsoftEmail.ts`, because Entra ID cannot vouch for an email it did not verify. Adding a provider
|
|
95
|
+
* here would skip the verified half of the linking gate for it — `./providerSignInGate.ts` reads the same
|
|
96
|
+
* list off the context.
|
|
97
|
+
*/
|
|
98
|
+
const TRUSTED_PROVIDERS = ["google", "apple"];
|
|
99
|
+
/**
|
|
76
100
|
* The seeding guard: a new account may be created only for an email a provider has **verified**.
|
|
77
101
|
*
|
|
78
102
|
* Passwordless sign-up (magic link / OTP) always creates `emailVerified: true`; a social sign-up
|
|
@@ -103,6 +127,32 @@ function refuseUnsafeProfileFields(user) {
|
|
|
103
127
|
});
|
|
104
128
|
}
|
|
105
129
|
/**
|
|
130
|
+
* Refuse a session for an account that may not act, in the shape the endpoint creating it can answer.
|
|
131
|
+
*
|
|
132
|
+
* **On the social callback, an `APIError` carrying the kit's code**, because that endpoint answers a refusal
|
|
133
|
+
* by redirecting to the caller's `errorCallbackURL` with `?error=<code>` — but only for an `APIError`
|
|
134
|
+
* (`callback.mjs:192`). A `PithyError` would escape it and leave a browser mid-OAuth on a bare 403 body
|
|
135
|
+
* instead of back on the adopter's screen. The code is the same string either way, and the reason travels as
|
|
136
|
+
* `error_description`, which is where Better Auth puts a refusal's message.
|
|
137
|
+
*
|
|
138
|
+
* **Everywhere else, the `PithyError` itself**, which `handleBetterAuth` renders through the one error
|
|
139
|
+
* envelope — or, on the magic-link verify redirect, turns into the same `?error=` redirect.
|
|
140
|
+
*/
|
|
141
|
+
function refuseSession(refusal, ctx) {
|
|
142
|
+
if (ctx?.path?.startsWith("/callback/")) {
|
|
143
|
+
const message = refusal.payload.message;
|
|
144
|
+
if (refusal.payload.code === "auth/account_deleted") throw new APIError("FORBIDDEN", {
|
|
145
|
+
code: "auth/account_deleted",
|
|
146
|
+
message
|
|
147
|
+
});
|
|
148
|
+
throw new APIError("FORBIDDEN", {
|
|
149
|
+
code: "auth/account_suspended",
|
|
150
|
+
message
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
throw refusal;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
106
156
|
* Who caused a row change, read from the endpoint context Better Auth hands a database hook.
|
|
107
157
|
*
|
|
108
158
|
* **The context is absent as often as it is present, and that absence is information.** A hook reached
|
|
@@ -138,7 +188,7 @@ function makeAuth(deps) {
|
|
|
138
188
|
appName: "Pithy",
|
|
139
189
|
baseURL: deps.baseURL,
|
|
140
190
|
basePath: deps.basePath,
|
|
141
|
-
secret: deps.secret,
|
|
191
|
+
...deps.sessionSecrets === void 0 ? { secret: deps.secret } : betterAuthSecrets(deps.sessionSecrets),
|
|
142
192
|
telemetry: { enabled: false },
|
|
143
193
|
trustedOrigins: deps.trustedOrigins,
|
|
144
194
|
database: {
|
|
@@ -146,7 +196,10 @@ function makeAuth(deps) {
|
|
|
146
196
|
type: "sqlite",
|
|
147
197
|
transaction: false
|
|
148
198
|
},
|
|
149
|
-
advanced: { database: {
|
|
199
|
+
advanced: { database: {
|
|
200
|
+
generateId: () => crypto.randomUUID(),
|
|
201
|
+
validateSchema: false
|
|
202
|
+
} },
|
|
150
203
|
onAPIError: { throw: true },
|
|
151
204
|
databaseHooks: {
|
|
152
205
|
user: {
|
|
@@ -236,6 +289,8 @@ function makeAuth(deps) {
|
|
|
236
289
|
* `deviceId` by override for the same reason.
|
|
237
290
|
*/
|
|
238
291
|
before: async (session, ctx) => {
|
|
292
|
+
const refusal = await lifecycleRefusalFor(deps.db, session.userId, null, /* @__PURE__ */ new Date());
|
|
293
|
+
if (refusal) refuseSession(refusal, ctx);
|
|
239
294
|
const carried = session.authenticatedAt;
|
|
240
295
|
const authenticatedAt = carried !== void 0 ? carried : /* @__PURE__ */ new Date();
|
|
241
296
|
const meta = ctx?.headers ? parseDeviceMeta(ctx.headers) : void 0;
|
|
@@ -384,7 +439,7 @@ before: async (session, ctx) => {
|
|
|
384
439
|
modelName: "pithyAuthAccounts",
|
|
385
440
|
accountLinking: {
|
|
386
441
|
enabled: true,
|
|
387
|
-
trustedProviders: [
|
|
442
|
+
trustedProviders: [...TRUSTED_PROVIDERS],
|
|
388
443
|
/**
|
|
389
444
|
* **A signed-in user may attach a provider whose email differs from their account's.**
|
|
390
445
|
*
|
|
@@ -428,4 +483,4 @@ before: async (session, ctx) => {
|
|
|
428
483
|
});
|
|
429
484
|
}
|
|
430
485
|
//#endregion
|
|
431
|
-
export { isUnverifiedSignup, makeAuth, socialProviders };
|
|
486
|
+
export { TRUSTED_PROVIDERS, isUnverifiedSignup, makeAuth, socialProviders };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
4
|
+
import type { Logger } from "@pithy-sh/core/src/logger/logger";
|
|
5
|
+
import type { AuthDatabase } from "../data/tables";
|
|
6
|
+
import { type SessionSecretVersions } from "./sessionSecrets";
|
|
7
|
+
/**
|
|
8
|
+
* **The JWKS private keys, moved under the session secret's newest version before the old one retires (#602).**
|
|
9
|
+
*
|
|
10
|
+
* Better Auth seals every key in `pithy_auth_jwks` under the session secret (`plugins/jwt/utils.mjs`), and a
|
|
11
|
+
* key has no expiry that would make it safe to stop being able to open it. An unattended rotation of
|
|
12
|
+
* `auth-session-secret` therefore waits, before it retires the previous version, for this Worker to say the
|
|
13
|
+
* keys have moved — the secret's registry entry names them as `auth-jwks` — and this is where they move.
|
|
14
|
+
*
|
|
15
|
+
* Each key is opened under whichever retained version it names (`$ba$<version>$…`), or — for one sealed
|
|
16
|
+
* before the kit handed Better Auth a version set, which carries no envelope — under each retained version
|
|
17
|
+
* in turn, oldest first. It is sealed again under the current version and written back only if the row
|
|
18
|
+
* still holds what was read, so two isolates moving the same key cannot interleave into a value neither
|
|
19
|
+
* wrote. Then the move is recorded in the secrets D1, where the manager reads it.
|
|
20
|
+
*
|
|
21
|
+
* **It never makes a key unopenable.** A key no retained version opens is left exactly as it is and
|
|
22
|
+
* counted; it was already lost, and nothing here can lose it further. It does not hold the record back,
|
|
23
|
+
* because waiting cannot make it open — and a retirement held forever over a key that will never be used
|
|
24
|
+
* is an envelope that grows on every rotation.
|
|
25
|
+
*/
|
|
26
|
+
/** What one pass did, in counts. */
|
|
27
|
+
export interface JwksReencryption {
|
|
28
|
+
/** Keys sealed again under the current version. */
|
|
29
|
+
moved: number;
|
|
30
|
+
/** Keys already under it. */
|
|
31
|
+
current: number;
|
|
32
|
+
/** Keys no retained version opens. Left as they were. */
|
|
33
|
+
unopenable: number;
|
|
34
|
+
}
|
|
35
|
+
/** Move every JWKS private key under the current version of the session secret. */
|
|
36
|
+
export declare function reencryptJwks(db: AuthDatabase, versions: SessionSecretVersions): Promise<JwksReencryption>;
|
|
37
|
+
/** Forget what this isolate has settled or is waiting on. For tests. */
|
|
38
|
+
export declare function resetJwksReencryption(): void;
|
|
39
|
+
/** What {@link ensureJwksSealedUnderCurrent} needs. */
|
|
40
|
+
export interface EnsureJwksDeps {
|
|
41
|
+
/** The application database the JWKS keys live in. */
|
|
42
|
+
db: AuthDatabase;
|
|
43
|
+
/**
|
|
44
|
+
* That database's binding name — `auth({ database })`. The holder the move is recorded under, so two auth
|
|
45
|
+
* Workers over two databases are waited for one by one rather than one answering for both.
|
|
46
|
+
*/
|
|
47
|
+
holder: string;
|
|
48
|
+
/** The secrets D1 the move is recorded in. */
|
|
49
|
+
secrets: D1Database;
|
|
50
|
+
/** The session secret's retained versions, as this request resolved them. */
|
|
51
|
+
versions: SessionSecretVersions;
|
|
52
|
+
/** Where a failure is said. Never fatal. */
|
|
53
|
+
log: Logger;
|
|
54
|
+
/** The clock. Defaults to now. */
|
|
55
|
+
now?: Date;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Make sure the JWKS keys are sealed under the current version, and that the secrets D1 says so — once per
|
|
59
|
+
* version per isolate.
|
|
60
|
+
*
|
|
61
|
+
* **Recorded with one version too.** While the secret holds one version there is nothing to move, and the
|
|
62
|
+
* record says the keys are under it ({@link recordSoleVersion}). That is how the manager knows this database
|
|
63
|
+
* holds keys before the first rotation: a holder it has never heard from is one it cannot wait for.
|
|
64
|
+
*
|
|
65
|
+
* **Not on the first request that sees the version.** Another isolate may still hold the envelope it cached
|
|
66
|
+
* before the append, with no key for the new version; a key re-sealed under it now would fail there. So the
|
|
67
|
+
* first sight is recorded, and the move waits until every cache filled before it has expired
|
|
68
|
+
* (`claimReencryption`, `REENCRYPTION_SETTLE_MS`). An isolate told to wait does not ask again until then.
|
|
69
|
+
*
|
|
70
|
+
* **Never fatal.** It runs on a request, and the request it rides on is somebody signing in; a failure here
|
|
71
|
+
* leaves the keys where they were — still opening, since the previous version is still held — and the
|
|
72
|
+
* retirement waiting. The next isolate tries again.
|
|
73
|
+
*/
|
|
74
|
+
export declare function ensureJwksSealedUnderCurrent(deps: EnsureJwksDeps): Promise<void>;
|
|
75
|
+
//# sourceMappingURL=jwksReencryption.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwksReencryption.d.ts","sourceRoot":"","sources":["../../src/instance/jwksReencryption.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAS/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,OAAO,EAAqB,KAAK,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAEjF;;;;;;;;;;;;;;;;;;GAkBG;AAEH,oCAAoC;AACpC,MAAM,WAAW,gBAAgB;IAC/B,mDAAmD;IACnD,KAAK,EAAE,MAAM,CAAC;IACd,6BAA6B;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,yDAAyD;IACzD,UAAU,EAAE,MAAM,CAAC;CACpB;AAyBD,mFAAmF;AACnF,wBAAsB,aAAa,CAAC,EAAE,EAAE,YAAY,EAAE,QAAQ,EAAE,qBAAqB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAmChH;AAWD,wEAAwE;AACxE,wBAAgB,qBAAqB,IAAI,IAAI,CAG5C;AAED,uDAAuD;AACvD,MAAM,WAAW,cAAc;IAC7B,sDAAsD;IACtD,EAAE,EAAE,YAAY,CAAC;IACjB;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;IACf,8CAA8C;IAC9C,OAAO,EAAE,UAAU,CAAC;IACpB,6EAA6E;IAC7E,QAAQ,EAAE,qBAAqB,CAAC;IAChC,4CAA4C;IAC5C,GAAG,EAAE,MAAM,CAAC;IACZ,kCAAkC;IAClC,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,4BAA4B,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CA6CtF"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { AUTH_JWKS_DEPENDENT, AUTH_SESSION_SECRET } from "./secrets.js";
|
|
4
|
+
import { betterAuthSecrets } from "./sessionSecrets.js";
|
|
5
|
+
import { createDatabase } from "@pithy-sh/core/src/data/db";
|
|
6
|
+
import { secretsTables } from "@pithy-sh/secrets/src/data/tables";
|
|
7
|
+
import { claimReencryption, currentSealedVersion, recordReencryption, recordSoleVersion } from "@pithy-sh/secrets/src/rotation/reencryption";
|
|
8
|
+
import { parseEnvelope, symmetricDecrypt, symmetricEncrypt } from "better-auth/crypto";
|
|
9
|
+
//#region src/instance/jwksReencryption.ts
|
|
10
|
+
/** Better Auth's own key structure for a version set, built the way `buildSecretConfig` builds it. */
|
|
11
|
+
function secretConfig(versions) {
|
|
12
|
+
const { secrets } = betterAuthSecrets(versions);
|
|
13
|
+
return {
|
|
14
|
+
keys: new Map(secrets.map((one) => [one.version, one.value])),
|
|
15
|
+
currentVersion: Number(versions.currentVersion)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** Open one stored private key, or `null` when no retained version opens it. */
|
|
19
|
+
async function opened(stored, versions, config) {
|
|
20
|
+
if (parseEnvelope(stored) !== null) return await symmetricDecrypt({
|
|
21
|
+
key: config,
|
|
22
|
+
data: stored
|
|
23
|
+
}).catch(() => null);
|
|
24
|
+
const oldestFirst = [...betterAuthSecrets(versions).secrets].sort((left, right) => left.version - right.version);
|
|
25
|
+
for (const candidate of oldestFirst) {
|
|
26
|
+
const plain = await symmetricDecrypt({
|
|
27
|
+
key: candidate.value,
|
|
28
|
+
data: stored
|
|
29
|
+
}).catch(() => null);
|
|
30
|
+
if (plain !== null) return plain;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
/** Move every JWKS private key under the current version of the session secret. */
|
|
35
|
+
async function reencryptJwks(db, versions) {
|
|
36
|
+
const config = secretConfig(versions);
|
|
37
|
+
const current = Number(versions.currentVersion);
|
|
38
|
+
const result = {
|
|
39
|
+
moved: 0,
|
|
40
|
+
current: 0,
|
|
41
|
+
unopenable: 0
|
|
42
|
+
};
|
|
43
|
+
const rows = await db.selectFrom("pithyAuthJwks").select(["id", "privateKey"]).execute();
|
|
44
|
+
for (const row of rows) {
|
|
45
|
+
let stored;
|
|
46
|
+
try {
|
|
47
|
+
stored = JSON.parse(row.privateKey);
|
|
48
|
+
} catch {
|
|
49
|
+
stored = void 0;
|
|
50
|
+
}
|
|
51
|
+
if (typeof stored !== "string") {
|
|
52
|
+
result.unopenable += 1;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (parseEnvelope(stored)?.version === current) {
|
|
56
|
+
result.current += 1;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const plain = await opened(stored, versions, config);
|
|
60
|
+
if (plain === null) {
|
|
61
|
+
result.unopenable += 1;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const sealed = JSON.stringify(await symmetricEncrypt({
|
|
65
|
+
key: config,
|
|
66
|
+
data: plain
|
|
67
|
+
}));
|
|
68
|
+
await db.updateTable("pithyAuthJwks").set({ privateKey: sealed }).where("id", "=", row.id).where("privateKey", "=", row.privateKey).execute();
|
|
69
|
+
result.moved += 1;
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The versions this isolate has finished with — moved, or failed to — keyed by version and fingerprint, so a
|
|
75
|
+
* version key reused after an `update` is a new version here too.
|
|
76
|
+
*/
|
|
77
|
+
const settled = /* @__PURE__ */ new Set();
|
|
78
|
+
/** The versions this isolate was told to wait on, with when to ask again. */
|
|
79
|
+
const waiting = /* @__PURE__ */ new Map();
|
|
80
|
+
/** Forget what this isolate has settled or is waiting on. For tests. */
|
|
81
|
+
function resetJwksReencryption() {
|
|
82
|
+
settled.clear();
|
|
83
|
+
waiting.clear();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Make sure the JWKS keys are sealed under the current version, and that the secrets D1 says so — once per
|
|
87
|
+
* version per isolate.
|
|
88
|
+
*
|
|
89
|
+
* **Recorded with one version too.** While the secret holds one version there is nothing to move, and the
|
|
90
|
+
* record says the keys are under it ({@link recordSoleVersion}). That is how the manager knows this database
|
|
91
|
+
* holds keys before the first rotation: a holder it has never heard from is one it cannot wait for.
|
|
92
|
+
*
|
|
93
|
+
* **Not on the first request that sees the version.** Another isolate may still hold the envelope it cached
|
|
94
|
+
* before the append, with no key for the new version; a key re-sealed under it now would fail there. So the
|
|
95
|
+
* first sight is recorded, and the move waits until every cache filled before it has expired
|
|
96
|
+
* (`claimReencryption`, `REENCRYPTION_SETTLE_MS`). An isolate told to wait does not ask again until then.
|
|
97
|
+
*
|
|
98
|
+
* **Never fatal.** It runs on a request, and the request it rides on is somebody signing in; a failure here
|
|
99
|
+
* leaves the keys where they were — still opening, since the previous version is still held — and the
|
|
100
|
+
* retirement waiting. The next isolate tries again.
|
|
101
|
+
*/
|
|
102
|
+
async function ensureJwksSealedUnderCurrent(deps) {
|
|
103
|
+
const { versions } = deps;
|
|
104
|
+
const sole = Object.keys(versions.versions).length < 2;
|
|
105
|
+
const now = deps.now ?? /* @__PURE__ */ new Date();
|
|
106
|
+
try {
|
|
107
|
+
const sealed = await currentSealedVersion(versions);
|
|
108
|
+
const key = `${deps.holder}:${sole ? "sole" : "rotating"}:${sealed.version}:${sealed.fingerprint}`;
|
|
109
|
+
if (settled.has(key) || (waiting.get(key) ?? 0) > now.getTime()) return;
|
|
110
|
+
try {
|
|
111
|
+
const secretsDb = createDatabase(deps.secrets, secretsTables);
|
|
112
|
+
const where = {
|
|
113
|
+
name: AUTH_SESSION_SECRET,
|
|
114
|
+
dependent: AUTH_JWKS_DEPENDENT,
|
|
115
|
+
holder: deps.holder
|
|
116
|
+
};
|
|
117
|
+
if (sole) {
|
|
118
|
+
settled.add(key);
|
|
119
|
+
await recordSoleVersion(secretsDb, {
|
|
120
|
+
...where,
|
|
121
|
+
sealed,
|
|
122
|
+
now
|
|
123
|
+
});
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const claim = await claimReencryption(secretsDb, {
|
|
127
|
+
...where,
|
|
128
|
+
sealed,
|
|
129
|
+
now
|
|
130
|
+
});
|
|
131
|
+
if (claim.state === "moved") {
|
|
132
|
+
settled.add(key);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (claim.state === "settling") {
|
|
136
|
+
waiting.set(key, claim.until.getTime());
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
settled.add(key);
|
|
140
|
+
const moved = await reencryptJwks(deps.db, versions);
|
|
141
|
+
await recordReencryption(secretsDb, {
|
|
142
|
+
...where,
|
|
143
|
+
...sealed,
|
|
144
|
+
now
|
|
145
|
+
});
|
|
146
|
+
deps.log.info("jwks re-encrypted under the current session secret", {
|
|
147
|
+
version: sealed.version,
|
|
148
|
+
...moved
|
|
149
|
+
});
|
|
150
|
+
if (moved.unopenable > 0) deps.log.warn("jwks keys no retained session secret opens were left as they were", { unopenable: moved.unopenable });
|
|
151
|
+
} catch (error) {
|
|
152
|
+
settled.add(key);
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
} catch (error) {
|
|
156
|
+
deps.log.warn("jwks re-encryption did not finish; the previous session secret stays until it does", { reason: error instanceof Error ? error.message : String(error) });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//#endregion
|
|
160
|
+
export { ensureJwksSealedUnderCurrent, reencryptJwks, resetJwksReencryption };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* Whether a Microsoft sign-in's email is the signer's own — the one decision the Microsoft provider adds (#262).
|
|
5
|
+
*
|
|
6
|
+
* ## Why Microsoft needs a rule of its own
|
|
7
|
+
*
|
|
8
|
+
* Every other provider the kit serves can say which address it verified. Entra ID cannot. A work or
|
|
9
|
+
* school ID token carries no `email_verified` claim, and its `email` is read from the directory's `mail`
|
|
10
|
+
* attribute — which a tenant admin sets to any string, a stranger's address at a domain they do not own
|
|
11
|
+
* included. Sign in, and the token asserts you are somebody else. That is the nOAuth takeover class, and
|
|
12
|
+
* it is a property of Entra ID, not of an app's configuration. Microsoft's own claims reference says of
|
|
13
|
+
* `email`: "This value isn't guaranteed to be correct, and is mutable over time - never use it for
|
|
14
|
+
* authorization." So Microsoft stays out of `trustedProviders` permanently, and this decides the flag.
|
|
15
|
+
*
|
|
16
|
+
* ## The three ways an address is verified, and nothing else
|
|
17
|
+
*
|
|
18
|
+
* - **`xms_edov === true`** — "email domain owner verified", the optional claim Microsoft published as
|
|
19
|
+
* the nOAuth remediation. The tenant provably owns the address's domain, so it cannot be a stranger's.
|
|
20
|
+
* Off unless the app registration asks for it; `docs/microsoft-oauth.md` walks that step.
|
|
21
|
+
* - **`tid` is the personal-accounts tenant.** A personal Microsoft account is in no directory, has no
|
|
22
|
+
* admin, and its address is the account itself, confirmed by Microsoft at sign-up. It never carries
|
|
23
|
+
* `xms_edov`, so it needs its own clause.
|
|
24
|
+
* - **Linking from inside the account, to the account's own address.** The verify-to-link challenge's
|
|
25
|
+
* second half. Somebody refused above signs in by magic link — which proves the address — and connects
|
|
26
|
+
* Microsoft from inside. That Microsoft identity naming the very address the session holds is verified
|
|
27
|
+
* by the kit, not by Microsoft. A different address is not, and Better Auth's link branch refuses it.
|
|
28
|
+
*
|
|
29
|
+
* Anything else — the claim absent, `false`, `"true"`, `1` — is unverified. **Absence is never read as
|
|
30
|
+
* verification**, and only the boolean `true` is. `email_verified` and `verified_primary_email` are read
|
|
31
|
+
* by Better Auth's own default and deliberately not here: Entra does not issue the first for work
|
|
32
|
+
* accounts, and the second is sourced from directory attributes this rule does not get to vouch for.
|
|
33
|
+
*
|
|
34
|
+
* `oid` and `tid` are immutable, so once an identity is linked it signs in on `oid` forever
|
|
35
|
+
* (`./providerSignInGate.ts`, row 1) and none of this runs again for it.
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* The tenant id every personal Microsoft account signs in under.
|
|
39
|
+
*
|
|
40
|
+
* Source: https://learn.microsoft.com/en-us/entra/identity-platform/id-token-claims-reference — the `tid`
|
|
41
|
+
* row: "For sign-ins to the personal Microsoft account tenant (services like Xbox, Teams for Life, or
|
|
42
|
+
* Outlook), the value is `9188040d-6c67-4c5b-b112-36a304b66dad`." The `iss` row names the same GUID.
|
|
43
|
+
* Compared exactly: Microsoft issues it lowercase, and a token that spells it otherwise is not one of its.
|
|
44
|
+
*/
|
|
45
|
+
export declare const MICROSOFT_PERSONAL_ACCOUNTS_TENANT_ID = "9188040d-6c67-4c5b-b112-36a304b66dad";
|
|
46
|
+
/** The part of a link-in-progress this rule reads: the signed-in account's own address. */
|
|
47
|
+
export interface LinkInProgress {
|
|
48
|
+
/** The address on the account the session belongs to, as Better Auth's `/link-social` stored it. */
|
|
49
|
+
readonly email: unknown;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Decide the flag from the decoded ID token and, on a link, the account it is being linked to.
|
|
53
|
+
*
|
|
54
|
+
* Pure, so every hostile shape can be driven without a request. `profile` is the whole decoded ID token —
|
|
55
|
+
* Better Auth hands `mapProfileToUser` the `decodeJwt` result itself (`@better-auth/core`
|
|
56
|
+
* `social-providers/microsoft-entra-id.mjs`), so `xms_edov` and `tid` are in reach as claims, not as
|
|
57
|
+
* fields somebody copied.
|
|
58
|
+
*/
|
|
59
|
+
export declare function microsoftEmailVerified(profile: Readonly<Record<string, unknown>>, link: LinkInProgress | null): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* The Microsoft block's `mapProfileToUser`: the rule, applied to this callback.
|
|
62
|
+
*
|
|
63
|
+
* **It always states the flag.** Better Auth computes its own default first and spreads this over it, so
|
|
64
|
+
* a mapper that omitted the key on the unverified path would leave that default — read off claims a
|
|
65
|
+
* tenant can influence — standing.
|
|
66
|
+
*/
|
|
67
|
+
export declare function microsoftProfileToUser(profile: Readonly<Record<string, unknown>>): Promise<{
|
|
68
|
+
emailVerified: boolean;
|
|
69
|
+
}>;
|
|
70
|
+
//# sourceMappingURL=microsoftEmail.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"microsoftEmail.d.ts","sourceRoot":"","sources":["../../src/instance/microsoftEmail.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,qCAAqC,yCAAyC,CAAC;AAE5F,2FAA2F;AAC3F,MAAM,WAAW,cAAc;IAC7B,oGAAoG;IACpG,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,EAC1C,IAAI,EAAE,cAAc,GAAG,IAAI,GAC1B,OAAO,CAQT;AAwBD;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GACzC,OAAO,CAAC;IAAE,aAAa,EAAE,OAAO,CAAA;CAAE,CAAC,CAErC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2026 Pithy
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
import { getOAuthState } from "better-auth/api";
|
|
4
|
+
//#region src/instance/microsoftEmail.ts
|
|
5
|
+
/**
|
|
6
|
+
* Whether a Microsoft sign-in's email is the signer's own — the one decision the Microsoft provider adds (#262).
|
|
7
|
+
*
|
|
8
|
+
* ## Why Microsoft needs a rule of its own
|
|
9
|
+
*
|
|
10
|
+
* Every other provider the kit serves can say which address it verified. Entra ID cannot. A work or
|
|
11
|
+
* school ID token carries no `email_verified` claim, and its `email` is read from the directory's `mail`
|
|
12
|
+
* attribute — which a tenant admin sets to any string, a stranger's address at a domain they do not own
|
|
13
|
+
* included. Sign in, and the token asserts you are somebody else. That is the nOAuth takeover class, and
|
|
14
|
+
* it is a property of Entra ID, not of an app's configuration. Microsoft's own claims reference says of
|
|
15
|
+
* `email`: "This value isn't guaranteed to be correct, and is mutable over time - never use it for
|
|
16
|
+
* authorization." So Microsoft stays out of `trustedProviders` permanently, and this decides the flag.
|
|
17
|
+
*
|
|
18
|
+
* ## The three ways an address is verified, and nothing else
|
|
19
|
+
*
|
|
20
|
+
* - **`xms_edov === true`** — "email domain owner verified", the optional claim Microsoft published as
|
|
21
|
+
* the nOAuth remediation. The tenant provably owns the address's domain, so it cannot be a stranger's.
|
|
22
|
+
* Off unless the app registration asks for it; `docs/microsoft-oauth.md` walks that step.
|
|
23
|
+
* - **`tid` is the personal-accounts tenant.** A personal Microsoft account is in no directory, has no
|
|
24
|
+
* admin, and its address is the account itself, confirmed by Microsoft at sign-up. It never carries
|
|
25
|
+
* `xms_edov`, so it needs its own clause.
|
|
26
|
+
* - **Linking from inside the account, to the account's own address.** The verify-to-link challenge's
|
|
27
|
+
* second half. Somebody refused above signs in by magic link — which proves the address — and connects
|
|
28
|
+
* Microsoft from inside. That Microsoft identity naming the very address the session holds is verified
|
|
29
|
+
* by the kit, not by Microsoft. A different address is not, and Better Auth's link branch refuses it.
|
|
30
|
+
*
|
|
31
|
+
* Anything else — the claim absent, `false`, `"true"`, `1` — is unverified. **Absence is never read as
|
|
32
|
+
* verification**, and only the boolean `true` is. `email_verified` and `verified_primary_email` are read
|
|
33
|
+
* by Better Auth's own default and deliberately not here: Entra does not issue the first for work
|
|
34
|
+
* accounts, and the second is sourced from directory attributes this rule does not get to vouch for.
|
|
35
|
+
*
|
|
36
|
+
* `oid` and `tid` are immutable, so once an identity is linked it signs in on `oid` forever
|
|
37
|
+
* (`./providerSignInGate.ts`, row 1) and none of this runs again for it.
|
|
38
|
+
*/
|
|
39
|
+
/**
|
|
40
|
+
* The tenant id every personal Microsoft account signs in under.
|
|
41
|
+
*
|
|
42
|
+
* Source: https://learn.microsoft.com/en-us/entra/identity-platform/id-token-claims-reference — the `tid`
|
|
43
|
+
* row: "For sign-ins to the personal Microsoft account tenant (services like Xbox, Teams for Life, or
|
|
44
|
+
* Outlook), the value is `9188040d-6c67-4c5b-b112-36a304b66dad`." The `iss` row names the same GUID.
|
|
45
|
+
* Compared exactly: Microsoft issues it lowercase, and a token that spells it otherwise is not one of its.
|
|
46
|
+
*/
|
|
47
|
+
const MICROSOFT_PERSONAL_ACCOUNTS_TENANT_ID = "9188040d-6c67-4c5b-b112-36a304b66dad";
|
|
48
|
+
/**
|
|
49
|
+
* Decide the flag from the decoded ID token and, on a link, the account it is being linked to.
|
|
50
|
+
*
|
|
51
|
+
* Pure, so every hostile shape can be driven without a request. `profile` is the whole decoded ID token —
|
|
52
|
+
* Better Auth hands `mapProfileToUser` the `decodeJwt` result itself (`@better-auth/core`
|
|
53
|
+
* `social-providers/microsoft-entra-id.mjs`), so `xms_edov` and `tid` are in reach as claims, not as
|
|
54
|
+
* fields somebody copied.
|
|
55
|
+
*/
|
|
56
|
+
function microsoftEmailVerified(profile, link) {
|
|
57
|
+
if (profile.xms_edov === true) return true;
|
|
58
|
+
if (profile.tid === "9188040d-6c67-4c5b-b112-36a304b66dad") return true;
|
|
59
|
+
if (link === null) return false;
|
|
60
|
+
const { email } = profile;
|
|
61
|
+
if (typeof email !== "string" || email.length === 0) return false;
|
|
62
|
+
if (typeof link.email !== "string" || link.email.length === 0) return false;
|
|
63
|
+
return email.toLowerCase() === link.email.toLowerCase();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The link this callback completes, or `null` when it is a sign-in.
|
|
67
|
+
*
|
|
68
|
+
* `link` is written by Better Auth's `/link-social` alone: `generateState` spreads the caller's
|
|
69
|
+
* `additionalData` first and assigns `link` last, so a sign-in overwrites any injected one
|
|
70
|
+
* (`./providerSignInGate.ts` records it, and plants against it).
|
|
71
|
+
*
|
|
72
|
+
* **`getOAuthState()` throws where no request state was established**, and here the throw is caught on
|
|
73
|
+
* purpose, unlike the gate's. The gate's safe direction is a loud failure; this one's is *unverified*,
|
|
74
|
+
* and that is exactly what a missing link means.
|
|
75
|
+
*/
|
|
76
|
+
async function linkInProgress() {
|
|
77
|
+
try {
|
|
78
|
+
const link = (await getOAuthState())?.link;
|
|
79
|
+
if (typeof link !== "object" || link === null || !("email" in link)) return null;
|
|
80
|
+
return { email: link.email };
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The Microsoft block's `mapProfileToUser`: the rule, applied to this callback.
|
|
87
|
+
*
|
|
88
|
+
* **It always states the flag.** Better Auth computes its own default first and spreads this over it, so
|
|
89
|
+
* a mapper that omitted the key on the unverified path would leave that default — read off claims a
|
|
90
|
+
* tenant can influence — standing.
|
|
91
|
+
*/
|
|
92
|
+
async function microsoftProfileToUser(profile) {
|
|
93
|
+
return { emailVerified: microsoftEmailVerified(profile, await linkInProgress()) };
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { MICROSOFT_PERSONAL_ACCOUNTS_TENANT_ID, microsoftEmailVerified, microsoftProfileToUser };
|
|
@@ -20,9 +20,10 @@ import type { BetterAuthPlugin } from "better-auth";
|
|
|
20
20
|
* The mitigating fact is that it requires a provider-signed, audience-bound assertion of the address,
|
|
21
21
|
* so it is not an address an attacker picks — which is why the claim above says *the redirect
|
|
22
22
|
* callback* and not *provider sign-in*.
|
|
23
|
-
* - **A plugin that calls `handleOAuthUserInfo`
|
|
24
|
-
*
|
|
25
|
-
*
|
|
23
|
+
* - **A plugin that calls `handleOAuthUserInfo` outside the redirect callback.** `plugins/one-tap/index.mjs:64`
|
|
24
|
+
* resolves a verified Google id token through Google's own `getUserInfo` with no OAuth state, which
|
|
25
|
+
* this gate passes through, or builds the user info itself when Google is not configured;
|
|
26
|
+
* `plugins/oauth-proxy/index.mjs:127` lifts it out of an encrypted proxy payload. The kit composes
|
|
26
27
|
* neither. `./refusalTransport` is where a composed plugin is ruled on.
|
|
27
28
|
* - **An adopter plugin that replaces `ctx.socialProviders` in its own `init`.** Kit plugins run first
|
|
28
29
|
* by contract (`./auth.ts` composes them ahead of the adopter's), so the wrap is installed before
|
|
@@ -42,8 +43,8 @@ import type { BetterAuthPlugin } from "better-auth";
|
|
|
42
43
|
* | 4 | Not attached, no account at that address | Refused |
|
|
43
44
|
*
|
|
44
45
|
* Rows 3 and 4 differ only in whether a row exists at an address **the caller chose**, and Better Auth
|
|
45
|
-
* answers them with two different codes — `account_not_linked` (`oauth2/link-account.mjs:
|
|
46
|
-
* only inside `if (dbUser)`) and `signup_disabled` (`:
|
|
46
|
+
* answers them with two different codes — `account_not_linked` (`oauth2/link-account.mjs:142`, returned
|
|
47
|
+
* only inside `if (dbUser)`) and `signup_disabled` (`:257`, the `else` of that same branch). Both reach
|
|
47
48
|
* the browser as `?error=<code>`. That is an account-enumeration oracle needing one provider account,
|
|
48
49
|
* none on the target, and no page render.
|
|
49
50
|
*
|
|
@@ -53,20 +54,21 @@ import type { BetterAuthPlugin } from "better-auth";
|
|
|
53
54
|
* another, and the last keyed on a value `callbackURL` supplies, so it rewrote *successful* sign-ins.
|
|
54
55
|
* By the time a response exists the fact that distinguished the two cases is gone and what is left is
|
|
55
56
|
* partly the caller's. `getUserInfo` is the last seam that holds the fact itself: `callback.mjs:120`
|
|
56
|
-
* calls it, `:
|
|
57
|
+
* calls it, `:172` calls `handleOAuthUserInfo`, and **nothing between them reads a user row** — so a refusal thrown
|
|
57
58
|
* here means neither of those two lines is ever reached and there is no pair left to collapse.
|
|
58
59
|
*
|
|
59
60
|
* ## Why a wrapper, and not a `getUserInfo` supplied in the config block
|
|
60
61
|
*
|
|
61
62
|
* `options.getUserInfo` short-circuits each provider's own resolver on its first line
|
|
62
|
-
* (`social-providers/google.mjs:118`, `facebook.mjs:93`, `apple.mjs:62`, `github.mjs:65
|
|
63
|
-
* one means reimplementing the profile fetch it replaced —
|
|
64
|
-
* access-token-owner check. `./githubUserInfo.ts` already records
|
|
65
|
-
* resolver silently dropped `name` and `image`, which had to be
|
|
66
|
-
* dependency's own resolver and reads its result, so nothing is
|
|
63
|
+
* (`social-providers/google.mjs:118`, `facebook.mjs:93`, `apple.mjs:62`, `github.mjs:65`,
|
|
64
|
+
* `microsoft-entra-id.mjs:99`), so supplying one means reimplementing the profile fetch it replaced —
|
|
65
|
+
* Google's id-token decode, Facebook's access-token-owner check. `./githubUserInfo.ts` already records
|
|
66
|
+
* what that costs: replacing GitHub's resolver silently dropped `name` and `image`, which had to be
|
|
67
|
+
* re-derived. Wrapping calls the dependency's own resolver and reads its result, so nothing is
|
|
68
|
+
* reimplemented and nothing reconstructed.
|
|
67
69
|
*
|
|
68
70
|
* **And it is the wrapping that makes the predicate read the right flag.** `mapProfileToUser` is applied
|
|
69
|
-
* *inside* each provider's own `getUserInfo` — every one of the
|
|
71
|
+
* *inside* each provider's own `getUserInfo` — every one of the five spreads `...userMap` over the
|
|
70
72
|
* `emailVerified` it computed — so the value this module reads is by construction the **effective** flag.
|
|
71
73
|
* Facebook is the trap that makes this load-bearing: it sits outside `trustedProviders` like GitHub, but
|
|
72
74
|
* `./auth.ts` asserts its address as verified with a documented reason, and its OAuth response carries no
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"providerSignInGate.d.ts","sourceRoot":"","sources":["../../src/instance/providerSignInGate.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,KAAK,EAAe,gBAAgB,EAAiB,MAAM,aAAa,CAAC;AAKhF
|
|
1
|
+
{"version":3,"file":"providerSignInGate.d.ts","sourceRoot":"","sources":["../../src/instance/providerSignInGate.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,KAAK,EAAe,gBAAgB,EAAiB,MAAM,aAAa,CAAC;AAKhF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyEG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,gCAAgC,CAAC;AAEtE;;;;;;GAMG;AACH,eAAO,MAAM,gCAAgC;IAC3C,wGAAwG;aACxG,aAAa,EAAE,kCAAkC;IACjD,8EAA8E;aAC9E,SAAS,EAAE,8BAA8B;CACjC,CAAC;AAEX,2GAA2G;AAC3G,MAAM,WAAW,sBAAsB;IACrC,kGAAkG;IAClG,IAAI,EAAE,SAAS,CAAC;IAChB;;;;;;;;;;;OAWG;IACH,OAAO,EAAE,OAAO,GAAG,SAAS,CAAC;CAC9B;AA0KD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,sBAAsB,GAAG,gBAAgB,CAkBjF"}
|