@hilbras/keystone 1.1.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/CHANGELOG.md +53 -0
- package/LICENSE +21 -0
- package/README.md +691 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +144 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +68 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +118 -0
- package/dist/config.js.map +1 -0
- package/dist/container.d.ts +27 -0
- package/dist/container.d.ts.map +1 -0
- package/dist/container.js +14 -0
- package/dist/container.js.map +1 -0
- package/dist/db/index.d.ts +17 -0
- package/dist/db/index.d.ts.map +1 -0
- package/dist/db/index.js +46 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/migrations/0000_skinny_ben_parker.sql +553 -0
- package/dist/db/migrations/0001_brief_joseph.sql +10 -0
- package/dist/db/migrations/0002_add_account_lockout.sql +3 -0
- package/dist/db/migrations/0003_steep_rachel_grey.sql +1 -0
- package/dist/db/migrations/0004_clever_bill_hollister.sql +20 -0
- package/dist/db/migrations/0005_steep_the_call.sql +2 -0
- package/dist/db/migrations/0006_ambitious_boomer.sql +17 -0
- package/dist/db/migrations/0007_greedy_vindicator.sql +40 -0
- package/dist/db/migrations/meta/0000_snapshot.json +2964 -0
- package/dist/db/migrations/meta/0001_snapshot.json +3031 -0
- package/dist/db/migrations/meta/0003_snapshot.json +3051 -0
- package/dist/db/migrations/meta/0004_snapshot.json +3163 -0
- package/dist/db/migrations/meta/0005_snapshot.json +3177 -0
- package/dist/db/migrations/meta/0006_snapshot.json +3270 -0
- package/dist/db/migrations/meta/0007_snapshot.json +3489 -0
- package/dist/db/migrations/meta/_journal.json +62 -0
- package/dist/db/schema.d.ts +3671 -0
- package/dist/db/schema.d.ts.map +1 -0
- package/dist/db/schema.js +475 -0
- package/dist/db/schema.js.map +1 -0
- package/dist/db/seed.d.ts +2 -0
- package/dist/db/seed.d.ts.map +1 -0
- package/dist/db/seed.js +139 -0
- package/dist/db/seed.js.map +1 -0
- package/dist/di.d.ts +12 -0
- package/dist/di.d.ts.map +1 -0
- package/dist/di.js +56 -0
- package/dist/di.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +295 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/errors.d.ts +46 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +78 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/result.d.ts +21 -0
- package/dist/lib/result.d.ts.map +1 -0
- package/dist/lib/result.js +22 -0
- package/dist/lib/result.js.map +1 -0
- package/dist/lib/webhookSignature.d.ts +11 -0
- package/dist/lib/webhookSignature.d.ts.map +1 -0
- package/dist/lib/webhookSignature.js +35 -0
- package/dist/lib/webhookSignature.js.map +1 -0
- package/dist/plugins/appContext.d.ts +4 -0
- package/dist/plugins/appContext.d.ts.map +1 -0
- package/dist/plugins/appContext.js +63 -0
- package/dist/plugins/appContext.js.map +1 -0
- package/dist/plugins/audit.d.ts +11 -0
- package/dist/plugins/audit.d.ts.map +1 -0
- package/dist/plugins/audit.js +23 -0
- package/dist/plugins/audit.js.map +1 -0
- package/dist/plugins/auth.d.ts +21 -0
- package/dist/plugins/auth.d.ts.map +1 -0
- package/dist/plugins/auth.js +190 -0
- package/dist/plugins/auth.js.map +1 -0
- package/dist/plugins/metrics.d.ts +8 -0
- package/dist/plugins/metrics.d.ts.map +1 -0
- package/dist/plugins/metrics.js +36 -0
- package/dist/plugins/metrics.js.map +1 -0
- package/dist/plugins/mtls.d.ts +14 -0
- package/dist/plugins/mtls.d.ts.map +1 -0
- package/dist/plugins/mtls.js +76 -0
- package/dist/plugins/mtls.js.map +1 -0
- package/dist/plugins/permissions.d.ts +9 -0
- package/dist/plugins/permissions.d.ts.map +1 -0
- package/dist/plugins/permissions.js +20 -0
- package/dist/plugins/permissions.js.map +1 -0
- package/dist/plugins/rateLimit.d.ts +20 -0
- package/dist/plugins/rateLimit.d.ts.map +1 -0
- package/dist/plugins/rateLimit.js +101 -0
- package/dist/plugins/rateLimit.js.map +1 -0
- package/dist/plugins/requestLogger.d.ts +4 -0
- package/dist/plugins/requestLogger.d.ts.map +1 -0
- package/dist/plugins/requestLogger.js +20 -0
- package/dist/plugins/requestLogger.js.map +1 -0
- package/dist/plugins/tracing.d.ts +3 -0
- package/dist/plugins/tracing.d.ts.map +1 -0
- package/dist/plugins/tracing.js +21 -0
- package/dist/plugins/tracing.js.map +1 -0
- package/dist/repositories/apiKey.d.ts +40 -0
- package/dist/repositories/apiKey.d.ts.map +1 -0
- package/dist/repositories/apiKey.js +122 -0
- package/dist/repositories/apiKey.js.map +1 -0
- package/dist/repositories/application.d.ts +13 -0
- package/dist/repositories/application.d.ts.map +1 -0
- package/dist/repositories/application.js +57 -0
- package/dist/repositories/application.js.map +1 -0
- package/dist/repositories/audit.d.ts +13 -0
- package/dist/repositories/audit.d.ts.map +1 -0
- package/dist/repositories/audit.js +26 -0
- package/dist/repositories/audit.js.map +1 -0
- package/dist/repositories/index.d.ts +10 -0
- package/dist/repositories/index.d.ts.map +1 -0
- package/dist/repositories/index.js +10 -0
- package/dist/repositories/index.js.map +1 -0
- package/dist/repositories/oidcConnection.d.ts +131 -0
- package/dist/repositories/oidcConnection.d.ts.map +1 -0
- package/dist/repositories/oidcConnection.js +59 -0
- package/dist/repositories/oidcConnection.js.map +1 -0
- package/dist/repositories/organization.d.ts +44 -0
- package/dist/repositories/organization.d.ts.map +1 -0
- package/dist/repositories/organization.js +145 -0
- package/dist/repositories/organization.js.map +1 -0
- package/dist/repositories/permission.d.ts +24 -0
- package/dist/repositories/permission.d.ts.map +1 -0
- package/dist/repositories/permission.js +133 -0
- package/dist/repositories/permission.js.map +1 -0
- package/dist/repositories/samlConnection.d.ts +107 -0
- package/dist/repositories/samlConnection.d.ts.map +1 -0
- package/dist/repositories/samlConnection.js +53 -0
- package/dist/repositories/samlConnection.js.map +1 -0
- package/dist/repositories/session.d.ts +73 -0
- package/dist/repositories/session.d.ts.map +1 -0
- package/dist/repositories/session.js +68 -0
- package/dist/repositories/session.js.map +1 -0
- package/dist/repositories/types.d.ts +155 -0
- package/dist/repositories/types.d.ts.map +1 -0
- package/dist/repositories/types.js +2 -0
- package/dist/repositories/types.js.map +1 -0
- package/dist/repositories/user.d.ts +40 -0
- package/dist/repositories/user.d.ts.map +1 -0
- package/dist/repositories/user.js +135 -0
- package/dist/repositories/user.js.map +1 -0
- package/dist/routes/admin/billing.d.ts +3 -0
- package/dist/routes/admin/billing.d.ts.map +1 -0
- package/dist/routes/admin/billing.js +31 -0
- package/dist/routes/admin/billing.js.map +1 -0
- package/dist/routes/admin/helpers.d.ts +36 -0
- package/dist/routes/admin/helpers.d.ts.map +1 -0
- package/dist/routes/admin/helpers.js +45 -0
- package/dist/routes/admin/helpers.js.map +1 -0
- package/dist/routes/admin/index.d.ts +3 -0
- package/dist/routes/admin/index.d.ts.map +1 -0
- package/dist/routes/admin/index.js +15 -0
- package/dist/routes/admin/index.js.map +1 -0
- package/dist/routes/admin/organizations.d.ts +3 -0
- package/dist/routes/admin/organizations.d.ts.map +1 -0
- package/dist/routes/admin/organizations.js +222 -0
- package/dist/routes/admin/organizations.js.map +1 -0
- package/dist/routes/admin/permissions.d.ts +3 -0
- package/dist/routes/admin/permissions.d.ts.map +1 -0
- package/dist/routes/admin/permissions.js +63 -0
- package/dist/routes/admin/permissions.js.map +1 -0
- package/dist/routes/admin/platform.d.ts +3 -0
- package/dist/routes/admin/platform.d.ts.map +1 -0
- package/dist/routes/admin/platform.js +294 -0
- package/dist/routes/admin/platform.js.map +1 -0
- package/dist/routes/admin/sso.d.ts +3 -0
- package/dist/routes/admin/sso.d.ts.map +1 -0
- package/dist/routes/admin/sso.js +97 -0
- package/dist/routes/admin/sso.js.map +1 -0
- package/dist/routes/admin/webhooks.d.ts +3 -0
- package/dist/routes/admin/webhooks.d.ts.map +1 -0
- package/dist/routes/admin/webhooks.js +68 -0
- package/dist/routes/admin/webhooks.js.map +1 -0
- package/dist/routes/admin.d.ts +2 -0
- package/dist/routes/admin.d.ts.map +1 -0
- package/dist/routes/admin.js +2 -0
- package/dist/routes/admin.js.map +1 -0
- package/dist/routes/apiKeys.d.ts +3 -0
- package/dist/routes/apiKeys.d.ts.map +1 -0
- package/dist/routes/apiKeys.js +50 -0
- package/dist/routes/apiKeys.js.map +1 -0
- package/dist/routes/auth.d.ts +3 -0
- package/dist/routes/auth.d.ts.map +1 -0
- package/dist/routes/auth.js +174 -0
- package/dist/routes/auth.js.map +1 -0
- package/dist/routes/authz.d.ts +3 -0
- package/dist/routes/authz.d.ts.map +1 -0
- package/dist/routes/authz.js +28 -0
- package/dist/routes/authz.js.map +1 -0
- package/dist/routes/config.d.ts +3 -0
- package/dist/routes/config.d.ts.map +1 -0
- package/dist/routes/config.js +63 -0
- package/dist/routes/config.js.map +1 -0
- package/dist/routes/emailVerification.d.ts +3 -0
- package/dist/routes/emailVerification.d.ts.map +1 -0
- package/dist/routes/emailVerification.js +64 -0
- package/dist/routes/emailVerification.js.map +1 -0
- package/dist/routes/federation.d.ts +3 -0
- package/dist/routes/federation.d.ts.map +1 -0
- package/dist/routes/federation.js +133 -0
- package/dist/routes/federation.js.map +1 -0
- package/dist/routes/helpers.d.ts +14 -0
- package/dist/routes/helpers.d.ts.map +1 -0
- package/dist/routes/helpers.js +15 -0
- package/dist/routes/helpers.js.map +1 -0
- package/dist/routes/magicLinks.d.ts +3 -0
- package/dist/routes/magicLinks.d.ts.map +1 -0
- package/dist/routes/magicLinks.js +82 -0
- package/dist/routes/magicLinks.js.map +1 -0
- package/dist/routes/oauth.d.ts +3 -0
- package/dist/routes/oauth.d.ts.map +1 -0
- package/dist/routes/oauth.js +146 -0
- package/dist/routes/oauth.js.map +1 -0
- package/dist/routes/oauth2.d.ts +3 -0
- package/dist/routes/oauth2.d.ts.map +1 -0
- package/dist/routes/oauth2.js +219 -0
- package/dist/routes/oauth2.js.map +1 -0
- package/dist/routes/oidcEnterprise.d.ts +3 -0
- package/dist/routes/oidcEnterprise.d.ts.map +1 -0
- package/dist/routes/oidcEnterprise.js +119 -0
- package/dist/routes/oidcEnterprise.js.map +1 -0
- package/dist/routes/password.d.ts +3 -0
- package/dist/routes/password.d.ts.map +1 -0
- package/dist/routes/password.js +83 -0
- package/dist/routes/password.js.map +1 -0
- package/dist/routes/profile.d.ts +3 -0
- package/dist/routes/profile.d.ts.map +1 -0
- package/dist/routes/profile.js +43 -0
- package/dist/routes/profile.js.map +1 -0
- package/dist/routes/saml.d.ts +3 -0
- package/dist/routes/saml.d.ts.map +1 -0
- package/dist/routes/saml.js +164 -0
- package/dist/routes/saml.js.map +1 -0
- package/dist/routes/scim.d.ts +3 -0
- package/dist/routes/scim.d.ts.map +1 -0
- package/dist/routes/scim.js +175 -0
- package/dist/routes/scim.js.map +1 -0
- package/dist/routes/sdk.d.ts +3 -0
- package/dist/routes/sdk.d.ts.map +1 -0
- package/dist/routes/sdk.js +129 -0
- package/dist/routes/sdk.js.map +1 -0
- package/dist/routes/serviceAccounts.d.ts +3 -0
- package/dist/routes/serviceAccounts.d.ts.map +1 -0
- package/dist/routes/serviceAccounts.js +82 -0
- package/dist/routes/serviceAccounts.js.map +1 -0
- package/dist/routes/sessions.d.ts +3 -0
- package/dist/routes/sessions.d.ts.map +1 -0
- package/dist/routes/sessions.js +55 -0
- package/dist/routes/sessions.js.map +1 -0
- package/dist/routes/setup.d.ts +3 -0
- package/dist/routes/setup.d.ts.map +1 -0
- package/dist/routes/setup.js +304 -0
- package/dist/routes/setup.js.map +1 -0
- package/dist/routes/smsOtp.d.ts +3 -0
- package/dist/routes/smsOtp.d.ts.map +1 -0
- package/dist/routes/smsOtp.js +50 -0
- package/dist/routes/smsOtp.js.map +1 -0
- package/dist/routes/totp.d.ts +3 -0
- package/dist/routes/totp.d.ts.map +1 -0
- package/dist/routes/totp.js +78 -0
- package/dist/routes/totp.js.map +1 -0
- package/dist/routes/webauthn.d.ts +3 -0
- package/dist/routes/webauthn.d.ts.map +1 -0
- package/dist/routes/webauthn.js +100 -0
- package/dist/routes/webauthn.js.map +1 -0
- package/dist/routes/workflows.d.ts +3 -0
- package/dist/routes/workflows.d.ts.map +1 -0
- package/dist/routes/workflows.js +140 -0
- package/dist/routes/workflows.js.map +1 -0
- package/dist/sdk/index.d.ts +5 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/dist/sdk/index.js +125 -0
- package/dist/sdk/index.js.map +1 -0
- package/dist/sdk/types.d.ts +110 -0
- package/dist/sdk/types.d.ts.map +1 -0
- package/dist/sdk/types.js +2 -0
- package/dist/sdk/types.js.map +1 -0
- package/dist/services/anomalyDetection.d.ts +14 -0
- package/dist/services/anomalyDetection.d.ts.map +1 -0
- package/dist/services/anomalyDetection.js +68 -0
- package/dist/services/anomalyDetection.js.map +1 -0
- package/dist/services/application/authentication.d.ts +38 -0
- package/dist/services/application/authentication.d.ts.map +1 -0
- package/dist/services/application/authentication.js +48 -0
- package/dist/services/application/authentication.js.map +1 -0
- package/dist/services/application/identity.d.ts +34 -0
- package/dist/services/application/identity.d.ts.map +1 -0
- package/dist/services/application/identity.js +34 -0
- package/dist/services/application/identity.js.map +1 -0
- package/dist/services/application/index.d.ts +4 -0
- package/dist/services/application/index.d.ts.map +1 -0
- package/dist/services/application/index.js +4 -0
- package/dist/services/application/index.js.map +1 -0
- package/dist/services/application/organization.d.ts +44 -0
- package/dist/services/application/organization.d.ts.map +1 -0
- package/dist/services/application/organization.js +53 -0
- package/dist/services/application/organization.js.map +1 -0
- package/dist/services/applications.d.ts +25 -0
- package/dist/services/applications.d.ts.map +1 -0
- package/dist/services/applications.js +64 -0
- package/dist/services/applications.js.map +1 -0
- package/dist/services/audit.d.ts +16 -0
- package/dist/services/audit.d.ts.map +1 -0
- package/dist/services/audit.js +28 -0
- package/dist/services/audit.js.map +1 -0
- package/dist/services/auditExport.d.ts +17 -0
- package/dist/services/auditExport.d.ts.map +1 -0
- package/dist/services/auditExport.js +37 -0
- package/dist/services/auditExport.js.map +1 -0
- package/dist/services/billing.d.ts +23 -0
- package/dist/services/billing.d.ts.map +1 -0
- package/dist/services/billing.js +67 -0
- package/dist/services/billing.js.map +1 -0
- package/dist/services/cache.d.ts +23 -0
- package/dist/services/cache.d.ts.map +1 -0
- package/dist/services/cache.js +114 -0
- package/dist/services/cache.js.map +1 -0
- package/dist/services/configuration/profiles.d.ts +13 -0
- package/dist/services/configuration/profiles.d.ts.map +1 -0
- package/dist/services/configuration/profiles.js +108 -0
- package/dist/services/configuration/profiles.js.map +1 -0
- package/dist/services/configuration.d.ts +50 -0
- package/dist/services/configuration.d.ts.map +1 -0
- package/dist/services/configuration.js +74 -0
- package/dist/services/configuration.js.map +1 -0
- package/dist/services/connectors/google.d.ts +17 -0
- package/dist/services/connectors/google.d.ts.map +1 -0
- package/dist/services/connectors/google.js +81 -0
- package/dist/services/connectors/google.js.map +1 -0
- package/dist/services/connectors/oidc.d.ts +14 -0
- package/dist/services/connectors/oidc.d.ts.map +1 -0
- package/dist/services/connectors/oidc.js +100 -0
- package/dist/services/connectors/oidc.js.map +1 -0
- package/dist/services/connectors/registry.d.ts +13 -0
- package/dist/services/connectors/registry.d.ts.map +1 -0
- package/dist/services/connectors/registry.js +109 -0
- package/dist/services/connectors/registry.js.map +1 -0
- package/dist/services/connectors/types.d.ts +36 -0
- package/dist/services/connectors/types.d.ts.map +1 -0
- package/dist/services/connectors/types.js +2 -0
- package/dist/services/connectors/types.js.map +1 -0
- package/dist/services/connectors/zitadel.d.ts +14 -0
- package/dist/services/connectors/zitadel.d.ts.map +1 -0
- package/dist/services/connectors/zitadel.js +70 -0
- package/dist/services/connectors/zitadel.js.map +1 -0
- package/dist/services/devices.d.ts +4 -0
- package/dist/services/devices.d.ts.map +1 -0
- package/dist/services/devices.js +59 -0
- package/dist/services/devices.js.map +1 -0
- package/dist/services/domain/authentication.d.ts +51 -0
- package/dist/services/domain/authentication.d.ts.map +1 -0
- package/dist/services/domain/authentication.js +218 -0
- package/dist/services/domain/authentication.js.map +1 -0
- package/dist/services/domain/authorization.d.ts +20 -0
- package/dist/services/domain/authorization.d.ts.map +1 -0
- package/dist/services/domain/authorization.js +36 -0
- package/dist/services/domain/authorization.js.map +1 -0
- package/dist/services/domain/identity.d.ts +43 -0
- package/dist/services/domain/identity.d.ts.map +1 -0
- package/dist/services/domain/identity.js +75 -0
- package/dist/services/domain/identity.js.map +1 -0
- package/dist/services/domain/index.d.ts +5 -0
- package/dist/services/domain/index.d.ts.map +1 -0
- package/dist/services/domain/index.js +5 -0
- package/dist/services/domain/index.js.map +1 -0
- package/dist/services/domain/organization.d.ts +60 -0
- package/dist/services/domain/organization.d.ts.map +1 -0
- package/dist/services/domain/organization.js +87 -0
- package/dist/services/domain/organization.js.map +1 -0
- package/dist/services/email.d.ts +24 -0
- package/dist/services/email.d.ts.map +1 -0
- package/dist/services/email.js +127 -0
- package/dist/services/email.js.map +1 -0
- package/dist/services/emailVerification.d.ts +20 -0
- package/dist/services/emailVerification.d.ts.map +1 -0
- package/dist/services/emailVerification.js +58 -0
- package/dist/services/emailVerification.js.map +1 -0
- package/dist/services/enterpriseSso.d.ts +34 -0
- package/dist/services/enterpriseSso.d.ts.map +1 -0
- package/dist/services/enterpriseSso.js +40 -0
- package/dist/services/enterpriseSso.js.map +1 -0
- package/dist/services/events/bus.d.ts +9 -0
- package/dist/services/events/bus.d.ts.map +1 -0
- package/dist/services/events/bus.js +36 -0
- package/dist/services/events/bus.js.map +1 -0
- package/dist/services/events/subscribers/anomaly.d.ts +3 -0
- package/dist/services/events/subscribers/anomaly.d.ts.map +1 -0
- package/dist/services/events/subscribers/anomaly.js +13 -0
- package/dist/services/events/subscribers/anomaly.js.map +1 -0
- package/dist/services/events/subscribers/auditLog.d.ts +3 -0
- package/dist/services/events/subscribers/auditLog.d.ts.map +1 -0
- package/dist/services/events/subscribers/auditLog.js +21 -0
- package/dist/services/events/subscribers/auditLog.js.map +1 -0
- package/dist/services/events/subscribers/webhook.d.ts +3 -0
- package/dist/services/events/subscribers/webhook.d.ts.map +1 -0
- package/dist/services/events/subscribers/webhook.js +36 -0
- package/dist/services/events/subscribers/webhook.js.map +1 -0
- package/dist/services/events/subscribers/webhookDispatch.d.ts +7 -0
- package/dist/services/events/subscribers/webhookDispatch.d.ts.map +1 -0
- package/dist/services/events/subscribers/webhookDispatch.js +14 -0
- package/dist/services/events/subscribers/webhookDispatch.js.map +1 -0
- package/dist/services/events/types.d.ts +21 -0
- package/dist/services/events/types.d.ts.map +1 -0
- package/dist/services/events/types.js +8 -0
- package/dist/services/events/types.js.map +1 -0
- package/dist/services/events/validate.d.ts +9 -0
- package/dist/services/events/validate.d.ts.map +1 -0
- package/dist/services/events/validate.js +103 -0
- package/dist/services/events/validate.js.map +1 -0
- package/dist/services/featureFlags.d.ts +15 -0
- package/dist/services/featureFlags.d.ts.map +1 -0
- package/dist/services/featureFlags.js +74 -0
- package/dist/services/featureFlags.js.map +1 -0
- package/dist/services/features.d.ts +12 -0
- package/dist/services/features.d.ts.map +1 -0
- package/dist/services/features.js +17 -0
- package/dist/services/features.js.map +1 -0
- package/dist/services/federation.d.ts +12 -0
- package/dist/services/federation.d.ts.map +1 -0
- package/dist/services/federation.js +22 -0
- package/dist/services/federation.js.map +1 -0
- package/dist/services/hibp.d.ts +2 -0
- package/dist/services/hibp.d.ts.map +1 -0
- package/dist/services/hibp.js +29 -0
- package/dist/services/hibp.js.map +1 -0
- package/dist/services/ipControls.d.ts +7 -0
- package/dist/services/ipControls.d.ts.map +1 -0
- package/dist/services/ipControls.js +92 -0
- package/dist/services/ipControls.js.map +1 -0
- package/dist/services/magicLinks.d.ts +16 -0
- package/dist/services/magicLinks.d.ts.map +1 -0
- package/dist/services/magicLinks.js +39 -0
- package/dist/services/magicLinks.js.map +1 -0
- package/dist/services/oauth2.d.ts +58 -0
- package/dist/services/oauth2.d.ts.map +1 -0
- package/dist/services/oauth2.js +144 -0
- package/dist/services/oauth2.js.map +1 -0
- package/dist/services/organizations.d.ts +39 -0
- package/dist/services/organizations.d.ts.map +1 -0
- package/dist/services/organizations.js +135 -0
- package/dist/services/organizations.js.map +1 -0
- package/dist/services/permissions.d.ts +19 -0
- package/dist/services/permissions.d.ts.map +1 -0
- package/dist/services/permissions.js +119 -0
- package/dist/services/permissions.js.map +1 -0
- package/dist/services/plugins/registry.d.ts +17 -0
- package/dist/services/plugins/registry.d.ts.map +1 -0
- package/dist/services/plugins/registry.js +196 -0
- package/dist/services/plugins/registry.js.map +1 -0
- package/dist/services/plugins/types.d.ts +86 -0
- package/dist/services/plugins/types.d.ts.map +1 -0
- package/dist/services/plugins/types.js +2 -0
- package/dist/services/plugins/types.js.map +1 -0
- package/dist/services/queue/bullmq.d.ts +16 -0
- package/dist/services/queue/bullmq.d.ts.map +1 -0
- package/dist/services/queue/bullmq.js +89 -0
- package/dist/services/queue/bullmq.js.map +1 -0
- package/dist/services/queue/inProcess.d.ts +15 -0
- package/dist/services/queue/inProcess.d.ts.map +1 -0
- package/dist/services/queue/inProcess.js +63 -0
- package/dist/services/queue/inProcess.js.map +1 -0
- package/dist/services/queue/index.d.ts +7 -0
- package/dist/services/queue/index.d.ts.map +1 -0
- package/dist/services/queue/index.js +19 -0
- package/dist/services/queue/index.js.map +1 -0
- package/dist/services/queue/types.d.ts +24 -0
- package/dist/services/queue/types.d.ts.map +1 -0
- package/dist/services/queue/types.js +2 -0
- package/dist/services/queue/types.js.map +1 -0
- package/dist/services/redis.d.ts +9 -0
- package/dist/services/redis.d.ts.map +1 -0
- package/dist/services/redis.js +15 -0
- package/dist/services/redis.js.map +1 -0
- package/dist/services/secrets/awsKms.d.ts +41 -0
- package/dist/services/secrets/awsKms.d.ts.map +1 -0
- package/dist/services/secrets/awsKms.js +81 -0
- package/dist/services/secrets/awsKms.js.map +1 -0
- package/dist/services/secrets/azureKeyVault.d.ts +44 -0
- package/dist/services/secrets/azureKeyVault.d.ts.map +1 -0
- package/dist/services/secrets/azureKeyVault.js +152 -0
- package/dist/services/secrets/azureKeyVault.js.map +1 -0
- package/dist/services/secrets/database.d.ts +29 -0
- package/dist/services/secrets/database.d.ts.map +1 -0
- package/dist/services/secrets/database.js +138 -0
- package/dist/services/secrets/database.js.map +1 -0
- package/dist/services/secrets/environment.d.ts +27 -0
- package/dist/services/secrets/environment.d.ts.map +1 -0
- package/dist/services/secrets/environment.js +102 -0
- package/dist/services/secrets/environment.js.map +1 -0
- package/dist/services/secrets/index.d.ts +30 -0
- package/dist/services/secrets/index.d.ts.map +1 -0
- package/dist/services/secrets/index.js +46 -0
- package/dist/services/secrets/index.js.map +1 -0
- package/dist/services/secrets/password.d.ts +10 -0
- package/dist/services/secrets/password.d.ts.map +1 -0
- package/dist/services/secrets/password.js +62 -0
- package/dist/services/secrets/password.js.map +1 -0
- package/dist/services/secrets/provider.d.ts +36 -0
- package/dist/services/secrets/provider.d.ts.map +1 -0
- package/dist/services/secrets/provider.js +2 -0
- package/dist/services/secrets/provider.js.map +1 -0
- package/dist/services/secrets/vault.d.ts +41 -0
- package/dist/services/secrets/vault.d.ts.map +1 -0
- package/dist/services/secrets/vault.js +133 -0
- package/dist/services/secrets/vault.js.map +1 -0
- package/dist/services/secrets.d.ts +2 -0
- package/dist/services/secrets.d.ts.map +1 -0
- package/dist/services/secrets.js +3 -0
- package/dist/services/secrets.js.map +1 -0
- package/dist/services/serviceAccounts.d.ts +24 -0
- package/dist/services/serviceAccounts.d.ts.map +1 -0
- package/dist/services/serviceAccounts.js +53 -0
- package/dist/services/serviceAccounts.js.map +1 -0
- package/dist/services/setup/configWriter.d.ts +18 -0
- package/dist/services/setup/configWriter.d.ts.map +1 -0
- package/dist/services/setup/configWriter.js +192 -0
- package/dist/services/setup/configWriter.js.map +1 -0
- package/dist/services/setup/diagnostics.d.ts +7 -0
- package/dist/services/setup/diagnostics.d.ts.map +1 -0
- package/dist/services/setup/diagnostics.js +73 -0
- package/dist/services/setup/diagnostics.js.map +1 -0
- package/dist/services/setup/token.d.ts +5 -0
- package/dist/services/setup/token.d.ts.map +1 -0
- package/dist/services/setup/token.js +37 -0
- package/dist/services/setup/token.js.map +1 -0
- package/dist/services/setup/types.d.ts +35 -0
- package/dist/services/setup/types.d.ts.map +1 -0
- package/dist/services/setup/types.js +2 -0
- package/dist/services/setup/types.js.map +1 -0
- package/dist/services/setup/validation.d.ts +7 -0
- package/dist/services/setup/validation.d.ts.map +1 -0
- package/dist/services/setup/validation.js +198 -0
- package/dist/services/setup/validation.js.map +1 -0
- package/dist/services/sms.d.ts +10 -0
- package/dist/services/sms.d.ts.map +1 -0
- package/dist/services/sms.js +62 -0
- package/dist/services/sms.js.map +1 -0
- package/dist/services/smsOtp.d.ts +7 -0
- package/dist/services/smsOtp.d.ts.map +1 -0
- package/dist/services/smsOtp.js +56 -0
- package/dist/services/smsOtp.js.map +1 -0
- package/dist/services/tokens.d.ts +66 -0
- package/dist/services/tokens.d.ts.map +1 -0
- package/dist/services/tokens.js +205 -0
- package/dist/services/tokens.js.map +1 -0
- package/dist/services/totp.d.ts +16 -0
- package/dist/services/totp.d.ts.map +1 -0
- package/dist/services/totp.js +96 -0
- package/dist/services/totp.js.map +1 -0
- package/dist/services/users.d.ts +39 -0
- package/dist/services/users.d.ts.map +1 -0
- package/dist/services/users.js +181 -0
- package/dist/services/users.js.map +1 -0
- package/dist/services/webauthn.d.ts +45 -0
- package/dist/services/webauthn.d.ts.map +1 -0
- package/dist/services/webauthn.js +147 -0
- package/dist/services/webauthn.js.map +1 -0
- package/dist/services/webhooks.d.ts +86 -0
- package/dist/services/webhooks.d.ts.map +1 -0
- package/dist/services/webhooks.js +164 -0
- package/dist/services/webhooks.js.map +1 -0
- package/dist/services/workflows/engine.d.ts +18 -0
- package/dist/services/workflows/engine.d.ts.map +1 -0
- package/dist/services/workflows/engine.js +104 -0
- package/dist/services/workflows/engine.js.map +1 -0
- package/dist/services/workflows/steps.d.ts +17 -0
- package/dist/services/workflows/steps.d.ts.map +1 -0
- package/dist/services/workflows/steps.js +109 -0
- package/dist/services/workflows/steps.js.map +1 -0
- package/dist/services/zitadel.d.ts +21 -0
- package/dist/services/zitadel.d.ts.map +1 -0
- package/dist/services/zitadel.js +228 -0
- package/dist/services/zitadel.js.map +1 -0
- package/dist/setup-server.d.ts +2 -0
- package/dist/setup-server.d.ts.map +1 -0
- package/dist/setup-server.js +63 -0
- package/dist/setup-server.js.map +1 -0
- package/dist/tests/errors.test.d.ts +2 -0
- package/dist/tests/errors.test.d.ts.map +1 -0
- package/dist/tests/errors.test.js +64 -0
- package/dist/tests/errors.test.js.map +1 -0
- package/dist/tests/events.test.d.ts +2 -0
- package/dist/tests/events.test.d.ts.map +1 -0
- package/dist/tests/events.test.js +45 -0
- package/dist/tests/events.test.js.map +1 -0
- package/dist/tests/featureFlags.test.d.ts +2 -0
- package/dist/tests/featureFlags.test.d.ts.map +1 -0
- package/dist/tests/featureFlags.test.js +24 -0
- package/dist/tests/featureFlags.test.js.map +1 -0
- package/dist/tests/integration/queue.test.d.ts +2 -0
- package/dist/tests/integration/queue.test.d.ts.map +1 -0
- package/dist/tests/integration/queue.test.js +49 -0
- package/dist/tests/integration/queue.test.js.map +1 -0
- package/dist/tests/plugins.test.d.ts +2 -0
- package/dist/tests/plugins.test.d.ts.map +1 -0
- package/dist/tests/plugins.test.js +56 -0
- package/dist/tests/plugins.test.js.map +1 -0
- package/dist/tests/result.test.d.ts +2 -0
- package/dist/tests/result.test.d.ts.map +1 -0
- package/dist/tests/result.test.js +46 -0
- package/dist/tests/result.test.js.map +1 -0
- package/dist/tests/setup/configWriter.test.d.ts +2 -0
- package/dist/tests/setup/configWriter.test.d.ts.map +1 -0
- package/dist/tests/setup/configWriter.test.js +55 -0
- package/dist/tests/setup/configWriter.test.js.map +1 -0
- package/dist/tests/setup/validation.test.d.ts +2 -0
- package/dist/tests/setup/validation.test.d.ts.map +1 -0
- package/dist/tests/setup/validation.test.js +26 -0
- package/dist/tests/setup/validation.test.js.map +1 -0
- package/dist/tests/smoke.test.d.ts +2 -0
- package/dist/tests/smoke.test.d.ts.map +1 -0
- package/dist/tests/smoke.test.js +124 -0
- package/dist/tests/smoke.test.js.map +1 -0
- package/dist/types.d.ts +50 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +17 -0
- package/dist/types.js.map +1 -0
- package/package.json +69 -0
- package/src/cli.ts +172 -0
- package/src/config.ts +143 -0
- package/src/container.ts +48 -0
- package/src/db/index.ts +53 -0
- package/src/db/migrations/0000_skinny_ben_parker.sql +553 -0
- package/src/db/migrations/0001_brief_joseph.sql +10 -0
- package/src/db/migrations/0002_add_account_lockout.sql +3 -0
- package/src/db/migrations/0003_steep_rachel_grey.sql +1 -0
- package/src/db/migrations/0004_clever_bill_hollister.sql +20 -0
- package/src/db/migrations/0005_steep_the_call.sql +2 -0
- package/src/db/migrations/0006_ambitious_boomer.sql +17 -0
- package/src/db/migrations/0007_greedy_vindicator.sql +40 -0
- package/src/db/migrations/meta/0000_snapshot.json +2964 -0
- package/src/db/migrations/meta/0001_snapshot.json +3031 -0
- package/src/db/migrations/meta/0003_snapshot.json +3051 -0
- package/src/db/migrations/meta/0004_snapshot.json +3163 -0
- package/src/db/migrations/meta/0005_snapshot.json +3177 -0
- package/src/db/migrations/meta/0006_snapshot.json +3270 -0
- package/src/db/migrations/meta/0007_snapshot.json +3489 -0
- package/src/db/migrations/meta/_journal.json +62 -0
- package/src/db/schema.ts +676 -0
- package/src/db/seed.ts +146 -0
- package/src/di.ts +81 -0
- package/src/index.ts +326 -0
- package/src/lib/errors.ts +139 -0
- package/src/lib/result.ts +44 -0
- package/src/lib/webhookSignature.ts +41 -0
- package/src/plugins/appContext.ts +76 -0
- package/src/plugins/audit.ts +42 -0
- package/src/plugins/auth.ts +230 -0
- package/src/plugins/metrics.ts +41 -0
- package/src/plugins/mtls.ts +94 -0
- package/src/plugins/permissions.ts +29 -0
- package/src/plugins/rateLimit.ts +133 -0
- package/src/plugins/requestLogger.ts +25 -0
- package/src/plugins/tracing.ts +22 -0
- package/src/repositories/apiKey.ts +158 -0
- package/src/repositories/application.ts +70 -0
- package/src/repositories/audit.ts +32 -0
- package/src/repositories/index.ts +9 -0
- package/src/repositories/oidcConnection.ts +111 -0
- package/src/repositories/organization.ts +167 -0
- package/src/repositories/permission.ts +153 -0
- package/src/repositories/samlConnection.ts +96 -0
- package/src/repositories/session.ts +86 -0
- package/src/repositories/types.ts +134 -0
- package/src/repositories/user.ts +161 -0
- package/src/routes/admin/billing.ts +48 -0
- package/src/routes/admin/helpers.ts +50 -0
- package/src/routes/admin/index.ts +16 -0
- package/src/routes/admin/organizations.ts +299 -0
- package/src/routes/admin/permissions.ts +80 -0
- package/src/routes/admin/platform.ts +335 -0
- package/src/routes/admin/sso.ts +138 -0
- package/src/routes/admin/webhooks.ts +73 -0
- package/src/routes/admin.ts +1 -0
- package/src/routes/apiKeys.ts +60 -0
- package/src/routes/auth.ts +214 -0
- package/src/routes/authz.ts +38 -0
- package/src/routes/config.ts +67 -0
- package/src/routes/emailVerification.ts +73 -0
- package/src/routes/federation.ts +156 -0
- package/src/routes/helpers.ts +17 -0
- package/src/routes/magicLinks.ts +107 -0
- package/src/routes/oauth.ts +167 -0
- package/src/routes/oauth2.ts +284 -0
- package/src/routes/oidcEnterprise.ts +166 -0
- package/src/routes/password.ts +90 -0
- package/src/routes/profile.ts +44 -0
- package/src/routes/saml.ts +209 -0
- package/src/routes/scim.ts +199 -0
- package/src/routes/sdk.ts +141 -0
- package/src/routes/serviceAccounts.ts +124 -0
- package/src/routes/sessions.ts +62 -0
- package/src/routes/setup.ts +315 -0
- package/src/routes/smsOtp.ts +67 -0
- package/src/routes/totp.ts +122 -0
- package/src/routes/webauthn.ts +147 -0
- package/src/routes/workflows.ts +157 -0
- package/src/sdk/index.ts +152 -0
- package/src/sdk/types.ts +54 -0
- package/src/services/anomalyDetection.ts +73 -0
- package/src/services/application/authentication.ts +75 -0
- package/src/services/application/identity.ts +56 -0
- package/src/services/application/index.ts +3 -0
- package/src/services/application/organization.ts +77 -0
- package/src/services/applications.ts +89 -0
- package/src/services/audit.ts +41 -0
- package/src/services/auditExport.ts +59 -0
- package/src/services/billing.ts +87 -0
- package/src/services/cache.ts +131 -0
- package/src/services/configuration/profiles.ts +115 -0
- package/src/services/configuration.ts +120 -0
- package/src/services/connectors/google.ts +92 -0
- package/src/services/connectors/oidc.ts +111 -0
- package/src/services/connectors/registry.ts +124 -0
- package/src/services/connectors/types.ts +38 -0
- package/src/services/connectors/zitadel.ts +77 -0
- package/src/services/devices.ts +76 -0
- package/src/services/domain/authentication.ts +279 -0
- package/src/services/domain/authorization.ts +49 -0
- package/src/services/domain/identity.ts +108 -0
- package/src/services/domain/index.ts +4 -0
- package/src/services/domain/organization.ts +114 -0
- package/src/services/email.ts +167 -0
- package/src/services/emailVerification.ts +75 -0
- package/src/services/enterpriseSso.ts +58 -0
- package/src/services/events/bus.ts +43 -0
- package/src/services/events/subscribers/anomaly.ts +13 -0
- package/src/services/events/subscribers/auditLog.ts +21 -0
- package/src/services/events/subscribers/webhook.ts +39 -0
- package/src/services/events/subscribers/webhookDispatch.ts +14 -0
- package/src/services/events/types.ts +106 -0
- package/src/services/events/validate.ts +118 -0
- package/src/services/featureFlags.ts +90 -0
- package/src/services/features.ts +22 -0
- package/src/services/federation.ts +30 -0
- package/src/services/hibp.ts +28 -0
- package/src/services/ipControls.ts +99 -0
- package/src/services/magicLinks.ts +51 -0
- package/src/services/oauth2.ts +216 -0
- package/src/services/organizations.ts +171 -0
- package/src/services/permissions.ts +146 -0
- package/src/services/plugins/registry.ts +242 -0
- package/src/services/plugins/types.ts +98 -0
- package/src/services/queue/bullmq.ts +104 -0
- package/src/services/queue/inProcess.ts +75 -0
- package/src/services/queue/index.ts +19 -0
- package/src/services/queue/types.ts +26 -0
- package/src/services/redis.ts +16 -0
- package/src/services/secrets/awsKms.ts +97 -0
- package/src/services/secrets/azureKeyVault.ts +171 -0
- package/src/services/secrets/database.ts +166 -0
- package/src/services/secrets/environment.ts +117 -0
- package/src/services/secrets/index.ts +50 -0
- package/src/services/secrets/password.ts +73 -0
- package/src/services/secrets/provider.ts +38 -0
- package/src/services/secrets/vault.ts +149 -0
- package/src/services/secrets.ts +2 -0
- package/src/services/serviceAccounts.ts +74 -0
- package/src/services/setup/configWriter.ts +200 -0
- package/src/services/setup/diagnostics.ts +76 -0
- package/src/services/setup/token.ts +42 -0
- package/src/services/setup/types.ts +40 -0
- package/src/services/setup/validation.ts +205 -0
- package/src/services/sms.ts +81 -0
- package/src/services/smsOtp.ts +74 -0
- package/src/services/tokens.ts +288 -0
- package/src/services/totp.ts +113 -0
- package/src/services/users.ts +239 -0
- package/src/services/webauthn.ts +193 -0
- package/src/services/webhooks.ts +190 -0
- package/src/services/workflows/engine.ts +125 -0
- package/src/services/workflows/steps.ts +129 -0
- package/src/services/zitadel.ts +277 -0
- package/src/setup-server.ts +71 -0
- package/src/tests/errors.test.ts +74 -0
- package/src/tests/events.test.ts +52 -0
- package/src/tests/featureFlags.test.ts +26 -0
- package/src/tests/integration/queue.test.ts +54 -0
- package/src/tests/plugins.test.ts +73 -0
- package/src/tests/result.test.ts +52 -0
- package/src/tests/setup/configWriter.test.ts +69 -0
- package/src/tests/setup/validation.test.ts +29 -0
- package/src/tests/smoke.test.ts +136 -0
- package/src/types.ts +70 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { FastifyInstance } from "fastify";
|
|
3
|
+
import { getSdk } from "../sdk/index.js";
|
|
4
|
+
import {
|
|
5
|
+
setSessionCookies,
|
|
6
|
+
clearSessionCookies,
|
|
7
|
+
getRefreshToken,
|
|
8
|
+
} from "../plugins/auth.js";
|
|
9
|
+
import { rateLimit } from "../plugins/rateLimit.js";
|
|
10
|
+
import { checkImpossibleTravel } from "../services/anomalyDetection.js";
|
|
11
|
+
import { sendSuspiciousLoginAlert } from "../services/email.js";
|
|
12
|
+
import { checkIpAllowed } from "../services/ipControls.js";
|
|
13
|
+
import { toPublicUser } from "../types.js";
|
|
14
|
+
import { verifyTOTP } from "../services/totp.js";
|
|
15
|
+
import { sendResultError } from "./helpers.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Fire-and-forget impossible-travel check after a successful login.
|
|
19
|
+
* Emits a suspicious-login email when the user's previous login came from a
|
|
20
|
+
* different IP within the anomaly window.
|
|
21
|
+
*/
|
|
22
|
+
function detectImpossibleTravel(user: { id: string; email: string }, ip?: string, userAgent?: string): void {
|
|
23
|
+
checkImpossibleTravel(user.id, ip)
|
|
24
|
+
.then(async (suspicious) => {
|
|
25
|
+
if (!suspicious) return;
|
|
26
|
+
await sendSuspiciousLoginAlert({
|
|
27
|
+
email: user.email,
|
|
28
|
+
ipAddress: ip,
|
|
29
|
+
userAgent,
|
|
30
|
+
reason: "Sign-in from a new location within minutes of the previous one",
|
|
31
|
+
});
|
|
32
|
+
})
|
|
33
|
+
.catch((err: unknown) => {
|
|
34
|
+
console.error("[anomaly] impossible-travel check failed:", err);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const RegisterSchema = z.object({
|
|
39
|
+
username: z.string().min(3).max(32).regex(/^[a-zA-Z0-9_-]+$/),
|
|
40
|
+
email: z.string().email().max(255),
|
|
41
|
+
password: z.string().min(8).max(128),
|
|
42
|
+
name: z.string().max(255).optional(),
|
|
43
|
+
client_id: z.string().optional(),
|
|
44
|
+
metadata: z.record(z.unknown()).optional(),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const LoginSchema = z.object({
|
|
48
|
+
email: z.string().email().max(255),
|
|
49
|
+
password: z.string().min(1).max(128),
|
|
50
|
+
client_id: z.string().optional(),
|
|
51
|
+
totp_code: z.string().min(6).max(8).optional(),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const RefreshSchema = z.object({
|
|
55
|
+
client_id: z.string().optional(),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
export default async function authRoutes(app: FastifyInstance) {
|
|
59
|
+
const sdk = getSdk();
|
|
60
|
+
|
|
61
|
+
app.post(
|
|
62
|
+
"/register",
|
|
63
|
+
{
|
|
64
|
+
preHandler: [
|
|
65
|
+
rateLimit({
|
|
66
|
+
keyPrefix: "register",
|
|
67
|
+
maxAttempts: 5,
|
|
68
|
+
windowSeconds: 900,
|
|
69
|
+
}),
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
async (request, reply) => {
|
|
73
|
+
const body = RegisterSchema.parse(request.body);
|
|
74
|
+
|
|
75
|
+
const ipCheck = await checkIpAllowed(body.client_id, request.ip);
|
|
76
|
+
if (!ipCheck.allowed) {
|
|
77
|
+
return reply.status(403).send({ error: ipCheck.reason, code: "IP_NOT_ALLOWED" });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const result = await sdk.authentication.register({
|
|
81
|
+
username: body.username,
|
|
82
|
+
email: body.email,
|
|
83
|
+
password: body.password,
|
|
84
|
+
name: body.name,
|
|
85
|
+
clientId: body.client_id,
|
|
86
|
+
metadata: body.metadata,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
if (!result.success) return sendResultError(reply, result);
|
|
90
|
+
|
|
91
|
+
await request.audit("user_registered", { userId: result.data.user.id, email: result.data.user.email });
|
|
92
|
+
setSessionCookies(reply, result.data.accessToken, result.data.refreshToken, body.client_id);
|
|
93
|
+
return { user: toPublicUser(result.data.user) };
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
app.post(
|
|
98
|
+
"/login",
|
|
99
|
+
{
|
|
100
|
+
preHandler: [
|
|
101
|
+
rateLimit({
|
|
102
|
+
keyPrefix: "login",
|
|
103
|
+
maxAttempts: 5,
|
|
104
|
+
windowSeconds: 900,
|
|
105
|
+
}),
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
async (request, reply) => {
|
|
109
|
+
const body = LoginSchema.parse(request.body);
|
|
110
|
+
|
|
111
|
+
const ipCheck = await checkIpAllowed(body.client_id, request.ip);
|
|
112
|
+
if (!ipCheck.allowed) {
|
|
113
|
+
return reply.status(403).send({ error: ipCheck.reason, code: "IP_NOT_ALLOWED" });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const result = await sdk.authentication.login({
|
|
117
|
+
email: body.email,
|
|
118
|
+
password: body.password,
|
|
119
|
+
clientId: body.client_id,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
if (!result.success) return sendResultError(reply, result);
|
|
123
|
+
|
|
124
|
+
if (body.totp_code) {
|
|
125
|
+
const validTotp = await verifyTOTP(result.data.user.id, body.totp_code);
|
|
126
|
+
if (!validTotp) {
|
|
127
|
+
return reply.status(401).send({ error: "Invalid two-factor code." });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
await request.audit("user_login", { userId: result.data.user.id });
|
|
132
|
+
detectImpossibleTravel(result.data.user, request.ip, request.headers["user-agent"]);
|
|
133
|
+
setSessionCookies(reply, result.data.accessToken, result.data.refreshToken, body.client_id);
|
|
134
|
+
return { user: toPublicUser(result.data.user) };
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
app.post(
|
|
139
|
+
"/token-login",
|
|
140
|
+
{
|
|
141
|
+
preHandler: [
|
|
142
|
+
rateLimit({
|
|
143
|
+
keyPrefix: "login",
|
|
144
|
+
maxAttempts: 5,
|
|
145
|
+
windowSeconds: 900,
|
|
146
|
+
}),
|
|
147
|
+
],
|
|
148
|
+
},
|
|
149
|
+
async (request, reply) => {
|
|
150
|
+
const body = LoginSchema.parse(request.body);
|
|
151
|
+
|
|
152
|
+
const ipCheck = await checkIpAllowed(body.client_id, request.ip);
|
|
153
|
+
if (!ipCheck.allowed) {
|
|
154
|
+
return reply.status(403).send({ error: ipCheck.reason, code: "IP_NOT_ALLOWED" });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const result = await sdk.authentication.login({
|
|
158
|
+
email: body.email,
|
|
159
|
+
password: body.password,
|
|
160
|
+
clientId: body.client_id,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
if (!result.success) return sendResultError(reply, result);
|
|
164
|
+
|
|
165
|
+
if (body.totp_code) {
|
|
166
|
+
const validTotp = await verifyTOTP(result.data.user.id, body.totp_code);
|
|
167
|
+
if (!validTotp) {
|
|
168
|
+
return reply.status(401).send({ error: "Invalid two-factor code." });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
await request.audit("user_token_login", { userId: result.data.user.id });
|
|
173
|
+
detectImpossibleTravel(result.data.user, request.ip, request.headers["user-agent"]);
|
|
174
|
+
return {
|
|
175
|
+
accessToken: result.data.accessToken,
|
|
176
|
+
user: toPublicUser(result.data.user),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
app.get("/me", { preHandler: [app.authenticate] }, async (request) => {
|
|
182
|
+
return { user: request.user ? toPublicUser(request.user) : null };
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
app.post("/refresh", async (request, reply) => {
|
|
186
|
+
const body = RefreshSchema.parse(request.body ?? {});
|
|
187
|
+
const clientId = body.client_id;
|
|
188
|
+
|
|
189
|
+
const refreshToken = getRefreshToken(request, clientId) || getRefreshToken(request);
|
|
190
|
+
if (!refreshToken) {
|
|
191
|
+
return reply.status(401).send({ error: "Unauthorized" });
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const result = await sdk.authentication.refresh(refreshToken, clientId);
|
|
195
|
+
if (!result.success) return sendResultError(reply, result);
|
|
196
|
+
|
|
197
|
+
await request.audit("token_refresh", {});
|
|
198
|
+
setSessionCookies(reply, result.data.accessToken, result.data.refreshToken, clientId);
|
|
199
|
+
return { success: true };
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
app.post("/logout", async (request, reply) => {
|
|
203
|
+
const appClientId = request.state?.app?.clientId;
|
|
204
|
+
const refreshToken =
|
|
205
|
+
getRefreshToken(request, appClientId) || getRefreshToken(request);
|
|
206
|
+
const result = await sdk.authentication.logout(refreshToken);
|
|
207
|
+
if (!result.success) return sendResultError(reply, result);
|
|
208
|
+
|
|
209
|
+
await request.audit("user_logout", { userId: request.user?.id });
|
|
210
|
+
clearSessionCookies(reply, appClientId);
|
|
211
|
+
clearSessionCookies(reply);
|
|
212
|
+
return { success: true };
|
|
213
|
+
});
|
|
214
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { FastifyInstance, FastifyRequest, FastifyReply } from "fastify";
|
|
3
|
+
import { getSdk } from "../sdk/index.js";
|
|
4
|
+
|
|
5
|
+
const CheckSchema = z.object({
|
|
6
|
+
resource: z.string().min(1),
|
|
7
|
+
action: z.string().min(1),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export default async function authzRoutes(app: FastifyInstance) {
|
|
11
|
+
const sdk = getSdk();
|
|
12
|
+
|
|
13
|
+
app.post(
|
|
14
|
+
"/authz/check",
|
|
15
|
+
{ preHandler: [app.authenticate] },
|
|
16
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
17
|
+
const body = CheckSchema.parse(request.body);
|
|
18
|
+
const membership = request.state?.membership;
|
|
19
|
+
|
|
20
|
+
if (!membership) {
|
|
21
|
+
return reply.status(403).send({
|
|
22
|
+
allowed: false,
|
|
23
|
+
reason: "No organization context",
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const allowed = await sdk.authorization.hasPermission(membership.role, body.resource, body.action);
|
|
28
|
+
await request.audit("authz_check", {
|
|
29
|
+
resource: body.resource,
|
|
30
|
+
action: body.action,
|
|
31
|
+
role: membership.role,
|
|
32
|
+
allowed,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return { allowed };
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { FastifyInstance, FastifyRequest, FastifyReply } from "fastify";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { createConfigWriter } from "../services/setup/configWriter.js";
|
|
4
|
+
import { queue } from "../services/queue/index.js";
|
|
5
|
+
|
|
6
|
+
const UpdateConfigSchema = z.object({
|
|
7
|
+
values: z.record(z.string()),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
async function requireOwner(request: FastifyRequest, reply: FastifyReply) {
|
|
11
|
+
await request.server.authenticate(request, reply);
|
|
12
|
+
if (reply.sent) return;
|
|
13
|
+
if (request.user!.role !== "owner") {
|
|
14
|
+
return reply.status(403).send({ error: "Forbidden: owner access required" });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default async function configRoutes(app: FastifyInstance) {
|
|
19
|
+
// Read current configuration values from the configured store (.env by default).
|
|
20
|
+
app.get("/", { preHandler: [requireOwner] }, async (_request: FastifyRequest, reply: FastifyReply) => {
|
|
21
|
+
try {
|
|
22
|
+
const writer = createConfigWriter();
|
|
23
|
+
const values = await writer.read();
|
|
24
|
+
return reply.send({ values });
|
|
25
|
+
} catch (err) {
|
|
26
|
+
app.log.error({ err }, "Failed to read configuration");
|
|
27
|
+
return reply.status(500).send({ error: "Failed to read configuration" });
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Update configuration values. A restart is required for most changes to take effect.
|
|
32
|
+
app.put("/", { preHandler: [requireOwner] }, async (request: FastifyRequest, reply: FastifyReply) => {
|
|
33
|
+
const parsed = UpdateConfigSchema.safeParse(request.body);
|
|
34
|
+
if (!parsed.success) {
|
|
35
|
+
return reply.status(400).send({ error: "Invalid input", details: parsed.error.issues });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const writer = createConfigWriter();
|
|
40
|
+
const backup = await writer.backup();
|
|
41
|
+
if (!backup.success) {
|
|
42
|
+
return reply.status(500).send({ error: backup.error.message, code: backup.error.code });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const writeResult = await writer.write(parsed.data.values);
|
|
46
|
+
if (!writeResult.success) {
|
|
47
|
+
return reply.status(500).send({ error: writeResult.error.message, code: writeResult.error.code });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return reply.send({ ok: true, backupPath: backup.data || undefined });
|
|
51
|
+
} catch (err) {
|
|
52
|
+
request.log.error({ err }, "Failed to update configuration");
|
|
53
|
+
return reply.status(500).send({ error: "Failed to update configuration" });
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Restart the server so new configuration is loaded.
|
|
58
|
+
app.post("/restart", { preHandler: [requireOwner] }, async (_request: FastifyRequest, reply: FastifyReply) => {
|
|
59
|
+
reply.status(202).send({ ok: true, message: "Server is restarting" });
|
|
60
|
+
try {
|
|
61
|
+
await queue.close?.();
|
|
62
|
+
} catch {
|
|
63
|
+
// ignore
|
|
64
|
+
}
|
|
65
|
+
setTimeout(() => process.exit(0), 500);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { FastifyInstance, FastifyRequest, FastifyReply } from "fastify";
|
|
3
|
+
import { findUserByEmail } from "../services/users.js";
|
|
4
|
+
import {
|
|
5
|
+
sendVerificationEmail,
|
|
6
|
+
consumeVerificationToken,
|
|
7
|
+
} from "../services/emailVerification.js";
|
|
8
|
+
import { rateLimit } from "../plugins/rateLimit.js";
|
|
9
|
+
|
|
10
|
+
const SendSchema = z.object({
|
|
11
|
+
email: z.string().email(),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export default async function emailVerificationRoutes(app: FastifyInstance) {
|
|
15
|
+
// Authenticated: resend the verification email for the signed-in user.
|
|
16
|
+
app.post("/email-verification/send", { preHandler: [app.authenticate] }, async (request: FastifyRequest, reply: FastifyReply) => {
|
|
17
|
+
const user = request.user;
|
|
18
|
+
if (!user) return reply.status(401).send({ error: "Unauthorized" });
|
|
19
|
+
if (user.emailVerified) return { success: true, alreadyVerified: true };
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
await sendVerificationEmail(user);
|
|
23
|
+
} catch (err) {
|
|
24
|
+
request.log.error({ err }, "Failed to send verification email");
|
|
25
|
+
return reply.status(500).send({ error: "Failed to send verification email" });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
await request.audit("email_verification_sent", { userId: user.id });
|
|
29
|
+
return { success: true };
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Unauthenticated: request a verification email by address.
|
|
33
|
+
// Always returns success to avoid user enumeration.
|
|
34
|
+
app.post("/email-verification/request", {
|
|
35
|
+
preHandler: [
|
|
36
|
+
rateLimit({
|
|
37
|
+
keyPrefix: "email-verification",
|
|
38
|
+
maxAttempts: 3,
|
|
39
|
+
windowSeconds: 900,
|
|
40
|
+
}),
|
|
41
|
+
],
|
|
42
|
+
}, async (request: FastifyRequest, reply: FastifyReply) => {
|
|
43
|
+
const body = SendSchema.parse(request.body);
|
|
44
|
+
const user = await findUserByEmail(body.email);
|
|
45
|
+
if (!user || user.emailVerified) {
|
|
46
|
+
return reply.status(200).send({ success: true });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
await sendVerificationEmail(user);
|
|
51
|
+
await request.audit("email_verification_sent", { userId: user.id });
|
|
52
|
+
} catch (err) {
|
|
53
|
+
request.log.error({ err }, "Failed to send verification email");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { success: true };
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
app.get("/email-verification/verify", async (request: FastifyRequest, reply: FastifyReply) => {
|
|
60
|
+
const query = request.query as { token?: string };
|
|
61
|
+
if (!query.token) {
|
|
62
|
+
return reply.status(400).send({ error: "Missing token" });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const user = await consumeVerificationToken(query.token);
|
|
66
|
+
if (!user) {
|
|
67
|
+
return reply.status(400).send({ error: "Invalid or expired token" });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
await request.audit("email_verified", { userId: user.id });
|
|
71
|
+
return { success: true, email: user.email };
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
4
|
+
import { config } from "../config.js";
|
|
5
|
+
import { listSupportedProviders, listConfiguredProviders } from "../services/connectors/registry.js";
|
|
6
|
+
import { getSdk } from "../sdk/index.js";
|
|
7
|
+
import { setSessionCookies, clearSessionCookies } from "../plugins/auth.js";
|
|
8
|
+
import { findApplicationByClientId } from "../services/applications.js";
|
|
9
|
+
import type { Application } from "../db/schema.js";
|
|
10
|
+
import { buildOAuthErrorRedirect, buildOAuthErrorResponse } from "../lib/errors.js";
|
|
11
|
+
import { sendResultError } from "./helpers.js";
|
|
12
|
+
|
|
13
|
+
function isProvider(value: string): boolean {
|
|
14
|
+
return listSupportedProviders().includes(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function randomState(): string {
|
|
18
|
+
return crypto.randomBytes(24).toString("base64url");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function publicUrl(): string {
|
|
22
|
+
return config.AUTH_API_PUBLIC_URL || `http://localhost:${config.PORT}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function callbackRedirectUri(provider: string): string {
|
|
26
|
+
return `${publicUrl()}/federation/${provider}/callback`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function setFederationState(reply: FastifyReply, state: string, clientId?: string): void {
|
|
30
|
+
reply.setCookie("fed_state", state, {
|
|
31
|
+
path: "/",
|
|
32
|
+
httpOnly: true,
|
|
33
|
+
secure: config.COOKIE_SECURE,
|
|
34
|
+
sameSite: "lax" as const,
|
|
35
|
+
domain: config.COOKIE_DOMAIN || undefined,
|
|
36
|
+
maxAge: 600,
|
|
37
|
+
});
|
|
38
|
+
if (clientId) {
|
|
39
|
+
reply.setCookie("fed_client_id", clientId, {
|
|
40
|
+
path: "/",
|
|
41
|
+
httpOnly: true,
|
|
42
|
+
secure: config.COOKIE_SECURE,
|
|
43
|
+
sameSite: "lax" as const,
|
|
44
|
+
domain: config.COOKIE_DOMAIN || undefined,
|
|
45
|
+
maxAge: 600,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function clearFederationCookies(reply: FastifyReply): void {
|
|
51
|
+
const opts = {
|
|
52
|
+
path: "/",
|
|
53
|
+
httpOnly: true,
|
|
54
|
+
secure: config.COOKIE_SECURE,
|
|
55
|
+
sameSite: "lax" as const,
|
|
56
|
+
domain: config.COOKIE_DOMAIN || undefined,
|
|
57
|
+
};
|
|
58
|
+
reply.clearCookie("fed_state", opts);
|
|
59
|
+
reply.clearCookie("fed_client_id", opts);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function redirectTargetUrl(clientId?: string, app?: Application): string {
|
|
63
|
+
if (app?.redirectUris?.length) return app.redirectUris[0];
|
|
64
|
+
return process.env.CLIENT_APP_URL || "http://localhost:5173";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const LinkIdentitySchema = z.object({
|
|
68
|
+
providerType: z.string(),
|
|
69
|
+
providerId: z.string().uuid(),
|
|
70
|
+
externalSub: z.string(),
|
|
71
|
+
email: z.string().email().optional(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export default async function federationRoutes(app: FastifyInstance) {
|
|
75
|
+
const sdk = getSdk();
|
|
76
|
+
|
|
77
|
+
app.get("/:provider/start", async (request, reply) => {
|
|
78
|
+
const { provider } = request.params as { provider: string };
|
|
79
|
+
if (!isProvider(provider)) {
|
|
80
|
+
return reply.status(400).send({ error: "Unsupported federation provider" });
|
|
81
|
+
}
|
|
82
|
+
const query = request.query as { client_id?: string; redirect_uri?: string };
|
|
83
|
+
const state = randomState();
|
|
84
|
+
setFederationState(reply, state, query.client_id);
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
const result = await sdk.identity.getFederationAuthorizeUrl(provider, state, callbackRedirectUri(provider));
|
|
88
|
+
if (!result.success) return sendResultError(reply, result);
|
|
89
|
+
return reply.redirect(result.data.url);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
request.log.error({ err }, "Federation start failed");
|
|
92
|
+
clearFederationCookies(reply);
|
|
93
|
+
const { statusCode, body } = buildOAuthErrorResponse(err);
|
|
94
|
+
return reply.status(statusCode).send(body);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
app.get("/:provider/callback", async (request, reply) => {
|
|
99
|
+
const { provider } = request.params as { provider: string };
|
|
100
|
+
if (!isProvider(provider)) {
|
|
101
|
+
return reply.status(400).send({ error: "Unsupported federation provider" });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const { code, state } = request.query as { code?: string; state?: string };
|
|
105
|
+
const cookieState = request.cookies.fed_state;
|
|
106
|
+
const clientId = request.cookies.fed_client_id;
|
|
107
|
+
|
|
108
|
+
if (!code || !state || state !== cookieState) {
|
|
109
|
+
clearFederationCookies(reply);
|
|
110
|
+
return reply.redirect(`${redirectTargetUrl(clientId)}?error=${encodeURIComponent("Invalid federation state")}`);
|
|
111
|
+
}
|
|
112
|
+
clearFederationCookies(reply);
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
const result = await sdk.identity.completeFederationLogin(provider, code, callbackRedirectUri(provider));
|
|
116
|
+
if (!result.success) return sendResultError(reply, result);
|
|
117
|
+
const app = clientId ? await findApplicationByClientId(clientId) : undefined;
|
|
118
|
+
setSessionCookies(reply, result.data.tokens.accessToken, result.data.tokens.refreshToken, app?.clientId);
|
|
119
|
+
return reply.redirect(redirectTargetUrl(clientId, app));
|
|
120
|
+
} catch (err) {
|
|
121
|
+
request.log.error({ err }, "Federation callback failed");
|
|
122
|
+
clearSessionCookies(reply);
|
|
123
|
+
const url = buildOAuthErrorRedirect(err, redirectTargetUrl(clientId), { state });
|
|
124
|
+
return reply.redirect(url);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
app.get("/providers", async () => {
|
|
129
|
+
return {
|
|
130
|
+
providers: listConfiguredProviders(),
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
app.post(
|
|
135
|
+
"/link",
|
|
136
|
+
{ preHandler: [app.authenticate] },
|
|
137
|
+
async (request: FastifyRequest, reply) => {
|
|
138
|
+
const body = LinkIdentitySchema.parse(request.body);
|
|
139
|
+
const result = await sdk.identity.linkUserIdentity(
|
|
140
|
+
request.user!.id,
|
|
141
|
+
body.providerId,
|
|
142
|
+
body.providerType,
|
|
143
|
+
body.externalSub,
|
|
144
|
+
body.email
|
|
145
|
+
);
|
|
146
|
+
if (!result.success) return sendResultError(reply, result);
|
|
147
|
+
await request.audit("federation_identity_linked", { userId: request.user!.id, providerType: body.providerType, providerId: body.providerId });
|
|
148
|
+
return reply.status(201).send({ success: true });
|
|
149
|
+
}
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
app.get("/identities", { preHandler: [app.authenticate] }, async (request: FastifyRequest) => {
|
|
153
|
+
const rows = await app.container.identityRepository.listByUserId(request.user!.id);
|
|
154
|
+
return { identities: rows };
|
|
155
|
+
});
|
|
156
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { FastifyReply } from "fastify";
|
|
2
|
+
|
|
3
|
+
export function sendResultError(reply: FastifyReply, result: { success: false; error: { statusCode?: number; message: string; code: string } }) {
|
|
4
|
+
return reply.status(result.error.statusCode ?? 400).send({ error: result.error.message, code: result.error.code });
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Escape special XML characters to prevent XML injection.
|
|
9
|
+
*/
|
|
10
|
+
export function escapeXml(value: string): string {
|
|
11
|
+
return value
|
|
12
|
+
.replace(/&/g, "&")
|
|
13
|
+
.replace(/</g, "<")
|
|
14
|
+
.replace(/>/g, ">")
|
|
15
|
+
.replace(/"/g, """)
|
|
16
|
+
.replace(/'/g, "'");
|
|
17
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { FastifyInstance, FastifyRequest } from "fastify";
|
|
3
|
+
import { findUserByEmail } from "../services/users.js";
|
|
4
|
+
import {
|
|
5
|
+
generateMagicLink,
|
|
6
|
+
storeMagicLink,
|
|
7
|
+
consumeMagicLink,
|
|
8
|
+
magicLinkExpiresAt,
|
|
9
|
+
} from "../services/magicLinks.js";
|
|
10
|
+
import { createTokenSet } from "../services/tokens.js";
|
|
11
|
+
import { fingerprintFromRequest, recordDevice } from "../services/devices.js";
|
|
12
|
+
import { setSessionCookies } from "../plugins/auth.js";
|
|
13
|
+
import { emailProvider } from "../services/email.js";
|
|
14
|
+
import { toPublicUser } from "../types.js";
|
|
15
|
+
import { config } from "../config.js";
|
|
16
|
+
import { rateLimit } from "../plugins/rateLimit.js";
|
|
17
|
+
|
|
18
|
+
const SendSchema = z.object({
|
|
19
|
+
email: z.string().email(),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
function magicLinkUrl(token: string): string {
|
|
23
|
+
// The link lands on the client app (Keystone admin UI or a client
|
|
24
|
+
// application), which exchanges the token via the verify endpoint.
|
|
25
|
+
const base = process.env.CLIENT_APP_URL || "http://localhost:5173";
|
|
26
|
+
return `${base}/#magic-link=${encodeURIComponent(token)}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function postLoginRedirect(): string {
|
|
30
|
+
return process.env.CLIENT_APP_URL || "http://localhost:5173";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default async function magicLinkRoutes(app: FastifyInstance) {
|
|
34
|
+
app.post("/magic-link/send", {
|
|
35
|
+
preHandler: [
|
|
36
|
+
rateLimit({
|
|
37
|
+
keyPrefix: "magic-link",
|
|
38
|
+
maxAttempts: 3,
|
|
39
|
+
windowSeconds: 900,
|
|
40
|
+
}),
|
|
41
|
+
],
|
|
42
|
+
}, async (request, reply) => {
|
|
43
|
+
const body = SendSchema.parse(request.body);
|
|
44
|
+
const user = await findUserByEmail(body.email);
|
|
45
|
+
if (!user) {
|
|
46
|
+
return reply.status(200).send({ success: true });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const { token, tokenHash } = generateMagicLink();
|
|
50
|
+
await storeMagicLink(user.id, tokenHash, magicLinkExpiresAt());
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
const url = magicLinkUrl(token);
|
|
54
|
+
const ttlMinutes = Math.round(config.MAGIC_LINK_TTL_SECONDS / 60);
|
|
55
|
+
await emailProvider.send({
|
|
56
|
+
to: user.email,
|
|
57
|
+
subject: "Your sign-in link",
|
|
58
|
+
text: `Click the link to sign in:\n\n${url}\n\nThis link expires in ${ttlMinutes} minutes and can only be used once.`,
|
|
59
|
+
html: `<p>Click <a href="${url}">here</a> to sign in.</p><p>This link expires in ${ttlMinutes} minutes and can only be used once.</p>`,
|
|
60
|
+
});
|
|
61
|
+
} catch (err) {
|
|
62
|
+
request.log.error({ err }, "Failed to send magic link email");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
await request.audit("magic_link_sent", { userId: user.id });
|
|
66
|
+
|
|
67
|
+
return { success: true };
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
app.get("/magic-link/verify", async (request: FastifyRequest, reply) => {
|
|
71
|
+
const query = request.query as { token?: string; redirect?: string };
|
|
72
|
+
if (!query.token) {
|
|
73
|
+
return reply.status(400).send({ error: "Missing token" });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const user = await consumeMagicLink(query.token);
|
|
77
|
+
if (!user) {
|
|
78
|
+
return reply.status(400).send({ error: "Invalid or expired token" });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const fingerprint = fingerprintFromRequest(request);
|
|
82
|
+
await recordDevice(user.id, fingerprint, request.ip, request.headers["user-agent"]);
|
|
83
|
+
|
|
84
|
+
const tokens = await createTokenSet(
|
|
85
|
+
user,
|
|
86
|
+
request.ip,
|
|
87
|
+
request.headers["user-agent"],
|
|
88
|
+
{},
|
|
89
|
+
fingerprint
|
|
90
|
+
);
|
|
91
|
+
setSessionCookies(reply, tokens.accessToken, tokens.refreshToken);
|
|
92
|
+
|
|
93
|
+
await request.audit("magic_link_verified", { userId: user.id });
|
|
94
|
+
|
|
95
|
+
// Browser flow: redirect back to the app after setting session cookies.
|
|
96
|
+
const accept = request.headers.accept || "";
|
|
97
|
+
if (accept.includes("text/html")) {
|
|
98
|
+
return reply.redirect(postLoginRedirect());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
user: toPublicUser(user),
|
|
103
|
+
accessToken: tokens.accessToken,
|
|
104
|
+
refreshToken: tokens.refreshToken,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}
|