@terreno/api 57.18.0 → 57.20.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/CHANGELOG.md +2071 -2
- package/dist/accountSecurity/accessActions.d.ts +38 -0
- package/dist/accountSecurity/accessActions.js +157 -0
- package/dist/accountSecurity/accountSecurityApp.d.ts +219 -0
- package/dist/accountSecurity/accountSecurityApp.js +438 -0
- package/dist/accountSecurity/accountSecurityService.d.ts +200 -0
- package/dist/accountSecurity/accountSecurityService.js +481 -0
- package/dist/accountSecurity/credentialActions.d.ts +16 -0
- package/dist/accountSecurity/credentialActions.js +189 -0
- package/dist/accountSecurity/membershipActions.d.ts +15 -0
- package/dist/accountSecurity/membershipActions.js +81 -0
- package/dist/accountSecurity/orgSecurityRoutes.d.ts +50 -0
- package/dist/accountSecurity/orgSecurityRoutes.js +149 -0
- package/dist/accountSecurity/securityEventHooks.d.ts +44 -0
- package/dist/accountSecurity/securityEventHooks.js +223 -0
- package/dist/accountSecurity/securityEvents.d.ts +162 -0
- package/dist/accountSecurity/securityEvents.js +114 -0
- package/dist/accountSecurity/securityNotifications.d.ts +125 -0
- package/dist/accountSecurity/securityNotifications.js +333 -0
- package/dist/api.js +38 -6
- package/dist/audit/auditEventModel.d.ts +5 -3
- package/dist/audit/auditEventModel.js +8 -6
- package/dist/audit/processQueue.js +4 -2
- package/dist/audit/record.d.ts +2 -2
- package/dist/audit/record.js +3 -0
- package/dist/authRecovery.js +29 -1
- package/dist/betterAuth.d.ts +179 -10
- package/dist/betterAuthApp.d.ts +15 -0
- package/dist/betterAuthApp.js +23 -0
- package/dist/betterAuthChallengeHeaders.d.ts +37 -0
- package/dist/betterAuthChallengeHeaders.js +180 -0
- package/dist/betterAuthFeatures.d.ts +54 -0
- package/dist/betterAuthFeatures.js +239 -0
- package/dist/betterAuthMethods.d.ts +73 -0
- package/dist/betterAuthMethods.js +55 -0
- package/dist/betterAuthOAuthHandoff.d.ts +10 -2
- package/dist/betterAuthOAuthHandoff.js +222 -24
- package/dist/betterAuthSetPassword.d.ts +33 -0
- package/dist/betterAuthSetPassword.js +166 -0
- package/dist/betterAuthSetup.d.ts +57 -8
- package/dist/betterAuthSetup.js +327 -36
- package/dist/consentApp.d.ts +9 -4
- package/dist/consentApp.js +344 -28
- package/dist/consentStatus.d.ts +60 -0
- package/dist/consentStatus.js +111 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.js +41 -2
- package/dist/expressServer.js +3 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +23 -3
- package/dist/mcp/oauth.js +151 -11
- package/dist/mcp/oauthMetadata.d.ts +2 -1
- package/dist/mcp/oauthMetadata.js +7 -4
- package/dist/models/consentResponse.js +51 -0
- package/dist/models/mcpOAuthClient.js +78 -2
- package/dist/models/mcpOAuthToken.js +28 -4
- package/dist/orgs/membershipGuards.d.ts +7 -0
- package/dist/orgs/membershipGuards.js +32 -0
- package/dist/orgs/orgsApp.d.ts +19 -1
- package/dist/orgs/orgsApp.js +55 -24
- package/dist/rateLimit/policies.js +8 -0
- package/dist/rbac/access.js +2 -1
- package/dist/rbac/resolve.d.ts +9 -1
- package/dist/rbac/resolve.js +21 -16
- package/dist/rbac/roleManager.d.ts +6 -0
- package/dist/rbac/roleManager.js +8 -7
- package/dist/rbac/roleModel.d.ts +1 -0
- package/dist/rbac/roleModel.js +45 -3
- package/dist/rbac/statements.d.ts +2 -2
- package/dist/rbac/statements.js +10 -1
- package/dist/rbac/types.d.ts +6 -0
- package/dist/rbac/userWriteGuards.js +2 -1
- package/dist/schemaMetadata.js +8 -1
- package/dist/seedRunner.js +7 -2
- package/dist/terrenoApp.d.ts +4 -0
- package/dist/terrenoApp.js +15 -0
- package/dist/tests/models.d.ts +5 -0
- package/dist/tests/models.js +8 -0
- package/dist/types/consentResponse.d.ts +8 -0
- package/dist/types/mcpOAuth.d.ts +35 -2
- package/package.json +6 -3
- package/src/__snapshots__/openApi.test.ts.snap +288 -0
- package/src/__snapshots__/openApiBuilder.test.ts.snap +96 -0
- package/src/accountSecurity/accessActions.test.ts +723 -0
- package/src/accountSecurity/accessActions.ts +173 -0
- package/src/accountSecurity/accountSecurity.test.ts +947 -0
- package/src/accountSecurity/accountSecurityApp.ts +720 -0
- package/src/accountSecurity/accountSecurityService.ts +826 -0
- package/src/accountSecurity/credentialActions.test.ts +867 -0
- package/src/accountSecurity/credentialActions.ts +260 -0
- package/src/accountSecurity/membershipActions.ts +89 -0
- package/src/accountSecurity/orgSecurityRoutes.test.ts +841 -0
- package/src/accountSecurity/orgSecurityRoutes.ts +232 -0
- package/src/accountSecurity/securityEventHooks.ts +294 -0
- package/src/accountSecurity/securityEvents.ts +165 -0
- package/src/accountSecurity/securityNotifications.test.ts +1323 -0
- package/src/accountSecurity/securityNotifications.ts +516 -0
- package/src/api.arrayOperations.test.ts +71 -0
- package/src/api.ts +38 -7
- package/src/audit/auditEventModel.ts +10 -7
- package/src/audit/processQueue.ts +4 -2
- package/src/audit/record.test.ts +15 -0
- package/src/audit/record.ts +10 -2
- package/src/authRecovery.test.ts +23 -0
- package/src/authRecovery.ts +34 -1
- package/src/authRecoveryBetterAuth.test.ts +169 -0
- package/src/betterAuth.ts +194 -8
- package/src/betterAuthApp.ts +32 -0
- package/src/betterAuthChallengeHeaders.test.ts +476 -0
- package/src/betterAuthChallengeHeaders.ts +227 -0
- package/src/betterAuthFeatures.test.ts +489 -0
- package/src/betterAuthFeatures.ts +283 -0
- package/src/betterAuthLinking.test.ts +844 -0
- package/src/betterAuthMethods.test.ts +138 -0
- package/src/betterAuthMethods.ts +98 -0
- package/src/betterAuthOAuthHandoff.test.ts +482 -69
- package/src/betterAuthOAuthHandoff.ts +293 -33
- package/src/betterAuthOAuthProxy.test.ts +421 -0
- package/src/betterAuthPasswordReset.test.ts +190 -0
- package/src/betterAuthSetPassword.test.ts +378 -0
- package/src/betterAuthSetPassword.ts +213 -0
- package/src/betterAuthSetup.test.ts +39 -7
- package/src/betterAuthSetup.ts +457 -38
- package/src/consentApp.test.ts +869 -2
- package/src/consentApp.ts +438 -36
- package/src/consentStatus.test.ts +226 -0
- package/src/consentStatus.ts +171 -0
- package/src/errors.test.ts +81 -0
- package/src/errors.ts +50 -1
- package/src/expressServer.test.ts +29 -0
- package/src/expressServer.ts +3 -2
- package/src/index.ts +30 -0
- package/src/mcp/oauth.test.ts +463 -2
- package/src/mcp/oauth.ts +184 -13
- package/src/mcp/oauthMetadata.ts +5 -2
- package/src/models/consentResponse.ts +53 -0
- package/src/models/mcpOAuthClient.ts +100 -3
- package/src/models/mcpOAuthToken.ts +30 -5
- package/src/orgs/membershipGuards.ts +39 -0
- package/src/orgs/orgRbac.test.ts +1 -1
- package/src/orgs/orgsApp.test.ts +101 -2
- package/src/orgs/orgsApp.ts +69 -28
- package/src/rateLimit/policies.test.ts +53 -0
- package/src/rateLimit/policies.ts +8 -0
- package/src/rbac/access.ts +4 -1
- package/src/rbac/resolve.test.ts +65 -0
- package/src/rbac/resolve.ts +37 -17
- package/src/rbac/roleManager.test.ts +64 -0
- package/src/rbac/roleManager.ts +15 -6
- package/src/rbac/roleModel.test.ts +174 -1
- package/src/rbac/roleModel.ts +66 -3
- package/src/rbac/routes.test.ts +58 -0
- package/src/rbac/statements.test.ts +4 -1
- package/src/rbac/statements.ts +11 -1
- package/src/rbac/types.ts +6 -1
- package/src/rbac/userWriteGuards.test.ts +48 -0
- package/src/rbac/userWriteGuards.ts +5 -2
- package/src/schemaMetadata.test.ts +39 -0
- package/src/schemaMetadata.ts +11 -1
- package/src/seedRunner.test.ts +56 -0
- package/src/seedRunner.ts +7 -2
- package/src/terrenoApp.ts +20 -0
- package/src/tests/accountSecurityTestApp.ts +290 -0
- package/src/tests/betterAuthStubs.ts +188 -0
- package/src/tests/mockOidcProvider.ts +105 -0
- package/src/tests/models.ts +13 -0
- package/src/tests/softwareAuthenticator.ts +183 -0
- package/src/types/consentResponse.ts +8 -0
- package/src/types/mcpOAuth.ts +39 -3
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Maps the auth feature fields of `BetterAuthConfig` (two-factor, passkeys, sign-up mode,
|
|
4
|
+
* account linking, and the rest) to Better Auth plugins and options, so consumers switch
|
|
5
|
+
* features on in config instead of assembling Better Auth plugins by hand.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.resolveAppleProvider = exports.createAppleClientSecret = exports.createLinkingDisabledPlugin = exports.createSignUpClosedPlugin = exports.isSignUpClosed = exports.buildFeatureOptions = exports.buildFeaturePlugins = void 0;
|
|
9
|
+
const node_crypto_1 = require("node:crypto");
|
|
10
|
+
// Before the passkey plugin: its @peculiar/x509 dependency loads tsyringe, which needs the
|
|
11
|
+
// Reflect polyfill, and `bun build --compile` can run tsyringe ahead of x509's own import.
|
|
12
|
+
require("reflect-metadata");
|
|
13
|
+
const expo_1 = require("@better-auth/expo");
|
|
14
|
+
const passkey_1 = require("@better-auth/passkey");
|
|
15
|
+
const api_1 = require("better-auth/api");
|
|
16
|
+
const plugins_1 = require("better-auth/plugins");
|
|
17
|
+
const luxon_1 = require("luxon");
|
|
18
|
+
const betterAuthChallengeHeaders_1 = require("./betterAuthChallengeHeaders");
|
|
19
|
+
const errors_1 = require("./errors");
|
|
20
|
+
const SECONDS_PER_DAY = 24 * 60 * 60;
|
|
21
|
+
/** Apple accepts client secrets that expire at most six months after they are issued. */
|
|
22
|
+
const APPLE_CLIENT_SECRET_DAYS = 180;
|
|
23
|
+
const APPLE_AUDIENCE = "https://appleid.apple.com";
|
|
24
|
+
const invalidPasskeyConfig = (detail) => new errors_1.APIError({ detail, status: 500, title: "passkey config is invalid" });
|
|
25
|
+
/**
|
|
26
|
+
* Refuses a passkey config whose origins a browser could never use with `rpID`, so the mistake
|
|
27
|
+
* shows at startup rather than as a failed WebAuthn ceremony.
|
|
28
|
+
*/
|
|
29
|
+
const assertPasskeyOrigins = (origins, rpID) => {
|
|
30
|
+
if (origins.length === 0) {
|
|
31
|
+
throw invalidPasskeyConfig("passkey.origin must list at least one origin");
|
|
32
|
+
}
|
|
33
|
+
for (const entry of origins) {
|
|
34
|
+
let url;
|
|
35
|
+
try {
|
|
36
|
+
url = new URL(entry);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
throw invalidPasskeyConfig(`passkey.origin "${entry}" is not an absolute URL`);
|
|
40
|
+
}
|
|
41
|
+
if ((url.protocol !== "https:" && url.protocol !== "http:") || url.origin !== entry) {
|
|
42
|
+
throw invalidPasskeyConfig(`passkey.origin "${entry}" must be a bare http(s) origin such as "https://app.example.com"`);
|
|
43
|
+
}
|
|
44
|
+
if (url.hostname !== rpID && !url.hostname.endsWith(`.${rpID}`)) {
|
|
45
|
+
throw invalidPasskeyConfig(`passkey.origin "${entry}" is not on passkey.rpID "${rpID}" or one of its subdomains`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
// The origin of an absolute URL, or undefined when it does not parse.
|
|
50
|
+
const originOf = (url) => {
|
|
51
|
+
if (!url) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
return new URL(url).origin;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Better Auth's `oAuthProxy` for `config.oauthProxy`. On the production server itself
|
|
63
|
+
* (`productionURL` on the same origin as `baseURL`), `currentURL` is set to `productionURL`, so
|
|
64
|
+
* every sign-in it starts skips the proxy: a server-side start (such as the OAuth handoff) has no
|
|
65
|
+
* request URL to compare, and clients may reach it on another host (a Cloud Run tag URL). Its
|
|
66
|
+
* own sign-ins then keep Better Auth's state-cookie check, while its callback still forwards
|
|
67
|
+
* previews' returns.
|
|
68
|
+
*/
|
|
69
|
+
const buildOAuthProxyPlugin = (proxy, baseURL) => {
|
|
70
|
+
const isProductionServer = originOf(baseURL) !== undefined && originOf(baseURL) === originOf(proxy.productionURL);
|
|
71
|
+
return (0, plugins_1.oAuthProxy)({
|
|
72
|
+
productionURL: proxy.productionURL,
|
|
73
|
+
...(isProductionServer ? { currentURL: proxy.productionURL } : {}),
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Builds the Better Auth plugins for every configured auth feature, in a fixed order. Features
|
|
78
|
+
* are opt-in: a config with none of the fields returns an empty list. `baseURL` is the resolved
|
|
79
|
+
* server URL (it defaults to `config.baseURL`), which `oauthProxy` compares with `productionURL`.
|
|
80
|
+
*/
|
|
81
|
+
const buildFeaturePlugins = (config, baseURL = config.baseURL) => {
|
|
82
|
+
const plugins = [];
|
|
83
|
+
if (config.twoFactor) {
|
|
84
|
+
const { backupCodes, issuer, trustDeviceDays } = config.twoFactor;
|
|
85
|
+
plugins.push((0, plugins_1.twoFactor)({
|
|
86
|
+
backupCodeOptions: backupCodes,
|
|
87
|
+
issuer,
|
|
88
|
+
trustDeviceMaxAge: trustDeviceDays === undefined ? undefined : trustDeviceDays * SECONDS_PER_DAY,
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
if (config.passkey) {
|
|
92
|
+
const { origin, rpID, rpName } = config.passkey;
|
|
93
|
+
assertPasskeyOrigins(origin, rpID);
|
|
94
|
+
plugins.push((0, passkey_1.passkey)({ origin, rpID, rpName }));
|
|
95
|
+
}
|
|
96
|
+
if (config.twoFactor || config.passkey) {
|
|
97
|
+
// After both, so its after hook sees the challenge cookies their hooks set.
|
|
98
|
+
plugins.push((0, betterAuthChallengeHeaders_1.createChallengeHeadersPlugin)());
|
|
99
|
+
}
|
|
100
|
+
if (config.lastLoginMethod) {
|
|
101
|
+
plugins.push((0, plugins_1.lastLoginMethod)({ storeInDatabase: true }));
|
|
102
|
+
}
|
|
103
|
+
if (config.multiSession) {
|
|
104
|
+
// Better Auth spreads options over its defaults, so an explicit undefined would erase its cap.
|
|
105
|
+
const { maximumSessions } = config.multiSession;
|
|
106
|
+
plugins.push((0, plugins_1.multiSession)(maximumSessions === undefined ? {} : { maximumSessions }));
|
|
107
|
+
}
|
|
108
|
+
if (config.oauthProxy) {
|
|
109
|
+
plugins.push(buildOAuthProxyPlugin(config.oauthProxy, baseURL));
|
|
110
|
+
}
|
|
111
|
+
if (config.nativeApps) {
|
|
112
|
+
plugins.push((0, expo_1.expo)());
|
|
113
|
+
}
|
|
114
|
+
return plugins;
|
|
115
|
+
};
|
|
116
|
+
exports.buildFeaturePlugins = buildFeaturePlugins;
|
|
117
|
+
/**
|
|
118
|
+
* Builds the Better Auth options the feature fields set: the account linking policy and closed
|
|
119
|
+
* email sign-up. `config.options` is merged over the result, so an explicit option still wins.
|
|
120
|
+
*/
|
|
121
|
+
const buildFeatureOptions = (config) => {
|
|
122
|
+
const options = {};
|
|
123
|
+
if (config.accountLinking) {
|
|
124
|
+
const { mode, trustedProviders } = config.accountLinking;
|
|
125
|
+
options.account = {
|
|
126
|
+
accountLinking: {
|
|
127
|
+
disableImplicitLinking: mode === "explicit",
|
|
128
|
+
enabled: mode !== "never",
|
|
129
|
+
// Linking by email must never trust an unverified local account (takeover by
|
|
130
|
+
// pre-registration), whatever the mode.
|
|
131
|
+
requireLocalEmailVerified: true,
|
|
132
|
+
...(trustedProviders ? { trustedProviders } : {}),
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if ((0, exports.isSignUpClosed)(config)) {
|
|
137
|
+
options.emailAndPassword = { disableSignUp: true };
|
|
138
|
+
}
|
|
139
|
+
return options;
|
|
140
|
+
};
|
|
141
|
+
exports.buildFeatureOptions = buildFeatureOptions;
|
|
142
|
+
/** Whether `signUp.mode` is `closed`. */
|
|
143
|
+
const isSignUpClosed = (config) => config.signUp?.mode === "closed";
|
|
144
|
+
exports.isSignUpClosed = isSignUpClosed;
|
|
145
|
+
/**
|
|
146
|
+
* The plugin `createBetterAuth` adds after `config.plugins` when `signUp.mode` is `closed`.
|
|
147
|
+
* Its init runs after every other plugin's, so it reaches the social providers Better Auth
|
|
148
|
+
* built from `socialProviders` and the ones `genericOAuth` registered, and marks each one so a
|
|
149
|
+
* sign-in for an unknown email fails with `signup_disabled`. It sets the top-level
|
|
150
|
+
* `disableSignUp` (read by the ID-token sign-in path), `options.disableSignUp` (read by the
|
|
151
|
+
* OAuth callback and the OAuth proxy), and `disableImplicitSignUp`; a client's
|
|
152
|
+
* `requestSignUp: true` cannot undo `disableSignUp`.
|
|
153
|
+
*/
|
|
154
|
+
const createSignUpClosedPlugin = () => {
|
|
155
|
+
return {
|
|
156
|
+
id: "terreno-sign-up-closed",
|
|
157
|
+
init: (ctx) => {
|
|
158
|
+
for (const provider of ctx.socialProviders) {
|
|
159
|
+
provider.disableImplicitSignUp = true;
|
|
160
|
+
provider.disableSignUp = true;
|
|
161
|
+
provider.options = { ...provider.options, disableSignUp: true };
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
exports.createSignUpClosedPlugin = createSignUpClosedPlugin;
|
|
167
|
+
/**
|
|
168
|
+
* The plugin `createBetterAuth` adds when linking is off (`accountLinking: {mode: "never"}`, or
|
|
169
|
+
* `options.account.accountLinking.enabled: false`). Better Auth itself only refuses such a link
|
|
170
|
+
* when the provider calls back; this refuses `POST {basePath}/link-social` up front with 403
|
|
171
|
+
* `LINKING_DISABLED`, so a client never sends the user to the provider for nothing.
|
|
172
|
+
*/
|
|
173
|
+
const createLinkingDisabledPlugin = () => {
|
|
174
|
+
return {
|
|
175
|
+
hooks: {
|
|
176
|
+
before: [
|
|
177
|
+
{
|
|
178
|
+
handler: (0, api_1.createAuthMiddleware)(async () => {
|
|
179
|
+
throw new api_1.APIError("FORBIDDEN", {
|
|
180
|
+
code: "LINKING_DISABLED",
|
|
181
|
+
message: "Account linking is turned off",
|
|
182
|
+
});
|
|
183
|
+
}),
|
|
184
|
+
matcher: (context) => context.path === "/link-social",
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
},
|
|
188
|
+
id: "terreno-linking-disabled",
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
exports.createLinkingDisabledPlugin = createLinkingDisabledPlugin;
|
|
192
|
+
const base64Url = (value) => Buffer.from(value).toString("base64url");
|
|
193
|
+
const loadApplePrivateKey = (privateKey) => {
|
|
194
|
+
try {
|
|
195
|
+
return (0, node_crypto_1.createPrivateKey)(privateKey.replace(/\\n/g, "\n"));
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
throw new errors_1.APIError({
|
|
199
|
+
cause: error,
|
|
200
|
+
status: 500,
|
|
201
|
+
title: "appleOAuth.privateKey must be a PEM (PKCS#8) Sign in with Apple key",
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Signs the ES256 client-secret JWT Sign in with Apple expects, valid for 180 days from now.
|
|
207
|
+
* Uses `node:crypto` because `createBetterAuth` is synchronous.
|
|
208
|
+
*/
|
|
209
|
+
const createAppleClientSecret = (key) => {
|
|
210
|
+
const signingKey = loadApplePrivateKey(key.privateKey);
|
|
211
|
+
const issuedAt = Math.floor(luxon_1.DateTime.now().toSeconds());
|
|
212
|
+
const header = { alg: "ES256", kid: key.keyId };
|
|
213
|
+
const payload = {
|
|
214
|
+
aud: APPLE_AUDIENCE,
|
|
215
|
+
exp: issuedAt + APPLE_CLIENT_SECRET_DAYS * SECONDS_PER_DAY,
|
|
216
|
+
iat: issuedAt,
|
|
217
|
+
iss: key.teamId,
|
|
218
|
+
sub: key.clientId,
|
|
219
|
+
};
|
|
220
|
+
const signingInput = `${base64Url(JSON.stringify(header))}.${base64Url(JSON.stringify(payload))}`;
|
|
221
|
+
const signature = (0, node_crypto_1.createSign)("SHA256")
|
|
222
|
+
.update(signingInput)
|
|
223
|
+
.sign({ dsaEncoding: "ieee-p1363", key: signingKey });
|
|
224
|
+
return `${signingInput}.${base64Url(signature)}`;
|
|
225
|
+
};
|
|
226
|
+
exports.createAppleClientSecret = createAppleClientSecret;
|
|
227
|
+
/**
|
|
228
|
+
* Resolves `appleOAuth` to a Better Auth provider config, signing the client secret when the
|
|
229
|
+
* key form is used.
|
|
230
|
+
*/
|
|
231
|
+
const resolveAppleProvider = (apple) => {
|
|
232
|
+
const clientSecret = "clientSecret" in apple ? apple.clientSecret : (0, exports.createAppleClientSecret)(apple);
|
|
233
|
+
return {
|
|
234
|
+
clientId: apple.clientId,
|
|
235
|
+
clientSecret,
|
|
236
|
+
...(apple.appBundleIdentifier ? { appBundleIdentifier: apple.appBundleIdentifier } : {}),
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
exports.resolveAppleProvider = resolveAppleProvider;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign-in methods discovery for Better Auth.
|
|
3
|
+
*
|
|
4
|
+
* Login screens call `GET {basePath}/terreno/methods` before anyone signs in, so they render
|
|
5
|
+
* only the methods this server has configured. The answer is read from Better Auth's final
|
|
6
|
+
* context on each request, which already includes `config.options` overrides and the providers
|
|
7
|
+
* that plugins such as `genericOAuth` register.
|
|
8
|
+
*/
|
|
9
|
+
import type { BetterAuthPlugin } from "better-auth";
|
|
10
|
+
/**
|
|
11
|
+
* How a sign-in with a new method relates to an existing account with the same email.
|
|
12
|
+
* - `verified-email`: Better Auth links it when the provider reports a verified email (the
|
|
13
|
+
* Better Auth default).
|
|
14
|
+
* - `explicit`: no implicit linking at sign-in; a signed-in user links the method themselves.
|
|
15
|
+
* - `never`: account linking is off.
|
|
16
|
+
*/
|
|
17
|
+
export type BetterAuthAccountLinkingMode = "verified-email" | "explicit" | "never";
|
|
18
|
+
/**
|
|
19
|
+
* The body of `GET {basePath}/terreno/methods`.
|
|
20
|
+
*/
|
|
21
|
+
export interface BetterAuthMethods {
|
|
22
|
+
/** Linking behavior for same-email sign-ins with another method. */
|
|
23
|
+
accountLinking: {
|
|
24
|
+
mode: BetterAuthAccountLinkingMode;
|
|
25
|
+
};
|
|
26
|
+
/** Email and password sign-in is enabled. */
|
|
27
|
+
emailPassword: boolean;
|
|
28
|
+
/** Several signed-in accounts per device (Better Auth's `multiSession` plugin is on). */
|
|
29
|
+
multiSession: boolean;
|
|
30
|
+
/** Passkey sign-in (the `passkey` plugin is on). */
|
|
31
|
+
passkey: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Ids of every OAuth provider that can start a sign-in, sorted: social providers (`google`,
|
|
34
|
+
* `github`, `apple`) and generic OAuth providers (such as `mock-oidc`). A generic provider
|
|
35
|
+
* whose discovery failed at startup is left out.
|
|
36
|
+
*/
|
|
37
|
+
providers: string[];
|
|
38
|
+
/** New users can sign up with email and password (`false` under `signUp: {mode: "closed"}`). */
|
|
39
|
+
signUp: boolean;
|
|
40
|
+
/** Two-factor authentication (Better Auth's `twoFactor` plugin is on). */
|
|
41
|
+
twoFactor: boolean;
|
|
42
|
+
}
|
|
43
|
+
/** The parts of Better Auth's endpoint context the methods endpoint reads. */
|
|
44
|
+
interface MethodsContext {
|
|
45
|
+
options: {
|
|
46
|
+
account?: {
|
|
47
|
+
accountLinking?: {
|
|
48
|
+
disableImplicitLinking?: boolean;
|
|
49
|
+
enabled?: boolean;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
emailAndPassword?: {
|
|
53
|
+
disableSignUp?: boolean;
|
|
54
|
+
enabled?: boolean;
|
|
55
|
+
};
|
|
56
|
+
plugins?: {
|
|
57
|
+
id: string;
|
|
58
|
+
}[];
|
|
59
|
+
};
|
|
60
|
+
socialProviders: {
|
|
61
|
+
id: string;
|
|
62
|
+
}[];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Reads the enabled sign-in methods from a Better Auth context.
|
|
66
|
+
*/
|
|
67
|
+
export declare const readBetterAuthMethods: (context: MethodsContext) => BetterAuthMethods;
|
|
68
|
+
/**
|
|
69
|
+
* The Better Auth plugin `createBetterAuth` always adds for `GET {basePath}/terreno/methods`.
|
|
70
|
+
* The endpoint is public: it needs no session and reveals only what the login screen shows.
|
|
71
|
+
*/
|
|
72
|
+
export declare const createMethodsPlugin: () => BetterAuthPlugin;
|
|
73
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Sign-in methods discovery for Better Auth.
|
|
4
|
+
*
|
|
5
|
+
* Login screens call `GET {basePath}/terreno/methods` before anyone signs in, so they render
|
|
6
|
+
* only the methods this server has configured. The answer is read from Better Auth's final
|
|
7
|
+
* context on each request, which already includes `config.options` overrides and the providers
|
|
8
|
+
* that plugins such as `genericOAuth` register.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.createMethodsPlugin = exports.readBetterAuthMethods = void 0;
|
|
12
|
+
const api_1 = require("better-auth/api");
|
|
13
|
+
const accountLinkingMode = (options) => {
|
|
14
|
+
const accountLinking = options.account?.accountLinking;
|
|
15
|
+
if (accountLinking?.enabled === false) {
|
|
16
|
+
return "never";
|
|
17
|
+
}
|
|
18
|
+
if (accountLinking?.disableImplicitLinking === true) {
|
|
19
|
+
return "explicit";
|
|
20
|
+
}
|
|
21
|
+
return "verified-email";
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Reads the enabled sign-in methods from a Better Auth context.
|
|
25
|
+
*/
|
|
26
|
+
const readBetterAuthMethods = (context) => {
|
|
27
|
+
const { options, socialProviders } = context;
|
|
28
|
+
const emailPassword = options.emailAndPassword?.enabled === true;
|
|
29
|
+
// Read from the final plugin list, so a plugin added through `config.plugins` counts too.
|
|
30
|
+
const pluginIds = new Set((options.plugins ?? []).map((plugin) => plugin.id));
|
|
31
|
+
return {
|
|
32
|
+
accountLinking: { mode: accountLinkingMode(options) },
|
|
33
|
+
emailPassword,
|
|
34
|
+
multiSession: pluginIds.has("multi-session"),
|
|
35
|
+
passkey: pluginIds.has("passkey"),
|
|
36
|
+
providers: [...new Set(socialProviders.map((provider) => provider.id))].sort(),
|
|
37
|
+
signUp: emailPassword && options.emailAndPassword?.disableSignUp !== true,
|
|
38
|
+
twoFactor: pluginIds.has("two-factor"),
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
exports.readBetterAuthMethods = readBetterAuthMethods;
|
|
42
|
+
/**
|
|
43
|
+
* The Better Auth plugin `createBetterAuth` always adds for `GET {basePath}/terreno/methods`.
|
|
44
|
+
* The endpoint is public: it needs no session and reveals only what the login screen shows.
|
|
45
|
+
*/
|
|
46
|
+
const createMethodsPlugin = () => {
|
|
47
|
+
const terrenoMethods = (0, api_1.createAuthEndpoint)("/terreno/methods", { method: "GET" }, async (ctx) => {
|
|
48
|
+
return ctx.json((0, exports.readBetterAuthMethods)(ctx.context));
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
endpoints: { terrenoMethods },
|
|
52
|
+
id: "terreno-methods",
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
exports.createMethodsPlugin = createMethodsPlugin;
|
|
@@ -4,8 +4,14 @@
|
|
|
4
4
|
* A social sign-in ends with a top-level redirect from the API to the web app, so the web app
|
|
5
5
|
* never sees the `set-auth-token` header. When the OAuth callback creates a session and
|
|
6
6
|
* redirects to a configured web origin, this adds a single-use, short-lived token to the
|
|
7
|
-
* redirect's URL fragment
|
|
7
|
+
* redirect's URL fragment (also when oauth-proxy finishes a preview's sign-in, since the preview
|
|
8
|
+
* creates the session on its own completion endpoint), which the web app exchanges for the bearer at
|
|
8
9
|
* `POST {basePath}/one-time-token/verify`. Fragments are not sent to servers or in `Referer`.
|
|
10
|
+
*
|
|
11
|
+
* Linking a provider to a signed-in bearer user works the same way in reverse: the web app
|
|
12
|
+
* mints a single-use link token with its bearer (`POST {basePath}/oauth-handoff/link-intent`),
|
|
13
|
+
* then posts it from a top-level form to `POST {basePath}/oauth-handoff/link-start`, which
|
|
14
|
+
* starts Better Auth's link flow with the state cookie first-party on the API's origin.
|
|
9
15
|
*/
|
|
10
16
|
import type { BetterAuthPlugin } from "better-auth";
|
|
11
17
|
import type { BetterAuthOAuthHandoffConfig } from "./betterAuth";
|
|
@@ -15,10 +21,12 @@ export interface CreateOAuthHandoffPluginsOptions {
|
|
|
15
21
|
config: BetterAuthOAuthHandoffConfig;
|
|
16
22
|
/** Returns the Better Auth instance, which exists only after these plugins are built. */
|
|
17
23
|
getAuth: () => unknown;
|
|
24
|
+
/** Whether account linking is off, so the link intent refuses with 403. */
|
|
25
|
+
isLinkingDisabled?: boolean;
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
20
28
|
* Builds the plugins `createBetterAuth` adds for `oauthHandoff`: Better Auth's `oneTimeToken`
|
|
21
29
|
* (server-minted only, hashed at rest, no `set-ott` header) and the callback after-hook that
|
|
22
30
|
* puts a token in the redirect fragment for configured web origins.
|
|
23
31
|
*/
|
|
24
|
-
export declare const createOAuthHandoffPlugins: ({ apiOrigin, config, getAuth, }: CreateOAuthHandoffPluginsOptions) => BetterAuthPlugin[];
|
|
32
|
+
export declare const createOAuthHandoffPlugins: ({ apiOrigin, config, getAuth, isLinkingDisabled, }: CreateOAuthHandoffPluginsOptions) => BetterAuthPlugin[];
|