@lowdefy/api 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/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 +99 -31
- 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 +172 -98
- 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/isUserBanned.js +28 -0
- 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 +57 -11
- package/dist/routes/auth/organizations/checkInvitationContactLink.js +60 -0
- package/dist/routes/auth/organizations/createAcceptExistingMemberHook.js +47 -12
- package/dist/routes/auth/organizations/createActiveOrgPolicyHook.js +41 -189
- package/dist/routes/auth/organizations/createAdmissionGateHook.js +5 -5
- package/dist/routes/auth/organizations/createAfterAcceptInvitationHook.js +40 -16
- package/dist/routes/auth/organizations/createBeforeAcceptInvitationHook.js +35 -0
- package/dist/routes/auth/organizations/createBeforeCreateInvitationHook.js +62 -0
- package/dist/routes/auth/organizations/createBeforeUpdateMemberRoleHook.js +45 -0
- package/dist/routes/auth/organizations/createEmailSendGate.js +35 -7
- 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 +25 -14
- 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 +78 -2
- 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/checkWebsocketAccess.js +81 -0
- package/dist/routes/websocket/createChannelRegistry.js +62 -17
- package/dist/routes/websocket/createWebSocketConnection.js +59 -21
- package/dist/routes/websocket/prepareChannel.js +11 -0
- 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
|
@@ -21,10 +21,14 @@
|
|
|
21
21
|
//
|
|
22
22
|
// A 4xx is the auth server rejecting the caller's attempt: expected traffic,
|
|
23
23
|
// one warn line, the same treatment the server error handler gives an
|
|
24
|
-
// AuthenticationError. Anything else is a fault in the auth engine
|
|
25
|
-
// adapter
|
|
26
|
-
//
|
|
27
|
-
|
|
24
|
+
// AuthenticationError. Anything else is a fault in the auth engine, its
|
|
25
|
+
// adapter or a hook, and goes to the error sink of a system context - this
|
|
26
|
+
// callback cannot reach the request's own context - so it is located, logged
|
|
27
|
+
// with its cause chain and sent to error tracking. A fault a hook routine
|
|
28
|
+
// already reported (handled, set by handleError) is not reported twice.
|
|
29
|
+
//
|
|
30
|
+
// BetterAuth does not await onError, and handleError never rejects.
|
|
31
|
+
function createOnAPIError({ createSystemContext, getAuth, logger }) {
|
|
28
32
|
return function onAPIError(error) {
|
|
29
33
|
const statusCode = error?.statusCode;
|
|
30
34
|
if (type.isInt(statusCode) && statusCode >= 400 && statusCode < 500) {
|
|
@@ -35,7 +39,13 @@ function createOnAPIError({ logger }) {
|
|
|
35
39
|
}, `Auth request rejected (${statusCode}): ${error.message}`);
|
|
36
40
|
return;
|
|
37
41
|
}
|
|
38
|
-
|
|
42
|
+
if (error?.handled === true) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const context = createSystemContext({
|
|
46
|
+
auth: getAuth()
|
|
47
|
+
});
|
|
48
|
+
context.handleError(error);
|
|
39
49
|
};
|
|
40
50
|
}
|
|
41
51
|
export default createOnAPIError;
|
|
@@ -13,13 +13,18 @@
|
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/ import send from '@lowdefy/connection-smtp/send';
|
|
16
|
-
import { ServiceError } from '@lowdefy/errors';
|
|
16
|
+
import { ConfigError, ServiceError } from '@lowdefy/errors';
|
|
17
|
+
import { type } from '@lowdefy/helpers';
|
|
17
18
|
import getConnectionConfig from '../connections/getConnectionConfig.js';
|
|
18
19
|
// auth.email sends every auth email (verification, password reset, magic link)
|
|
19
20
|
// through an SMTP connection. Resolving the connection per fire lets operators
|
|
20
21
|
// and the connection's delivery filter apply, so failures - and filtering -
|
|
21
|
-
// surface here at send time rather than at construction.
|
|
22
|
-
|
|
22
|
+
// surface here at send time rather than at construction. A failure names the
|
|
23
|
+
// connection and points at auth.email, never the recipient: the server's reply
|
|
24
|
+
// quotes the address it refused.
|
|
25
|
+
function createSendEmail({ authEmailConfig }) {
|
|
26
|
+
const { connectionId } = authEmailConfig;
|
|
27
|
+
const configKey = authEmailConfig['~k'];
|
|
23
28
|
return async function sendEmail({ to, subject, html, text, context }) {
|
|
24
29
|
const connectionConfig = await getConnectionConfig(context, {
|
|
25
30
|
connectionId
|
|
@@ -44,10 +49,15 @@ function createSendEmail({ connectionId }) {
|
|
|
44
49
|
if (ServiceError.isServiceError(error)) {
|
|
45
50
|
throw new ServiceError(undefined, {
|
|
46
51
|
cause: error,
|
|
47
|
-
service: 'SMTP'
|
|
52
|
+
service: 'SMTP',
|
|
53
|
+
configKey
|
|
48
54
|
});
|
|
49
55
|
}
|
|
50
|
-
|
|
56
|
+
const code = type.isNone(error?.code) ? '' : ` (${error.code})`;
|
|
57
|
+
throw new ConfigError(`Auth email could not be sent through connection "${connectionId}"${code}. Check the SMTP connection auth.email.connectionId names.`, {
|
|
58
|
+
cause: error,
|
|
59
|
+
configKey
|
|
60
|
+
});
|
|
51
61
|
}
|
|
52
62
|
};
|
|
53
63
|
}
|
|
@@ -69,9 +69,9 @@ function getBetterAuth({ appMeta, authJson, config, createSystemContext, dev, lo
|
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
71
|
// Ensure the unique indexes the organization writes rely on at startup. The
|
|
72
|
-
// tenant signup mint
|
|
73
|
-
// them. A failure is logged by ensureAuthIndexes
|
|
74
|
-
// cool-down, so nothing is left to handle here.
|
|
72
|
+
// tenant signup mint and the CreateOrganization step await the same ensure
|
|
73
|
+
// and write nothing without them. A failure is logged by ensureAuthIndexes
|
|
74
|
+
// and retried after its cool-down, so nothing is left to handle here.
|
|
75
75
|
if (authJson.database) {
|
|
76
76
|
ensureAuthIndexes({
|
|
77
77
|
auth: instance,
|
|
@@ -24,8 +24,10 @@ import { type } from '@lowdefy/helpers';
|
|
|
24
24
|
import { ConfigError, LowdefyInternalError } from '@lowdefy/errors';
|
|
25
25
|
import getCanonicalUrl from '../../context/getCanonicalUrl.js';
|
|
26
26
|
import buildAdminPlugin from './buildAdminPlugin.js';
|
|
27
|
+
import buildBearerPlugin from './mobile/buildBearerPlugin.js';
|
|
27
28
|
import buildCaptchaPlugin from './buildCaptchaPlugin.js';
|
|
28
29
|
import buildMcpTokenPlugin from './buildMcpTokenPlugin.js';
|
|
30
|
+
import buildOneTimeTokenPlugin from './mobile/buildOneTimeTokenPlugin.js';
|
|
29
31
|
import buildHooks from './hooks/buildHooks.js';
|
|
30
32
|
import buildOrganizationPlugin from './organizations/buildOrganizationPlugin.js';
|
|
31
33
|
import buildOauthPostLogin from './buildOauthPostLogin.js';
|
|
@@ -40,6 +42,7 @@ import { CLIENT_ADDRESS_HEADER } from './handleAuthRequest.js';
|
|
|
40
42
|
import modelNames from './modelNames.js';
|
|
41
43
|
import renderAuthEmail from '../../email/renderAuthEmail.js';
|
|
42
44
|
import resolveCookiePrefix from './resolveCookiePrefix.js';
|
|
45
|
+
import runReportingFaults from './runReportingFaults.js';
|
|
43
46
|
import sanitizeSessionResponse from './sanitizeSessionResponse.js';
|
|
44
47
|
// Decision 4: under the "pinned" org policy, disable every mounted
|
|
45
48
|
// /organization/* HTTP path EXCEPT /organization/accept-invitation (the
|
|
@@ -196,7 +199,8 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
196
199
|
}
|
|
197
200
|
if (!type.isString(authConfig.secret)) {
|
|
198
201
|
throw new ConfigError('Auth "secret" did not resolve to a string. Check the _secret operator reference and that the secret is set.', {
|
|
199
|
-
received: type.typeOf(authConfig.secret)
|
|
202
|
+
received: type.typeOf(authConfig.secret),
|
|
203
|
+
configKey: authConfig['~k']
|
|
200
204
|
});
|
|
201
205
|
}
|
|
202
206
|
// Secrets are opaque at build time; strength checks are startup warnings.
|
|
@@ -232,7 +236,7 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
232
236
|
plugins
|
|
233
237
|
});
|
|
234
238
|
const sendEmail = type.isNone(authConfig.email) ? undefined : createSendEmail({
|
|
235
|
-
|
|
239
|
+
authEmailConfig: authConfig.email
|
|
236
240
|
});
|
|
237
241
|
// BetterAuth builds password-reset, magic-link and email-verification links,
|
|
238
242
|
// and its trusted origins (the CSRF Origin check and the allowed
|
|
@@ -287,11 +291,13 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
287
291
|
// indexes, or reads inside it.
|
|
288
292
|
//
|
|
289
293
|
// contactId is the link to the app's canonical record for the human
|
|
290
|
-
// behind this login.
|
|
291
|
-
//
|
|
292
|
-
//
|
|
293
|
-
//
|
|
294
|
-
//
|
|
294
|
+
// behind this login. Apps compare it in author and access checks, so it
|
|
295
|
+
// is an authorization input and is set once: accepting an invitation
|
|
296
|
+
// sets it only on a login with no link and refuses an invitation naming
|
|
297
|
+
// a different contact (checkInvitationContactLink), and no auth step
|
|
298
|
+
// writes it after that. The platform never resolves it to whatever it
|
|
299
|
+
// points at - that is module work, and which record the name means is
|
|
300
|
+
// the module's business. A first-class field
|
|
295
301
|
// rather than a key inside profile, because a link to another entity is
|
|
296
302
|
// a different kind of thing from an opaque attribute bag - it belongs
|
|
297
303
|
// with the user's other structural facts.
|
|
@@ -363,6 +369,18 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
363
369
|
if (!type.isNone(database)) {
|
|
364
370
|
options.database = database;
|
|
365
371
|
}
|
|
372
|
+
// Phone sessions are marked so they can be given their own lifetime
|
|
373
|
+
// (mobile.auth.sessionExpiresIn) - see createMobileSessionCreateHook. input:
|
|
374
|
+
// false keeps the mark out of every client-writable body.
|
|
375
|
+
if (type.isNumber(authConfig.mobile?.sessionExpiresIn)) {
|
|
376
|
+
options.session.additionalFields = {
|
|
377
|
+
mobile: {
|
|
378
|
+
type: 'boolean',
|
|
379
|
+
required: false,
|
|
380
|
+
input: false
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
366
384
|
if (authConfig.session.crossSubDomainCookies.enabled === true) {
|
|
367
385
|
options.advanced.crossSubDomainCookies = {
|
|
368
386
|
enabled: true,
|
|
@@ -381,21 +399,26 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
381
399
|
const context = createSystemContext({
|
|
382
400
|
auth: getAuth()
|
|
383
401
|
});
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
402
|
+
await runReportingFaults({
|
|
403
|
+
context,
|
|
404
|
+
run: async ()=>{
|
|
405
|
+
const { subject, html, text } = await renderAuthEmail({
|
|
406
|
+
flow: 'resetPassword',
|
|
407
|
+
vars: {
|
|
408
|
+
url
|
|
409
|
+
},
|
|
410
|
+
authEmailConfig: authConfig.email,
|
|
411
|
+
baseURL: baseUrlOrigin,
|
|
412
|
+
context
|
|
413
|
+
});
|
|
414
|
+
await sendEmail({
|
|
415
|
+
to: user.email,
|
|
416
|
+
subject,
|
|
417
|
+
html,
|
|
418
|
+
text,
|
|
419
|
+
context
|
|
420
|
+
});
|
|
421
|
+
}
|
|
399
422
|
});
|
|
400
423
|
};
|
|
401
424
|
} else {
|
|
@@ -410,21 +433,26 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
410
433
|
const context = createSystemContext({
|
|
411
434
|
auth: getAuth()
|
|
412
435
|
});
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
436
|
+
await runReportingFaults({
|
|
437
|
+
context,
|
|
438
|
+
run: async ()=>{
|
|
439
|
+
const { subject, html, text } = await renderAuthEmail({
|
|
440
|
+
flow: 'verifyEmail',
|
|
441
|
+
vars: {
|
|
442
|
+
url
|
|
443
|
+
},
|
|
444
|
+
authEmailConfig: authConfig.email,
|
|
445
|
+
baseURL: baseUrlOrigin,
|
|
446
|
+
context
|
|
447
|
+
});
|
|
448
|
+
await sendEmail({
|
|
449
|
+
to: user.email,
|
|
450
|
+
subject,
|
|
451
|
+
html,
|
|
452
|
+
text,
|
|
453
|
+
context
|
|
454
|
+
});
|
|
455
|
+
}
|
|
428
456
|
});
|
|
429
457
|
}
|
|
430
458
|
};
|
|
@@ -441,42 +469,47 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
441
469
|
const context = createSystemContext({
|
|
442
470
|
auth: getAuth()
|
|
443
471
|
});
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
472
|
+
await runReportingFaults({
|
|
473
|
+
context,
|
|
474
|
+
run: async ()=>{
|
|
475
|
+
// One email serves both paths when the code is also configured: mail
|
|
476
|
+
// security that pre-fetches links burns the single-use magic-link
|
|
477
|
+
// token before the person clicks, and the code they type into the tab
|
|
478
|
+
// they started from survives that. createVerificationOTP is the
|
|
479
|
+
// plugin's server-only mint - it stores the sign-in code and returns
|
|
480
|
+
// it, sending nothing - so the code rides on the link email instead of
|
|
481
|
+
// arriving as a second one.
|
|
482
|
+
const otpVars = emailOTPEnabled ? {
|
|
483
|
+
otp: await getAuth().api.createVerificationOTP({
|
|
484
|
+
body: {
|
|
485
|
+
email,
|
|
486
|
+
type: 'sign-in'
|
|
487
|
+
}
|
|
488
|
+
}),
|
|
489
|
+
expiresIn: authConfig.emailOTP.expiresIn
|
|
490
|
+
} : {};
|
|
491
|
+
const { subject, html, text } = await renderAuthEmail({
|
|
492
|
+
flow: 'magicLink',
|
|
493
|
+
vars: {
|
|
494
|
+
url: buildMagicLinkUrl({
|
|
495
|
+
authConfig,
|
|
496
|
+
config,
|
|
497
|
+
url
|
|
498
|
+
}),
|
|
499
|
+
...otpVars
|
|
500
|
+
},
|
|
501
|
+
authEmailConfig: authConfig.email,
|
|
502
|
+
baseURL: baseUrlOrigin,
|
|
503
|
+
context
|
|
504
|
+
});
|
|
505
|
+
await sendEmail({
|
|
506
|
+
to: email,
|
|
507
|
+
subject,
|
|
508
|
+
html,
|
|
509
|
+
text,
|
|
510
|
+
context
|
|
511
|
+
});
|
|
512
|
+
}
|
|
480
513
|
});
|
|
481
514
|
}
|
|
482
515
|
}));
|
|
@@ -491,29 +524,48 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
491
524
|
expiresIn: authConfig.emailOTP.expiresIn,
|
|
492
525
|
allowedAttempts: authConfig.emailOTP.allowedAttempts,
|
|
493
526
|
disableSignUp: authConfig.emailOTP.disableSignUp,
|
|
527
|
+
// With phones, a resend keeps the live code rather than replacing it, so
|
|
528
|
+
// a stranger's send cannot take a known phone's code away
|
|
529
|
+
// (holdEmailCode). Web-only apps keep BetterAuth's default.
|
|
530
|
+
...type.isNone(authConfig.mobile) ? {} : {
|
|
531
|
+
resendStrategy: 'reuse'
|
|
532
|
+
},
|
|
494
533
|
sendVerificationOTP: async ({ email, otp, type: otpType })=>{
|
|
495
|
-
if (otpType !== 'sign-in') {
|
|
496
|
-
throw new ConfigError(`Auth "emailOTP" only wires the sign-in code. A "${otpType}" one-time code was requested, and the Lowdefy engine has no email flow for it.`);
|
|
497
|
-
}
|
|
498
534
|
const context = createSystemContext({
|
|
499
535
|
auth: getAuth()
|
|
500
536
|
});
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
537
|
+
// The type is the caller's: BetterAuth's send-verification-otp endpoint takes it from
|
|
538
|
+
// the request body, so another type is a refused request, not an engine fault. BetterAuth
|
|
539
|
+
// may run this send in the background, where a throw is only its own error line, so the
|
|
540
|
+
// refusal is one warning and no email.
|
|
541
|
+
if (otpType !== 'sign-in') {
|
|
542
|
+
context.logger.warn({
|
|
543
|
+
event: 'auth_email_otp_type_refused',
|
|
544
|
+
otpType
|
|
545
|
+
}, `Auth "emailOTP" only sends the sign-in code. A "${otpType}" one-time code was requested and not sent.`);
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
await runReportingFaults({
|
|
549
|
+
context,
|
|
550
|
+
run: async ()=>{
|
|
551
|
+
const { subject, html, text } = await renderAuthEmail({
|
|
552
|
+
flow: 'emailOTP',
|
|
553
|
+
vars: {
|
|
554
|
+
otp,
|
|
555
|
+
expiresIn: authConfig.emailOTP.expiresIn
|
|
556
|
+
},
|
|
557
|
+
authEmailConfig: authConfig.email,
|
|
558
|
+
baseURL: baseUrlOrigin,
|
|
559
|
+
context
|
|
560
|
+
});
|
|
561
|
+
await sendEmail({
|
|
562
|
+
to: email,
|
|
563
|
+
subject,
|
|
564
|
+
html,
|
|
565
|
+
text,
|
|
566
|
+
context
|
|
567
|
+
});
|
|
568
|
+
}
|
|
517
569
|
});
|
|
518
570
|
}
|
|
519
571
|
}));
|
|
@@ -638,6 +690,14 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
638
690
|
// it the route authenticates nobody.
|
|
639
691
|
buildMcpTokenPlugin());
|
|
640
692
|
}
|
|
693
|
+
// Phones hold ordinary sessions and send them as bearers. Both plugins are on
|
|
694
|
+
// only when the app has a mobile section, and neither changes what a browser
|
|
695
|
+
// sends or receives: the session token header goes to native requests only
|
|
696
|
+
// (buildBearerPlugin), and only the server mints one-time tokens
|
|
697
|
+
// (buildOneTimeTokenPlugin, minted by the mobile sign-in page).
|
|
698
|
+
if (!type.isNone(authConfig.mobile)) {
|
|
699
|
+
options.plugins.push(buildBearerPlugin(), buildOneTimeTokenPlugin());
|
|
700
|
+
}
|
|
641
701
|
// The admin plugin is framework-controlled - it owns the user row's ban fields
|
|
642
702
|
// and the endpoints the ban, delete and revoke-sessions steps call. Its whole
|
|
643
703
|
// HTTP surface is disabled (see ADMIN_PATHS_DISABLED).
|
|
@@ -665,7 +725,8 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
665
725
|
authConfig,
|
|
666
726
|
basePath: config.basePath ?? '',
|
|
667
727
|
baseUrlOrigin,
|
|
668
|
-
getAuth
|
|
728
|
+
getAuth,
|
|
729
|
+
logger
|
|
669
730
|
});
|
|
670
731
|
// Phone login sends SMS through the "phone.otp.send" hook binding - there
|
|
671
732
|
// is no built-in SMS transport, so build validation requires the binding
|
|
@@ -683,17 +744,28 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
683
744
|
// build), the same stance as password reset. The accept URL is only
|
|
684
745
|
// buildable when the origin is pinned AND authPages.acceptInvitation is set;
|
|
685
746
|
// otherwise the stock InvitationEmail renders branded with no CTA button.
|
|
686
|
-
async function sendInvitationEmail(
|
|
747
|
+
async function sendInvitationEmail(args) {
|
|
748
|
+
const context = createSystemContext({
|
|
749
|
+
auth: getAuth()
|
|
750
|
+
});
|
|
751
|
+
await runReportingFaults({
|
|
752
|
+
context,
|
|
753
|
+
run: ()=>renderAndSendInvitation({
|
|
754
|
+
...args,
|
|
755
|
+
context
|
|
756
|
+
})
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
async function renderAndSendInvitation({ email, organization, invitation, context }) {
|
|
687
760
|
if (type.isNone(authConfig.email)) {
|
|
688
|
-
throw new ConfigError('Cannot send the invitation email. Configure "auth.email".'
|
|
761
|
+
throw new ConfigError('Cannot send the invitation email. Configure "auth.email".', {
|
|
762
|
+
configKey: authConfig['~k']
|
|
763
|
+
});
|
|
689
764
|
}
|
|
690
765
|
const acceptPath = authConfig.authPages?.acceptInvitation;
|
|
691
766
|
const canBuildAcceptUrl = type.isString(baseUrlOrigin) && type.isString(acceptPath);
|
|
692
767
|
const basePath = config.basePath ?? '';
|
|
693
768
|
const acceptUrl = canBuildAcceptUrl ? `${baseUrlOrigin}${basePath}${acceptPath}?invitationId=${invitation.id}` : undefined;
|
|
694
|
-
const context = createSystemContext({
|
|
695
|
-
auth: getAuth()
|
|
696
|
-
});
|
|
697
769
|
const { subject, html, text } = await renderAuthEmail({
|
|
698
770
|
flow: 'invitation',
|
|
699
771
|
vars: {
|
|
@@ -746,6 +818,8 @@ function getBetterAuthConfig({ appMeta, authJson, config = {}, createSystemConte
|
|
|
746
818
|
// line, not an error - see createOnAPIError.
|
|
747
819
|
options.onAPIError = {
|
|
748
820
|
onError: createOnAPIError({
|
|
821
|
+
createSystemContext,
|
|
822
|
+
getAuth,
|
|
749
823
|
logger
|
|
750
824
|
})
|
|
751
825
|
};
|
|
@@ -12,10 +12,13 @@
|
|
|
12
12
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
|
-
*/ import
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
import createAcceptActiveOrgGuardHook from '../organizations/createAcceptActiveOrgGuardHook.js';
|
|
16
17
|
import createActiveOrgPolicyHook from '../organizations/createActiveOrgPolicyHook.js';
|
|
17
18
|
import createAdmissionGateHook from '../organizations/createAdmissionGateHook.js';
|
|
18
19
|
import createAutoJoinHook from '../organizations/createAutoJoinHook.js';
|
|
20
|
+
import createMobileSessionCreateHook from '../mobile/createMobileSessionCreateHook.js';
|
|
21
|
+
import createMobileSessionRefreshHook from '../mobile/createMobileSessionRefreshHook.js';
|
|
19
22
|
// Engine-tier hook bindings - hard-coded framework behavior, not entries in
|
|
20
23
|
// auth.hooks. Keyed by point; each point lists BetterAuth-native callbacks
|
|
21
24
|
// (before: (data, ctx) => false | { data } | undefined, after: (data, ctx)).
|
|
@@ -63,6 +66,17 @@ function buildEngineHooks({ authConfig, getAuth, logger }) {
|
|
|
63
66
|
})
|
|
64
67
|
];
|
|
65
68
|
}
|
|
69
|
+
// Phone sessions get their own lifetime. After the organization policy hook,
|
|
70
|
+
// which picks the session's organization and leaves its expiry alone.
|
|
71
|
+
const sessionExpiresIn = authConfig.mobile?.sessionExpiresIn;
|
|
72
|
+
if (type.isNumber(sessionExpiresIn)) {
|
|
73
|
+
engineHooks['session.create.before'].push(createMobileSessionCreateHook({
|
|
74
|
+
sessionExpiresIn
|
|
75
|
+
}));
|
|
76
|
+
engineHooks['session.update.before'].push(createMobileSessionRefreshHook({
|
|
77
|
+
sessionExpiresIn
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
66
80
|
return engineHooks;
|
|
67
81
|
}
|
|
68
82
|
export default buildEngineHooks;
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/ import { APIError } from 'better-auth/api';
|
|
16
|
+
import { isExpectedError } from '@lowdefy/errors';
|
|
16
17
|
// Translates the routine's terminal control for after and synthetic points.
|
|
17
18
|
// These fire after the write committed, so `:return` data is ignored and a
|
|
18
19
|
// `:reject`/throw surfaces as an operational error on the underlying
|
|
@@ -22,13 +23,27 @@ function createUserAfterHook({ dispatch }) {
|
|
|
22
23
|
const result = await dispatch(data, ctx);
|
|
23
24
|
if (result.status === 'reject') {
|
|
24
25
|
// Keep the rejecting UserError as the cause so the server-side trace still
|
|
25
|
-
// names the routine control that refused the write.
|
|
26
|
+
// names the routine control that refused the write. The code matches the
|
|
27
|
+
// before hook's, so a sign-in refused at either timing lands on the error
|
|
28
|
+
// page the same way.
|
|
26
29
|
throw new APIError('BAD_REQUEST', {
|
|
27
30
|
cause: result.error,
|
|
28
|
-
message: result.error.message
|
|
31
|
+
message: result.error.message,
|
|
32
|
+
code: 'AUTH_HOOK_REJECTED'
|
|
29
33
|
});
|
|
30
34
|
}
|
|
31
35
|
if (result.status === 'error') {
|
|
36
|
+
// An expected outcome (a :throw, a nested call's refusal) is a 400 like :reject, code
|
|
37
|
+
// included, not a 500. runRoutine has logged it as a warning.
|
|
38
|
+
if (isExpectedError(result.error)) {
|
|
39
|
+
throw new APIError('BAD_REQUEST', {
|
|
40
|
+
cause: result.error,
|
|
41
|
+
message: result.error.message,
|
|
42
|
+
code: 'AUTH_HOOK_REJECTED'
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
// A fault runRoutine has already reported (handled), so createOnAPIError does not
|
|
46
|
+
// log it again.
|
|
32
47
|
throw result.error;
|
|
33
48
|
}
|
|
34
49
|
};
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/ import { APIError } from 'better-auth/api';
|
|
16
16
|
import { type } from '@lowdefy/helpers';
|
|
17
|
-
import { ConfigError } from '@lowdefy/errors';
|
|
17
|
+
import { ConfigError, isExpectedError } from '@lowdefy/errors';
|
|
18
18
|
// Translates the routine's terminal control into BetterAuth's before-hook
|
|
19
19
|
// contract. The routine `:return`s plain data - the ceremony lives here:
|
|
20
20
|
// returned data replaces the record ({ data } wrap), a fall-through leaves
|
|
@@ -24,8 +24,9 @@ function createUserBeforeHook({ dispatch, hook }) {
|
|
|
24
24
|
const result = await dispatch(data, ctx);
|
|
25
25
|
if (result.status === 'return') {
|
|
26
26
|
if (!type.isObject(result.response)) {
|
|
27
|
+
// The type, not the value: the value is a user record being written.
|
|
27
28
|
throw new ConfigError(`Auth hook "${hook.id}" at point "${hook.point}" returned a ${type.typeOf(result.response)}. A before hook ":return" value replaces the record being written, so it should be an object.`, {
|
|
28
|
-
|
|
29
|
+
configKey: hook['~k']
|
|
29
30
|
});
|
|
30
31
|
}
|
|
31
32
|
return {
|
|
@@ -34,13 +35,28 @@ function createUserBeforeHook({ dispatch, hook }) {
|
|
|
34
35
|
}
|
|
35
36
|
if (result.status === 'reject') {
|
|
36
37
|
// Keep the rejecting UserError as the cause so the server-side trace still
|
|
37
|
-
// names the routine control that refused the write.
|
|
38
|
+
// names the routine control that refused the write. BetterAuth redirects a
|
|
39
|
+
// refused sign-in to the error page only when the APIError carries a code,
|
|
40
|
+
// so without one the browser is shown the JSON.
|
|
38
41
|
throw new APIError('BAD_REQUEST', {
|
|
39
42
|
cause: result.error,
|
|
40
|
-
message: result.error.message
|
|
43
|
+
message: result.error.message,
|
|
44
|
+
code: 'AUTH_HOOK_REJECTED'
|
|
41
45
|
});
|
|
42
46
|
}
|
|
43
47
|
if (result.status === 'error') {
|
|
48
|
+
// An expected outcome (a :throw, a nested call's refusal) refuses the write as
|
|
49
|
+
// :reject does - a 400 with its code, not a 500. runRoutine has logged it as a
|
|
50
|
+
// warning.
|
|
51
|
+
if (isExpectedError(result.error)) {
|
|
52
|
+
throw new APIError('BAD_REQUEST', {
|
|
53
|
+
cause: result.error,
|
|
54
|
+
message: result.error.message,
|
|
55
|
+
code: 'AUTH_HOOK_REJECTED'
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
// A fault runRoutine has already reported (handled), so createOnAPIError does not
|
|
59
|
+
// log it again.
|
|
44
60
|
throw result.error;
|
|
45
61
|
}
|
|
46
62
|
// The routine fell through without a terminal control - record unchanged.
|
|
@@ -0,0 +1,28 @@
|
|
|
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 admin plugin's ban rule, as its session.create.before applies it: any truthy
|
|
16
|
+
// banned is a ban, and only a banExpires that reads as a past date lifts it. The
|
|
17
|
+
// truthiness checks are deliberate - a user this calls unbanned while the plugin
|
|
18
|
+
// refuses them would be sent an email the sign-in then rejects. They are an
|
|
19
|
+
// exception to the type.isNone() rule: type.isNone(banExpires) would read a
|
|
20
|
+
// banExpires of 0 as a 1970 expiry and lift a ban the plugin keeps.
|
|
21
|
+
function isUserBanned({ user }) {
|
|
22
|
+
if (!user.banned) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
const expired = Boolean(user.banExpires) && new Date(user.banExpires).getTime() < Date.now();
|
|
26
|
+
return !expired;
|
|
27
|
+
}
|
|
28
|
+
export default isUserBanned;
|