|
| 1 | +import dns from 'node:dns/promises'; |
1 | 2 | import { OpenAPIHono } from '@hono/zod-openapi'; |
2 | 3 | import { and, asc, eq } from 'drizzle-orm'; |
3 | 4 | import { domainsTable } from '#/db/schema/domains'; |
@@ -79,6 +80,89 @@ const domainHandlers = app |
79 | 80 | logEvent('info', 'Domain removed', { tenantId, domain: deleted.domain, removedBy: user.id }); |
80 | 81 |
|
81 | 82 | return ctx.json(deleted); |
| 83 | + }) |
| 84 | + |
| 85 | + /** |
| 86 | + * Get a single domain with its verification token. |
| 87 | + */ |
| 88 | + .openapi(domainRoutes.getDomain, async (ctx) => { |
| 89 | + const db = ctx.var.db; |
| 90 | + const { tenantId, id } = ctx.req.valid('param'); |
| 91 | + |
| 92 | + const [domain] = await db |
| 93 | + .select() |
| 94 | + .from(domainsTable) |
| 95 | + .where(and(eq(domainsTable.id, id), eq(domainsTable.tenantId, tenantId))) |
| 96 | + .limit(1); |
| 97 | + |
| 98 | + if (!domain) { |
| 99 | + throw new AppError(404, 'not_found', 'warn', { meta: { resource: 'domain' } }); |
| 100 | + } |
| 101 | + |
| 102 | + return ctx.json(domain); |
| 103 | + }) |
| 104 | + |
| 105 | + /** |
| 106 | + * Verify a domain via DNS TXT record lookup. |
| 107 | + */ |
| 108 | + .openapi(domainRoutes.verifyDomain, async (ctx) => { |
| 109 | + const db = ctx.var.db; |
| 110 | + const { tenantId, id } = ctx.req.valid('param'); |
| 111 | + const user = ctx.var.user; |
| 112 | + |
| 113 | + const [domain] = await db |
| 114 | + .select() |
| 115 | + .from(domainsTable) |
| 116 | + .where(and(eq(domainsTable.id, id), eq(domainsTable.tenantId, tenantId))) |
| 117 | + .limit(1); |
| 118 | + |
| 119 | + if (!domain) { |
| 120 | + throw new AppError(404, 'not_found', 'warn', { meta: { resource: 'domain' } }); |
| 121 | + } |
| 122 | + |
| 123 | + if (!domain.verificationToken) { |
| 124 | + throw new AppError(422, 'invalid_request', 'warn', { meta: { reason: 'Domain has no verification token' } }); |
| 125 | + } |
| 126 | + |
| 127 | + const hostname = `_cella-verification.${domain.domain}`; |
| 128 | + let recordsFound: string[] = []; |
| 129 | + |
| 130 | + try { |
| 131 | + const txtRecords = await dns.resolveTxt(hostname); |
| 132 | + // dns.resolveTxt returns string[][] — each record is an array of chunks, join them |
| 133 | + recordsFound = txtRecords.map((chunks) => chunks.join('')); |
| 134 | + } catch (err: unknown) { |
| 135 | + // ENOTFOUND / ENODATA means no TXT records exist — not an error |
| 136 | + const code = err instanceof Error && 'code' in err ? (err as NodeJS.ErrnoException).code : undefined; |
| 137 | + if (code !== 'ENOTFOUND' && code !== 'ENODATA') { |
| 138 | + logEvent('warn', 'DNS lookup failed', { domain: domain.domain, error: String(err) }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + const now = new Date().toISOString(); |
| 143 | + const verified = recordsFound.includes(domain.verificationToken); |
| 144 | + |
| 145 | + const [updated] = await db |
| 146 | + .update(domainsTable) |
| 147 | + .set({ |
| 148 | + lastCheckedAt: now, |
| 149 | + ...(verified ? { verified: true, verifiedAt: now } : {}), |
| 150 | + }) |
| 151 | + .where(eq(domainsTable.id, id)) |
| 152 | + .returning(); |
| 153 | + |
| 154 | + logEvent('info', `Domain verification ${verified ? 'succeeded' : 'failed'}`, { |
| 155 | + tenantId, |
| 156 | + domain: domain.domain, |
| 157 | + verified, |
| 158 | + verifiedBy: user.id, |
| 159 | + }); |
| 160 | + |
| 161 | + return ctx.json({ |
| 162 | + success: verified, |
| 163 | + domain: updated, |
| 164 | + ...(!verified ? { diagnostics: { recordsFound, expectedToken: domain.verificationToken } } : {}), |
| 165 | + }); |
82 | 166 | }); |
83 | 167 |
|
84 | 168 | export default domainHandlers; |
0 commit comments