@lowdefy/api 0.0.0-experimental-20260720072521 → 0.0.0-experimental-20260720135014
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/context/createApiContext.js +1 -9
- package/dist/context/createAuthorize.js +8 -19
- package/dist/context/createEvaluateOperators.js +1 -2
- package/dist/index.js +3 -13
- package/dist/routes/agent/callAgent.js +180 -7
- package/dist/routes/{websocket/authorizeWebsocket.js → auth/callbacks/addUserFieldsToSession.js} +11 -10
- package/dist/routes/auth/callbacks/addUserFieldsToToken.js +30 -0
- package/dist/routes/auth/{hooks/composeAfterSlot.js → callbacks/createCallbackPlugins.js} +6 -11
- package/dist/routes/auth/callbacks/createCallbacks.js +46 -0
- package/dist/routes/auth/callbacks/createJWTCallback.js +77 -0
- package/dist/routes/auth/callbacks/createRedirectCallback.js +41 -0
- package/dist/routes/auth/callbacks/createSessionCallback.js +81 -0
- package/dist/routes/auth/callbacks/createSignInCallback.js +40 -0
- package/dist/routes/auth/callbacks/validateSessionRoles.js +33 -0
- package/dist/routes/{websocket/getWebsocketConfig.js → auth/createAdapter.js} +8 -14
- package/dist/routes/auth/createLogger.js +38 -0
- package/dist/routes/auth/createProviders.js +25 -0
- package/dist/routes/auth/events/createCreateUserEvent.js +41 -0
- package/dist/routes/auth/{organizations/getHookRequestHeaders.js → events/createEventPlugins.js} +6 -9
- package/dist/routes/auth/events/createEvents.js +56 -0
- package/dist/routes/auth/events/createLinkAccountEvent.js +43 -0
- package/dist/routes/auth/{strategies/getAuthStrategies.js → events/createSessionEvent.js} +17 -14
- package/dist/routes/auth/events/createSignInEvent.js +46 -0
- package/dist/routes/auth/events/createSignOutEvent.js +43 -0
- package/dist/routes/auth/events/createUpdateUserEvent.js +41 -0
- package/dist/routes/auth/getNextAuthConfig.js +75 -0
- package/dist/routes/endpoints/authorizeApiEndpoint.js +2 -8
- package/dist/routes/endpoints/callEndpoint.js +0 -20
- package/dist/routes/endpoints/control/controlReject.js +1 -3
- package/dist/routes/endpoints/control/controlThrow.js +1 -4
- package/dist/routes/endpoints/handleEndpointCall.js +1 -62
- package/dist/routes/endpoints/handleRequest.js +1 -8
- package/dist/routes/endpoints/handleValidateSchema.js +1 -3
- package/dist/routes/endpoints/runRoutine.js +0 -18
- package/dist/routes/endpoints/test/runTest.js +4 -3
- package/dist/routes/page/getPageConfig.js +8 -42
- package/dist/routes/request/authorizeRequest.js +2 -8
- package/dist/routes/request/callRequest.js +1 -8
- package/dist/routes/request/callRequestResolver.js +2 -6
- package/dist/test/testContext.js +6 -6
- package/package.json +10 -16
- package/dist/context/applySystemTrust.js +0 -33
- package/dist/context/createSystemContext.js +0 -72
- package/dist/context/normalizeInjectedCaller.js +0 -41
- package/dist/context/resolveAuthentication.js +0 -107
- package/dist/context/resolveStrategyCaller.js +0 -56
- package/dist/email/renderAuthEmail.js +0 -98
- package/dist/email/resolveThemeLogo.js +0 -35
- package/dist/routes/agent/prepareAgent.js +0 -169
- package/dist/routes/auth/buildAdminPlugin.js +0 -55
- package/dist/routes/auth/buildCaptchaPlugin.js +0 -61
- package/dist/routes/auth/buildPhoneNumberPlugin.js +0 -57
- package/dist/routes/auth/buildProviders.js +0 -51
- package/dist/routes/auth/createAuthLogger.js +0 -31
- package/dist/routes/auth/createSendEmail.js +0 -49
- package/dist/routes/auth/getBetterAuth.js +0 -72
- package/dist/routes/auth/getBetterAuthConfig.js +0 -439
- package/dist/routes/auth/getUserAdminRole.js +0 -32
- package/dist/routes/auth/hooks/authHookPoints.js +0 -192
- package/dist/routes/auth/hooks/buildEngineHooks.js +0 -43
- package/dist/routes/auth/hooks/buildHooks.js +0 -111
- package/dist/routes/auth/hooks/composeBeforeSlot.js +0 -41
- package/dist/routes/auth/hooks/createHookDispatch.js +0 -59
- package/dist/routes/auth/hooks/createUserAfterHook.js +0 -33
- package/dist/routes/auth/hooks/createUserBeforeHook.js +0 -47
- package/dist/routes/auth/hooks/findHookUser.js +0 -27
- package/dist/routes/auth/modelNames.js +0 -29
- package/dist/routes/auth/organizations/buildOrganizationPlugin.js +0 -136
- package/dist/routes/auth/organizations/createActiveOrgPolicyHook.js +0 -211
- package/dist/routes/auth/organizations/createAfterAcceptInvitationHook.js +0 -75
- package/dist/routes/auth/organizations/createAutoJoinHook.js +0 -63
- package/dist/routes/auth/organizations/ensureOrganization.js +0 -95
- package/dist/routes/auth/organizations/findPendingInvitation.js +0 -43
- package/dist/routes/auth/organizations/getOrganizationBinding.js +0 -65
- package/dist/routes/auth/organizations/resolvePinnedOrganization.js +0 -48
- package/dist/routes/auth/organizations/syncUserAdminRole.js +0 -102
- package/dist/routes/auth/resolveCookiePrefix.js +0 -30
- package/dist/routes/auth/strategies/createAuthStrategies.js +0 -77
- package/dist/routes/endpoints/handleAgentCall.js +0 -83
- package/dist/routes/endpoints/handleAuthStep.js +0 -124
- package/dist/routes/endpoints/handleRenderNotification.js +0 -189
- package/dist/routes/endpoints/runDetachedEndpoint.js +0 -85
- package/dist/routes/endpoints/runScheduledEndpoint.js +0 -102
- package/dist/routes/endpoints/runWebhookEndpoint.js +0 -118
- package/dist/routes/endpoints/runWebhookVerify.js +0 -94
- package/dist/routes/endpoints/scheduleBackground.js +0 -48
- package/dist/routes/notifications/derivePreview.js +0 -30
- package/dist/routes/notifications/getNotificationConfig.js +0 -32
- package/dist/routes/notifications/resolveNotificationLinks.js +0 -72
- package/dist/routes/page/dynamic/resolveDynamicContent.js +0 -172
- package/dist/routes/page/dynamic/unescapeOperators.js +0 -36
- package/dist/routes/page/dynamic/validateFragment.js +0 -104
- package/dist/routes/request/resolveTenant.js +0 -57
- package/dist/routes/websocket/createChannelRegistry.js +0 -278
- package/dist/routes/websocket/createWebSocketConnection.js +0 -131
- package/dist/routes/websocket/getWebsocketResolver.js +0 -33
- package/dist/routes/websocket/prepareChannel.js +0 -84
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import { betterAuth } from 'better-auth';
|
|
16
|
-
import ensureOrganization from './organizations/ensureOrganization.js';
|
|
17
|
-
import getBetterAuthConfig from './getBetterAuthConfig.js';
|
|
18
|
-
import { registerOrganizationBinding } from './organizations/getOrganizationBinding.js';
|
|
19
|
-
import { registerUserAdminRole } from './getUserAdminRole.js';
|
|
20
|
-
let instance;
|
|
21
|
-
// The BetterAuth instance is constructed once per process at first use -
|
|
22
|
-
// it handles /api/auth/* and resolves sessions for every request.
|
|
23
|
-
// The engine-tier hooks receive a getAuth accessor instead of the instance
|
|
24
|
-
// itself - they fire only after construction completes, so the lazy
|
|
25
|
-
// reference is always resolved by then.
|
|
26
|
-
function getBetterAuth({ appMeta, authJson, config, createSystemContext, dev, logger, plugins, secrets }) {
|
|
27
|
-
if (instance) return instance;
|
|
28
|
-
instance = betterAuth(getBetterAuthConfig({
|
|
29
|
-
appMeta,
|
|
30
|
-
authJson,
|
|
31
|
-
config,
|
|
32
|
-
createSystemContext,
|
|
33
|
-
dev,
|
|
34
|
-
getAuth: ()=>instance,
|
|
35
|
-
logger,
|
|
36
|
-
plugins,
|
|
37
|
-
secrets
|
|
38
|
-
}));
|
|
39
|
-
// Retain the configured user-admin role per instance - handleAuthStep
|
|
40
|
-
// reads it to enforce the user-administration floor on auth steps.
|
|
41
|
-
registerUserAdminRole({
|
|
42
|
-
auth: instance,
|
|
43
|
-
userAdminRole: authJson.userAdminRole ?? null
|
|
44
|
-
});
|
|
45
|
-
// Retain the organizations declaration per instance - request-time reads
|
|
46
|
-
// (the _organization operator, step organizationId defaulting) resolve the
|
|
47
|
-
// policy and the ensured pinned org from it.
|
|
48
|
-
if (authJson.organizations) {
|
|
49
|
-
registerOrganizationBinding({
|
|
50
|
-
auth: instance,
|
|
51
|
-
database: Boolean(authJson.database),
|
|
52
|
-
organizations: authJson.organizations
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
// Ensure the pinned organization exists at startup - created if missing,
|
|
56
|
-
// untouched otherwise. The engine hooks await the same memoized ensure per
|
|
57
|
-
// fire, so a failure here only defers seeding to the first sign-in. A
|
|
58
|
-
// strategies-only app has no database - seeding an in-memory org would be
|
|
59
|
-
// meaningless, and no session user can ever reach the membership wall.
|
|
60
|
-
if (authJson.organizations?.policy === 'pinned' && authJson.database) {
|
|
61
|
-
ensureOrganization({
|
|
62
|
-
auth: instance,
|
|
63
|
-
slug: authJson.organizations.org
|
|
64
|
-
}).catch((error)=>{
|
|
65
|
-
logger.warn({
|
|
66
|
-
err: error
|
|
67
|
-
}, `Failed to ensure the pinned organization "${authJson.organizations.org}" at startup.`);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
return instance;
|
|
71
|
-
}
|
|
72
|
-
export default getBetterAuth;
|
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import { genericOAuth, magicLink, twoFactor } from 'better-auth/plugins';
|
|
16
|
-
import { passkey } from '@better-auth/passkey';
|
|
17
|
-
import { ServerParser } from '@lowdefy/operators';
|
|
18
|
-
import { _app, _secret } from '@lowdefy/operators-js/operators/server';
|
|
19
|
-
import { type } from '@lowdefy/helpers';
|
|
20
|
-
import { ConfigError, LowdefyInternalError } from '@lowdefy/errors';
|
|
21
|
-
import buildAdminPlugin from './buildAdminPlugin.js';
|
|
22
|
-
import buildCaptchaPlugin from './buildCaptchaPlugin.js';
|
|
23
|
-
import buildHooks from './hooks/buildHooks.js';
|
|
24
|
-
import buildOrganizationPlugin from './organizations/buildOrganizationPlugin.js';
|
|
25
|
-
import buildPhoneNumberPlugin from './buildPhoneNumberPlugin.js';
|
|
26
|
-
import buildProviders from './buildProviders.js';
|
|
27
|
-
import createAuthLogger from './createAuthLogger.js';
|
|
28
|
-
import createSendEmail from './createSendEmail.js';
|
|
29
|
-
import modelNames from './modelNames.js';
|
|
30
|
-
import renderAuthEmail from '../../email/renderAuthEmail.js';
|
|
31
|
-
import resolveCookiePrefix from './resolveCookiePrefix.js';
|
|
32
|
-
// Decision 4: under the "pinned" org policy, disable every mounted
|
|
33
|
-
// /organization/* HTTP path EXCEPT /organization/accept-invitation (the
|
|
34
|
-
// invitation flow depends on it and BetterAuth gates it itself). /organization/create
|
|
35
|
-
// is already off via allowUserToCreateOrganization:false. The audited admin steps
|
|
36
|
-
// bypass the router (callPluginEndpoint), so disabling these closes client HTTP
|
|
37
|
-
// access without breaking them. Set on the top-level `options.disabledPaths` (a
|
|
38
|
-
// BetterAuth CORE option, not an org-plugin option): the router's onRequest matches
|
|
39
|
-
// exactly via disabledPaths.includes(normalizedPath), with basePath/`/api/auth`
|
|
40
|
-
// stripped first, so entries must be the exact basePath-stripped `/organization/<segment>`.
|
|
41
|
-
// Verified against the mounted surface of better-auth@1.6.23. Team (`*-team*`) and
|
|
42
|
-
// dynamic-access-control role (`*-role`) endpoints are conditional on
|
|
43
|
-
// teams.enabled / dynamicAccessControl.enabled - neither is set by
|
|
44
|
-
// buildOrganizationPlugin, so they are not mounted and are not listed here.
|
|
45
|
-
const ORG_CLIENT_PATHS_DISABLED_WHEN_PINNED = [
|
|
46
|
-
// Mutations
|
|
47
|
-
'/organization/set-active',
|
|
48
|
-
'/organization/update',
|
|
49
|
-
'/organization/delete',
|
|
50
|
-
'/organization/leave',
|
|
51
|
-
'/organization/update-member-role',
|
|
52
|
-
'/organization/remove-member',
|
|
53
|
-
'/organization/invite-member',
|
|
54
|
-
'/organization/cancel-invitation',
|
|
55
|
-
// Reads
|
|
56
|
-
'/organization/list-members',
|
|
57
|
-
'/organization/get-active-member',
|
|
58
|
-
'/organization/get-active-member-role',
|
|
59
|
-
'/organization/get-full-organization',
|
|
60
|
-
'/organization/list',
|
|
61
|
-
'/organization/get-invitation',
|
|
62
|
-
'/organization/list-invitations',
|
|
63
|
-
'/organization/list-user-invitations',
|
|
64
|
-
'/organization/check-slug',
|
|
65
|
-
'/organization/reject-invitation',
|
|
66
|
-
'/organization/has-permission'
|
|
67
|
-
];
|
|
68
|
-
// Assembles the BetterAuthOptions object from the auth.json build artifact.
|
|
69
|
-
// Build has validated the config and written all defaults, so this function
|
|
70
|
-
// resolves the _secret operators and maps the Lowdefy surface onto
|
|
71
|
-
// BetterAuth's options - no fallback defaults here.
|
|
72
|
-
// createSystemContext builds a fresh off-request context per hook fire - the
|
|
73
|
-
// bridge a firing hook uses to invoke its InternalApi endpoint.
|
|
74
|
-
// getAuth returns the constructed BetterAuth instance - the engine-tier
|
|
75
|
-
// hooks resolve it lazily at fire time, after construction completes.
|
|
76
|
-
function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemContext, dev = false, getAuth, logger, plugins, secrets }) {
|
|
77
|
-
if (type.isNone(getAuth)) {
|
|
78
|
-
throw new LowdefyInternalError('No getAuth accessor was provided to getBetterAuthConfig - the engine-tier membership hooks resolve the BetterAuth instance through it.');
|
|
79
|
-
}
|
|
80
|
-
const operatorsParser = new ServerParser({
|
|
81
|
-
lowdefyApp: appMeta,
|
|
82
|
-
operators: {
|
|
83
|
-
_app,
|
|
84
|
-
_secret
|
|
85
|
-
},
|
|
86
|
-
secrets,
|
|
87
|
-
user: {}
|
|
88
|
-
});
|
|
89
|
-
const { output: authConfig, errors: operatorErrors } = operatorsParser.parse({
|
|
90
|
-
input: authJson,
|
|
91
|
-
location: 'auth',
|
|
92
|
-
payload: {}
|
|
93
|
-
});
|
|
94
|
-
if (operatorErrors.length > 0) {
|
|
95
|
-
// Startup fails on the first error; log the rest so they can all be
|
|
96
|
-
// fixed in one pass instead of one boot per error.
|
|
97
|
-
operatorErrors.slice(1).forEach((error)=>logger.error(error));
|
|
98
|
-
throw operatorErrors[0];
|
|
99
|
-
}
|
|
100
|
-
if (!type.isString(authConfig.secret)) {
|
|
101
|
-
throw new ConfigError('Auth "secret" did not resolve to a string. Check the _secret operator reference and that the secret is set.', {
|
|
102
|
-
received: type.typeOf(authConfig.secret)
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
// Secrets are opaque at build time; strength checks are startup warnings.
|
|
106
|
-
if (authConfig.secret.length < 32) {
|
|
107
|
-
logger.warn('Auth "secret" is shorter than 32 characters. Use a long random value, e.g. `openssl rand -base64 32`.');
|
|
108
|
-
}
|
|
109
|
-
// A strategies-only app configures no database - BetterAuth is constructed
|
|
110
|
-
// without an adapter (its stateless mode, backed by a memory adapter), so
|
|
111
|
-
// getSession runs guard-free and returns null on every request.
|
|
112
|
-
let database;
|
|
113
|
-
if (!type.isNone(authConfig.database)) {
|
|
114
|
-
const adapterPlugin = plugins.adapters[authConfig.database.type];
|
|
115
|
-
if (type.isNone(adapterPlugin)) {
|
|
116
|
-
throw new ConfigError(`Auth database adapter type "${authConfig.database.type}" not found at database "${authConfig.database.id}".`, {
|
|
117
|
-
configKey: authConfig.database['~k']
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
try {
|
|
121
|
-
database = adapterPlugin({
|
|
122
|
-
properties: authConfig.database.properties ?? {}
|
|
123
|
-
});
|
|
124
|
-
} catch (error) {
|
|
125
|
-
// Adapter plugins throw plain errors (missing uri, malformed
|
|
126
|
-
// connection string) - attach the database block's config location.
|
|
127
|
-
throw new ConfigError(`Auth database "${authConfig.database.id}" failed to construct: ${error.message}`, {
|
|
128
|
-
cause: error,
|
|
129
|
-
configKey: authConfig.database['~k']
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
const { socialProviders, genericOAuthConfigs } = buildProviders({
|
|
134
|
-
authConfig,
|
|
135
|
-
plugins
|
|
136
|
-
});
|
|
137
|
-
const sendEmail = type.isNone(authConfig.email) ? undefined : createSendEmail({
|
|
138
|
-
connectionId: authConfig.email.connectionId
|
|
139
|
-
});
|
|
140
|
-
// BetterAuth builds password-reset, magic-link and email-verification links,
|
|
141
|
-
// and its CSRF Origin allowlist, from the base URL. When the deployment's
|
|
142
|
-
// canonical origin is pinned via BETTER_AUTH_URL, both are fixed and cannot
|
|
143
|
-
// be steered by a spoofed Host / X-Forwarded-Host header (CWE-640
|
|
144
|
-
// password-reset poisoning). Without it, fall back to per-request host
|
|
145
|
-
// derivation - the zero-config path that supports arbitrary proxies and
|
|
146
|
-
// multi-host deployments - and warn in production that the host is then
|
|
147
|
-
// caller-controlled.
|
|
148
|
-
const canonicalUrl = process.env.BETTER_AUTH_URL?.trim();
|
|
149
|
-
let baseURL;
|
|
150
|
-
if (canonicalUrl) {
|
|
151
|
-
baseURL = canonicalUrl;
|
|
152
|
-
} else {
|
|
153
|
-
if (!dev) {
|
|
154
|
-
logger.warn('Auth base URL is not pinned. Set BETTER_AUTH_URL to the app\'s canonical origin (e.g. https://app.example.com) so password-reset, magic-link and verification email links cannot be spoofed through the Host header.');
|
|
155
|
-
}
|
|
156
|
-
baseURL = {
|
|
157
|
-
allowedHosts: [
|
|
158
|
-
'*'
|
|
159
|
-
],
|
|
160
|
-
protocol: dev ? 'http' : 'auto'
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
// When BETTER_AUTH_URL is not pinned baseURL is an object and the origin is
|
|
164
|
-
// unknown - used for logo resolution AND the invitation accept-URL fallback.
|
|
165
|
-
const baseUrlOrigin = type.isString(baseURL) ? baseURL : undefined;
|
|
166
|
-
const options = {
|
|
167
|
-
appName: appMeta?.name ?? 'Lowdefy',
|
|
168
|
-
baseURL,
|
|
169
|
-
basePath: `${config.basePath ?? ''}/api/auth`,
|
|
170
|
-
secret: authConfig.secret,
|
|
171
|
-
telemetry: {
|
|
172
|
-
enabled: false
|
|
173
|
-
},
|
|
174
|
-
logger: createAuthLogger({
|
|
175
|
-
logger
|
|
176
|
-
}),
|
|
177
|
-
user: {
|
|
178
|
-
modelName: modelNames.user,
|
|
179
|
-
// Internal additionalFields, deliberately not an app-facing surface:
|
|
180
|
-
// attributes holds admin-set, cross-app authorization inputs;
|
|
181
|
-
// profile is the opaque display-and-app-data bag written by module or
|
|
182
|
-
// app logic (UpdateUserProfile) - the platform never validates,
|
|
183
|
-
// indexes, or reads inside it.
|
|
184
|
-
additionalFields: {
|
|
185
|
-
attributes: {
|
|
186
|
-
type: 'json',
|
|
187
|
-
required: false,
|
|
188
|
-
input: false
|
|
189
|
-
},
|
|
190
|
-
profile: {
|
|
191
|
-
type: 'json',
|
|
192
|
-
required: false,
|
|
193
|
-
input: false
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
},
|
|
197
|
-
verification: {
|
|
198
|
-
modelName: modelNames.verification
|
|
199
|
-
},
|
|
200
|
-
session: {
|
|
201
|
-
modelName: modelNames.session,
|
|
202
|
-
expiresIn: authConfig.session.expiresIn,
|
|
203
|
-
updateAge: authConfig.session.updateAge,
|
|
204
|
-
cookieCache: {
|
|
205
|
-
enabled: authConfig.session.cookieCache.enabled,
|
|
206
|
-
maxAge: authConfig.session.cookieCache.maxAge
|
|
207
|
-
}
|
|
208
|
-
},
|
|
209
|
-
account: {
|
|
210
|
-
modelName: modelNames.account,
|
|
211
|
-
accountLinking: {
|
|
212
|
-
enabled: authConfig.account.accountLinking.enabled,
|
|
213
|
-
trustedProviders: authConfig.account.accountLinking.trustedProviders
|
|
214
|
-
}
|
|
215
|
-
},
|
|
216
|
-
rateLimit: {
|
|
217
|
-
enabled: authConfig.rateLimit.enabled,
|
|
218
|
-
window: authConfig.rateLimit.window,
|
|
219
|
-
max: authConfig.rateLimit.max
|
|
220
|
-
},
|
|
221
|
-
advanced: {
|
|
222
|
-
cookiePrefix: resolveCookiePrefix({
|
|
223
|
-
appMeta,
|
|
224
|
-
dev
|
|
225
|
-
})
|
|
226
|
-
},
|
|
227
|
-
plugins: []
|
|
228
|
-
};
|
|
229
|
-
if (!type.isNone(database)) {
|
|
230
|
-
options.database = database;
|
|
231
|
-
}
|
|
232
|
-
if (authConfig.session.crossSubDomainCookies.enabled === true) {
|
|
233
|
-
options.advanced.crossSubDomainCookies = {
|
|
234
|
-
enabled: true,
|
|
235
|
-
domain: authConfig.session.crossSubDomainCookies.domain
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
if (authConfig.emailAndPassword?.enabled === true) {
|
|
239
|
-
options.emailAndPassword = {
|
|
240
|
-
enabled: true,
|
|
241
|
-
requireEmailVerification: authConfig.emailAndPassword.requireEmailVerification,
|
|
242
|
-
minPasswordLength: authConfig.emailAndPassword.minPasswordLength,
|
|
243
|
-
disableSignUp: authConfig.emailAndPassword.disableSignUp
|
|
244
|
-
};
|
|
245
|
-
if (sendEmail) {
|
|
246
|
-
options.emailAndPassword.sendResetPassword = async ({ user, url })=>{
|
|
247
|
-
const context = createSystemContext({
|
|
248
|
-
auth: getAuth()
|
|
249
|
-
});
|
|
250
|
-
const { subject, html, text } = await renderAuthEmail({
|
|
251
|
-
flow: 'resetPassword',
|
|
252
|
-
vars: {
|
|
253
|
-
url
|
|
254
|
-
},
|
|
255
|
-
authEmailConfig: authConfig.email,
|
|
256
|
-
baseURL: baseUrlOrigin,
|
|
257
|
-
context
|
|
258
|
-
});
|
|
259
|
-
await sendEmail({
|
|
260
|
-
to: user.email,
|
|
261
|
-
subject,
|
|
262
|
-
html,
|
|
263
|
-
text,
|
|
264
|
-
context
|
|
265
|
-
});
|
|
266
|
-
};
|
|
267
|
-
} else {
|
|
268
|
-
// Password reset is implicit in emailAndPassword, so a missing email
|
|
269
|
-
// config is a startup warning, not a build failure.
|
|
270
|
-
logger.warn('Auth "email" is not configured - password reset emails cannot be sent, so the reset flow is unavailable.');
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (sendEmail) {
|
|
274
|
-
options.emailVerification = {
|
|
275
|
-
sendVerificationEmail: async ({ user, url })=>{
|
|
276
|
-
const context = createSystemContext({
|
|
277
|
-
auth: getAuth()
|
|
278
|
-
});
|
|
279
|
-
const { subject, html, text } = await renderAuthEmail({
|
|
280
|
-
flow: 'verifyEmail',
|
|
281
|
-
vars: {
|
|
282
|
-
url
|
|
283
|
-
},
|
|
284
|
-
authEmailConfig: authConfig.email,
|
|
285
|
-
baseURL: baseUrlOrigin,
|
|
286
|
-
context
|
|
287
|
-
});
|
|
288
|
-
await sendEmail({
|
|
289
|
-
to: user.email,
|
|
290
|
-
subject,
|
|
291
|
-
html,
|
|
292
|
-
text,
|
|
293
|
-
context
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
if (Object.keys(socialProviders).length > 0) {
|
|
299
|
-
options.socialProviders = socialProviders;
|
|
300
|
-
}
|
|
301
|
-
if (authConfig.magicLink?.enabled === true) {
|
|
302
|
-
options.plugins.push(magicLink({
|
|
303
|
-
expiresIn: authConfig.magicLink.expiresIn,
|
|
304
|
-
disableSignUp: authConfig.magicLink.disableSignUp,
|
|
305
|
-
sendMagicLink: async ({ email, url })=>{
|
|
306
|
-
const context = createSystemContext({
|
|
307
|
-
auth: getAuth()
|
|
308
|
-
});
|
|
309
|
-
const { subject, html, text } = await renderAuthEmail({
|
|
310
|
-
flow: 'magicLink',
|
|
311
|
-
vars: {
|
|
312
|
-
url
|
|
313
|
-
},
|
|
314
|
-
authEmailConfig: authConfig.email,
|
|
315
|
-
baseURL: baseUrlOrigin,
|
|
316
|
-
context
|
|
317
|
-
});
|
|
318
|
-
await sendEmail({
|
|
319
|
-
to: email,
|
|
320
|
-
subject,
|
|
321
|
-
html,
|
|
322
|
-
text,
|
|
323
|
-
context
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
}));
|
|
327
|
-
}
|
|
328
|
-
if (genericOAuthConfigs.length > 0) {
|
|
329
|
-
options.plugins.push(genericOAuth({
|
|
330
|
-
config: genericOAuthConfigs
|
|
331
|
-
}));
|
|
332
|
-
}
|
|
333
|
-
if (authConfig.twoFactor?.enabled === true) {
|
|
334
|
-
options.plugins.push(twoFactor({
|
|
335
|
-
issuer: appMeta?.name,
|
|
336
|
-
schema: {
|
|
337
|
-
twoFactor: {
|
|
338
|
-
modelName: modelNames.twoFactor
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}));
|
|
342
|
-
}
|
|
343
|
-
if (authConfig.passkey?.enabled === true) {
|
|
344
|
-
options.plugins.push(passkey({
|
|
345
|
-
rpID: authConfig.passkey.rpId,
|
|
346
|
-
rpName: authConfig.passkey.rpName,
|
|
347
|
-
schema: {
|
|
348
|
-
passkey: {
|
|
349
|
-
modelName: modelNames.passkey
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}));
|
|
353
|
-
}
|
|
354
|
-
// Captcha is server middleware only - the client half is the Captcha block
|
|
355
|
-
// and the captchaToken action param carrying the x-captcha-response header.
|
|
356
|
-
if (authConfig.captcha?.enabled === true) {
|
|
357
|
-
options.plugins.push(buildCaptchaPlugin({
|
|
358
|
-
authConfig
|
|
359
|
-
}));
|
|
360
|
-
}
|
|
361
|
-
// The admin plugin is framework-controlled - it backs the admin steps and
|
|
362
|
-
// impersonation. A configured auth.userAdminRole registers a curated
|
|
363
|
-
// access control (see buildAdminPlugin).
|
|
364
|
-
options.plugins.push(buildAdminPlugin({
|
|
365
|
-
authConfig
|
|
366
|
-
}));
|
|
367
|
-
const { afterEmailVerification, databaseHooks, phoneVerified, sendPhoneOtp, sendPhonePasswordResetOtp } = buildHooks({
|
|
368
|
-
authConfig,
|
|
369
|
-
createSystemContext,
|
|
370
|
-
getAuth
|
|
371
|
-
});
|
|
372
|
-
if (Object.keys(databaseHooks).length > 0) {
|
|
373
|
-
options.databaseHooks = databaseHooks;
|
|
374
|
-
}
|
|
375
|
-
if (afterEmailVerification) {
|
|
376
|
-
options.emailVerification = {
|
|
377
|
-
...options.emailVerification,
|
|
378
|
-
afterEmailVerification
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
// Phone login sends SMS through the "phone.otp.send" hook binding - there
|
|
382
|
-
// is no built-in SMS transport, so build validation requires the binding
|
|
383
|
-
// when the plugin is enabled.
|
|
384
|
-
if (authConfig.phoneNumber?.enabled === true) {
|
|
385
|
-
options.plugins.push(buildPhoneNumberPlugin({
|
|
386
|
-
authConfig,
|
|
387
|
-
phoneVerified,
|
|
388
|
-
sendPhoneOtp,
|
|
389
|
-
sendPhonePasswordResetOtp
|
|
390
|
-
}));
|
|
391
|
-
}
|
|
392
|
-
// Organizations are always on, so the plugin always gets an invitation
|
|
393
|
-
// sender. Without auth.email the sender throws at invite-send time (not at
|
|
394
|
-
// build), the same stance as password reset. The accept URL is only
|
|
395
|
-
// buildable when the origin is pinned AND authPages.acceptInvitation is set;
|
|
396
|
-
// otherwise the stock InvitationEmail renders branded with no CTA button.
|
|
397
|
-
async function sendInvitationEmail({ email, organization, invitation }) {
|
|
398
|
-
if (type.isNone(authConfig.email)) {
|
|
399
|
-
throw new Error('Cannot send the invitation email. Configure "auth.email".');
|
|
400
|
-
}
|
|
401
|
-
const acceptPath = authConfig.authPages?.acceptInvitation;
|
|
402
|
-
const canBuildAcceptUrl = type.isString(baseUrlOrigin) && type.isString(acceptPath);
|
|
403
|
-
const basePath = config.basePath ?? '';
|
|
404
|
-
const acceptUrl = canBuildAcceptUrl ? `${baseUrlOrigin}${basePath}${acceptPath}?invitationId=${invitation.id}` : undefined;
|
|
405
|
-
const context = createSystemContext({
|
|
406
|
-
auth: getAuth()
|
|
407
|
-
});
|
|
408
|
-
const { subject, html, text } = await renderAuthEmail({
|
|
409
|
-
flow: 'invitation',
|
|
410
|
-
vars: {
|
|
411
|
-
url: acceptUrl,
|
|
412
|
-
organizationName: organization.name,
|
|
413
|
-
invitationId: invitation.id
|
|
414
|
-
},
|
|
415
|
-
authEmailConfig: authConfig.email,
|
|
416
|
-
baseURL: baseUrlOrigin,
|
|
417
|
-
context
|
|
418
|
-
});
|
|
419
|
-
await sendEmail({
|
|
420
|
-
to: email,
|
|
421
|
-
subject,
|
|
422
|
-
html,
|
|
423
|
-
text,
|
|
424
|
-
context
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
options.plugins.push(buildOrganizationPlugin({
|
|
428
|
-
authConfig,
|
|
429
|
-
getAuth,
|
|
430
|
-
sendInvitationEmail
|
|
431
|
-
}));
|
|
432
|
-
// Policy-aware lockdown of the org plugin's client HTTP endpoints (Decision 4).
|
|
433
|
-
// pinned: disable the full set (everything except accept-invitation/create).
|
|
434
|
-
// tenant: self-serve, leave the org endpoints enabled.
|
|
435
|
-
const policy = authConfig.organizations?.policy ?? 'pinned';
|
|
436
|
-
options.disabledPaths = policy === 'pinned' ? ORG_CLIENT_PATHS_DISABLED_WHEN_PINNED : [];
|
|
437
|
-
return options;
|
|
438
|
-
}
|
|
439
|
-
export default getBetterAuthConfig;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
-
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
|
|
8
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
|
|
10
|
-
Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
See the License for the specific language governing permissions and
|
|
14
|
-
limitations under the License.
|
|
15
|
-
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
-
// The engine retains the configured auth.userAdminRole per auth instance -
|
|
17
|
-
// the step interface layer (handleAuthStep) reads it synchronously to enforce
|
|
18
|
-
// the user-administration floor on user-initiated auth steps. null means
|
|
19
|
-
// unconfigured, which is a meaningful state: user-initiated admin steps are
|
|
20
|
-
// refused until the app declares who administers users.
|
|
21
|
-
const userAdminRoleByAuth = new WeakMap();
|
|
22
|
-
function registerUserAdminRole({ auth, userAdminRole }) {
|
|
23
|
-
userAdminRoleByAuth.set(auth, userAdminRole ?? null);
|
|
24
|
-
}
|
|
25
|
-
function getUserAdminRole({ auth }) {
|
|
26
|
-
if (type.isNone(auth)) {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
return userAdminRoleByAuth.get(auth) ?? null;
|
|
30
|
-
}
|
|
31
|
-
export { registerUserAdminRole };
|
|
32
|
-
export default getUserAdminRole;
|