feat: adds domain and assets

This commit is contained in:
2026-01-28 17:17:13 -05:00
parent accdd9ac07
commit e6b73a330f
16 changed files with 1259 additions and 17 deletions

View File

@@ -0,0 +1,23 @@
namespace api.Features.Domains.Common;
public record DomainResponse(
Guid Id,
Guid WorkspaceId,
string Hostname,
string Status,
string VerificationToken,
string VerificationRecord,
DateTime CreatedAt
);
public record DomainListResponse(
IEnumerable<DomainResponse> Domains
);
public record DomainVerificationResponse(
Guid Id,
string Hostname,
bool IsVerified,
string Status,
string? Message
);

View File

@@ -0,0 +1,105 @@
using System.Security.Claims;
using System.Security.Cryptography;
using api.Data;
using api.Features.Auth.Common;
using api.Features.Domains.Common;
using api.Models;
using FastEndpoints;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
namespace api.Features.Domains.Endpoints;
public class AddDomainRequest
{
public Guid WorkspaceId { get; set; }
public string Hostname { get; set; } = string.Empty;
}
public class AddDomainValidator : Validator<AddDomainRequest>
{
public AddDomainValidator()
{
RuleFor(x => x.Hostname)
.NotEmpty().WithMessage("Hostname is required")
.MaximumLength(255).WithMessage("Hostname must not exceed 255 characters")
.Matches(@"^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$")
.WithMessage("Invalid hostname format");
}
}
public class AddDomainEndpoint(AppDbContext db)
: Endpoint<AddDomainRequest, DomainResponse>
{
public override void Configure()
{
Post("/workspaces/{WorkspaceId}/domains");
}
public override async Task HandleAsync(AddDomainRequest req, CancellationToken ct)
{
var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
// Verify workspace ownership
var workspaceExists = await db.Workspaces
.AnyAsync(w => w.Id == req.WorkspaceId && w.OwnerUserId == userId, ct);
if (!workspaceExists)
{
await HttpContext.Response.SendAsync(new MessageResponse("Workspace not found"), 404, cancellation: ct);
return;
}
// Normalize hostname (lowercase, no trailing dots)
var hostname = req.Hostname.ToLowerInvariant().TrimEnd('.');
// Check if domain already exists globally
var domainExists = await db.Domains
.AnyAsync(d => d.Hostname == hostname, ct);
if (domainExists)
{
await HttpContext.Response.SendAsync(new MessageResponse("Domain is already registered"), 409, cancellation: ct);
return;
}
// Generate verification token
var verificationToken = GenerateVerificationToken();
var domain = new Domain
{
Id = Guid.NewGuid(),
WorkspaceId = req.WorkspaceId,
Hostname = hostname,
Status = DomainStatus.Pending,
VerificationToken = verificationToken,
CreatedAt = DateTime.UtcNow
};
db.Domains.Add(domain);
await db.SaveChangesAsync(ct);
var response = new DomainResponse(
domain.Id,
domain.WorkspaceId,
domain.Hostname,
domain.Status.ToString(),
domain.VerificationToken,
GetVerificationRecord(domain.VerificationToken),
domain.CreatedAt
);
await HttpContext.Response.SendAsync(response, 201, cancellation: ct);
}
private static string GenerateVerificationToken()
{
var bytes = RandomNumberGenerator.GetBytes(16);
return $"trakqr-verify-{Convert.ToHexString(bytes).ToLowerInvariant()}";
}
private static string GetVerificationRecord(string token)
{
return $"TXT _trakqr-verification {token}";
}
}

View File

@@ -0,0 +1,55 @@
using System.Security.Claims;
using api.Data;
using api.Features.Auth.Common;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace api.Features.Domains.Endpoints;
public class DeleteDomainRequest
{
public Guid WorkspaceId { get; set; }
public Guid Id { get; set; }
}
public class DeleteDomainEndpoint(AppDbContext db)
: Endpoint<DeleteDomainRequest>
{
public override void Configure()
{
Delete("/workspaces/{WorkspaceId}/domains/{Id}");
}
public override async Task HandleAsync(DeleteDomainRequest req, CancellationToken ct)
{
var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
var domain = await db.Domains
.Include(d => d.Workspace)
.FirstOrDefaultAsync(d => d.Id == req.Id && d.WorkspaceId == req.WorkspaceId && d.Workspace.OwnerUserId == userId, ct);
if (domain is null)
{
await HttpContext.Response.SendAsync(new MessageResponse("Domain not found"), 404, cancellation: ct);
return;
}
// Check if any links are using this domain
var linksUsingDomain = await db.ShortLinks
.AnyAsync(l => l.DomainId == domain.Id, ct);
if (linksUsingDomain)
{
await HttpContext.Response.SendAsync(
new MessageResponse("Cannot delete domain: it has associated short links"),
400,
cancellation: ct);
return;
}
db.Domains.Remove(domain);
await db.SaveChangesAsync(ct);
await HttpContext.Response.SendAsync(new MessageResponse("Domain deleted"), 200, cancellation: ct);
}
}

View File

@@ -0,0 +1,50 @@
using System.Security.Claims;
using api.Data;
using api.Features.Auth.Common;
using api.Features.Domains.Common;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace api.Features.Domains.Endpoints;
public class GetDomainRequest
{
public Guid WorkspaceId { get; set; }
public Guid Id { get; set; }
}
public class GetDomainEndpoint(AppDbContext db)
: Endpoint<GetDomainRequest, DomainResponse>
{
public override void Configure()
{
Get("/workspaces/{WorkspaceId}/domains/{Id}");
}
public override async Task HandleAsync(GetDomainRequest req, CancellationToken ct)
{
var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
var domain = await db.Domains
.Where(d => d.Id == req.Id && d.WorkspaceId == req.WorkspaceId && d.Workspace.OwnerUserId == userId)
.FirstOrDefaultAsync(ct);
if (domain is null)
{
await HttpContext.Response.SendAsync(new MessageResponse("Domain not found"), 404, cancellation: ct);
return;
}
var response = new DomainResponse(
domain.Id,
domain.WorkspaceId,
domain.Hostname,
domain.Status.ToString(),
domain.VerificationToken,
$"TXT _trakqr-verification {domain.VerificationToken}",
domain.CreatedAt
);
await HttpContext.Response.SendAsync(response, 200, cancellation: ct);
}
}

View File

@@ -0,0 +1,53 @@
using System.Security.Claims;
using api.Data;
using api.Features.Auth.Common;
using api.Features.Domains.Common;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace api.Features.Domains.Endpoints;
public class ListDomainsRequest
{
public Guid WorkspaceId { get; set; }
}
public class ListDomainsEndpoint(AppDbContext db)
: Endpoint<ListDomainsRequest, DomainListResponse>
{
public override void Configure()
{
Get("/workspaces/{WorkspaceId}/domains");
}
public override async Task HandleAsync(ListDomainsRequest req, CancellationToken ct)
{
var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
// Verify workspace ownership
var workspaceExists = await db.Workspaces
.AnyAsync(w => w.Id == req.WorkspaceId && w.OwnerUserId == userId, ct);
if (!workspaceExists)
{
await HttpContext.Response.SendAsync(new MessageResponse("Workspace not found"), 404, cancellation: ct);
return;
}
var domains = await db.Domains
.Where(d => d.WorkspaceId == req.WorkspaceId)
.OrderByDescending(d => d.CreatedAt)
.Select(d => new DomainResponse(
d.Id,
d.WorkspaceId,
d.Hostname,
d.Status.ToString(),
d.VerificationToken,
$"TXT _trakqr-verification {d.VerificationToken}",
d.CreatedAt
))
.ToListAsync(ct);
await HttpContext.Response.SendAsync(new DomainListResponse(domains), 200, cancellation: ct);
}
}

View File

@@ -0,0 +1,91 @@
using System.Security.Claims;
using api.Data;
using api.Features.Auth.Common;
using api.Features.Domains.Common;
using api.Models;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace api.Features.Domains.Endpoints;
public class VerifyDomainRequest
{
public Guid WorkspaceId { get; set; }
public Guid Id { get; set; }
}
public class VerifyDomainEndpoint(AppDbContext db)
: Endpoint<VerifyDomainRequest, DomainVerificationResponse>
{
public override void Configure()
{
Post("/workspaces/{WorkspaceId}/domains/{Id}/verify");
}
public override async Task HandleAsync(VerifyDomainRequest req, CancellationToken ct)
{
var userId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
var domain = await db.Domains
.Include(d => d.Workspace)
.FirstOrDefaultAsync(d => d.Id == req.Id && d.WorkspaceId == req.WorkspaceId && d.Workspace.OwnerUserId == userId, ct);
if (domain is null)
{
await HttpContext.Response.SendAsync(new MessageResponse("Domain not found"), 404, cancellation: ct);
return;
}
// Already verified or active
if (domain.Status != DomainStatus.Pending)
{
var alreadyResponse = new DomainVerificationResponse(
domain.Id,
domain.Hostname,
true,
domain.Status.ToString(),
"Domain is already verified"
);
await HttpContext.Response.SendAsync(alreadyResponse, 200, cancellation: ct);
return;
}
// Check DNS TXT record
var isVerified = await CheckDnsVerificationAsync(domain.Hostname, domain.VerificationToken);
if (isVerified)
{
domain.Status = DomainStatus.Verified;
await db.SaveChangesAsync(ct);
var successResponse = new DomainVerificationResponse(
domain.Id,
domain.Hostname,
true,
domain.Status.ToString(),
"Domain verified successfully"
);
await HttpContext.Response.SendAsync(successResponse, 200, cancellation: ct);
}
else
{
var failedResponse = new DomainVerificationResponse(
domain.Id,
domain.Hostname,
false,
domain.Status.ToString(),
$"Verification failed. Please add a TXT record for _trakqr-verification.{domain.Hostname} with value: {domain.VerificationToken}"
);
await HttpContext.Response.SendAsync(failedResponse, 200, cancellation: ct);
}
}
private static Task<bool> CheckDnsVerificationAsync(string hostname, string expectedToken)
{
// For testing purposes, we'll check if the domain starts with "verified-"
// In production, this would be replaced with actual DNS TXT record lookup
// using a library like DnsClient
var isVerified = hostname.StartsWith("verified-");
return Task.FromResult(isVerified);
}
}