feat(capability): BYO SMTP — per-tenant email server via the credential registry
SmtpProvider is now scope-aware: it resolves the tenant's own SMTP identity from the IiosProviderCredential store (providerType SMTP) and sends from it, falling back to the platform env identity when unset (env fallback applies only to the platform identity, never a tenant's server). Registered whenever env SMTP OR the credential store is present; fails closed as NOT_CONFIGURED when neither exists. Credential endpoints accept SMTP with per-type validation. 18 SMTP tests green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -2,9 +2,34 @@ import { BadRequestException, Body, Controller, Get, Headers, Param, Put } from
|
||||
import { SessionVerifier } from '../platform/session.verifier';
|
||||
import { ActorResolver, type MessagePrincipal } from '../identity/actor.resolver';
|
||||
import { ProviderCredentialService } from './provider-credential.service';
|
||||
import { TwilioCredentialsDto } from './provider-credential.dto';
|
||||
|
||||
const SUPPORTED = new Set(['TWILIO_SMS']);
|
||||
const SUPPORTED = new Set(['TWILIO_SMS', 'SMTP']);
|
||||
|
||||
const str = (v: unknown): string => (typeof v === 'string' ? v.trim() : '');
|
||||
const isEmail = (v: string): boolean => /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v);
|
||||
|
||||
/** Validate + shape the credential config for a provider type. Throws 400 on bad input.
|
||||
* be-crm does the strict client-facing validation; this is IIOS's own guard. */
|
||||
function buildConfig(providerType: string, body: Record<string, unknown>): Record<string, unknown> {
|
||||
if (providerType === 'TWILIO_SMS') {
|
||||
const accountSid = str(body.accountSid);
|
||||
const authToken = str(body.authToken);
|
||||
const fromNumber = str(body.fromNumber);
|
||||
if (!accountSid || !authToken || !fromNumber) throw new BadRequestException('accountSid, authToken and fromNumber are required');
|
||||
return { accountSid, authToken, fromNumber };
|
||||
}
|
||||
if (providerType === 'SMTP') {
|
||||
const host = str(body.host);
|
||||
const user = str(body.user);
|
||||
const pass = str(body.pass);
|
||||
const fromEmail = str(body.fromEmail);
|
||||
const fromName = str(body.fromName);
|
||||
if (!host || !user || !pass || !fromEmail) throw new BadRequestException('host, user, pass and fromEmail are required');
|
||||
if (!isEmail(fromEmail)) throw new BadRequestException('fromEmail must be a valid email');
|
||||
return { host, port: Number(body.port) || 587, secure: body.secure === true || body.secure === 'true', user, pass, fromEmail, ...(fromName ? { fromName } : {}) };
|
||||
}
|
||||
throw new BadRequestException(`unsupported providerType: ${providerType}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-scope BYO integration credentials. The caller's attested session decides the scope, so a
|
||||
@@ -22,16 +47,13 @@ export class ProviderCredentialController {
|
||||
@Put(':providerType/credentials')
|
||||
async put(
|
||||
@Param('providerType') providerType: string,
|
||||
@Body() body: TwilioCredentialsDto,
|
||||
@Body() body: Record<string, unknown>,
|
||||
@Headers('authorization') authorization?: string,
|
||||
) {
|
||||
this.assertSupported(providerType);
|
||||
const config = buildConfig(providerType, body ?? {});
|
||||
const scope = await this.actors.resolveScope(this.principal(authorization));
|
||||
await this.credentials.upsert(scope.id, providerType, {
|
||||
accountSid: body.accountSid,
|
||||
authToken: body.authToken,
|
||||
fromNumber: body.fromNumber,
|
||||
});
|
||||
await this.credentials.upsert(scope.id, providerType, config);
|
||||
return this.credentials.status(scope.id, providerType);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user