@lowdefy/server 7.1.0 → 7.3.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/client/clientBuildImports.test.mjs +1 -0
- package/client/getShownPage.test.mjs +1 -0
- package/client/shouldReloadForTypes.test.mjs +1 -0
- package/lib/build/mobileConfig.js +26 -0
- package/lib/client/auth/AuthConfigured.jsx +75 -48
- package/lib/client/auth/createFetchCheckingBuild.test.mjs +3 -3
- package/lib/client/auth/sessionCaller.js +56 -0
- package/lib/client/createLogUsage.test.mjs +2 -2
- package/lib/server/getClientAddress.test.mjs +6 -7
- package/lib/server/log/createHandleError.test.mjs +5 -5
- package/lib/server/log/createLogger.test.mjs +3 -3
- package/lib/server/log/projectErrorForLog.js +6 -4
- package/lib/server/log/redactUrlQuery.test.mjs +1 -0
- package/lib/server/log/serializeErrorForLog.test.mjs +50 -113
- package/lib/server/sentry/captureSentryError.test.mjs +4 -7
- package/lib/server/sentry/initSentry.serverless.test.mjs +6 -6
- package/lib/server/sentry/initSentry.test.mjs +3 -3
- package/lib/server/sentry/scrubEvent.test.mjs +2 -0
- package/lowdefy/build.mjs +4 -1
- package/lowdefy/build.test.mjs +11 -13
- package/lowdefy/createCustomPluginMessagesMap.mjs +2 -4
- package/lowdefy/createCustomPluginTypesMap.mjs +39 -11
- package/lowdefy/createCustomPluginTypesMap.test.mjs +249 -0
- package/package.json +32 -33
- package/package.original.json +32 -33
- package/src/app.js +19 -2
- package/src/app.mobile.test.mjs +351 -0
- package/src/app.test.mjs +31 -27
- package/src/app.timeout.test.mjs +141 -0
- package/src/html/collectPageTypesAssets.test.mjs +1 -0
- package/src/html/getPageAssets.test.mjs +1 -0
- package/src/html/renderPage.contentSecurityPolicy.test.mjs +13 -10
- package/src/html/renderPage.test.mjs +16 -13
- package/src/html/template.test.mjs +1 -0
- package/src/initServer.test.mjs +12 -12
- package/src/lib/getPathSegments.js +16 -1
- package/src/lib/getRequestSurface.js +25 -0
- package/src/middleware/apiContext.js +7 -4
- package/src/middleware/apiContext.test.mjs +48 -30
- package/src/middleware/createSameOriginGuard.test.mjs +1 -0
- package/src/middleware/errorHandler.js +33 -45
- package/src/middleware/errorHandler.test.mjs +11 -11
- package/src/middleware/refuseOtherBuild.test.mjs +3 -3
- package/src/middleware/requestTimeout.js +7 -1
- package/src/middleware/stampBuildId.test.mjs +1 -0
- package/src/middleware/stampCaller.test.mjs +1 -0
- package/src/routes/agentBearer.test.mjs +175 -0
- package/src/routes/apiPage.js +12 -1
- package/src/routes/apiPage.test.mjs +5 -5
- package/src/routes/auth.test.mjs +6 -6
- package/src/routes/clientError.js +14 -2
- package/src/routes/clientError.test.mjs +106 -0
- package/src/routes/endpoints.js +24 -0
- package/src/routes/endpointsLink.test.mjs +112 -0
- package/src/routes/endpointsTextAnswer.test.mjs +8 -8
- package/src/routes/mcp.js +6 -0
- package/src/routes/mcp.test.mjs +23 -8
- package/src/routes/mcpMemberToken.test.mjs +8 -8
- package/src/routes/mcpProtectedResourceMetadata.test.mjs +2 -2
- package/src/routes/mobileRoot.js +30 -0
- package/src/routes/mobileSignIn.js +46 -0
- package/src/routes/mobileSignIn.test.mjs +58 -0
- package/src/routes/mobileSignInCallback.js +34 -0
- package/src/routes/mobileSignInCallback.test.mjs +51 -0
- package/src/routes/mountMobileWellKnown.js +35 -0
- package/src/routes/mountMobileWellKnown.test.mjs +79 -0
- package/src/routes/mountOauthDiscovery.test.mjs +5 -5
- package/src/routes/mountPageRoutes.js +9 -2
- package/src/routes/mountPageRoutes.test.mjs +33 -3
- package/src/routes/normalizeMcpProtocolVersionHeader.test.mjs +1 -0
- package/src/routes/requestBody.test.mjs +12 -12
- package/src/routes/websocket.js +4 -2
- package/src/routes/websocket.test.mjs +2 -2
- package/lib/server/log/maskCredentialKeys.js +0 -37
package/src/routes/mcp.test.mjs
CHANGED
|
@@ -15,20 +15,20 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
-
import {
|
|
18
|
+
import { vi, afterAll, beforeEach, expect, test } from 'vitest';
|
|
19
19
|
|
|
20
20
|
// The real URI helpers drive the challenge strings, so the header assertions
|
|
21
21
|
// below exercise the actual BETTER_AUTH_URL derivation - only createMcpServer
|
|
22
22
|
// is stubbed out.
|
|
23
23
|
const uriHelpers = await import('@lowdefy/api/routes/mcp/getMcpUri.js');
|
|
24
24
|
|
|
25
|
-
const mockCreateMcpServer =
|
|
26
|
-
|
|
25
|
+
const mockCreateMcpServer = vi.fn();
|
|
26
|
+
vi.doMock('@lowdefy/api', () => ({
|
|
27
27
|
...uriHelpers,
|
|
28
28
|
createMcpServer: mockCreateMcpServer,
|
|
29
29
|
}));
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
vi.doMock('@hono/mcp', () => ({
|
|
32
32
|
StreamableHTTPTransport: class {
|
|
33
33
|
handleRequest() {
|
|
34
34
|
return new Response(JSON.stringify({ served: true }), {
|
|
@@ -40,7 +40,7 @@ jest.unstable_mockModule('@hono/mcp', () => ({
|
|
|
40
40
|
}));
|
|
41
41
|
|
|
42
42
|
const mockAuthJson = { configured: true, oauthProvider: { consentPage: '/oauth/consent' } };
|
|
43
|
-
|
|
43
|
+
vi.doMock('../../lib/build/auth.js', () => ({
|
|
44
44
|
default: mockAuthJson,
|
|
45
45
|
}));
|
|
46
46
|
|
|
@@ -48,7 +48,7 @@ const { default: mcpHandler } = await import('./mcp.js');
|
|
|
48
48
|
|
|
49
49
|
const originalBetterAuthUrl = process.env.BETTER_AUTH_URL;
|
|
50
50
|
|
|
51
|
-
const mockReadConfigFile =
|
|
51
|
+
const mockReadConfigFile = vi.fn();
|
|
52
52
|
|
|
53
53
|
let contextOverrides;
|
|
54
54
|
|
|
@@ -57,7 +57,7 @@ function createApp() {
|
|
|
57
57
|
app.use('*', async (c, next) => {
|
|
58
58
|
c.set('lowdefyContext', {
|
|
59
59
|
config: { basePath: '' },
|
|
60
|
-
logger: { debug:
|
|
60
|
+
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
|
61
61
|
readConfigFile: mockReadConfigFile,
|
|
62
62
|
...contextOverrides,
|
|
63
63
|
});
|
|
@@ -73,7 +73,7 @@ beforeEach(() => {
|
|
|
73
73
|
contextOverrides = {};
|
|
74
74
|
mockReadConfigFile.mockReset();
|
|
75
75
|
mockCreateMcpServer.mockReset();
|
|
76
|
-
mockCreateMcpServer.mockResolvedValue({ connect:
|
|
76
|
+
mockCreateMcpServer.mockResolvedValue({ connect: vi.fn() });
|
|
77
77
|
mockReadConfigFile.mockResolvedValue({ configured: true, hasPublicTool: true, endpoints: [] });
|
|
78
78
|
});
|
|
79
79
|
|
|
@@ -213,3 +213,18 @@ test('mcpHandler serves openly with no envelope when oauthProvider is not config
|
|
|
213
213
|
expect(res.status).toEqual(200);
|
|
214
214
|
expect(await res.json()).toEqual({ served: true });
|
|
215
215
|
});
|
|
216
|
+
|
|
217
|
+
test('mcpHandler answers a GET with 405, since the stateless server offers no event stream', async () => {
|
|
218
|
+
contextOverrides.mcpAuth = { tokenStatus: 'valid' };
|
|
219
|
+
const res = await createApp().request('/api/mcp', { method: 'GET' });
|
|
220
|
+
expect(res.status).toEqual(405);
|
|
221
|
+
expect(res.headers.get('Allow')).toEqual('POST');
|
|
222
|
+
expect(await res.json()).toEqual({ error: 'Method not allowed.' });
|
|
223
|
+
expect(mockCreateMcpServer).not.toHaveBeenCalled();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test('mcpHandler challenges a GET with an invalid token before answering 405', async () => {
|
|
227
|
+
contextOverrides.mcpAuth = { tokenStatus: 'invalid', parseableJwt: true };
|
|
228
|
+
const res = await createApp().request('/api/mcp', { method: 'GET' });
|
|
229
|
+
expect(res.status).toEqual(401);
|
|
230
|
+
});
|
|
@@ -15,20 +15,20 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
-
import {
|
|
18
|
+
import { vi, afterAll, beforeEach, expect, test } from 'vitest';
|
|
19
19
|
|
|
20
20
|
// The real URI helpers drive the challenge strings, so the header assertions
|
|
21
21
|
// below exercise the actual BETTER_AUTH_URL derivation - only createMcpServer
|
|
22
22
|
// is stubbed out.
|
|
23
23
|
const uriHelpers = await import('@lowdefy/api/routes/mcp/getMcpUri.js');
|
|
24
24
|
|
|
25
|
-
const mockCreateMcpServer =
|
|
26
|
-
|
|
25
|
+
const mockCreateMcpServer = vi.fn();
|
|
26
|
+
vi.doMock('@lowdefy/api', () => ({
|
|
27
27
|
...uriHelpers,
|
|
28
28
|
createMcpServer: mockCreateMcpServer,
|
|
29
29
|
}));
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
vi.doMock('@hono/mcp', () => ({
|
|
32
32
|
StreamableHTTPTransport: class {
|
|
33
33
|
handleRequest() {
|
|
34
34
|
return new Response(JSON.stringify({ served: true }), {
|
|
@@ -40,7 +40,7 @@ jest.unstable_mockModule('@hono/mcp', () => ({
|
|
|
40
40
|
}));
|
|
41
41
|
|
|
42
42
|
const mockAuthJson = { configured: true, oauthProvider: { consentPage: '/oauth/consent' } };
|
|
43
|
-
|
|
43
|
+
vi.doMock('../../lib/build/auth.js', () => ({
|
|
44
44
|
default: mockAuthJson,
|
|
45
45
|
}));
|
|
46
46
|
|
|
@@ -48,7 +48,7 @@ const { default: mcpHandler } = await import('./mcp.js');
|
|
|
48
48
|
|
|
49
49
|
const originalBetterAuthUrl = process.env.BETTER_AUTH_URL;
|
|
50
50
|
|
|
51
|
-
const mockReadConfigFile =
|
|
51
|
+
const mockReadConfigFile = vi.fn();
|
|
52
52
|
|
|
53
53
|
let contextOverrides;
|
|
54
54
|
|
|
@@ -57,7 +57,7 @@ function createApp() {
|
|
|
57
57
|
app.use('*', async (c, next) => {
|
|
58
58
|
c.set('lowdefyContext', {
|
|
59
59
|
config: { basePath: '' },
|
|
60
|
-
logger: { debug:
|
|
60
|
+
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
|
61
61
|
readConfigFile: mockReadConfigFile,
|
|
62
62
|
...contextOverrides,
|
|
63
63
|
});
|
|
@@ -73,7 +73,7 @@ beforeEach(() => {
|
|
|
73
73
|
contextOverrides = {};
|
|
74
74
|
mockReadConfigFile.mockReset();
|
|
75
75
|
mockCreateMcpServer.mockReset();
|
|
76
|
-
mockCreateMcpServer.mockResolvedValue({ connect:
|
|
76
|
+
mockCreateMcpServer.mockResolvedValue({ connect: vi.fn() });
|
|
77
77
|
mockReadConfigFile.mockResolvedValue({ configured: true, hasPublicTool: true, endpoints: [] });
|
|
78
78
|
});
|
|
79
79
|
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
-
import {
|
|
18
|
+
import { vi, afterAll, beforeEach, expect, test } from 'vitest';
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
vi.doMock('../../lib/build/config.js', () => ({
|
|
21
21
|
default: { basePath: '' },
|
|
22
22
|
}));
|
|
23
23
|
|
|
@@ -0,0 +1,30 @@
|
|
|
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 { getMobileRootConfig } from '@lowdefy/api';
|
|
18
|
+
|
|
19
|
+
// The root config a shell syncs on start (getMobileRootConfig), filtered to
|
|
20
|
+
// the caller. An app without a mobile key has no shell, so it answers 404.
|
|
21
|
+
async function mobileRootHandler(c) {
|
|
22
|
+
const context = c.get('lowdefyContext');
|
|
23
|
+
const rootConfig = await getMobileRootConfig(context);
|
|
24
|
+
if (rootConfig === null) {
|
|
25
|
+
return c.json({ name: 'NotFoundError', message: 'This app has no mobile config.' }, 404);
|
|
26
|
+
}
|
|
27
|
+
return c.json(rootConfig);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default mobileRootHandler;
|
|
@@ -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 { handleMobileSignIn } from '@lowdefy/api';
|
|
18
|
+
import { type } from '@lowdefy/helpers';
|
|
19
|
+
|
|
20
|
+
import authJson from '../../lib/build/auth.js';
|
|
21
|
+
import getAuth from '../../lib/server/auth/getAuth.js';
|
|
22
|
+
import lowdefyConfig from '../../lib/build/config.js';
|
|
23
|
+
import themeConfig from '../../lib/build/theme.js';
|
|
24
|
+
|
|
25
|
+
// GET and POST /auth/mobile - the sign-in page a phone shell opens in a system
|
|
26
|
+
// sheet. handleMobileSignIn sends the person through the app's sign-in page,
|
|
27
|
+
// asks them to confirm (GET), and on Continue (POST) returns a one-time token
|
|
28
|
+
// for a new phone session, bound to the shell's PKCE code challenge, to the
|
|
29
|
+
// shell's scheme (iOS) or app link (Android). The page takes the app theme's
|
|
30
|
+
// primary colour. An app without a mobile section has no such page, so the
|
|
31
|
+
// path falls through to the app's own pages.
|
|
32
|
+
function mobileSignInHandler(c, next) {
|
|
33
|
+
if (type.isNone(authJson.mobile)) {
|
|
34
|
+
return next();
|
|
35
|
+
}
|
|
36
|
+
const { logger } = c.get('lowdefyContext');
|
|
37
|
+
return handleMobileSignIn({
|
|
38
|
+
auth: authJson.configured === true ? getAuth({ logger }) : null,
|
|
39
|
+
authJson,
|
|
40
|
+
basePath: lowdefyConfig.basePath ?? '',
|
|
41
|
+
primaryColor: themeConfig.antd?.token?.colorPrimary,
|
|
42
|
+
request: c.req.raw,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default mobileSignInHandler;
|
|
@@ -0,0 +1,58 @@
|
|
|
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 { vi, beforeEach, expect, test } from 'vitest';
|
|
18
|
+
|
|
19
|
+
const authJson = {};
|
|
20
|
+
const handleMobileSignIn = vi.fn(() => new Response('mobile sign-in'));
|
|
21
|
+
|
|
22
|
+
vi.doMock('@lowdefy/api', () => ({ handleMobileSignIn }));
|
|
23
|
+
vi.doMock('../../lib/build/auth.js', () => ({ default: authJson }));
|
|
24
|
+
vi.doMock('../../lib/build/config.js', () => ({ default: {} }));
|
|
25
|
+
vi.doMock('../../lib/build/theme.js', () => ({
|
|
26
|
+
default: { antd: { token: { colorPrimary: '#00aa55' } } },
|
|
27
|
+
}));
|
|
28
|
+
vi.doMock('../../lib/server/auth/getAuth.js', () => ({ default: () => null }));
|
|
29
|
+
const { default: mobileSignInHandler } = await import('./mobileSignIn.js');
|
|
30
|
+
|
|
31
|
+
function createContext() {
|
|
32
|
+
return {
|
|
33
|
+
get: () => ({ logger: {} }),
|
|
34
|
+
req: { raw: new Request('http://localhost/auth/mobile') },
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
delete authJson.mobile;
|
|
40
|
+
handleMobileSignIn.mockClear();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('mobileSignInHandler falls through to the app pages when the app has no mobile section', async () => {
|
|
44
|
+
const next = vi.fn(() => 'page');
|
|
45
|
+
expect(await mobileSignInHandler(createContext(), next)).toEqual('page');
|
|
46
|
+
expect(handleMobileSignIn).not.toHaveBeenCalled();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('mobileSignInHandler serves the mobile sign-in page when the app has a mobile section', async () => {
|
|
50
|
+
authJson.mobile = { shells: [] };
|
|
51
|
+
const next = vi.fn();
|
|
52
|
+
const res = await mobileSignInHandler(createContext(), next);
|
|
53
|
+
expect(await res.text()).toEqual('mobile sign-in');
|
|
54
|
+
expect(next).not.toHaveBeenCalled();
|
|
55
|
+
expect(handleMobileSignIn).toHaveBeenCalledWith(
|
|
56
|
+
expect.objectContaining({ basePath: '', primaryColor: '#00aa55' })
|
|
57
|
+
);
|
|
58
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
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 { handleMobileSignInCallback } from '@lowdefy/api';
|
|
18
|
+
import { type } from '@lowdefy/helpers';
|
|
19
|
+
|
|
20
|
+
import authJson from '../../lib/build/auth.js';
|
|
21
|
+
import themeConfig from '../../lib/build/theme.js';
|
|
22
|
+
|
|
23
|
+
// GET /auth/mobile/callback - the Android app link the sign-in sheet returns
|
|
24
|
+
// to. Android opens it in the shell; this page shows only when the link is not
|
|
25
|
+
// verified, and asks the person to return to the app. An app without a mobile
|
|
26
|
+
// section has no such page, so the path falls through to the app's own pages.
|
|
27
|
+
function mobileSignInCallbackHandler(c, next) {
|
|
28
|
+
if (type.isNone(authJson.mobile)) {
|
|
29
|
+
return next();
|
|
30
|
+
}
|
|
31
|
+
return handleMobileSignInCallback({ primaryColor: themeConfig.antd?.token?.colorPrimary });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default mobileSignInCallbackHandler;
|
|
@@ -0,0 +1,51 @@
|
|
|
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 { vi, beforeEach, expect, test } from 'vitest';
|
|
18
|
+
|
|
19
|
+
const authJson = {};
|
|
20
|
+
|
|
21
|
+
vi.doMock('../../lib/build/auth.js', () => ({ default: authJson }));
|
|
22
|
+
vi.doMock('../../lib/build/theme.js', () => ({
|
|
23
|
+
default: { antd: { token: { colorPrimary: '#00aa55' } } },
|
|
24
|
+
}));
|
|
25
|
+
const { default: mobileSignInCallbackHandler } = await import('./mobileSignInCallback.js');
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
delete authJson.mobile;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('mobileSignInCallbackHandler falls through to the app pages when the app has no mobile section', async () => {
|
|
32
|
+
const next = vi.fn(() => 'page');
|
|
33
|
+
expect(await mobileSignInCallbackHandler({}, next)).toEqual('page');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('mobileSignInCallbackHandler asks the person to return to the app and never echoes the token', async () => {
|
|
37
|
+
authJson.mobile = { shells: [] };
|
|
38
|
+
const next = vi.fn();
|
|
39
|
+
const res = await mobileSignInCallbackHandler(
|
|
40
|
+
{ req: { raw: new Request('http://localhost/auth/mobile/callback?token=ott-secret&state=s') } },
|
|
41
|
+
next
|
|
42
|
+
);
|
|
43
|
+
expect(next).not.toHaveBeenCalled();
|
|
44
|
+
expect(res.status).toEqual(200);
|
|
45
|
+
expect(res.headers.get('cache-control')).toEqual('no-store');
|
|
46
|
+
expect(res.headers.get('referrer-policy')).toEqual('no-referrer');
|
|
47
|
+
const html = await res.text();
|
|
48
|
+
expect(html).toContain('Return to the app');
|
|
49
|
+
expect(html).toContain('--primary: #00aa55;');
|
|
50
|
+
expect(html).not.toContain('ott-secret');
|
|
51
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
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 { createAppleAppSiteAssociation, createAssetLinks } from '@lowdefy/api';
|
|
18
|
+
|
|
19
|
+
// The universal link (iOS) and app link (Android) documents for the app's
|
|
20
|
+
// shells, from mobile.shells. Both are public, unauthenticated and constant per
|
|
21
|
+
// build, so they are built once and mount outside the api context, before the
|
|
22
|
+
// /.well-known/* catch-all. A document no shell needs is not mounted, so the
|
|
23
|
+
// catch-all answers 404 for it.
|
|
24
|
+
function mountMobileWellKnown({ app, basePath, mobileConfig }) {
|
|
25
|
+
const appleAppSiteAssociation = createAppleAppSiteAssociation({ mobileConfig, basePath });
|
|
26
|
+
if (appleAppSiteAssociation !== null) {
|
|
27
|
+
app.get('/.well-known/apple-app-site-association', (c) => c.json(appleAppSiteAssociation));
|
|
28
|
+
}
|
|
29
|
+
const assetLinks = createAssetLinks({ mobileConfig });
|
|
30
|
+
if (assetLinks !== null) {
|
|
31
|
+
app.get('/.well-known/assetlinks.json', (c) => c.json(assetLinks));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default mountMobileWellKnown;
|
|
@@ -0,0 +1,79 @@
|
|
|
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 { expect, test } from 'vitest';
|
|
18
|
+
import { Hono } from 'hono';
|
|
19
|
+
|
|
20
|
+
import mountMobileWellKnown from './mountMobileWellKnown.js';
|
|
21
|
+
import wellKnownFallbackHandler from './wellKnownFallback.js';
|
|
22
|
+
|
|
23
|
+
function createApp({ mobileConfig, basePath = '' }) {
|
|
24
|
+
const app = new Hono();
|
|
25
|
+
mountMobileWellKnown({ app, basePath, mobileConfig });
|
|
26
|
+
app.all('/.well-known/*', wellKnownFallbackHandler);
|
|
27
|
+
return app;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
test('mountMobileWellKnown serves both documents when shells take iOS and Android links', async () => {
|
|
31
|
+
const app = createApp({
|
|
32
|
+
mobileConfig: {
|
|
33
|
+
shells: [
|
|
34
|
+
{ id: 'ios', ios: { bundleId: 'com.example.ios', teamId: 'TEAM1' } },
|
|
35
|
+
{ id: 'droid', android: { package: 'com.example.droid', sha256: ['AA:BB'] } },
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
basePath: '/app',
|
|
39
|
+
});
|
|
40
|
+
const aasa = await app.request('/.well-known/apple-app-site-association');
|
|
41
|
+
expect(aasa.status).toEqual(200);
|
|
42
|
+
const aasaBody = await aasa.json();
|
|
43
|
+
expect(aasaBody.applinks.details[0].appIDs).toEqual(['TEAM1.com.example.ios']);
|
|
44
|
+
expect(aasaBody.applinks.details[0].components).toEqual([
|
|
45
|
+
{ '/': '/app/api/*', exclude: true },
|
|
46
|
+
{ '/': '/app/*' },
|
|
47
|
+
]);
|
|
48
|
+
const assetLinks = await app.request('/.well-known/assetlinks.json');
|
|
49
|
+
expect(assetLinks.status).toEqual(200);
|
|
50
|
+
const assetLinksBody = await assetLinks.json();
|
|
51
|
+
expect(assetLinksBody[0].target.package_name).toEqual('com.example.droid');
|
|
52
|
+
// The sign-in sheet's Android app link (/auth/mobile/callback) is verified by this relation.
|
|
53
|
+
expect(assetLinksBody[0].relation).toEqual(['delegate_permission/common.handle_all_urls']);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('mountMobileWellKnown leaves both to the 404 catch-all for an app without shells', async () => {
|
|
57
|
+
const app = createApp({ mobileConfig: {} });
|
|
58
|
+
const aasa = await app.request('/.well-known/apple-app-site-association');
|
|
59
|
+
expect(aasa.status).toEqual(404);
|
|
60
|
+
expect((await aasa.json()).name).toEqual('NotFoundError');
|
|
61
|
+
const assetLinks = await app.request('/.well-known/assetlinks.json');
|
|
62
|
+
expect(assetLinks.status).toEqual(404);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('mountMobileWellKnown serves only the document a shell needs', async () => {
|
|
66
|
+
const app = createApp({
|
|
67
|
+
mobileConfig: { shells: [{ id: 'ios', ios: { bundleId: 'com.example.ios', teamId: 'T' } }] },
|
|
68
|
+
});
|
|
69
|
+
expect((await app.request('/.well-known/apple-app-site-association')).status).toEqual(200);
|
|
70
|
+
expect((await app.request('/.well-known/assetlinks.json')).status).toEqual(404);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('the catch-all still answers other well-known paths', async () => {
|
|
74
|
+
const app = createApp({
|
|
75
|
+
mobileConfig: { shells: [{ id: 'ios', ios: { bundleId: 'com.example.ios', teamId: 'T' } }] },
|
|
76
|
+
});
|
|
77
|
+
const res = await app.request('/.well-known/openid-configuration');
|
|
78
|
+
expect(res.status).toEqual(404);
|
|
79
|
+
});
|
|
@@ -15,25 +15,25 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
-
import {
|
|
18
|
+
import { vi, beforeEach, expect, test } from 'vitest';
|
|
19
19
|
|
|
20
|
-
const mockCreateAsMetadataHandler =
|
|
20
|
+
const mockCreateAsMetadataHandler = vi.fn(
|
|
21
21
|
() => async () =>
|
|
22
22
|
new Response(JSON.stringify({ issuer: 'https://app.test.com/api/auth' }), {
|
|
23
23
|
status: 200,
|
|
24
24
|
headers: { 'content-type': 'application/json' },
|
|
25
25
|
})
|
|
26
26
|
);
|
|
27
|
-
|
|
27
|
+
vi.doMock('@lowdefy/api', () => ({
|
|
28
28
|
createAsMetadataHandler: mockCreateAsMetadataHandler,
|
|
29
29
|
}));
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
vi.doMock('./mcpProtectedResourceMetadata.js', () => ({
|
|
32
32
|
default: (c) => c.json({ resource: 'mcp' }),
|
|
33
33
|
}));
|
|
34
34
|
|
|
35
35
|
const mockAuthJson = { configured: true };
|
|
36
|
-
|
|
36
|
+
vi.doMock('../../lib/build/auth.js', () => ({
|
|
37
37
|
default: mockAuthJson,
|
|
38
38
|
}));
|
|
39
39
|
|
|
@@ -15,14 +15,21 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import getRequestPath from '../lib/getRequestPath.js';
|
|
18
|
+
import mobileSignInCallbackHandler from './mobileSignInCallback.js';
|
|
19
|
+
import mobileSignInHandler from './mobileSignIn.js';
|
|
18
20
|
|
|
19
|
-
// `/` (home)
|
|
20
|
-
//
|
|
21
|
+
// `/` (home), `/404`, `/auth/mobile` (the sign-in page a phone shell opens, and
|
|
22
|
+
// its confirmation post) and `/auth/mobile/callback` (its Android app link) are
|
|
23
|
+
// fixed routes ahead of the page matcher; every other path renders the page
|
|
24
|
+
// it matches. `.*`, not `.+`: under a basePath,
|
|
21
25
|
// `<basePath>/` arrives with an empty rest, and is the app root like
|
|
22
26
|
// `<basePath>` itself.
|
|
23
27
|
function mountPageRoutes({ app, basePath, renderPage }) {
|
|
24
28
|
app.get('/', (c) => renderPage(c, { path: '', matchedPath: '' }));
|
|
25
29
|
app.get('/404', (c) => renderPage(c, { path: '404', matchedPath: '404', status: 404 }));
|
|
30
|
+
app.get('/auth/mobile', mobileSignInHandler);
|
|
31
|
+
app.post('/auth/mobile', mobileSignInHandler);
|
|
32
|
+
app.get('/auth/mobile/callback', mobileSignInCallbackHandler);
|
|
26
33
|
app.get('/:rest{.*}', (c) => renderPage(c, getRequestPath({ c, basePath, prefix: '/' })));
|
|
27
34
|
}
|
|
28
35
|
|
|
@@ -15,10 +15,18 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
+
import { vi, expect, test } from 'vitest';
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
vi.doMock('./mobileSignIn.js', () => ({
|
|
21
|
+
default: (c) => c.json({ mobileSignIn: c.req.query('shell'), method: c.req.method }),
|
|
22
|
+
}));
|
|
23
|
+
vi.doMock('./mobileSignInCallback.js', () => ({
|
|
24
|
+
default: (c) => c.json({ mobileSignInCallback: true }),
|
|
25
|
+
}));
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
const { default: mountPageRoutes } = await import('./mountPageRoutes.js');
|
|
28
|
+
|
|
29
|
+
function request({ basePath, path, method = 'GET' }) {
|
|
22
30
|
const app = basePath === '' ? new Hono() : new Hono().basePath(basePath);
|
|
23
31
|
mountPageRoutes({
|
|
24
32
|
app,
|
|
@@ -26,7 +34,7 @@ function request({ basePath, path }) {
|
|
|
26
34
|
renderPage: (c, { path: pagePath, matchedPath, status = 200 }) =>
|
|
27
35
|
c.json({ path: pagePath, matchedPath }, status),
|
|
28
36
|
});
|
|
29
|
-
return app.request(path);
|
|
37
|
+
return app.request(path, { method });
|
|
30
38
|
}
|
|
31
39
|
|
|
32
40
|
test.each([
|
|
@@ -54,3 +62,25 @@ test.each([[''], ['/app']])(
|
|
|
54
62
|
expect(await res.json()).toEqual({ path: '404', matchedPath: '404' });
|
|
55
63
|
}
|
|
56
64
|
);
|
|
65
|
+
|
|
66
|
+
test.each([[''], ['/app']])(
|
|
67
|
+
'with basePath "%s", /auth/mobile is the mobile sign-in route, not a page',
|
|
68
|
+
async (basePath) => {
|
|
69
|
+
const res = await request({ basePath, path: `${basePath}/auth/mobile?shell=field&state=s` });
|
|
70
|
+
expect(await res.json()).toEqual({ mobileSignIn: 'field', method: 'GET' });
|
|
71
|
+
const posted = await request({
|
|
72
|
+
basePath,
|
|
73
|
+
path: `${basePath}/auth/mobile?shell=field&state=s`,
|
|
74
|
+
method: 'POST',
|
|
75
|
+
});
|
|
76
|
+
expect(await posted.json()).toEqual({ mobileSignIn: 'field', method: 'POST' });
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
test.each([[''], ['/app']])(
|
|
81
|
+
'with basePath "%s", /auth/mobile/callback is the sign-in app link route, not a page',
|
|
82
|
+
async (basePath) => {
|
|
83
|
+
const res = await request({ basePath, path: `${basePath}/auth/mobile/callback?state=s` });
|
|
84
|
+
expect(await res.json()).toEqual({ mobileSignInCallback: true });
|
|
85
|
+
}
|
|
86
|
+
);
|
|
@@ -15,23 +15,23 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { Hono } from 'hono';
|
|
18
|
-
import {
|
|
18
|
+
import { vi, afterAll, beforeAll, expect, test } from 'vitest';
|
|
19
19
|
|
|
20
20
|
import { isSameOriginRequest, parseRequestBody } from '@lowdefy/api';
|
|
21
21
|
|
|
22
|
-
const mockCallEndpoint =
|
|
23
|
-
const mockGetEndpointConfig =
|
|
24
|
-
const mockRunWebhookEndpoint =
|
|
25
|
-
|
|
26
|
-
acceptDetachedEndpoint:
|
|
27
|
-
callAgent:
|
|
22
|
+
const mockCallEndpoint = vi.fn();
|
|
23
|
+
const mockGetEndpointConfig = vi.fn().mockRejectedValue(new Error('not found'));
|
|
24
|
+
const mockRunWebhookEndpoint = vi.fn();
|
|
25
|
+
vi.doMock('@lowdefy/api', () => ({
|
|
26
|
+
acceptDetachedEndpoint: vi.fn(),
|
|
27
|
+
callAgent: vi.fn(),
|
|
28
28
|
callEndpoint: mockCallEndpoint,
|
|
29
|
-
callRequest:
|
|
29
|
+
callRequest: vi.fn(),
|
|
30
30
|
getEndpointConfig: mockGetEndpointConfig,
|
|
31
|
-
logClientError:
|
|
31
|
+
logClientError: vi.fn(),
|
|
32
32
|
isSameOriginRequest,
|
|
33
33
|
parseRequestBody,
|
|
34
|
-
redactErrorResponse:
|
|
34
|
+
redactErrorResponse: vi.fn(),
|
|
35
35
|
runWebhookEndpoint: mockRunWebhookEndpoint,
|
|
36
36
|
}));
|
|
37
37
|
|
|
@@ -45,12 +45,12 @@ const routes = {
|
|
|
45
45
|
'/api/usage': (await import('./usage.js')).default,
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
const logger = { debug:
|
|
48
|
+
const logger = { debug: vi.fn(), error: vi.fn(), info: vi.fn(), warn: vi.fn() };
|
|
49
49
|
|
|
50
50
|
function createApp() {
|
|
51
51
|
const app = new Hono();
|
|
52
52
|
app.use('*', async (c, next) => {
|
|
53
|
-
c.set('lowdefyContext', { logger, handleError:
|
|
53
|
+
c.set('lowdefyContext', { logger, handleError: vi.fn() });
|
|
54
54
|
await next();
|
|
55
55
|
});
|
|
56
56
|
Object.entries(routes).forEach(([path, handler]) => app.all(path, handler));
|