This commit is contained in:
john 2025-05-16 16:10:01 +02:00
parent 14fd359ea8
commit a4ef2b4a20
26 changed files with 331 additions and 78 deletions

View file

@ -14,7 +14,7 @@ internal class SessionAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
IAuthenticationModule authModule,
IAuthModule authModule,
CurrentUserContext currentUserContext
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{

View file

@ -8,7 +8,7 @@ namespace Femto.Api.Controllers.Auth;
[ApiController]
[Route("auth")]
public class AuthController(IAuthenticationModule authModule) : ControllerBase
public class AuthController(IAuthModule authModule) : ControllerBase
{
[HttpPost("login")]
public async Task<ActionResult<LoginResponse>> Login([FromBody] LoginRequest request)
@ -34,10 +34,10 @@ public class AuthController(IAuthenticationModule authModule) : ControllerBase
return new RegisterResponse(result.UserId, result.Username);
}
[HttpPost("delete-session")]
public async Task<ActionResult> DeleteSession([FromBody] DeleteSessionRequest request)
[HttpDelete("session")]
public async Task<ActionResult> DeleteSession()
{
// TODO
HttpContext.Response.Cookies.Delete("session");
return Ok(new { });
}
}

View file

@ -4,6 +4,7 @@ using Femto.Modules.Media.Contracts;
using Femto.Modules.Media.Contracts.LoadFile;
using Femto.Modules.Media.Contracts.SaveFile;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Femto.Api.Controllers.Media;
@ -13,6 +14,7 @@ namespace Femto.Api.Controllers.Media;
public class MediaController(IMediaModule mediaModule) : ControllerBase
{
[HttpPost]
[Authorize]
public async Task<ActionResult<UploadMediaResponse>> UploadMedia(
IFormFile file,
CancellationToken cancellationToken
@ -29,6 +31,7 @@ public class MediaController(IMediaModule mediaModule) : ControllerBase
}
[HttpGet("{id}")]
[Authorize]
public async Task GetMedia(Guid id, CancellationToken cancellationToken)
{
var res = await mediaModule.PostQuery(new LoadFileQuery(id), cancellationToken);

View file

@ -1,9 +1,7 @@
using Femto.Api.Controllers.Posts.Dto;
using Femto.Modules.Blog;
using Femto.Modules.Blog.Application;
using Femto.Modules.Blog.Application.Commands.CreatePost;
using Femto.Modules.Blog.Application.Queries.GetPosts;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -44,6 +42,7 @@ public class PostsController(IBlogModule blogModule) : ControllerBase
}
[HttpPost]
[Authorize]
public async Task<ActionResult<CreatePostResponse>> Post(
[FromBody] CreatePostRequest req,
CancellationToken cancellationToken

View file

@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
@ -28,8 +29,4 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Middleware\" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,79 @@
using Femto.Common.Domain;
using Femto.Common.Logs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.WebUtilities;
namespace Femto.Api.Middleware;
public class ExceptionMapperMiddleware(
RequestDelegate next,
IWebHostEnvironment env,
ILogger<ExceptionMapperMiddleware> logger
)
{
public async Task Invoke(HttpContext context, ProblemDetailsFactory problemDetailsFactory)
{
try
{
await next(context);
if (context.Response.StatusCode >= 400)
{
logger.LogFailedRequest(
context.Request.Method,
context.Request.Path,
context.Response.StatusCode,
context.TraceIdentifier,
ReasonPhrases.GetReasonPhrase(context.Response.StatusCode)
);
}
}
catch (DomainError e)
{
context.Response.StatusCode = 400;
context.Response.ContentType = "application/json";
var problemDetails = problemDetailsFactory.CreateProblemDetails(
context,
statusCode: 400,
title: "client error",
detail: e.Message
);
logger.LogFailedRequest(
e,
context.Request.Method,
context.Request.Path,
context.Response.StatusCode,
context.TraceIdentifier,
e.Message
);
await context.Response.WriteAsJsonAsync(problemDetails);
}
catch (Exception e)
{
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
var problemDetails = problemDetailsFactory.CreateProblemDetails(
context,
statusCode: 500,
title: "server error error",
detail: env.IsDevelopment() ? e.Message : "Something went wrong"
);
logger.LogFailedRequest(
e,
context.Request.Method,
context.Request.Path,
context.Response.StatusCode,
context.TraceIdentifier,
e.Message
);
await context.Response.WriteAsJsonAsync(problemDetails);
}
finally { }
}
}

View file

@ -2,11 +2,18 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using Femto.Api;
using Femto.Api.Auth;
using Femto.Api.Middleware;
using Femto.Common;
using Femto.Common.Domain;
using Femto.Modules.Auth.Application;
using Femto.Modules.Blog.Application;
using Femto.Modules.Media.Application;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.WebUtilities;
using Serilog;
const string CorsPolicyName = "DefaultCorsPolicy";
@ -22,6 +29,7 @@ var blobStorageRoot = builder.Configuration.GetValue<string>("BlobStorageRoot");
if (blobStorageRoot is null)
throw new Exception("no blob storage root found");
builder.Services.InitializeBlogModule(connectionString);
builder.Services.InitializeMediaModule(connectionString, blobStorageRoot);
builder.Services.InitializeAuthenticationModule(connectionString);
@ -29,15 +37,16 @@ builder.Services.InitializeAuthenticationModule(connectionString);
builder.Services.AddScoped<CurrentUserContext, CurrentUserContext>();
builder.Services.AddScoped<ICurrentUserContext>(s => s.GetRequiredService<CurrentUserContext>());
builder.Services.AddControllers();
builder.Services.AddCors(options =>
{
options.AddPolicy(
CorsPolicyName,
b =>
{
b.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:5173");
b.AllowAnyHeader()
.AllowAnyMethod()
.WithOrigins("http://localhost:5173")
.AllowCredentials();
}
);
});
@ -60,12 +69,51 @@ builder
options => { }
);
builder.Services.AddAuthorization(); // if not already added
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseCors(CorsPolicyName);
app.UseAuthentication();
app.UseAuthorization();
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var exceptionHandlerFeature = context.Features.Get<IExceptionHandlerFeature>();
var exception = exceptionHandlerFeature?.Error;
var problemDetailsFactory =
errorApp.ApplicationServices.GetRequiredService<ProblemDetailsFactory>();
var statusCode = exception switch
{
DomainError => 400,
_ => 500,
};
var message = exception switch
{
DomainError domainError => domainError.Message,
{ } e => e.Message,
_ => ReasonPhrases.GetReasonPhrase(statusCode),
};
var problemDetails = problemDetailsFactory.CreateProblemDetails(
httpContext: context,
title: "An error occurred",
detail: message,
statusCode: statusCode
);
// problemDetails.Extensions["traceId"] = context.TraceIdentifier;
context.Response.StatusCode = statusCode;
context.Response.ContentType = "application/problem+json";
await context.Response.WriteAsJsonAsync(problemDetails);
});
});
// app.UseMiddleware<ExceptionMapperMiddleware>();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();