@lowdefy/api 7.0.0 → 7.2.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/dist/context/createAuthorizeOutcome.js +1 -1
- package/dist/context/createEvaluateOperators.js +2 -1
- package/dist/context/isWebSocketOriginAllowed.js +3 -1
- package/dist/context/readMemberships.js +10 -4
- package/dist/context/resolveAuthentication.js +95 -21
- package/dist/email/renderAuthEmail.js +3 -2
- package/dist/index.js +7 -1
- package/dist/response/redactErrorResponse.js +8 -4
- package/dist/routes/agent/callAgent.js +20 -2
- package/dist/routes/agent/redactMessages.js +136 -0
- package/dist/routes/auth/createAuthLogger.js +23 -6
- package/dist/routes/auth/createMcpAccessTokenClaims.js +8 -6
- package/dist/routes/auth/createOnAPIError.js +15 -5
- package/dist/routes/auth/createSendEmail.js +15 -5
- package/dist/routes/auth/getBetterAuth.js +3 -3
- package/dist/routes/auth/getBetterAuthConfig.js +163 -92
- package/dist/routes/auth/hooks/buildEngineHooks.js +15 -1
- package/dist/routes/auth/hooks/createUserAfterHook.js +17 -2
- package/dist/routes/auth/hooks/createUserBeforeHook.js +20 -4
- package/dist/routes/auth/mobile/buildBearerPlugin.js +45 -0
- package/dist/routes/auth/mobile/buildOneTimeTokenPlugin.js +50 -0
- package/dist/routes/auth/mobile/createCodeVerifierCheck.js +57 -0
- package/dist/routes/auth/mobile/createKnownDeviceHook.js +76 -0
- package/dist/routes/auth/mobile/createMobileAuthPageResponse.js +79 -0
- package/dist/routes/auth/mobile/createMobileSessionCreateHook.js +41 -0
- package/dist/routes/auth/mobile/createMobileSessionRefreshHook.js +40 -0
- package/dist/routes/auth/mobile/createMobileSignInCsrfToken.js +32 -0
- package/dist/routes/auth/mobile/createPhoneSession.js +59 -0
- package/dist/routes/auth/mobile/escapeHtml.js +26 -0
- package/dist/routes/auth/mobile/getCodeHolderIdentifier.js +20 -0
- package/dist/routes/auth/mobile/getKnownDeviceIdentifier.js +20 -0
- package/dist/routes/auth/mobile/getMobileSessionExpiry.js +18 -0
- package/dist/routes/auth/mobile/getMobileSignInReturnUrl.js +31 -0
- package/dist/routes/auth/mobile/getOneTimeTokenIdentifiers.js +26 -0
- package/dist/routes/auth/mobile/handleMobileSignIn.js +303 -0
- package/dist/routes/auth/mobile/handleMobileSignInCallback.js +30 -0
- package/dist/routes/auth/mobile/isNativeAuthRequest.js +33 -0
- package/dist/routes/auth/mobile/matchesCodeChallenge.js +29 -0
- package/dist/routes/auth/mobile/mintOneTimeToken.js +41 -0
- package/dist/routes/auth/mobile/readDeviceKeyHash.js +34 -0
- package/dist/routes/auth/mobile/readMobileSignInParams.js +90 -0
- package/dist/routes/auth/mobile/sha256Base64Url.js +22 -0
- package/dist/routes/auth/mobile/storeCodeChallenge.js +29 -0
- package/dist/routes/auth/organizations/buildOrganizationPlugin.js +43 -6
- package/dist/routes/auth/organizations/createAcceptExistingMemberHook.js +7 -2
- package/dist/routes/auth/organizations/createActiveOrgPolicyHook.js +41 -189
- package/dist/routes/auth/organizations/createAdmissionGateHook.js +5 -5
- package/dist/routes/auth/organizations/createBeforeCreateInvitationHook.js +62 -0
- package/dist/routes/auth/organizations/createBeforeUpdateMemberRoleHook.js +45 -0
- package/dist/routes/auth/organizations/createSuspendedMemberGate.js +146 -0
- package/dist/routes/auth/organizations/createSuspendedOrganizationsFilter.js +44 -0
- package/dist/routes/auth/organizations/ensureAuthIndexes.js +11 -10
- package/dist/routes/auth/organizations/getOrganizationBinding.js +4 -2
- package/dist/routes/auth/organizations/isActiveMember.js +26 -0
- package/dist/routes/auth/organizations/isEmailAdmitted.js +12 -5
- package/dist/routes/auth/organizations/memberRowsLimit.js +19 -0
- package/dist/routes/auth/organizations/mintOrganization.js +258 -0
- package/dist/routes/auth/organizations/organizationAccessControl.js +5 -1
- package/dist/routes/auth/organizations/provisionOrganization.js +117 -0
- package/dist/routes/auth/organizations/readSuspendedOrganizationIds.js +36 -0
- package/dist/routes/auth/requestHooks/buildRequestHooks.js +72 -0
- package/dist/routes/auth/requestHooks/createEmailCodeHolderGate.js +58 -0
- package/dist/routes/auth/requestHooks/createEmailSendLimit.js +116 -0
- package/dist/routes/auth/requestHooks/createSignInRefusedRedirectHook.js +75 -0
- package/dist/routes/auth/requestHooks/holdEmailCode.js +80 -0
- package/dist/routes/auth/requestHooks/reserveSendSlot.js +70 -0
- package/dist/routes/auth/runReportingFaults.js +32 -0
- package/dist/routes/connections/resolveUnwalledReach.js +14 -16
- package/dist/routes/endpoints/callEndpoint.js +6 -3
- package/dist/routes/endpoints/control/controlFor.js +9 -6
- package/dist/routes/endpoints/control/controlIf.js +8 -3
- package/dist/routes/endpoints/control/controlLog.js +3 -1
- package/dist/routes/endpoints/control/controlParallelFor.js +9 -6
- package/dist/routes/endpoints/control/controlReturn.js +3 -2
- package/dist/routes/endpoints/control/controlSetState.js +1 -1
- package/dist/routes/endpoints/control/controlSwitch.js +6 -3
- package/dist/routes/endpoints/control/controlWhile.js +6 -3
- package/dist/routes/endpoints/createLinkAnswer.js +76 -0
- package/dist/routes/endpoints/dispatchDetached.js +38 -0
- package/dist/routes/endpoints/forwardScheduledEndpoint.js +32 -11
- package/dist/routes/endpoints/handleAgentCall.js +1 -1
- package/dist/routes/endpoints/handleAuthStep.js +19 -4
- package/dist/routes/endpoints/handleEndpointCall.js +6 -4
- package/dist/routes/endpoints/handleRequest.js +3 -2
- package/dist/routes/endpoints/handleValidateDynamic.js +1 -1
- package/dist/routes/endpoints/matchLinkEndpoint.js +30 -0
- package/dist/routes/endpoints/runLinkEndpoint.js +148 -0
- package/dist/routes/endpoints/runRoutine.js +5 -5
- package/dist/routes/endpoints/scheduleBackground.js +13 -11
- package/dist/routes/endpoints/test/runTest.js +30 -11
- package/dist/routes/mcp/createMcpServer.js +2 -7
- package/dist/routes/mcp/runAfterToolCall.js +9 -3
- package/dist/routes/mobile/createAppleAppSiteAssociation.js +51 -0
- package/dist/routes/mobile/createAssetLinks.js +38 -0
- package/dist/routes/mobile/filterMobileMenus.js +31 -0
- package/dist/routes/mobile/getMobileAppEvents.js +23 -0
- package/dist/routes/mobile/getMobileRootConfig.js +109 -0
- package/dist/routes/mobile/getOpenablePageIds.js +21 -0
- package/dist/routes/page/dynamic/policy/checkAction.js +67 -2
- package/dist/routes/page/dynamic/policy/checkEvents.js +8 -2
- package/dist/routes/page/dynamic/policy/checkPolicy.js +86 -6
- package/dist/routes/page/dynamic/policy/checkValue.js +97 -6
- package/dist/routes/page/dynamic/policy/collectSchemaKinds.js +173 -0
- package/dist/routes/page/dynamic/policy/findDataRows.js +94 -0
- package/dist/routes/page/dynamic/policy/findEmbeddedUrls.js +24 -12
- package/dist/routes/page/dynamic/policy/findHtmlUrls.js +64 -0
- package/dist/routes/page/dynamic/policy/overlapsStatePath.js +28 -0
- package/dist/routes/page/dynamic/resolveDynamicContent.js +23 -17
- package/dist/routes/page/getPageConfig.js +25 -1
- package/dist/routes/request/callRequestResolver.js +29 -36
- package/dist/routes/request/stampTypedError.js +37 -0
- package/dist/routes/request/summarisedError.js +62 -0
- package/dist/routes/request/wrapResolverError.js +61 -0
- package/dist/routes/websocket/createChannelRegistry.js +13 -15
- package/dist/routes/websocket/createWebSocketConnection.js +5 -8
- package/dist/test/createMemoryAuth.js +125 -0
- package/package.json +20 -22
- package/dist/routes/page/dynamic/policy/collectUrlKinds.js +0 -117
- package/dist/routes/request/maskCredentialProperties.js +0 -34
|
@@ -0,0 +1,57 @@
|
|
|
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 { APIError } from 'better-auth/api';
|
|
16
|
+
import { type } from '@lowdefy/helpers';
|
|
17
|
+
import getOneTimeTokenIdentifiers from './getOneTimeTokenIdentifiers.js';
|
|
18
|
+
import matchesCodeChallenge from './matchesCodeChallenge.js';
|
|
19
|
+
// The hooks.before on /one-time-token/verify that binds the exchange to the
|
|
20
|
+
// shell that started the sign-in (PKCE, RFC 7636). The one-time token reaches
|
|
21
|
+
// the shell by a redirect to its custom scheme, which on Android any app can
|
|
22
|
+
// register and catch, so the token alone is not enough: the exchange must also
|
|
23
|
+
// send the code_verifier whose S256 challenge /auth/mobile stored beside the
|
|
24
|
+
// token (storeCodeChallenge). The verifier never leaves the shell's memory.
|
|
25
|
+
//
|
|
26
|
+
// The challenge is consumed on every attempt, and a refused attempt burns the
|
|
27
|
+
// token, so a caught token gets one guess and the shell's own exchange then
|
|
28
|
+
// fails closed instead of racing it. A token with no stored challenge is
|
|
29
|
+
// refused the same way. Every refusal answers as an unknown token does.
|
|
30
|
+
//
|
|
31
|
+
// A bare handler: buildOneTimeTokenPlugin owns the createAuthMiddleware
|
|
32
|
+
// wrapper.
|
|
33
|
+
function createCodeVerifierCheck() {
|
|
34
|
+
return async function codeVerifierCheck(ctx) {
|
|
35
|
+
const token = ctx.body?.token;
|
|
36
|
+
// No token is the route's own validation error.
|
|
37
|
+
if (!type.isString(token)) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const { internalAdapter } = ctx.context;
|
|
41
|
+
const identifiers = getOneTimeTokenIdentifiers({
|
|
42
|
+
token
|
|
43
|
+
});
|
|
44
|
+
const challenge = await internalAdapter.consumeVerificationValue(identifiers.challenge);
|
|
45
|
+
if (matchesCodeChallenge({
|
|
46
|
+
codeChallenge: challenge?.value,
|
|
47
|
+
codeVerifier: ctx.body.code_verifier
|
|
48
|
+
})) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
await internalAdapter.consumeVerificationValue(identifiers.token);
|
|
52
|
+
throw new APIError('BAD_REQUEST', {
|
|
53
|
+
message: 'Invalid token'
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export default createCodeVerifierCheck;
|
|
@@ -0,0 +1,76 @@
|
|
|
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
|
+
import getCodeHolderIdentifier from './getCodeHolderIdentifier.js';
|
|
17
|
+
import getKnownDeviceIdentifier from './getKnownDeviceIdentifier.js';
|
|
18
|
+
import readDeviceKeyHash from './readDeviceKeyHash.js';
|
|
19
|
+
const KNOWN_DEVICE_SECONDS = 90 * 24 * 60 * 60;
|
|
20
|
+
// The engine-tier request hooks.after on /sign-in/email-otp for apps with a
|
|
21
|
+
// mobile section, registered after the two-factor challenge: a phone that
|
|
22
|
+
// signed in by code, sending its device key, is recorded as known for the
|
|
23
|
+
// address for 90 days, so createEmailSendLimit lets it past the address's send
|
|
24
|
+
// cap. It runs only when the request created a session and no second factor
|
|
25
|
+
// stopped it (the challenge answers first and ends the slot). The code is used,
|
|
26
|
+
// so its hold goes too.
|
|
27
|
+
function createKnownDeviceHook({ getAuth }) {
|
|
28
|
+
return async function knownDevice(ctx) {
|
|
29
|
+
const rawEmail = ctx.body?.email;
|
|
30
|
+
const deviceKeyHash = readDeviceKeyHash({
|
|
31
|
+
request: ctx.request
|
|
32
|
+
});
|
|
33
|
+
if (type.isNone(ctx.context.newSession) || deviceKeyHash === null || !type.isString(rawEmail)) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
const email = rawEmail.trim().toLowerCase();
|
|
37
|
+
const identifier = getKnownDeviceIdentifier({
|
|
38
|
+
deviceKeyHash,
|
|
39
|
+
email
|
|
40
|
+
});
|
|
41
|
+
const { adapter } = await getAuth().$context;
|
|
42
|
+
const now = new Date();
|
|
43
|
+
await adapter.deleteMany({
|
|
44
|
+
model: 'verification',
|
|
45
|
+
where: [
|
|
46
|
+
{
|
|
47
|
+
field: 'identifier',
|
|
48
|
+
value: identifier
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
});
|
|
52
|
+
await adapter.create({
|
|
53
|
+
model: 'verification',
|
|
54
|
+
data: {
|
|
55
|
+
identifier,
|
|
56
|
+
value: 'known',
|
|
57
|
+
expiresAt: new Date(now.getTime() + KNOWN_DEVICE_SECONDS * 1000),
|
|
58
|
+
createdAt: now,
|
|
59
|
+
updatedAt: now
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
await adapter.deleteMany({
|
|
63
|
+
model: 'verification',
|
|
64
|
+
where: [
|
|
65
|
+
{
|
|
66
|
+
field: 'identifier',
|
|
67
|
+
value: getCodeHolderIdentifier({
|
|
68
|
+
email
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
return undefined;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export default createKnownDeviceHook;
|
|
@@ -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
|
+
*/ import { randomBytes } from 'node:crypto';
|
|
16
|
+
const DEFAULT_PRIMARY_COLOR = '#1677ff';
|
|
17
|
+
// Only a plain hex colour is written into the page's style.
|
|
18
|
+
const HEX_COLOR = /^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
19
|
+
function createStyle({ primaryColor }) {
|
|
20
|
+
return `
|
|
21
|
+
:root { color-scheme: light dark; --primary: ${primaryColor}; }
|
|
22
|
+
* { box-sizing: border-box; }
|
|
23
|
+
body { margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center;
|
|
24
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
25
|
+
background: Canvas; color: CanvasText; }
|
|
26
|
+
main { width: 100%; max-width: 400px; padding: 32px 24px; text-align: center; }
|
|
27
|
+
h1 { font-size: 22px; line-height: 1.3; margin: 0 0 12px; }
|
|
28
|
+
p { font-size: 15px; line-height: 1.5; margin: 0 0 24px; opacity: 0.8; }
|
|
29
|
+
form { display: flex; flex-direction: column; gap: 12px; }
|
|
30
|
+
button { font: inherit; font-size: 16px; font-weight: 600; padding: 12px 16px; border-radius: 8px;
|
|
31
|
+
cursor: pointer; border: 1px solid var(--primary); }
|
|
32
|
+
button.primary { background: var(--primary); color: #fff; }
|
|
33
|
+
button.secondary { background: transparent; color: var(--primary); }
|
|
34
|
+
`;
|
|
35
|
+
}
|
|
36
|
+
// The small server-rendered pages of the phone sign-in sheet (the
|
|
37
|
+
// confirmation page and the app-link fallback). They run no app code and load
|
|
38
|
+
// nothing: a strict Content-Security-Policy allows only the page's own style
|
|
39
|
+
// and script, by a nonce made for each response, and no site may frame them,
|
|
40
|
+
// so no other page can overlay or click the confirmation. They are never
|
|
41
|
+
// stored by a cache, and send no Referer by default. A page that posts a form
|
|
42
|
+
// to its own origin takes `same-origin`: under `no-referrer` a browser sends
|
|
43
|
+
// that post with `Origin: null`, which isSameOriginRequest refuses.
|
|
44
|
+
function createMobileAuthPageResponse({ content, primaryColor, referrerPolicy = 'no-referrer', script = null, status = 200, title, headers = {} }) {
|
|
45
|
+
const nonce = randomBytes(16).toString('base64');
|
|
46
|
+
const color = HEX_COLOR.test(primaryColor ?? '') ? primaryColor : DEFAULT_PRIMARY_COLOR;
|
|
47
|
+
const scriptElement = script === null ? '' : `<script nonce="${nonce}">${script}</script>`;
|
|
48
|
+
const html = `<!DOCTYPE html>
|
|
49
|
+
<html lang="en">
|
|
50
|
+
<head>
|
|
51
|
+
<meta charset="utf-8" />
|
|
52
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
53
|
+
<meta name="referrer" content="${referrerPolicy}" />
|
|
54
|
+
<title>${title}</title>
|
|
55
|
+
<style nonce="${nonce}">${createStyle({
|
|
56
|
+
primaryColor: color
|
|
57
|
+
})}</style>
|
|
58
|
+
${scriptElement}
|
|
59
|
+
</head>
|
|
60
|
+
<body>
|
|
61
|
+
<main>
|
|
62
|
+
${content}
|
|
63
|
+
</main>
|
|
64
|
+
</body>
|
|
65
|
+
</html>`;
|
|
66
|
+
return new Response(html, {
|
|
67
|
+
status,
|
|
68
|
+
headers: {
|
|
69
|
+
...headers,
|
|
70
|
+
'cache-control': 'no-store',
|
|
71
|
+
'content-type': 'text/html; charset=utf-8',
|
|
72
|
+
'content-security-policy': `default-src 'none'; style-src 'nonce-${nonce}'; script-src 'nonce-${nonce}'; base-uri 'none'; frame-ancestors 'none'`,
|
|
73
|
+
'referrer-policy': referrerPolicy,
|
|
74
|
+
'x-content-type-options': 'nosniff',
|
|
75
|
+
'x-frame-options': 'DENY'
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
export default createMobileAuthPageResponse;
|
|
@@ -0,0 +1,41 @@
|
|
|
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 getMobileSessionExpiry from './getMobileSessionExpiry.js';
|
|
16
|
+
import isNativeAuthRequest from './isNativeAuthRequest.js';
|
|
17
|
+
// session.create.before, bound when mobile.auth.sessionExpiresIn is set. A
|
|
18
|
+
// session created by a phone signing in natively (the email-code screen) is
|
|
19
|
+
// marked mobile and given the phone lifetime. A session created in the sign-in
|
|
20
|
+
// sheet is the sheet browser's and stays one: the phone gets a new session of
|
|
21
|
+
// its own once the person confirms, marked when it is created
|
|
22
|
+
// (createPhoneSession).
|
|
23
|
+
function createMobileSessionCreateHook({ sessionExpiresIn }) {
|
|
24
|
+
return async function mobileSessionCreate(session, ctx) {
|
|
25
|
+
if (!isNativeAuthRequest({
|
|
26
|
+
request: ctx?.request
|
|
27
|
+
})) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
data: {
|
|
32
|
+
...session,
|
|
33
|
+
mobile: true,
|
|
34
|
+
expiresAt: getMobileSessionExpiry({
|
|
35
|
+
sessionExpiresIn
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export default createMobileSessionCreateHook;
|
|
@@ -0,0 +1,40 @@
|
|
|
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 getMobileSessionExpiry from './getMobileSessionExpiry.js';
|
|
16
|
+
// session.update.before, bound when mobile.auth.sessionExpiresIn is set.
|
|
17
|
+
// BetterAuth slides a session forward by auth.session.expiresIn when it is
|
|
18
|
+
// read (getSession), and hands this hook only the changed fields. getSession
|
|
19
|
+
// has put the session it is refreshing on the endpoint context first, so a
|
|
20
|
+
// refresh of a phone session is told by its mobile mark and slid forward by
|
|
21
|
+
// the phone lifetime instead. Every other update passes through unchanged.
|
|
22
|
+
function createMobileSessionRefreshHook({ sessionExpiresIn }) {
|
|
23
|
+
return async function mobileSessionRefresh(changes, ctx) {
|
|
24
|
+
if (changes.expiresAt === undefined) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
if (ctx?.context?.session?.session?.mobile !== true) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
data: {
|
|
32
|
+
...changes,
|
|
33
|
+
expiresAt: getMobileSessionExpiry({
|
|
34
|
+
sessionExpiresIn
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export default createMobileSessionRefreshHook;
|
|
@@ -0,0 +1,32 @@
|
|
|
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 { createHmac } from 'node:crypto';
|
|
16
|
+
// The CSRF token on the mobile sign-in confirmation page: an HMAC, under the
|
|
17
|
+
// auth secret, of the browser session the page was shown to and the sign-in
|
|
18
|
+
// it confirms (shell, state, code challenge, platform). The POST that mints
|
|
19
|
+
// the one-time token must carry it, so it only comes from that page, shown to
|
|
20
|
+
// that session, for that sign-in.
|
|
21
|
+
function createMobileSignInCsrfToken({ params, secret, sessionId }) {
|
|
22
|
+
const { codeChallenge, platform, shell, state } = params;
|
|
23
|
+
return createHmac('sha256', secret).update([
|
|
24
|
+
'lowdefy-mobile-sign-in',
|
|
25
|
+
sessionId,
|
|
26
|
+
shell.id,
|
|
27
|
+
state,
|
|
28
|
+
codeChallenge,
|
|
29
|
+
platform
|
|
30
|
+
].join('\n')).digest('base64url');
|
|
31
|
+
}
|
|
32
|
+
export default createMobileSignInCsrfToken;
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
import getMobileSessionExpiry from './getMobileSessionExpiry.js';
|
|
17
|
+
// A new session for the phone, for the person signed in to the sheet's
|
|
18
|
+
// browser. The browser's own session is never handed to the phone, so the
|
|
19
|
+
// phone and the browser (Chrome, on Android) never share a token, and signing
|
|
20
|
+
// out on one leaves the other signed in.
|
|
21
|
+
//
|
|
22
|
+
// - It carries the browser session's active organization: the organization
|
|
23
|
+
// policy hook (session.create.before) picks the oldest membership, so a
|
|
24
|
+
// person who switched organization in the browser is set back to theirs.
|
|
25
|
+
// - With mobile.auth.sessionExpiresIn it is marked mobile and given the phone
|
|
26
|
+
// lifetime, as a session made by native sign-in is.
|
|
27
|
+
//
|
|
28
|
+
// Throws BetterAuth's APIError when the organization policy refuses the person.
|
|
29
|
+
async function createPhoneSession({ auth, authJson, browserSession, request }) {
|
|
30
|
+
const { adapter, internalAdapter } = await auth.$context;
|
|
31
|
+
const override = {
|
|
32
|
+
userAgent: request.headers.get('user-agent') ?? ''
|
|
33
|
+
};
|
|
34
|
+
const { sessionExpiresIn } = authJson.mobile;
|
|
35
|
+
if (type.isNumber(sessionExpiresIn)) {
|
|
36
|
+
override.mobile = true;
|
|
37
|
+
override.expiresAt = getMobileSessionExpiry({
|
|
38
|
+
sessionExpiresIn
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
const session = await internalAdapter.createSession(browserSession.session.userId, false, override, true);
|
|
42
|
+
const { activeOrganizationId } = browserSession.session;
|
|
43
|
+
if (!type.isNone(activeOrganizationId) && session.activeOrganizationId !== activeOrganizationId) {
|
|
44
|
+
await adapter.update({
|
|
45
|
+
model: 'session',
|
|
46
|
+
where: [
|
|
47
|
+
{
|
|
48
|
+
field: 'id',
|
|
49
|
+
value: session.id
|
|
50
|
+
}
|
|
51
|
+
],
|
|
52
|
+
update: {
|
|
53
|
+
activeOrganizationId
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return session;
|
|
58
|
+
}
|
|
59
|
+
export default createPhoneSession;
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
*/ const HTML_ESCAPES = {
|
|
16
|
+
'&': '&',
|
|
17
|
+
'<': '<',
|
|
18
|
+
'>': '>',
|
|
19
|
+
'"': '"',
|
|
20
|
+
"'": '''
|
|
21
|
+
};
|
|
22
|
+
// Text and attribute values for the server-rendered mobile sign-in pages.
|
|
23
|
+
function escapeHtml(value) {
|
|
24
|
+
return String(value).replace(/[&<>"']/g, (character)=>HTML_ESCAPES[character]);
|
|
25
|
+
}
|
|
26
|
+
export default escapeHtml;
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
*/ // The verification record naming the known phone (by its device key's hash)
|
|
16
|
+
// that asked for an address's live sign-in code (holdEmailCode).
|
|
17
|
+
function getCodeHolderIdentifier({ email }) {
|
|
18
|
+
return `email-code-holder:${email}`;
|
|
19
|
+
}
|
|
20
|
+
export default getCodeHolderIdentifier;
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
*/ // The verification record that marks a phone (by its device key's hash) as one
|
|
16
|
+
// that has signed in with an email address by code before.
|
|
17
|
+
function getKnownDeviceIdentifier({ deviceKeyHash, email }) {
|
|
18
|
+
return `email-known-device:${email}:${deviceKeyHash}`;
|
|
19
|
+
}
|
|
20
|
+
export default getKnownDeviceIdentifier;
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
*/ function getMobileSessionExpiry({ sessionExpiresIn }) {
|
|
16
|
+
return new Date(Date.now() + sessionExpiresIn * 1000);
|
|
17
|
+
}
|
|
18
|
+
export default getMobileSessionExpiry;
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
*/ // Where the sign-in returns to the shell, with the query (the one-time token
|
|
16
|
+
// and state, or error=cancelled and state):
|
|
17
|
+
// - iOS: <scheme>://auth. ASWebAuthenticationSession hands the redirect only to
|
|
18
|
+
// the app that opened it.
|
|
19
|
+
// - Android: the https app link <app origin><basePath>/auth/mobile/callback,
|
|
20
|
+
// which Android opens only in the shell assetlinks.json names (verified
|
|
21
|
+
// links), never in another app that registered the scheme. The origin is
|
|
22
|
+
// the pinned canonical one (BETTER_AUTH_URL or the environment url) when
|
|
23
|
+
// set, as the shell's own app url is; else the request's.
|
|
24
|
+
function getMobileSignInReturnUrl({ auth, params, query, url }) {
|
|
25
|
+
if (params.platform === 'android') {
|
|
26
|
+
const origin = auth.options?.baseURL ? new URL(auth.options.baseURL).origin : url.origin;
|
|
27
|
+
return `${origin}${url.pathname}/callback?${query.toString()}`;
|
|
28
|
+
}
|
|
29
|
+
return `${params.shell.scheme}://auth?${query.toString()}`;
|
|
30
|
+
}
|
|
31
|
+
export default getMobileSignInReturnUrl;
|
|
@@ -0,0 +1,26 @@
|
|
|
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 sha256Base64Url from './sha256Base64Url.js';
|
|
16
|
+
// The verification records a one-time token is kept under: the token itself
|
|
17
|
+
// (written by BetterAuth's one-time-token plugin, hashed as storeToken: 'hashed'
|
|
18
|
+
// hashes it) and the PKCE code challenge it is bound to (storeCodeChallenge).
|
|
19
|
+
function getOneTimeTokenIdentifiers({ token }) {
|
|
20
|
+
const hashedToken = sha256Base64Url(token);
|
|
21
|
+
return {
|
|
22
|
+
challenge: `one-time-token-challenge:${hashedToken}`,
|
|
23
|
+
token: `one-time-token:${hashedToken}`
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export default getOneTimeTokenIdentifiers;
|