wip
This commit is contained in:
parent
0dc41337da
commit
14fd359ea8
28 changed files with 156 additions and 52 deletions
|
@ -35,6 +35,8 @@ public static class AuthenticationStartup
|
|||
builder.EnableSensitiveDataLogging();
|
||||
});
|
||||
|
||||
services.AddScoped<DbContext>(s => s.GetRequiredService<AuthContext>());
|
||||
|
||||
services.AddMediatR(c =>
|
||||
{
|
||||
c.RegisterServicesFromAssembly(typeof(AuthenticationStartup).Assembly);
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
using Femto.Modules.Auth.Application.Services;
|
||||
using Femto.Modules.Auth.Data;
|
||||
using Femto.Modules.Auth.Models;
|
||||
using MediatR;
|
||||
|
@ -8,7 +7,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
|
||||
namespace Femto.Modules.Auth.Application.Commands.Login;
|
||||
|
||||
internal class LoginCommandHandler(AuthContext context, SessionGenerator sessionGenerator)
|
||||
internal class LoginCommandHandler(AuthContext context)
|
||||
: ICommandHandler<LoginCommand, LoginResult>
|
||||
{
|
||||
public async Task<LoginResult> Handle(LoginCommand request, CancellationToken cancellationToken)
|
||||
|
@ -19,14 +18,12 @@ internal class LoginCommandHandler(AuthContext context, SessionGenerator session
|
|||
);
|
||||
|
||||
if (user is null)
|
||||
throw new DomainException("invalid credentials");
|
||||
throw new DomainError("invalid credentials");
|
||||
|
||||
if (!user.HasPassword(request.Password))
|
||||
throw new DomainException("invalid credentials");
|
||||
throw new DomainError("invalid credentials");
|
||||
|
||||
var session = sessionGenerator.GenerateSession();
|
||||
|
||||
await context.AddAsync(session, cancellationToken);
|
||||
var session = user.StartNewSession();
|
||||
|
||||
return new(new Session(session.Id, session.Expires), user.Id, user.Username);
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
using Femto.Modules.Auth.Application.Services;
|
||||
using Femto.Modules.Auth.Data;
|
||||
using Femto.Modules.Auth.Models;
|
||||
|
||||
namespace Femto.Modules.Auth.Application.Commands.Register;
|
||||
|
||||
internal class RegisterCommandHandler(AuthContext context, SessionGenerator sessionGenerator) : ICommandHandler<RegisterCommand, RegisterResult>
|
||||
internal class RegisterCommandHandler(AuthContext context) : ICommandHandler<RegisterCommand, RegisterResult>
|
||||
{
|
||||
public async Task<RegisterResult> Handle(RegisterCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
|
|
@ -3,4 +3,8 @@ using Femto.Modules.Auth.Application.Dto;
|
|||
|
||||
namespace Femto.Modules.Auth.Application.Commands.ValidateSession;
|
||||
|
||||
public record ValidateSessionCommand(string SessionId) : ICommand<ValidateSessionResult>;
|
||||
/// <summary>
|
||||
/// Validate an existing session, and then return either the current session, or a new one in case the expiry is further in the future
|
||||
/// </summary>
|
||||
/// <param name="SessionId"></param>
|
||||
public record ValidateSessionCommand(string SessionId) : ICommand<ValidateSessionResult>;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
using Femto.Modules.Auth.Data;
|
||||
using Femto.Modules.Auth.Errors;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Femto.Modules.Auth.Application.Commands.ValidateSession;
|
||||
|
@ -21,7 +22,7 @@ internal class ValidateSessionCommandHandler(AuthContext context)
|
|||
);
|
||||
|
||||
if (user is null)
|
||||
throw new DomainException("invalid session");
|
||||
throw new InvalidSessionError();
|
||||
|
||||
var session = user.StartNewSession();
|
||||
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
using Femto.Modules.Auth.Models;
|
||||
|
||||
namespace Femto.Modules.Auth.Application.Services;
|
||||
|
||||
public class SessionGenerator
|
||||
{
|
||||
public UserSession GenerateSession()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
|
||||
namespace Femto.Modules.Auth.Data;
|
||||
|
||||
internal class AuthContext : DbContext, IOutboxContext
|
||||
internal class AuthContext(DbContextOptions<AuthContext> options) : DbContext(options), IOutboxContext
|
||||
{
|
||||
public virtual DbSet<UserIdentity> Users { get; }
|
||||
public virtual DbSet<OutboxEntry> Outbox { get; }
|
||||
|
|
14
Femto.Modules.Auth/Errors/InvalidSessionError.cs
Normal file
14
Femto.Modules.Auth/Errors/InvalidSessionError.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Femto.Common.Domain;
|
||||
|
||||
namespace Femto.Modules.Auth.Errors;
|
||||
|
||||
public class InvalidSessionError : DomainError
|
||||
{
|
||||
private const string Code = "invalid_session";
|
||||
|
||||
public InvalidSessionError()
|
||||
: base("invalid_session") { }
|
||||
|
||||
public InvalidSessionError(Exception e)
|
||||
: base("invalid_session", e) { }
|
||||
}
|
|
@ -14,7 +14,7 @@ internal class UserIdentity : Entity
|
|||
|
||||
public UserPassword Password { get; private set; }
|
||||
|
||||
public ICollection<UserSession> Sessions { get; private set; }
|
||||
public ICollection<UserSession> Sessions { get; private set; } = [];
|
||||
|
||||
private UserIdentity() { }
|
||||
|
||||
|
@ -57,4 +57,4 @@ internal class UserIdentity : Entity
|
|||
}
|
||||
}
|
||||
|
||||
public class SetPasswordError(string message, Exception inner) : DomainException(message, inner);
|
||||
public class SetPasswordError(string message, Exception inner) : DomainError(message, inner);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue