Move public crossings onto typed Bifrost transport
This commit is contained in:
parent
bd3a5a974b
commit
cde3a78874
18 changed files with 1686 additions and 2073 deletions
|
|
@ -470,282 +470,6 @@ app.MapPost("/patronage/stripe/webhook", async (
|
|||
};
|
||||
}).AllowAnonymous();
|
||||
|
||||
var bridgeActions = app.MapGroup("/bridge/actions");
|
||||
|
||||
bridgeActions.MapPost("/request", async (
|
||||
HttpRequest request,
|
||||
BridgeActionRequest command,
|
||||
ICurrentBifrostActorAccessor actorAccessor,
|
||||
BridgeActionService bridgeActionService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
var actor = await actorAccessor.GetAsync(cancellationToken);
|
||||
var caller = BuildBridgeCaller(actor, request, bridgeOptions.Value);
|
||||
if (!caller.IsAllowedTransport)
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
var result = await bridgeActionService.RequestAsync(command, caller, cancellationToken);
|
||||
return result.Status == BridgeActionStatus.Denied
|
||||
? JsonResult(result, appJsonSerializerOptions, StatusCodes.Status403Forbidden)
|
||||
: JsonResult(result, appJsonSerializerOptions, StatusCodes.Status202Accepted);
|
||||
});
|
||||
|
||||
bridgeActions.MapGet("/{id:guid}", async (
|
||||
Guid id,
|
||||
HttpRequest request,
|
||||
ICurrentBifrostActorAccessor actorAccessor,
|
||||
BifrostDbContext dbContext,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
var actor = await actorAccessor.GetAsync(cancellationToken);
|
||||
var caller = BuildBridgeCaller(actor, request, bridgeOptions.Value);
|
||||
if (!caller.IsAllowedTransport)
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
var action = await dbContext.BridgeActions
|
||||
.AsNoTracking()
|
||||
.SingleOrDefaultAsync(x => x.Id == id, cancellationToken);
|
||||
|
||||
if (action is null)
|
||||
{
|
||||
return Results.NotFound();
|
||||
}
|
||||
|
||||
if (!caller.CanOperate(action))
|
||||
{
|
||||
return Results.Forbid();
|
||||
}
|
||||
|
||||
return JsonResult(BridgeActionResult.From(action), appJsonSerializerOptions);
|
||||
});
|
||||
|
||||
bridgeActions.MapPost("/{id:guid}/start", async (
|
||||
Guid id,
|
||||
HttpRequest request,
|
||||
ICurrentBifrostActorAccessor actorAccessor,
|
||||
BridgeActionService bridgeActionService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
var actor = await actorAccessor.GetAsync(cancellationToken);
|
||||
var caller = BuildBridgeCaller(actor, request, bridgeOptions.Value);
|
||||
if (!caller.IsAllowedTransport)
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await bridgeActionService.StartAsync(id, caller, cancellationToken);
|
||||
return result is null
|
||||
? Results.NotFound()
|
||||
: JsonResult(result, appJsonSerializerOptions, StatusCodes.Status202Accepted);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Results.Forbid();
|
||||
}
|
||||
catch (BridgeActionException error)
|
||||
{
|
||||
return Results.BadRequest(error.Message);
|
||||
}
|
||||
});
|
||||
|
||||
bridgeActions.MapPost("/{id:guid}/complete", async (
|
||||
Guid id,
|
||||
HttpRequest request,
|
||||
BridgeActionReceiptRequest command,
|
||||
ICurrentBifrostActorAccessor actorAccessor,
|
||||
BridgeActionService bridgeActionService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
var actor = await actorAccessor.GetAsync(cancellationToken);
|
||||
var caller = BuildBridgeCaller(actor, request, bridgeOptions.Value);
|
||||
if (!caller.IsAllowedTransport)
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await bridgeActionService.CompleteAsync(id, command, caller, cancellationToken);
|
||||
return result is null
|
||||
? Results.NotFound()
|
||||
: JsonResult(result, appJsonSerializerOptions);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Results.Forbid();
|
||||
}
|
||||
catch (BridgeActionException error)
|
||||
{
|
||||
return Results.BadRequest(error.Message);
|
||||
}
|
||||
});
|
||||
|
||||
bridgeActions.MapPost("/{id:guid}/fail", async (
|
||||
Guid id,
|
||||
HttpRequest request,
|
||||
BridgeActionFailureRequest command,
|
||||
ICurrentBifrostActorAccessor actorAccessor,
|
||||
BridgeActionService bridgeActionService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
var actor = await actorAccessor.GetAsync(cancellationToken);
|
||||
var caller = BuildBridgeCaller(actor, request, bridgeOptions.Value);
|
||||
if (!caller.IsAllowedTransport)
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await bridgeActionService.FailAsync(id, command, caller, cancellationToken);
|
||||
return result is null
|
||||
? Results.NotFound()
|
||||
: JsonResult(result, appJsonSerializerOptions);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Results.Forbid();
|
||||
}
|
||||
});
|
||||
|
||||
var dispatchRuns = app.MapGroup("/dispatch/runs");
|
||||
|
||||
dispatchRuns.MapPost("/start", async (
|
||||
HttpRequest request,
|
||||
AgentDispatchRunStartRequest command,
|
||||
AgentDispatchRunService agentDispatchRunService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
if (!HasValidLocalBridgeToken(request, bridgeOptions.Value))
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await agentDispatchRunService.StartAsync(command, null, cancellationToken);
|
||||
return JsonResult(result, appJsonSerializerOptions, StatusCodes.Status202Accepted);
|
||||
}
|
||||
catch (AgentDispatchRunException error)
|
||||
{
|
||||
return Results.Text(error.Message, statusCode: StatusCodes.Status400BadRequest);
|
||||
}
|
||||
});
|
||||
|
||||
dispatchRuns.MapPost("/{id:guid}/complete", async (
|
||||
Guid id,
|
||||
HttpRequest request,
|
||||
AgentDispatchRunCompletionRequest command,
|
||||
AgentDispatchRunService agentDispatchRunService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
if (!HasValidLocalBridgeToken(request, bridgeOptions.Value))
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await agentDispatchRunService.CompleteAsync(id, command, null, cancellationToken);
|
||||
return result is null
|
||||
? Results.NotFound()
|
||||
: JsonResult(result, appJsonSerializerOptions);
|
||||
}
|
||||
catch (AgentDispatchRunException error)
|
||||
{
|
||||
return Results.Text(error.Message, statusCode: StatusCodes.Status400BadRequest);
|
||||
}
|
||||
});
|
||||
|
||||
dispatchRuns.MapPost("/{id:guid}/fail", async (
|
||||
Guid id,
|
||||
HttpRequest request,
|
||||
AgentDispatchRunFailureRequest command,
|
||||
AgentDispatchRunService agentDispatchRunService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
if (!HasValidLocalBridgeToken(request, bridgeOptions.Value))
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await agentDispatchRunService.FailAsync(id, command, null, cancellationToken);
|
||||
return result is null
|
||||
? Results.NotFound()
|
||||
: JsonResult(result, appJsonSerializerOptions);
|
||||
}
|
||||
catch (AgentDispatchRunException error)
|
||||
{
|
||||
return Results.Text(error.Message, statusCode: StatusCodes.Status400BadRequest);
|
||||
}
|
||||
});
|
||||
|
||||
var transportReceipts = app.MapGroup("/transport/receipts");
|
||||
|
||||
transportReceipts.MapPost("", async (
|
||||
HttpRequest request,
|
||||
AgentTransportReceiptRequest command,
|
||||
AgentTransportReceiptService agentTransportReceiptService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
if (!HasValidLocalBridgeToken(request, bridgeOptions.Value))
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await agentTransportReceiptService.RecordAsync(command, null, cancellationToken);
|
||||
return JsonResult(result, appJsonSerializerOptions, StatusCodes.Status202Accepted);
|
||||
}
|
||||
catch (AgentTransportReceiptException error)
|
||||
{
|
||||
return Results.Text(error.Message, statusCode: StatusCodes.Status400BadRequest);
|
||||
}
|
||||
});
|
||||
|
||||
var governanceReceipts = app.MapGroup("/governance/receipts");
|
||||
|
||||
governanceReceipts.MapPost("", async (
|
||||
HttpRequest request,
|
||||
GovernanceActivityReceiptRequest command,
|
||||
GovernanceActivityReceiptService governanceActivityReceiptService,
|
||||
IOptions<BridgeOptions> bridgeOptions,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
if (!HasValidLocalBridgeToken(request, bridgeOptions.Value))
|
||||
{
|
||||
return Results.Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await governanceActivityReceiptService.RecordAsync(command, null, cancellationToken);
|
||||
return JsonResult(result, appJsonSerializerOptions, StatusCodes.Status202Accepted);
|
||||
}
|
||||
catch (GovernanceActivityReceiptException error)
|
||||
{
|
||||
return Results.Text(error.Message, statusCode: StatusCodes.Status400BadRequest);
|
||||
}
|
||||
});
|
||||
|
||||
var eveGovernance = app.MapGroup("/eve/governance")
|
||||
.RequireAuthorization(ActiveMemberRequirement.PolicyName);
|
||||
|
||||
|
|
@ -763,77 +487,6 @@ eveGovernance.MapGet("/surface", async (
|
|||
"application/json");
|
||||
});
|
||||
|
||||
eveGovernance.MapPost("/commands", async (
|
||||
MotionEveCommandRequest command,
|
||||
ICurrentBifrostActorAccessor actorAccessor,
|
||||
MotionGovernanceService motionGovernanceService,
|
||||
MotionEveSurfaceService motionEveSurfaceService,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
var actor = await actorAccessor.GetAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
switch (command.Command)
|
||||
{
|
||||
case "motion.create":
|
||||
if (command.Scope is null ||
|
||||
command.Category is null ||
|
||||
command.ClosesAtUtc is null ||
|
||||
string.IsNullOrWhiteSpace(command.Title) ||
|
||||
string.IsNullOrWhiteSpace(command.Summary))
|
||||
{
|
||||
return Results.BadRequest("motion.create requires scope, category, title, summary, and closesAtUtc.");
|
||||
}
|
||||
|
||||
await motionGovernanceService.CreateMotionAsync(
|
||||
actor,
|
||||
new CreateMotionCommand(
|
||||
command.Scope.Value,
|
||||
command.ProjectId,
|
||||
command.Category.Value,
|
||||
command.Title,
|
||||
command.Summary,
|
||||
command.ClosesAtUtc.Value),
|
||||
cancellationToken);
|
||||
break;
|
||||
case "motion.vote":
|
||||
if (command.MotionId is null || command.Choice is null)
|
||||
{
|
||||
return Results.BadRequest("motion.vote requires motionId and choice.");
|
||||
}
|
||||
|
||||
await motionGovernanceService.CastVoteAsync(
|
||||
actor,
|
||||
command.MotionId.Value,
|
||||
command.Choice.Value,
|
||||
command.Comment,
|
||||
cancellationToken);
|
||||
break;
|
||||
case "motion.close":
|
||||
if (command.MotionId is null)
|
||||
{
|
||||
return Results.BadRequest("motion.close requires motionId.");
|
||||
}
|
||||
|
||||
await motionGovernanceService.CloseMotionAsync(actor, command.MotionId.Value, cancellationToken);
|
||||
break;
|
||||
default:
|
||||
return Results.BadRequest($"Unknown Eve governance command '{command.Command}'.");
|
||||
}
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Results.Forbid();
|
||||
}
|
||||
|
||||
var state = await motionGovernanceService.GetStateAsync(actor, cancellationToken);
|
||||
var surface = motionEveSurfaceService.BuildSurface(state);
|
||||
return Results.Text(
|
||||
JsonSerializer.Serialize(surface, appJsonSerializerOptions),
|
||||
"application/json");
|
||||
});
|
||||
|
||||
app.MapGet("/auth/heimdall/wait", async (
|
||||
HttpContext httpContext,
|
||||
HeimdallAuthService heimdallAuthService,
|
||||
|
|
@ -926,28 +579,6 @@ static JsonSerializerOptions CreateAppJsonSerializerOptions()
|
|||
return options;
|
||||
}
|
||||
|
||||
static BridgeCaller BuildBridgeCaller(
|
||||
CurrentBifrostActor actor,
|
||||
HttpRequest request,
|
||||
BridgeOptions bridgeOptions)
|
||||
{
|
||||
var isLocalBridge = HasValidLocalBridgeToken(request, bridgeOptions);
|
||||
|
||||
return new BridgeCaller(
|
||||
actor.IsActiveMember,
|
||||
isLocalBridge,
|
||||
actor.UserAccount?.Id,
|
||||
actor.DisplayName);
|
||||
}
|
||||
|
||||
static bool HasValidLocalBridgeToken(HttpRequest request, BridgeOptions bridgeOptions)
|
||||
{
|
||||
var bridgeToken = request.Headers["X-Bifrost-Bridge-Token"].ToString();
|
||||
return
|
||||
bridgeOptions.HasLocalBridgeToken &&
|
||||
string.Equals(bridgeToken, bridgeOptions.LocalBridgeToken, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
public partial class Program;
|
||||
|
||||
public sealed record NativeBifrostRegistrationRequest(
|
||||
|
|
|
|||
Loading…
Reference in a new issue