@lowdefy/server-dev 0.0.0-experimental-20260716082808 → 0.0.0-experimental-20260717101828
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/lib/client/auth/Auth.jsx +3 -5
- package/lib/client/auth/AuthConfigured.jsx +116 -35
- package/lib/client/auth/AuthNotConfigured.js +22 -2
- package/lib/client/utils/usePageConfig.js +7 -0
- package/lib/server/auth/createSystemContext.js +70 -0
- package/lib/server/auth/getAuth.js +46 -0
- package/lib/server/auth/getHeadlessUser.js +7 -8
- package/lib/server/auth/getMockUser.js +11 -7
- package/lib/server/auth/{getAuthConfig.js → getStrategies.js} +12 -10
- package/lib/server/auth/headlessUser.js +3 -2
- package/lib/server/createLowdefyContext.js +56 -41
- package/lib/server/loadDynamicJsMap.js +47 -0
- package/lib/server/log/logRequest.js +1 -3
- package/manager/processes/startServer.mjs +0 -2
- package/manager/utils/createCustomPluginTypesMap.mjs +1 -2
- package/manager/watchers/serverArtifactWatcher.mjs +0 -2
- package/package.json +36 -34
- package/package.original.json +36 -34
- package/src/app.js +27 -14
- package/src/middleware/errorHandler.js +12 -1
- package/src/routes/auth.js +15 -21
- package/src/routes/detached.js +10 -6
- package/src/routes/docs/runRequest.js +2 -4
- package/src/routes/endpoints.js +14 -9
- package/src/routes/jitPage.js +21 -2
- package/src/routes/user.js +27 -0
- package/lib/server/auth/getDevSession.js +0 -70
- package/lib/server/auth/getDevSession.test.mjs +0 -138
- package/lib/server/auth/session.js +0 -39
- package/manager/processes/warnAuthUrlPortMismatch.mjs +0 -47
- package/manager/processes/warnAuthUrlPortMismatch.test.mjs +0 -93
- package/src/routes/apiPage.js +0 -37
- package/src/routes/auth.test.mjs +0 -84
package/lib/client/auth/Auth.jsx
CHANGED
|
@@ -14,8 +14,6 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
/* eslint-disable react/jsx-props-no-spreading */
|
|
18
|
-
|
|
19
17
|
import React from 'react';
|
|
20
18
|
import AuthConfigured from './AuthConfigured.jsx';
|
|
21
19
|
import AuthNotConfigured from './AuthNotConfigured.js';
|
|
@@ -25,15 +23,15 @@ import { serializer } from '@lowdefy/helpers';
|
|
|
25
23
|
// Client code imports the build JSON directly — Vite handles JSON imports;
|
|
26
24
|
// the lib/build/*.js wrappers are server-only (they read from disk).
|
|
27
25
|
// Deserialize to restore arrays from their ~arr build markers (providers
|
|
28
|
-
// must be a real array for
|
|
26
|
+
// must be a real array for provider lookups in createAuthMethods).
|
|
29
27
|
import rawAuthConfig from '../../../build/auth.json';
|
|
30
28
|
|
|
31
29
|
const authConfig = serializer.deserialize(rawAuthConfig);
|
|
32
30
|
|
|
33
|
-
function Auth({ children,
|
|
31
|
+
function Auth({ children, user }) {
|
|
34
32
|
if (authConfig.configured === true) {
|
|
35
33
|
return (
|
|
36
|
-
<AuthConfigured
|
|
34
|
+
<AuthConfigured serverUser={user} authConfig={authConfig}>
|
|
37
35
|
{(auth) => children(auth)}
|
|
38
36
|
</AuthConfigured>
|
|
39
37
|
);
|
|
@@ -14,17 +14,17 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
/* eslint-disable react/jsx-props-no-spreading */
|
|
18
|
-
|
|
19
17
|
import React, { useEffect, useRef } from 'react';
|
|
18
|
+
import { createAuthClient } from 'better-auth/react';
|
|
20
19
|
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
} from '
|
|
20
|
+
adminClient,
|
|
21
|
+
genericOAuthClient,
|
|
22
|
+
magicLinkClient,
|
|
23
|
+
organizationClient,
|
|
24
|
+
phoneNumberClient,
|
|
25
|
+
twoFactorClient,
|
|
26
|
+
} from 'better-auth/client/plugins';
|
|
27
|
+
import { passkeyClient } from '@better-auth/passkey/client';
|
|
28
28
|
|
|
29
29
|
import { serializer } from '@lowdefy/helpers';
|
|
30
30
|
|
|
@@ -32,42 +32,123 @@ import rawLowdefyConfig from '../../../build/config.json';
|
|
|
32
32
|
|
|
33
33
|
const lowdefyConfig = serializer.deserialize(rawLowdefyConfig);
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
const authClient = createAuthClient({
|
|
36
|
+
baseURL: `${window.location.origin}${lowdefyConfig.basePath ?? ''}/api/auth`,
|
|
37
|
+
plugins: [
|
|
38
|
+
adminClient(),
|
|
39
|
+
genericOAuthClient(),
|
|
40
|
+
magicLinkClient(),
|
|
41
|
+
organizationClient(),
|
|
42
|
+
passkeyClient(),
|
|
43
|
+
phoneNumberClient(),
|
|
44
|
+
twoFactorClient(),
|
|
45
|
+
],
|
|
46
|
+
});
|
|
40
47
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
// The server resolves the caller per request and embeds it in the page
|
|
49
|
+
// config, so the first render never flashes unauthenticated. The BetterAuth
|
|
50
|
+
// client store takes over once its session fetch settles.
|
|
51
|
+
//
|
|
52
|
+
// Roles and merged attributes resolve server-side from the active member row
|
|
53
|
+
// - the base session carries neither. The last server-resolved caller is
|
|
54
|
+
// kept in a ref: while the session user is unchanged its roles and
|
|
55
|
+
// attributes stay authoritative, and UpdateSession refreshes the ref from
|
|
56
|
+
// /api/user after a change (e.g. SetActiveOrganization).
|
|
57
|
+
function Session({ children, reloadSuppressedRef, serverUser }) {
|
|
58
|
+
const { data: session, isPending } = authClient.useSession();
|
|
59
|
+
const resolvedUserRef = useRef(serverUser);
|
|
60
|
+
const wasAuthenticated = useRef(Boolean(serverUser));
|
|
45
61
|
|
|
46
62
|
useEffect(() => {
|
|
47
|
-
if (
|
|
63
|
+
if (session) {
|
|
64
|
+
wasAuthenticated.current = true;
|
|
65
|
+
}
|
|
66
|
+
// Reload after sign-out (or session revocation) so the server can apply
|
|
67
|
+
// the page auth fork - a protected page redirects to the login page.
|
|
68
|
+
// A logout with a callbackUrl suppresses the reload - its own navigation
|
|
69
|
+
// would otherwise race this one.
|
|
70
|
+
if (wasAuthenticated.current && !isPending && !session && !reloadSuppressedRef.current) {
|
|
48
71
|
window.location.reload();
|
|
49
72
|
}
|
|
50
|
-
}, [
|
|
73
|
+
}, [session, isPending]);
|
|
51
74
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
75
|
+
if (isPending) {
|
|
76
|
+
return children(resolvedUserRef.current, resolvedUserRef);
|
|
77
|
+
}
|
|
78
|
+
if (!session?.user) {
|
|
79
|
+
return children(null, resolvedUserRef);
|
|
56
80
|
}
|
|
57
|
-
|
|
81
|
+
const resolved = resolvedUserRef.current;
|
|
82
|
+
const user =
|
|
83
|
+
resolved && resolved.id === session.user.id
|
|
84
|
+
? { ...session.user, roles: resolved.roles, attributes: resolved.attributes }
|
|
85
|
+
: { roles: [], ...session.user };
|
|
86
|
+
return children(user, resolvedUserRef);
|
|
58
87
|
}
|
|
59
88
|
|
|
60
|
-
function AuthConfigured({ authConfig, children,
|
|
61
|
-
const
|
|
89
|
+
function AuthConfigured({ authConfig, children, serverUser }) {
|
|
90
|
+
const reloadSuppressedRef = useRef(false);
|
|
91
|
+
const auth = {
|
|
92
|
+
authConfig,
|
|
93
|
+
getSession: ({ disableCookieCache } = {}) =>
|
|
94
|
+
authClient.getSession(disableCookieCache ? { query: { disableCookieCache: true } } : {}),
|
|
95
|
+
// The server-resolved caller - roles from the active member row and the
|
|
96
|
+
// merged attributes bag - for re-syncing after session changes.
|
|
97
|
+
getResolvedUser: async () => {
|
|
98
|
+
const response = await fetch(`${lowdefyConfig.basePath ?? ''}/api/user`, {
|
|
99
|
+
credentials: 'same-origin',
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
// Throw instead of parsing an error page - a silent { user: undefined }
|
|
103
|
+
// would read as a client-side logout in updateSession.
|
|
104
|
+
throw new Error(`Failed to fetch the resolved user (HTTP ${response.status}).`);
|
|
105
|
+
}
|
|
106
|
+
return response.json();
|
|
107
|
+
},
|
|
108
|
+
suppressSignOutReload: () => {
|
|
109
|
+
reloadSuppressedRef.current = true;
|
|
110
|
+
},
|
|
111
|
+
acceptInvitation: (params) => authClient.organization.acceptInvitation(params),
|
|
112
|
+
// addPasskey runs the WebAuthn browser ceremony itself - options fetch,
|
|
113
|
+
// authenticator prompt, verification.
|
|
114
|
+
addPasskey: (params) => authClient.passkey.addPasskey(params),
|
|
115
|
+
changePassword: (params) => authClient.changePassword(params),
|
|
116
|
+
deletePasskey: (params) => authClient.passkey.deletePasskey(params),
|
|
117
|
+
impersonateUser: (params) => authClient.admin.impersonateUser(params),
|
|
118
|
+
phoneNumberRequestPasswordReset: (params) =>
|
|
119
|
+
authClient.phoneNumber.requestPasswordReset(params),
|
|
120
|
+
phoneNumberResetPassword: (params) => authClient.phoneNumber.resetPassword(params),
|
|
121
|
+
phoneNumberSendOtp: (params) => authClient.phoneNumber.sendOtp(params),
|
|
122
|
+
phoneNumberVerify: (params) => authClient.phoneNumber.verify(params),
|
|
123
|
+
requestPasswordReset: (params) => authClient.requestPasswordReset(params),
|
|
124
|
+
resetPassword: (params) => authClient.resetPassword(params),
|
|
125
|
+
revokeOtherSessions: () => authClient.revokeOtherSessions(),
|
|
126
|
+
sendVerificationEmail: (params) => authClient.sendVerificationEmail(params),
|
|
127
|
+
setActiveOrganization: (params) => authClient.organization.setActive(params),
|
|
128
|
+
signInEmail: (params) => authClient.signIn.email(params),
|
|
129
|
+
signInMagicLink: (params) => authClient.signIn.magicLink(params),
|
|
130
|
+
signInOauth2: (params) => authClient.signIn.oauth2(params),
|
|
131
|
+
signInPasskey: (params) => authClient.signIn.passkey(params),
|
|
132
|
+
signInPhoneNumber: (params) => authClient.signIn.phoneNumber(params),
|
|
133
|
+
signInSocial: (params) => authClient.signIn.social(params),
|
|
134
|
+
signOut: () => authClient.signOut(),
|
|
135
|
+
signUpEmail: (params) => authClient.signUp.email(params),
|
|
136
|
+
stopImpersonating: () => authClient.admin.stopImpersonating(),
|
|
137
|
+
twoFactorDisable: (params) => authClient.twoFactor.disable(params),
|
|
138
|
+
twoFactorEnable: (params) => authClient.twoFactor.enable(params),
|
|
139
|
+
twoFactorVerifyBackupCode: (params) => authClient.twoFactor.verifyBackupCode(params),
|
|
140
|
+
twoFactorVerifyTotp: (params) => authClient.twoFactor.verifyTotp(params),
|
|
141
|
+
};
|
|
62
142
|
return (
|
|
63
|
-
<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
143
|
+
<Session reloadSuppressedRef={reloadSuppressedRef} serverUser={serverUser}>
|
|
144
|
+
{(user, resolvedUserRef) => {
|
|
145
|
+
auth.user = user;
|
|
146
|
+
auth.updateResolvedUser = (resolved) => {
|
|
147
|
+
resolvedUserRef.current = resolved;
|
|
148
|
+
};
|
|
149
|
+
return children(auth);
|
|
150
|
+
}}
|
|
151
|
+
</Session>
|
|
71
152
|
);
|
|
72
153
|
}
|
|
73
154
|
|
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
/* eslint-disable react/jsx-props-no-spreading */
|
|
17
16
|
|
|
18
17
|
function authNotConfigured() {
|
|
19
18
|
throw new Error('Auth not configured.');
|
|
@@ -22,9 +21,30 @@ function authNotConfigured() {
|
|
|
22
21
|
function AuthNotConfigured({ authConfig, children }) {
|
|
23
22
|
const auth = {
|
|
24
23
|
authConfig,
|
|
24
|
+
user: null,
|
|
25
|
+
acceptInvitation: authNotConfigured,
|
|
26
|
+
addPasskey: authNotConfigured,
|
|
27
|
+
changePassword: authNotConfigured,
|
|
28
|
+
deletePasskey: authNotConfigured,
|
|
29
|
+
getResolvedUser: authNotConfigured,
|
|
25
30
|
getSession: authNotConfigured,
|
|
26
|
-
|
|
31
|
+
impersonateUser: authNotConfigured,
|
|
32
|
+
requestPasswordReset: authNotConfigured,
|
|
33
|
+
resetPassword: authNotConfigured,
|
|
34
|
+
revokeOtherSessions: authNotConfigured,
|
|
35
|
+
sendVerificationEmail: authNotConfigured,
|
|
36
|
+
setActiveOrganization: authNotConfigured,
|
|
37
|
+
signInEmail: authNotConfigured,
|
|
38
|
+
signInMagicLink: authNotConfigured,
|
|
39
|
+
signInOauth2: authNotConfigured,
|
|
40
|
+
signInSocial: authNotConfigured,
|
|
27
41
|
signOut: authNotConfigured,
|
|
42
|
+
signUpEmail: authNotConfigured,
|
|
43
|
+
stopImpersonating: authNotConfigured,
|
|
44
|
+
twoFactorDisable: authNotConfigured,
|
|
45
|
+
twoFactorEnable: authNotConfigured,
|
|
46
|
+
twoFactorVerifyBackupCode: authNotConfigured,
|
|
47
|
+
twoFactorVerifyTotp: authNotConfigured,
|
|
28
48
|
};
|
|
29
49
|
|
|
30
50
|
return children(auth);
|
|
@@ -54,6 +54,13 @@ async function fetchPageConfig(url) {
|
|
|
54
54
|
if (res.status === 404) {
|
|
55
55
|
return null;
|
|
56
56
|
}
|
|
57
|
+
if (res.status === 401) {
|
|
58
|
+
// Logged-out navigation to a protected page - full load to the login
|
|
59
|
+
// page so it can return here after sign-in.
|
|
60
|
+
const { redirect } = await res.json();
|
|
61
|
+
window.location.assign(redirect ?? '/404');
|
|
62
|
+
return new Promise(() => {});
|
|
63
|
+
}
|
|
57
64
|
const data = await res.json();
|
|
58
65
|
if (data?.buildError) {
|
|
59
66
|
return data;
|
|
@@ -0,0 +1,70 @@
|
|
|
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
|
+
*/
|
|
16
|
+
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
import { createSystemContext as buildSystemContext } from '@lowdefy/api';
|
|
19
|
+
import { getSecretsFromEnv } from '@lowdefy/node-utils';
|
|
20
|
+
import { v4 as uuid } from 'uuid';
|
|
21
|
+
|
|
22
|
+
import agents from '../../../build/plugins/agents.js';
|
|
23
|
+
import appMeta from '../../build/appMeta.js';
|
|
24
|
+
import config from '../../build/config.js';
|
|
25
|
+
import connections from '../../../build/plugins/connections.js';
|
|
26
|
+
import createHandleError from '../log/createHandleError.js';
|
|
27
|
+
import createLogger from '../log/createLogger.js';
|
|
28
|
+
import fileCache from '../fileCache.js';
|
|
29
|
+
import i18nConfig from '../../build/i18n.js';
|
|
30
|
+
import loadDynamicJsMap from '../loadDynamicJsMap.js';
|
|
31
|
+
import notifications from '../../../build/plugins/notifications.js';
|
|
32
|
+
import operators from '../../../build/plugins/operators/server.js';
|
|
33
|
+
import steps from '../../../build/plugins/steps.js';
|
|
34
|
+
import websockets from '../../../build/plugins/websockets.js';
|
|
35
|
+
|
|
36
|
+
const secrets = getSecretsFromEnv();
|
|
37
|
+
|
|
38
|
+
// Builds a fresh system context per auth hook fire - the trusted internal
|
|
39
|
+
// caller the BetterAuth engine uses to invoke hook endpoints. All fields are
|
|
40
|
+
// startup singletons except the per-fire rid, logger, handleError, the
|
|
41
|
+
// dynamic jsMap re-read (JIT rebuilds rewrite serverJsMap.js), and auth -
|
|
42
|
+
// auth is threaded in by the caller (createHookDispatch) rather than
|
|
43
|
+
// imported directly, since it comes from the same BetterAuth construction
|
|
44
|
+
// that this factory is a dependency of (see createHookDispatch.js).
|
|
45
|
+
function createSystemContext({ auth } = {}) {
|
|
46
|
+
const rid = uuid();
|
|
47
|
+
const buildDirectory = path.join(process.cwd(), 'build');
|
|
48
|
+
return buildSystemContext({
|
|
49
|
+
rid,
|
|
50
|
+
agents,
|
|
51
|
+
appMeta,
|
|
52
|
+
auth,
|
|
53
|
+
buildDirectory,
|
|
54
|
+
config,
|
|
55
|
+
configDirectory: process.env.LOWDEFY_DIRECTORY_CONFIG || process.cwd(),
|
|
56
|
+
connections,
|
|
57
|
+
createHandleError,
|
|
58
|
+
fileCache,
|
|
59
|
+
i18n: i18nConfig,
|
|
60
|
+
jsMap: loadDynamicJsMap(buildDirectory),
|
|
61
|
+
logger: createLogger({ rid }),
|
|
62
|
+
notifications,
|
|
63
|
+
operators,
|
|
64
|
+
secrets,
|
|
65
|
+
steps,
|
|
66
|
+
websockets,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export default createSystemContext;
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { getBetterAuth } from '@lowdefy/api';
|
|
18
|
+
import { getSecretsFromEnv } from '@lowdefy/node-utils';
|
|
19
|
+
|
|
20
|
+
import adapters from '../../../build/plugins/auth/adapters.js';
|
|
21
|
+
import appMeta from '../../build/appMeta.js';
|
|
22
|
+
import authJson from '../../build/auth.js';
|
|
23
|
+
import createSystemContext from './createSystemContext.js';
|
|
24
|
+
import lowdefyConfig from '../../build/config.js';
|
|
25
|
+
import providers from '../../../build/plugins/auth/providers.js';
|
|
26
|
+
|
|
27
|
+
// Returns the BetterAuth instance (memoized in @lowdefy/api), or null when
|
|
28
|
+
// auth is not configured. dev: true relaxes cookies for plain-http localhost
|
|
29
|
+
// and derives a per-app cookie prefix so parallel dev apps do not collide.
|
|
30
|
+
function getAuth({ logger }) {
|
|
31
|
+
if (authJson.configured !== true) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return getBetterAuth({
|
|
35
|
+
appMeta,
|
|
36
|
+
authJson,
|
|
37
|
+
config: lowdefyConfig,
|
|
38
|
+
createSystemContext,
|
|
39
|
+
dev: true,
|
|
40
|
+
logger,
|
|
41
|
+
plugins: { adapters, providers },
|
|
42
|
+
secrets: getSecretsFromEnv(),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default getAuth;
|
|
@@ -16,24 +16,23 @@
|
|
|
16
16
|
|
|
17
17
|
import { HEADLESS_USER_COOKIE } from './headlessUser.js';
|
|
18
18
|
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
// unaffected.
|
|
19
|
+
// Mirrors the e2e user-injection pattern (server-e2e/lib/server/auth/getUser.js):
|
|
20
|
+
// the headless renderer sets a base64-JSON user cookie on its own browser
|
|
21
|
+
// context, so its /api/* fetches carry a pre-resolved caller while the
|
|
22
|
+
// developer's real browser (no cookie) is unaffected. Returns the raw decoded
|
|
23
|
+
// user, or null on miss - the caller floors the shape via normalizeInjectedCaller.
|
|
25
24
|
function getHeadlessUser(c) {
|
|
26
25
|
const cookieHeader = c.req.header('cookie') ?? '';
|
|
27
26
|
const match = cookieHeader.match(new RegExp(`${HEADLESS_USER_COOKIE}=([^;]+)`));
|
|
28
27
|
if (!match) {
|
|
29
|
-
return
|
|
28
|
+
return null;
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
try {
|
|
33
32
|
const decoded = Buffer.from(decodeURIComponent(match[1]), 'base64').toString();
|
|
34
33
|
return JSON.parse(decoded);
|
|
35
34
|
} catch {
|
|
36
|
-
return
|
|
35
|
+
return null;
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
38
|
|
|
@@ -18,10 +18,10 @@ import { serializer } from '@lowdefy/helpers';
|
|
|
18
18
|
|
|
19
19
|
import authJson from '../../build/auth.js';
|
|
20
20
|
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
21
|
+
// dev.mockUser is a pre-resolved caller that substitutes for the whole
|
|
22
|
+
// resolveAuthentication step - no auth engine, no session lookup, no
|
|
23
|
+
// membership wall. Its roles are authoritative. LOWDEFY_DEV_USER takes
|
|
24
|
+
// priority over auth.dev.mockUser from lowdefy.yaml.
|
|
25
25
|
function getMockUser() {
|
|
26
26
|
const mockUserJson = process.env.LOWDEFY_DEV_USER;
|
|
27
27
|
let mockUser;
|
|
@@ -37,9 +37,12 @@ function getMockUser() {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
if (!mockUser) {
|
|
40
|
-
return
|
|
40
|
+
return null;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
// Deserialize to restore arrays from ~arr markers and remove other build markers
|
|
44
|
+
mockUser = serializer.deserialize(mockUser);
|
|
45
|
+
|
|
43
46
|
if (authJson.configured !== true) {
|
|
44
47
|
throw new Error(
|
|
45
48
|
'Mock user configured but auth is not configured in lowdefy.yaml. ' +
|
|
@@ -47,8 +50,9 @@ function getMockUser() {
|
|
|
47
50
|
);
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
//
|
|
51
|
-
return
|
|
53
|
+
// The roles/attributes floor is applied at injection by
|
|
54
|
+
// normalizeInjectedCaller (createLowdefyContext.js) - return the raw user.
|
|
55
|
+
return mockUser;
|
|
52
56
|
}
|
|
53
57
|
|
|
54
58
|
export default getMockUser;
|
|
@@ -14,25 +14,27 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import { getAuthStrategies } from '@lowdefy/api';
|
|
18
18
|
import { getSecretsFromEnv } from '@lowdefy/node-utils';
|
|
19
19
|
|
|
20
|
-
import adapters from '../../../build/plugins/auth/adapters.js';
|
|
21
20
|
import appMeta from '../../build/appMeta.js';
|
|
22
21
|
import authJson from '../../build/auth.js';
|
|
23
|
-
import
|
|
24
|
-
import events from '../../../build/plugins/auth/events.js';
|
|
25
|
-
import providers from '../../../build/plugins/auth/providers.js';
|
|
22
|
+
import strategyTypes from '../../../build/plugins/auth/strategies.js';
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
// Returns the API auth strategy verifiers (memoized in @lowdefy/api), or an
|
|
25
|
+
// empty list when auth is not configured - resolveAuthentication tries them
|
|
26
|
+
// in config order when no session resolves.
|
|
27
|
+
function getStrategies({ logger }) {
|
|
28
|
+
if (authJson.configured !== true) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
return getAuthStrategies({
|
|
29
32
|
appMeta,
|
|
30
33
|
authJson,
|
|
31
|
-
dev: true,
|
|
32
34
|
logger,
|
|
33
|
-
plugins: {
|
|
35
|
+
plugins: { strategies: strategyTypes },
|
|
34
36
|
secrets: getSecretsFromEnv(),
|
|
35
37
|
});
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
export default
|
|
40
|
+
export default getStrategies;
|
|
@@ -15,12 +15,13 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
// Shared between the headless renderer (getBrowser.js, which sets the cookie on
|
|
18
|
-
// its browser context) and the dev
|
|
18
|
+
// its browser context) and the dev caller resolver (getHeadlessUser.js, which
|
|
19
19
|
// decodes it) so the cookie name and injected user shape live in one place.
|
|
20
20
|
const HEADLESS_USER_COOKIE = 'lowdefy_headless_user';
|
|
21
21
|
|
|
22
|
+
// BetterAuth's session user carries `id`, never `sub`. attributes is floored to
|
|
23
|
+
// {} by normalizeInjectedCaller at injection, so it is not spelled out here.
|
|
22
24
|
const headlessUser = {
|
|
23
|
-
sub: 'lowdefy-headless',
|
|
24
25
|
id: 'lowdefy-headless',
|
|
25
26
|
name: 'Lowdefy Headless',
|
|
26
27
|
roles: [],
|
|
@@ -14,9 +14,13 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import fs from 'node:fs';
|
|
18
17
|
import path from 'node:path';
|
|
19
|
-
import {
|
|
18
|
+
import {
|
|
19
|
+
createApiContext,
|
|
20
|
+
normalizeInjectedCaller,
|
|
21
|
+
resolveAuthentication,
|
|
22
|
+
resolvePinnedOrganization,
|
|
23
|
+
} from '@lowdefy/api';
|
|
20
24
|
import { getSecretsFromEnv } from '@lowdefy/node-utils';
|
|
21
25
|
import { v4 as uuid } from 'uuid';
|
|
22
26
|
|
|
@@ -27,55 +31,36 @@ import connections from '../../build/plugins/connections.js';
|
|
|
27
31
|
import createHandleError from './log/createHandleError.js';
|
|
28
32
|
import createLogger from './log/createLogger.js';
|
|
29
33
|
import fileCache from './fileCache.js';
|
|
30
|
-
import
|
|
34
|
+
import getAuth from './auth/getAuth.js';
|
|
35
|
+
import getHeadlessUser from './auth/getHeadlessUser.js';
|
|
36
|
+
import getMockUser from './auth/getMockUser.js';
|
|
37
|
+
import getStrategies from './auth/getStrategies.js';
|
|
31
38
|
import i18nConfig from '../build/i18n.js';
|
|
39
|
+
import loadDynamicJsMap from './loadDynamicJsMap.js';
|
|
32
40
|
import logRequest from './log/logRequest.js';
|
|
33
41
|
import notifications, {
|
|
34
42
|
interpolateProperties,
|
|
35
43
|
renderEmail,
|
|
36
44
|
} from '../../build/plugins/notifications.js';
|
|
37
45
|
import operators from '../../build/plugins/operators/server.js';
|
|
38
|
-
import
|
|
46
|
+
import steps from '../../build/plugins/steps.js';
|
|
39
47
|
import websockets from '../../build/plugins/websockets.js';
|
|
40
48
|
|
|
41
49
|
const secrets = getSecretsFromEnv();
|
|
42
50
|
|
|
43
|
-
// Dynamic JS map loading for JIT-built pages — the build rewrites
|
|
44
|
-
// serverJsMap.js when a JIT page discovers new _js operators.
|
|
45
|
-
let cachedJsMapMtime = null;
|
|
46
|
-
let cachedJsMap = staticJsMap;
|
|
47
|
-
|
|
48
|
-
function loadDynamicJsMap(buildDirectory) {
|
|
49
|
-
const jsMapPath = path.join(buildDirectory, 'plugins', 'operators', 'serverJsMap.js');
|
|
50
|
-
try {
|
|
51
|
-
const stat = fs.statSync(jsMapPath);
|
|
52
|
-
if (cachedJsMapMtime && stat.mtimeMs === cachedJsMapMtime) {
|
|
53
|
-
return cachedJsMap;
|
|
54
|
-
}
|
|
55
|
-
cachedJsMapMtime = stat.mtimeMs;
|
|
56
|
-
// For server-side, we can read and eval the JS file
|
|
57
|
-
const content = fs.readFileSync(jsMapPath, 'utf8');
|
|
58
|
-
const fn = new Function('exports', content.replace('export default', 'exports.default ='));
|
|
59
|
-
const exports = {};
|
|
60
|
-
fn(exports);
|
|
61
|
-
cachedJsMap = { ...staticJsMap, ...(exports.default ?? {}) };
|
|
62
|
-
return cachedJsMap;
|
|
63
|
-
} catch {
|
|
64
|
-
return cachedJsMap;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
51
|
// Builds the per-request lowdefy API context (connections, secrets,
|
|
69
|
-
// operators, logger,
|
|
70
|
-
// (src/middleware/apiContext.js) so run_request
|
|
71
|
-
// build an identical context outside the Hono
|
|
72
|
-
// callRequest directly for agent-driven request
|
|
52
|
+
// operators, logger, resolved user, etc.). Factored out of the /api/*
|
|
53
|
+
// middleware (src/middleware/apiContext.js) so run_request
|
|
54
|
+
// (lib/docs/runRequest.js) can build an identical context outside the Hono
|
|
55
|
+
// middleware chain, to call callRequest directly for agent-driven request
|
|
56
|
+
// execution.
|
|
73
57
|
async function createLowdefyContext({ c }) {
|
|
74
58
|
const buildDirectory = path.join(process.cwd(), 'build');
|
|
75
59
|
const jsMap = loadDynamicJsMap(buildDirectory);
|
|
76
60
|
|
|
61
|
+
const rid = uuid();
|
|
77
62
|
const context = {
|
|
78
|
-
rid
|
|
63
|
+
rid,
|
|
79
64
|
agents,
|
|
80
65
|
appMeta,
|
|
81
66
|
buildDirectory,
|
|
@@ -90,10 +75,7 @@ async function createLowdefyContext({ c }) {
|
|
|
90
75
|
i18n: i18nConfig,
|
|
91
76
|
interpolateProperties,
|
|
92
77
|
jsMap,
|
|
93
|
-
|
|
94
|
-
console.error(err);
|
|
95
|
-
},
|
|
96
|
-
logger: console,
|
|
78
|
+
logger: createLogger({ rid }),
|
|
97
79
|
notifications,
|
|
98
80
|
operators,
|
|
99
81
|
renderEmail,
|
|
@@ -103,12 +85,45 @@ async function createLowdefyContext({ c }) {
|
|
|
103
85
|
hostname: c.req.header('host'),
|
|
104
86
|
},
|
|
105
87
|
secrets,
|
|
88
|
+
steps,
|
|
106
89
|
websockets,
|
|
107
90
|
};
|
|
108
|
-
context.logger = createLogger();
|
|
109
91
|
context.handleError = createHandleError({ context });
|
|
110
|
-
|
|
111
|
-
|
|
92
|
+
const mockUser = getMockUser();
|
|
93
|
+
const headlessUser = getHeadlessUser(c);
|
|
94
|
+
if (mockUser) {
|
|
95
|
+
// The mock user is a pre-resolved caller - it substitutes for the
|
|
96
|
+
// whole resolveAuthentication step and its roles are authoritative.
|
|
97
|
+
// No auth engine runs while dev.mockUser is active (see src/app.js),
|
|
98
|
+
// so mock mode never touches the auth database. normalizeInjectedCaller
|
|
99
|
+
// floors it to the resolved-caller shape.
|
|
100
|
+
context.auth = null;
|
|
101
|
+
context.user = normalizeInjectedCaller(mockUser);
|
|
102
|
+
} else if (headlessUser) {
|
|
103
|
+
// The headless renderer (docs/MCP screenshot and state tools) injects a
|
|
104
|
+
// user cookie on its own browser context (getBrowser.js), so its /api/*
|
|
105
|
+
// fetches carry a pre-resolved caller and auth-protected pages render.
|
|
106
|
+
// The developer's real browser has no cookie and resolves through the
|
|
107
|
+
// auth engine below, so it is unaffected.
|
|
108
|
+
context.auth = null;
|
|
109
|
+
context.user = normalizeInjectedCaller(headlessUser);
|
|
110
|
+
} else {
|
|
111
|
+
// Hoisted once per request - resolveAuthentication also needs it, and
|
|
112
|
+
// getBetterAuth memoizes the instance, but this keeps the auth engine
|
|
113
|
+
// construction to a single call site per request.
|
|
114
|
+
context.auth = getAuth({ logger: context.logger });
|
|
115
|
+
// The engine is constructed lazily on the first request, which would
|
|
116
|
+
// otherwise race the startup pinned-org ensure - await the memoized
|
|
117
|
+
// resolve so createApiContext reads a retained binding.
|
|
118
|
+
await resolvePinnedOrganization({ auth: context.auth, logger: context.logger });
|
|
119
|
+
if (!c.req.path.includes('/api/auth')) {
|
|
120
|
+
// resolveAuthentication is the single writer of context.user.
|
|
121
|
+
await resolveAuthentication(context, {
|
|
122
|
+
auth: context.auth,
|
|
123
|
+
headers: c.req.raw.headers,
|
|
124
|
+
strategies: getStrategies({ logger: context.logger }),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
112
127
|
}
|
|
113
128
|
createApiContext(context);
|
|
114
129
|
logRequest({ context });
|