-
Notifications
You must be signed in to change notification settings - Fork 514
Managed email provider #1222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Managed email provider #1222
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
808a9ee
managed email provider
BilalG1 afccb8d
Merge branch 'dev' into managed-emails-provider
BilalG1 2f66239
small fixes
BilalG1 6d4e37c
Merge branch 'managed-emails-provider' of https://github.com/stack-au…
BilalG1 f95f93d
fix migration and test
BilalG1 b1a19a3
fix test
BilalG1 78d8e5d
fix ui
BilalG1 a7480c6
Merge branch 'dev' into managed-emails-provider
BilalG1 0d2efdb
ui fixes
BilalG1 5f39082
Merge branch 'dev' into managed-emails-provider
BilalG1 274c396
Merge branch 'dev' into managed-emails-provider
BilalG1 b51559f
fix naming
BilalG1 c93a214
empty
BilalG1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
apps/backend/prisma/migrations/20260224000000_managed_email_domains/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| CREATE TYPE "ManagedEmailDomainStatus" AS ENUM ('PENDING_DNS', 'PENDING_VERIFICATION', 'VERIFIED', 'APPLIED', 'FAILED'); | ||
|
|
||
| CREATE TABLE "ManagedEmailDomain" ( | ||
| "id" UUID NOT NULL, | ||
| "tenancyId" UUID NOT NULL, | ||
| "projectId" TEXT NOT NULL, | ||
| "branchId" TEXT NOT NULL, | ||
| "subdomain" TEXT NOT NULL, | ||
| "senderLocalPart" TEXT NOT NULL, | ||
|
BilalG1 marked this conversation as resolved.
|
||
| "resendDomainId" TEXT NOT NULL, | ||
| "nameServerRecords" JSONB NOT NULL, | ||
| "status" "ManagedEmailDomainStatus" NOT NULL DEFAULT 'PENDING_VERIFICATION', | ||
| "providerStatusRaw" TEXT, | ||
| "isActive" BOOLEAN NOT NULL DEFAULT true, | ||
| "lastError" TEXT, | ||
| "verifiedAt" TIMESTAMP(3), | ||
| "appliedAt" TIMESTAMP(3), | ||
| "lastWebhookAt" TIMESTAMP(3), | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "ManagedEmailDomain_pkey" PRIMARY KEY ("id"), | ||
| CONSTRAINT "ManagedEmailDomain_tenancyId_fkey" FOREIGN KEY ("tenancyId") REFERENCES "Tenancy"("id") ON DELETE CASCADE ON UPDATE CASCADE | ||
| ); | ||
|
|
||
| CREATE UNIQUE INDEX "ManagedEmailDomain_resendDomainId_key" ON "ManagedEmailDomain"("resendDomainId"); | ||
| CREATE UNIQUE INDEX "ManagedEmailDomain_tenancyId_subdomain_key" ON "ManagedEmailDomain"("tenancyId", "subdomain"); | ||
| CREATE INDEX "ManagedEmailDomain_tenancy_status_active_idx" ON "ManagedEmailDomain"("tenancyId", "status", "isActive"); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
apps/backend/src/app/api/latest/integrations/resend/webhooks/route.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { processResendDomainWebhookEvent } from "@/lib/managed-email-onboarding"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { yupBoolean, yupMixed, yupNumber, yupObject, yupString, yupTuple } from "@stackframe/stack-shared/dist/schema-fields"; | ||
| import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env"; | ||
| import { StackAssertionError, StatusError } from "@stackframe/stack-shared/dist/utils/errors"; | ||
| import { Result } from "@stackframe/stack-shared/dist/utils/results"; | ||
| import { Webhook } from "svix"; | ||
|
|
||
| function decodeBody(bodyBuffer: ArrayBuffer) { | ||
| return new TextDecoder().decode(bodyBuffer); | ||
| } | ||
|
|
||
| function ensureResendWebhookSignature(headers: Record<string, string[] | undefined>, bodyBuffer: ArrayBuffer) { | ||
| const webhookSecret = getEnvVariable("STACK_RESEND_WEBHOOK_SECRET"); | ||
| const svixId = headers["svix-id"]?.[0] ?? null; | ||
| const svixTimestamp = headers["svix-timestamp"]?.[0] ?? null; | ||
| const svixSignature = headers["svix-signature"]?.[0] ?? null; | ||
| if (svixId == null || svixTimestamp == null || svixSignature == null) { | ||
| throw new StatusError(400, "Missing Svix signature headers for Resend webhook"); | ||
| } | ||
|
|
||
| const verifier = new Webhook(webhookSecret); | ||
| const result = Result.fromThrowing(() => verifier.verify(decodeBody(bodyBuffer), { | ||
| "svix-id": svixId, | ||
| "svix-timestamp": svixTimestamp, | ||
| "svix-signature": svixSignature, | ||
| })); | ||
| if (result.status === "error") { | ||
| throw new StatusError(400, "Invalid Resend webhook signature"); | ||
| } | ||
| } | ||
|
|
||
| type ResendDomainWebhookPayload = { | ||
| type?: string, | ||
| data?: { | ||
| id?: string, | ||
| status?: string, | ||
| error?: string, | ||
| }, | ||
| }; | ||
|
|
||
| export const POST = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| headers: yupObject({ | ||
| "svix-id": yupTuple([yupString().defined()]).defined(), | ||
| "svix-timestamp": yupTuple([yupString().defined()]).defined(), | ||
| "svix-signature": yupTuple([yupString().defined()]).defined(), | ||
| }).defined(), | ||
| body: yupMixed().optional(), | ||
| method: yupString().oneOf(["POST"]).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| received: yupBoolean().defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async (req, fullReq) => { | ||
| ensureResendWebhookSignature(req.headers, fullReq.bodyBuffer); | ||
|
|
||
| const payloadResult = Result.fromThrowing(() => JSON.parse(decodeBody(fullReq.bodyBuffer)) as ResendDomainWebhookPayload); | ||
| if (payloadResult.status === "error") { | ||
| throw new StatusError(400, "Invalid JSON payload in Resend webhook"); | ||
| } | ||
| if (payloadResult.data.type !== "domain.updated") { | ||
| return { | ||
| statusCode: 200, | ||
| bodyType: "json", | ||
| body: { received: true }, | ||
| }; | ||
| } | ||
| const payload = payloadResult.data; | ||
|
|
||
| const domainId = payload.data?.id; | ||
| const providerStatusRaw = payload.data?.status; | ||
| if (domainId == null || providerStatusRaw == null) { | ||
| throw new StackAssertionError("Resend webhook payload missing required domain fields", { | ||
| payload, | ||
| }); | ||
| } | ||
|
|
||
| await processResendDomainWebhookEvent({ | ||
| domainId, | ||
| providerStatusRaw, | ||
| errorMessage: payload.data?.error, | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| bodyType: "json", | ||
| body: { | ||
| received: true, | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
40 changes: 40 additions & 0 deletions
40
apps/backend/src/app/api/latest/internal/emails/managed-onboarding/apply/route.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { applyManagedEmailProvider } from "@/lib/managed-email-onboarding"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { adaptSchema, adminAuthTypeSchema, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
|
|
||
| export const POST = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adminAuthTypeSchema.defined(), | ||
| tenancy: adaptSchema.defined(), | ||
| }).defined(), | ||
| body: yupObject({ | ||
| domain_id: yupString().defined(), | ||
| }).defined(), | ||
| method: yupString().oneOf(["POST"]).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| status: yupString().oneOf(["applied"]).defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async ({ auth, body }) => { | ||
| const result = await applyManagedEmailProvider({ | ||
| tenancy: auth.tenancy, | ||
| domainId: body.domain_id, | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| bodyType: "json", | ||
| body: { | ||
| status: result.status, | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
44 changes: 44 additions & 0 deletions
44
apps/backend/src/app/api/latest/internal/emails/managed-onboarding/check/route.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { checkManagedEmailProviderStatus } from "@/lib/managed-email-onboarding"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { adaptSchema, adminAuthTypeSchema, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
|
|
||
| export const POST = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adminAuthTypeSchema.defined(), | ||
| tenancy: adaptSchema.defined(), | ||
| }).defined(), | ||
| body: yupObject({ | ||
| domain_id: yupString().defined(), | ||
| subdomain: yupString().defined(), | ||
| sender_local_part: yupString().defined(), | ||
| }).defined(), | ||
| method: yupString().oneOf(["POST"]).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| status: yupString().oneOf(["pending_dns", "pending_verification", "verified", "applied", "failed"]).defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async ({ auth, body }) => { | ||
| const checkResult = await checkManagedEmailProviderStatus({ | ||
| tenancy: auth.tenancy, | ||
| domainId: body.domain_id, | ||
| subdomain: body.subdomain, | ||
| senderLocalPart: body.sender_local_part, | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| bodyType: "json", | ||
| body: { | ||
| status: checkResult.status, | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
48 changes: 48 additions & 0 deletions
48
apps/backend/src/app/api/latest/internal/emails/managed-onboarding/list/route.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { listManagedEmailProviderDomains } from "@/lib/managed-email-onboarding"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { adaptSchema, adminAuthTypeSchema, yupArray, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
|
|
||
| export const GET = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adminAuthTypeSchema.defined(), | ||
| tenancy: adaptSchema.defined(), | ||
| }).defined(), | ||
| method: yupString().oneOf(["GET"]).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| items: yupArray(yupObject({ | ||
| domain_id: yupString().defined(), | ||
| subdomain: yupString().defined(), | ||
| sender_local_part: yupString().defined(), | ||
| status: yupString().oneOf(["pending_dns", "pending_verification", "verified", "applied", "failed"]).defined(), | ||
| name_server_records: yupArray(yupString().defined()).defined(), | ||
| }).defined()).defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async ({ auth }) => { | ||
| const items = await listManagedEmailProviderDomains({ | ||
| tenancy: auth.tenancy, | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| bodyType: "json", | ||
| body: { | ||
| items: items.map((item) => ({ | ||
| domain_id: item.domainId, | ||
| subdomain: item.subdomain, | ||
| sender_local_part: item.senderLocalPart, | ||
| status: item.status, | ||
| name_server_records: item.nameServerRecords, | ||
| })), | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
50 changes: 50 additions & 0 deletions
50
apps/backend/src/app/api/latest/internal/emails/managed-onboarding/setup/route.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { setupManagedEmailProvider } from "@/lib/managed-email-onboarding"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { adaptSchema, adminAuthTypeSchema, yupArray, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
|
|
||
| export const POST = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adminAuthTypeSchema.defined(), | ||
| tenancy: adaptSchema.defined(), | ||
| }).defined(), | ||
| body: yupObject({ | ||
| subdomain: yupString().defined(), | ||
| sender_local_part: yupString().defined(), | ||
| }).defined(), | ||
| method: yupString().oneOf(["POST"]).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| domain_id: yupString().defined(), | ||
| subdomain: yupString().defined(), | ||
| sender_local_part: yupString().defined(), | ||
| name_server_records: yupArray(yupString().defined()).defined(), | ||
| status: yupString().oneOf(["pending_dns", "pending_verification", "verified", "applied", "failed"]).defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async ({ auth, body }) => { | ||
| const setupResult = await setupManagedEmailProvider({ | ||
| subdomain: body.subdomain, | ||
| senderLocalPart: body.sender_local_part, | ||
| tenancy: auth.tenancy, | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| bodyType: "json", | ||
| body: { | ||
| domain_id: setupResult.domainId, | ||
| subdomain: setupResult.subdomain, | ||
| sender_local_part: setupResult.senderLocalPart, | ||
| name_server_records: setupResult.nameServerRecords, | ||
| status: setupResult.status, | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.