some changes

This commit is contained in:
john 2025-05-17 23:47:19 +02:00
parent 4ec9720541
commit b47bac67ca
37 changed files with 397 additions and 190 deletions

View file

@ -1,34 +1,44 @@
using Femto.Common.Infrastructure;
using Femto.Common.Infrastructure.Outbox;
using Femto.Common.Integration;
using Femto.Modules.Auth.Data;
using Femto.Modules.Auth.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Quartz;
namespace Femto.Modules.Auth.Application;
public static class AuthStartup
{
public static void InitializeAuthenticationModule(
this IServiceCollection rootContainer,
string connectionString
)
public static void InitializeAuthenticationModule(this IServiceCollection rootContainer,
string connectionString, IEventBus eventBus)
{
var hostBuilder = Host.CreateDefaultBuilder();
hostBuilder.ConfigureServices(services => ConfigureServices(services, connectionString));
hostBuilder.ConfigureServices(services => ConfigureServices(services, connectionString, eventBus));
var host = hostBuilder.Build();
rootContainer.AddScoped<IAuthModule>(_ => new AuthModule(host));
rootContainer.AddHostedService(services => new AuthApplication(host));
eventBus.Subscribe((evt, cancellationToken) => EventSubscriber(evt, host.Services, cancellationToken));
}
private static void ConfigureServices(IServiceCollection services, string connectionString)
private static void ConfigureServices(IServiceCollection services, string connectionString, IEventPublisher publisher)
{
services.AddDbContext<AuthContext>(builder =>
{
builder.UseNpgsql(connectionString);
builder.UseSnakeCaseNamingConvention();
});
services.AddQuartzHostedService(options =>
{
options.WaitForJobsToComplete = true;
});
services.AddOutbox<AuthContext, OutboxMessageHandler>();
services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(AuthStartup).Assembly));
@ -41,9 +51,35 @@ public static class AuthStartup
services.ConfigureDomainServices<AuthContext>();
services.AddMediatR(c =>
services.AddSingleton(publisher);
}
private static async Task EventSubscriber(
IEvent evt,
IServiceProvider provider,
CancellationToken cancellationToken
)
{
using var scope = provider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<AuthContext>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<AuthApplication>>();
var publisher = scope.ServiceProvider.GetRequiredService<IPublisher>();
IEventHandler? handler = evt switch
{
c.RegisterServicesFromAssembly(typeof(AuthStartup).Assembly);
});
_ => null,
};
if (handler is null)
return;
await handler.Handle(evt, cancellationToken);
if (context.ChangeTracker.HasChanges())
{
await context.EmitDomainEvents(logger, publisher, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
}
}
}

View file

@ -1,23 +0,0 @@
using Femto.Modules.Auth.Data;
using Femto.Modules.Auth.Models;
namespace Femto.Modules.Auth.Contracts;
internal class AuthenticationService(AuthContext context) : IAuthenticationService
{
public async Task<UserInfo> Register(string username, string password)
{
var user = new UserIdentity(username).WithPassword(password);
await context.AddAsync(user);
await context.SaveChangesAsync();
return new(user.Id, user.Username);
}
public async Task<UserInfo> Authenticate(string username, string password)
{
throw new NotImplementedException();
}
}
public class AuthenticationError(string message, Exception inner) : Exception(message, inner);

View file

@ -1,7 +0,0 @@
namespace Femto.Modules.Auth.Contracts;
public interface IAuthenticationService
{
public Task<UserInfo?> Register(string username, string password);
public Task<UserInfo?> Authenticate(string username, string password);
}

View file

@ -1,3 +0,0 @@
namespace Femto.Modules.Auth.Contracts;
public record UserInfo(Guid UserId, string Username);

View file

@ -14,14 +14,12 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.14.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Femto.Common\Femto.Common.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\DomainEventHandlers\" />
<ProjectReference Include="..\Femto.Modules.Auth.Contracts\Femto.Modules.Auth.Contracts.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,22 @@
using Femto.Common.Infrastructure.Outbox;
using Femto.Common.Integration;
using Microsoft.Extensions.Logging;
namespace Femto.Modules.Auth.Infrastructure;
public class OutboxMessageHandler(IEventPublisher publisher, ILogger<OutboxMessageHandler> logger) : IOutboxMessageHandler
{
public async Task HandleMessage<TNotification>(
TNotification notification,
CancellationToken executionContextCancellationToken
)
{
if (notification is IEvent evt)
{
await publisher.Publish(evt);
} else
{
logger.LogWarning("ignoring non IEvent {Type} in outbox message handler", typeof(TNotification));
}
}
}

View file

@ -0,0 +1,20 @@
using Femto.Common.Infrastructure.Outbox;
using Femto.Modules.Auth.Contracts.Events;
using Femto.Modules.Auth.Data;
using Femto.Modules.Auth.Models.Events;
using MediatR;
namespace Femto.Modules.Auth.Models.DomainEventHandlers;
internal class UserWasCreatedHandler(Outbox<AuthContext> outbox)
: INotificationHandler<UserWasCreatedEvent>
{
public async Task Handle(UserWasCreatedEvent notification, CancellationToken cancellationToken)
{
await outbox.AddMessage(
notification.User.Id,
new UserWasCreatedIntegrationEvent(notification.User.Id, notification.User.Username),
cancellationToken
);
}
}

View file

@ -1,12 +1,11 @@
namespace Femto.Modules.Auth.Models;
public class UserSession
internal class UserSession
{
private static TimeSpan SessionTimeout { get; } = TimeSpan.FromMinutes(30);
private static TimeSpan ExpiryBuffer { get; } = TimeSpan.FromMinutes(5);
public string Id { get; private set; }
public DateTimeOffset Expires { get; private set; }
public bool ExpiresSoon => Expires < DateTimeOffset.UtcNow + ExpiryBuffer;
private UserSession() {}