Compare commits
No commits in common. "dac3acfecfff94fa1aef08cf009b815ddd809e0b" and "e282e2ece31a2f974f3ca988e2cece1d570e4d6a" have entirely different histories.
dac3acfecf
...
e282e2ece3
22 changed files with 255 additions and 241 deletions
|
@ -1,16 +1,26 @@
|
||||||
|
using Femto.Api.Auth;
|
||||||
using Femto.Api.Sessions;
|
using Femto.Api.Sessions;
|
||||||
using Femto.Common;
|
using Femto.Common;
|
||||||
|
using Femto.Modules.Auth.Application.Interface.CreateSignupCode;
|
||||||
|
using Femto.Modules.Auth.Application.Interface.GetSignupCodesQuery;
|
||||||
|
using Femto.Modules.Auth.Application.Interface.Register;
|
||||||
using Femto.Modules.Auth.Application.Services;
|
using Femto.Modules.Auth.Application.Services;
|
||||||
using Femto.Modules.Auth.Contracts;
|
using Femto.Modules.Auth.Contracts;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace Femto.Api.Controllers.Auth;
|
namespace Femto.Api.Controllers.Auth;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("auth")]
|
[Route("auth")]
|
||||||
public class AuthController(ICurrentUserContext currentUserContext, IAuthService authService)
|
public class AuthController(
|
||||||
: ControllerBase
|
IAuthModule authModule,
|
||||||
|
IOptions<CookieSettings> cookieSettings,
|
||||||
|
ICurrentUserContext currentUserContext,
|
||||||
|
ILogger<AuthController> logger,
|
||||||
|
IAuthService authService
|
||||||
|
) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpPost("login")]
|
[HttpPost("login")]
|
||||||
public async Task<ActionResult<LoginResponse>> Login(
|
public async Task<ActionResult<LoginResponse>> Login(
|
||||||
|
@ -18,17 +28,16 @@ public class AuthController(ICurrentUserContext currentUserContext, IAuthService
|
||||||
CancellationToken cancellationToken
|
CancellationToken cancellationToken
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var result = await authService.GetUserWithCredentials(
|
var user = await authService.GetUserWithCredentials(
|
||||||
request.Username,
|
request.Username,
|
||||||
request.Password,
|
request.Password,
|
||||||
request.RememberMe,
|
|
||||||
cancellationToken
|
cancellationToken
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result is null)
|
if (user is null)
|
||||||
return this.BadRequest();
|
return this.BadRequest();
|
||||||
|
|
||||||
var (user, session) = result;
|
var session = await authService.CreateStrongSession(user.Id);
|
||||||
|
|
||||||
HttpContext.SetSession(session, user);
|
HttpContext.SetSession(session, user);
|
||||||
|
|
||||||
|
@ -38,15 +47,13 @@ public class AuthController(ICurrentUserContext currentUserContext, IAuthService
|
||||||
[HttpPost("register")]
|
[HttpPost("register")]
|
||||||
public async Task<ActionResult<RegisterResponse>> Register([FromBody] RegisterRequest request)
|
public async Task<ActionResult<RegisterResponse>> Register([FromBody] RegisterRequest request)
|
||||||
{
|
{
|
||||||
var (user, session) = await authService.CreateUserWithCredentials(
|
var user = await authModule.Command(
|
||||||
request.Username,
|
new RegisterCommand(request.Username, request.Password, request.SignupCode)
|
||||||
request.Password,
|
|
||||||
request.SignupCode,
|
|
||||||
request.RememberMe
|
|
||||||
);
|
);
|
||||||
|
|
||||||
HttpContext.SetSession(session, user);
|
var session = await authService.CreateStrongSession(user.Id);
|
||||||
|
|
||||||
|
HttpContext.SetSession(session, user);
|
||||||
return new RegisterResponse(
|
return new RegisterResponse(
|
||||||
user.Id,
|
user.Id,
|
||||||
user.Username,
|
user.Username,
|
||||||
|
@ -99,7 +106,10 @@ public class AuthController(ICurrentUserContext currentUserContext, IAuthService
|
||||||
CancellationToken cancellationToken
|
CancellationToken cancellationToken
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
await authService.AddSignupCode(request.Code, request.Name, cancellationToken);
|
await authModule.Command(
|
||||||
|
new CreateSignupCodeCommand(request.Code, request.Email, request.Name),
|
||||||
|
cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
return Ok(new { });
|
return Ok(new { });
|
||||||
}
|
}
|
||||||
|
@ -110,7 +120,7 @@ public class AuthController(ICurrentUserContext currentUserContext, IAuthService
|
||||||
CancellationToken cancellationToken
|
CancellationToken cancellationToken
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var codes = await authService.GetSignupCodes(cancellationToken);
|
var codes = await authModule.Query(new GetSignupCodesQuery(), cancellationToken);
|
||||||
|
|
||||||
return new ListSignupCodesResult(
|
return new ListSignupCodesResult(
|
||||||
codes.Select(c => new SignupCodeDto(
|
codes.Select(c => new SignupCodeDto(
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
namespace Femto.Api.Controllers.Auth;
|
namespace Femto.Api.Controllers.Auth;
|
||||||
|
|
||||||
public record LoginRequest(string Username, string Password, bool RememberMe);
|
public record LoginRequest(string Username, string Password);
|
|
@ -1,3 +1,3 @@
|
||||||
namespace Femto.Api.Controllers.Auth;
|
namespace Femto.Api.Controllers.Auth;
|
||||||
|
|
||||||
public record RegisterRequest(string Username, string Password, string SignupCode, bool RememberMe);
|
public record RegisterRequest(string Username, string Password, string SignupCode, string? Email);
|
|
@ -12,7 +12,7 @@ public static class DomainServiceExtensions
|
||||||
services.AddScoped<DbContext>(s => s.GetRequiredService<TContext>());
|
services.AddScoped<DbContext>(s => s.GetRequiredService<TContext>());
|
||||||
services.AddTransient(
|
services.AddTransient(
|
||||||
typeof(IPipelineBehavior<,>),
|
typeof(IPipelineBehavior<,>),
|
||||||
typeof(DDDPipelineBehaviour<,>)
|
typeof(SaveChangesPipelineBehaviour<,>)
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,10 @@ using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Femto.Common.Infrastructure;
|
namespace Femto.Common.Infrastructure;
|
||||||
|
|
||||||
public class DDDPipelineBehaviour<TRequest, TResponse>(
|
public class SaveChangesPipelineBehaviour<TRequest, TResponse>(
|
||||||
DbContext context,
|
DbContext context,
|
||||||
IPublisher publisher,
|
IPublisher publisher,
|
||||||
ILogger<DDDPipelineBehaviour<TRequest, TResponse>> logger
|
ILogger<SaveChangesPipelineBehaviour<TRequest, TResponse>> logger
|
||||||
) : IPipelineBehavior<TRequest, TResponse>
|
) : IPipelineBehavior<TRequest, TResponse>
|
||||||
where TRequest : notnull
|
where TRequest : notnull
|
||||||
{
|
{
|
|
@ -41,6 +41,7 @@ public static class AuthStartup
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
rootContainer.ExposeScopedService<IAuthModule>();
|
||||||
rootContainer.ExposeScopedService<IAuthService>();
|
rootContainer.ExposeScopedService<IAuthService>();
|
||||||
|
|
||||||
rootContainer.AddHostedService(services => new AuthApplication(host));
|
rootContainer.AddHostedService(services => new AuthApplication(host));
|
||||||
|
@ -84,12 +85,8 @@ public static class AuthStartup
|
||||||
|
|
||||||
services.AddSingleton(publisher);
|
services.AddSingleton(publisher);
|
||||||
services.AddSingleton<SessionStorage>();
|
services.AddSingleton<SessionStorage>();
|
||||||
|
|
||||||
services.AddScoped(
|
|
||||||
typeof(IPipelineBehavior<,>),
|
|
||||||
typeof(SaveChangesPipelineBehaviour<,>)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
services.AddScoped<IAuthModule, AuthModule>();
|
||||||
services.AddScoped<IAuthService, AuthService>();
|
services.AddScoped<IAuthService, AuthService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.CreateSignupCode;
|
||||||
|
|
||||||
|
public record CreateSignupCodeCommand(string Code, string RecipientEmail, string RecipientName): ICommand;
|
|
@ -0,0 +1,15 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using Femto.Modules.Auth.Data;
|
||||||
|
using Femto.Modules.Auth.Models;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.CreateSignupCode;
|
||||||
|
|
||||||
|
internal class CreateSignupCodeCommandHandler(AuthContext context) : ICommandHandler<CreateSignupCodeCommand>
|
||||||
|
{
|
||||||
|
public async Task Handle(CreateSignupCodeCommand command, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var code = new SignupCode(command.RecipientEmail, command.RecipientName, command.Code);
|
||||||
|
|
||||||
|
await context.SignupCodes.AddAsync(code, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.Deauthenticate;
|
||||||
|
|
||||||
|
public record DeauthenticateCommand(Guid UserId, string SessionId, string? RememberMeToken) : ICommand;
|
|
@ -0,0 +1,12 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using Femto.Modules.Auth.Data;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.Deauthenticate;
|
||||||
|
|
||||||
|
internal class DeauthenticateCommandHandler(AuthContext context) : ICommandHandler<DeauthenticateCommand>
|
||||||
|
{
|
||||||
|
public async Task Handle(DeauthenticateCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using Femto.Modules.Auth.Application.Dto;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.GetSignupCodesQuery;
|
||||||
|
|
||||||
|
public record GetSignupCodesQuery: IQuery<ICollection<SignupCodeDto>>;
|
|
@ -0,0 +1,55 @@
|
||||||
|
using Dapper;
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using Femto.Common.Infrastructure.DbConnection;
|
||||||
|
using Femto.Modules.Auth.Application.Dto;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.GetSignupCodesQuery;
|
||||||
|
|
||||||
|
public class GetSignupCodesQueryHandler(IDbConnectionFactory connectionFactory)
|
||||||
|
: IQueryHandler<GetSignupCodesQuery, ICollection<SignupCodeDto>>
|
||||||
|
{
|
||||||
|
public async Task<ICollection<SignupCodeDto>> Handle(
|
||||||
|
GetSignupCodesQuery request,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
|
{
|
||||||
|
using var conn = connectionFactory.GetConnection();
|
||||||
|
|
||||||
|
// lang=sql
|
||||||
|
const string sql = """
|
||||||
|
SELECT
|
||||||
|
sc.code as Code,
|
||||||
|
sc.recipient_email as Email,
|
||||||
|
sc.recipient_name as Name,
|
||||||
|
sc.redeeming_user_id as RedeemedByUserId,
|
||||||
|
u.username as RedeemedByUsername,
|
||||||
|
sc.expires_at as ExpiresOn
|
||||||
|
FROM authn.signup_code sc
|
||||||
|
LEFT JOIN authn.user_identity u ON u.id = sc.redeeming_user_id
|
||||||
|
ORDER BY sc.created_at DESC
|
||||||
|
""";
|
||||||
|
|
||||||
|
var result = await conn.QueryAsync<QueryResultRow>(sql);
|
||||||
|
|
||||||
|
return result
|
||||||
|
.Select(row => new SignupCodeDto(
|
||||||
|
row.Code,
|
||||||
|
row.Email,
|
||||||
|
row.Name,
|
||||||
|
row.RedeemedByUserId,
|
||||||
|
row.RedeemedByUsername,
|
||||||
|
row.ExpiresOn
|
||||||
|
))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class QueryResultRow
|
||||||
|
{
|
||||||
|
public string Code { get; set; }
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public Guid? RedeemedByUserId { get; set; }
|
||||||
|
public string? RedeemedByUsername { get; set; }
|
||||||
|
public DateTimeOffset? ExpiresOn { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using Femto.Modules.Auth.Application.Dto;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.GetUserInfo;
|
||||||
|
|
||||||
|
public record GetUserInfoCommand(Guid ForUser) : ICommand<UserInfo?>;
|
|
@ -0,0 +1,27 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using Femto.Modules.Auth.Application.Dto;
|
||||||
|
using Femto.Modules.Auth.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.GetUserInfo;
|
||||||
|
|
||||||
|
internal class GetUserInfoCommandHandler(AuthContext context)
|
||||||
|
: ICommandHandler<GetUserInfoCommand, UserInfo?>
|
||||||
|
{
|
||||||
|
public async Task<UserInfo?> Handle(
|
||||||
|
GetUserInfoCommand request,
|
||||||
|
CancellationToken cancellationToken
|
||||||
|
)
|
||||||
|
{
|
||||||
|
|
||||||
|
var user = await context.Users.SingleOrDefaultAsync(
|
||||||
|
u => u.Id == request.ForUser,
|
||||||
|
cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
if (user is null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new UserInfo(user);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using Femto.Modules.Auth.Application.Dto;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.Register;
|
||||||
|
|
||||||
|
public record RegisterCommand(string Username, string Password, string SignupCode) : ICommand<UserInfo>;
|
|
@ -0,0 +1,43 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using Femto.Modules.Auth.Application.Dto;
|
||||||
|
using Femto.Modules.Auth.Data;
|
||||||
|
using Femto.Modules.Auth.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Interface.Register;
|
||||||
|
|
||||||
|
internal class RegisterCommandHandler(AuthContext context)
|
||||||
|
: ICommandHandler<RegisterCommand, UserInfo>
|
||||||
|
{
|
||||||
|
public async Task<UserInfo> Handle(RegisterCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var now = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
|
var code = await context
|
||||||
|
.SignupCodes.Where(c => c.Code == request.SignupCode)
|
||||||
|
.Where(c => c.ExpiresAt == null || c.ExpiresAt > now)
|
||||||
|
.Where(c => c.RedeemingUserId == null)
|
||||||
|
.SingleOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (code is null)
|
||||||
|
throw new DomainError("invalid signup code");
|
||||||
|
|
||||||
|
var usernameTaken = await context.Users.AnyAsync(
|
||||||
|
u => u.Username == request.Username,
|
||||||
|
cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
if (usernameTaken)
|
||||||
|
throw new DomainError("username taken");
|
||||||
|
|
||||||
|
var user = new UserIdentity(request.Username);
|
||||||
|
|
||||||
|
await context.AddAsync(user, cancellationToken);
|
||||||
|
|
||||||
|
user.SetPassword(request.Password);
|
||||||
|
|
||||||
|
code.Redeem(user.Id);
|
||||||
|
|
||||||
|
return new UserInfo(user);
|
||||||
|
}
|
||||||
|
}
|
20
Femto.Modules.Auth/Application/Services/AuthModule.cs
Normal file
20
Femto.Modules.Auth/Application/Services/AuthModule.cs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Services;
|
||||||
|
|
||||||
|
internal class AuthModule(IMediator mediator) : IAuthModule
|
||||||
|
{
|
||||||
|
public async Task Command(ICommand command, CancellationToken cancellationToken = default) =>
|
||||||
|
await mediator.Send(command, cancellationToken);
|
||||||
|
|
||||||
|
public async Task<TResponse> Command<TResponse>(
|
||||||
|
ICommand<TResponse> command,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
) => await mediator.Send(command, cancellationToken);
|
||||||
|
|
||||||
|
public async Task<TResponse> Query<TResponse>(
|
||||||
|
IQuery<TResponse> query,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
) => await mediator.Send(query, cancellationToken);
|
||||||
|
}
|
|
@ -1,6 +1,4 @@
|
||||||
using Dapper;
|
|
||||||
using Femto.Common.Domain;
|
using Femto.Common.Domain;
|
||||||
using Femto.Common.Infrastructure.DbConnection;
|
|
||||||
using Femto.Modules.Auth.Application.Dto;
|
using Femto.Modules.Auth.Application.Dto;
|
||||||
using Femto.Modules.Auth.Data;
|
using Femto.Modules.Auth.Data;
|
||||||
using Femto.Modules.Auth.Infrastructure;
|
using Femto.Modules.Auth.Infrastructure;
|
||||||
|
@ -9,35 +7,25 @@ using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Femto.Modules.Auth.Application.Services;
|
namespace Femto.Modules.Auth.Application.Services;
|
||||||
|
|
||||||
internal class AuthService(
|
internal class AuthService(AuthContext context, SessionStorage storage) : IAuthService
|
||||||
AuthContext context,
|
|
||||||
SessionStorage storage,
|
|
||||||
IDbConnectionFactory connectionFactory
|
|
||||||
) : IAuthService
|
|
||||||
{
|
{
|
||||||
public async Task<UserAndSession?> GetUserWithCredentials(string username,
|
public async Task<UserInfo?> GetUserWithCredentials(
|
||||||
|
string username,
|
||||||
string password,
|
string password,
|
||||||
bool createLongTermSession,
|
CancellationToken cancellationToken = default
|
||||||
CancellationToken cancellationToken = default)
|
)
|
||||||
{
|
{
|
||||||
var user = await context
|
var user = await context
|
||||||
.Users.Where(u => u.Username == username)
|
.Users.Where(u => u.Username == username)
|
||||||
.SingleOrDefaultAsync(cancellationToken);
|
.SingleOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
if (user is null)
|
if (user is null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (!user.HasPassword(password))
|
if (!user.HasPassword(password))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var session = new Session(user.Id, true);
|
return new UserInfo(user.Id, user.Username, user.Roles.Select(r => r.Role).ToList());
|
||||||
|
|
||||||
await storage.AddSession(session);
|
|
||||||
|
|
||||||
return new(
|
|
||||||
new UserInfo(user.Id, user.Username, user.Roles.Select(r => r.Role).ToList()),
|
|
||||||
session
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<UserInfo?> GetUserWithId(Guid? userId, CancellationToken cancellationToken)
|
public Task<UserInfo?> GetUserWithId(Guid? userId, CancellationToken cancellationToken)
|
||||||
|
@ -76,96 +64,6 @@ internal class AuthService(
|
||||||
await storage.DeleteSession(sessionId);
|
await storage.DeleteSession(sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<UserAndSession> CreateUserWithCredentials(string username,
|
|
||||||
string password,
|
|
||||||
string signupCode,
|
|
||||||
bool createLongTermSession,
|
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
var now = DateTimeOffset.UtcNow;
|
|
||||||
|
|
||||||
var code = await context
|
|
||||||
.SignupCodes.Where(c => c.Code == signupCode)
|
|
||||||
.Where(c => c.ExpiresAt == null || c.ExpiresAt > now)
|
|
||||||
.Where(c => c.RedeemingUserId == null)
|
|
||||||
.SingleOrDefaultAsync(cancellationToken);
|
|
||||||
|
|
||||||
if (code is null)
|
|
||||||
throw new DomainError("invalid signup code");
|
|
||||||
|
|
||||||
var usernameTaken = await context.Users.AnyAsync(
|
|
||||||
u => u.Username == username,
|
|
||||||
cancellationToken
|
|
||||||
);
|
|
||||||
|
|
||||||
if (usernameTaken)
|
|
||||||
throw new DomainError("username taken");
|
|
||||||
|
|
||||||
var user = new UserIdentity(username);
|
|
||||||
|
|
||||||
await context.AddAsync(user, cancellationToken);
|
|
||||||
|
|
||||||
user.SetPassword(password);
|
|
||||||
|
|
||||||
code.Redeem(user.Id);
|
|
||||||
|
|
||||||
var session = new Session(user.Id, true);
|
|
||||||
|
|
||||||
await storage.AddSession(session);
|
|
||||||
|
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
|
||||||
|
|
||||||
return new(new UserInfo(user), session);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task AddSignupCode(
|
|
||||||
string code,
|
|
||||||
string recipientName,
|
|
||||||
CancellationToken cancellationToken
|
|
||||||
)
|
|
||||||
{
|
|
||||||
await context.SignupCodes.AddAsync(
|
|
||||||
new SignupCode("", recipientName, code),
|
|
||||||
cancellationToken
|
|
||||||
);
|
|
||||||
|
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<ICollection<SignupCodeDto>> GetSignupCodes(
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
)
|
|
||||||
{
|
|
||||||
using var conn = connectionFactory.GetConnection();
|
|
||||||
|
|
||||||
// lang=sql
|
|
||||||
const string sql = """
|
|
||||||
SELECT
|
|
||||||
sc.code as Code,
|
|
||||||
sc.recipient_email as Email,
|
|
||||||
sc.recipient_name as Name,
|
|
||||||
sc.redeeming_user_id as RedeemedByUserId,
|
|
||||||
u.username as RedeemedByUsername,
|
|
||||||
sc.expires_at as ExpiresOn
|
|
||||||
FROM authn.signup_code sc
|
|
||||||
LEFT JOIN authn.user_identity u ON u.id = sc.redeeming_user_id
|
|
||||||
ORDER BY sc.created_at DESC
|
|
||||||
""";
|
|
||||||
|
|
||||||
var result = await conn.QueryAsync<GetSignupCodesQueryResultRow>(sql, cancellationToken);
|
|
||||||
|
|
||||||
return result
|
|
||||||
.Select(row => new SignupCodeDto(
|
|
||||||
row.Code,
|
|
||||||
row.Email,
|
|
||||||
row.Name,
|
|
||||||
row.RedeemedByUserId,
|
|
||||||
row.RedeemedByUsername,
|
|
||||||
row.ExpiresOn
|
|
||||||
))
|
|
||||||
.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<LongTermSession> CreateLongTermSession(Guid userId, bool isStrong)
|
public async Task<LongTermSession> CreateLongTermSession(Guid userId, bool isStrong)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
@ -185,14 +83,4 @@ internal class AuthService(
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class GetSignupCodesQueryResultRow
|
|
||||||
{
|
|
||||||
public string Code { get; set; }
|
|
||||||
public string Email { get; set; }
|
|
||||||
public string Name { get; set; }
|
|
||||||
public Guid? RedeemedByUserId { get; set; }
|
|
||||||
public string? RedeemedByUsername { get; set; }
|
|
||||||
public DateTimeOffset? ExpiresOn { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
10
Femto.Modules.Auth/Application/Services/IAuthModule.cs
Normal file
10
Femto.Modules.Auth/Application/Services/IAuthModule.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
using Femto.Common.Domain;
|
||||||
|
|
||||||
|
namespace Femto.Modules.Auth.Application.Services;
|
||||||
|
|
||||||
|
public interface IAuthModule
|
||||||
|
{
|
||||||
|
Task Command(ICommand command, CancellationToken cancellationToken = default);
|
||||||
|
Task<TResponse> Command<TResponse>(ICommand<TResponse> command, CancellationToken cancellationToken = default);
|
||||||
|
Task<TResponse> Query<TResponse>(IQuery<TResponse> query, CancellationToken cancellationToken = default);
|
||||||
|
}
|
|
@ -11,36 +11,10 @@ namespace Femto.Modules.Auth.Application.Services;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IAuthService
|
public interface IAuthService
|
||||||
{
|
{
|
||||||
public Task<UserAndSession?> GetUserWithCredentials(
|
public Task<UserInfo?> GetUserWithCredentials(string username, string password, CancellationToken cancellationToken = default);
|
||||||
string username,
|
public Task<UserInfo?> GetUserWithId(Guid? userId, CancellationToken cancellationToken = default);
|
||||||
string password,
|
|
||||||
bool createLongTermSession,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
);
|
|
||||||
public Task<UserInfo?> GetUserWithId(
|
|
||||||
Guid? userId,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
);
|
|
||||||
public Task<Session> CreateStrongSession(Guid userId);
|
public Task<Session> CreateStrongSession(Guid userId);
|
||||||
public Task<Session> CreateWeakSession(Guid userId);
|
public Task<Session> CreateWeakSession(Guid userId);
|
||||||
public Task<Session?> GetSession(string sessionId);
|
public Task<Session?> GetSession(string sessionId);
|
||||||
public Task DeleteSession(string sessionId);
|
public Task DeleteSession(string sessionId);
|
||||||
|
}
|
||||||
public Task<UserAndSession> CreateUserWithCredentials(string username,
|
|
||||||
string password,
|
|
||||||
string signupCode,
|
|
||||||
bool createLongTermSession,
|
|
||||||
CancellationToken cancellationToken = default);
|
|
||||||
|
|
||||||
public Task AddSignupCode(
|
|
||||||
string code,
|
|
||||||
string recipientName,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
);
|
|
||||||
|
|
||||||
public Task<ICollection<SignupCodeDto>> GetSignupCodes(
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public record UserAndSession(UserInfo User, Session Session);
|
|
|
@ -1,10 +1,6 @@
|
||||||
using Femto.Common.Domain;
|
|
||||||
using Femto.Common.Infrastructure.Outbox;
|
using Femto.Common.Infrastructure.Outbox;
|
||||||
using Femto.Modules.Auth.Models;
|
using Femto.Modules.Auth.Models;
|
||||||
using MediatR;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace Femto.Modules.Auth.Data;
|
namespace Femto.Modules.Auth.Data;
|
||||||
|
|
||||||
|
@ -21,43 +17,4 @@ internal class AuthContext(DbContextOptions<AuthContext> options) : DbContext(op
|
||||||
builder.HasDefaultSchema("authn");
|
builder.HasDefaultSchema("authn");
|
||||||
builder.ApplyConfigurationsFromAssembly(typeof(AuthContext).Assembly);
|
builder.ApplyConfigurationsFromAssembly(typeof(AuthContext).Assembly);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int SaveChanges()
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Use SaveChangesAsync instead");
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
await EmitDomainEvents(cancellationToken);
|
|
||||||
|
|
||||||
return await base.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task EmitDomainEvents(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var logger = this.GetService<ILogger<AuthContext>>();
|
|
||||||
var publisher = this.GetService<IPublisher>();
|
|
||||||
var domainEvents = this
|
|
||||||
.ChangeTracker.Entries<Entity>()
|
|
||||||
.SelectMany(e =>
|
|
||||||
{
|
|
||||||
var events = e.Entity.DomainEvents;
|
|
||||||
e.Entity.ClearDomainEvents();
|
|
||||||
return events;
|
|
||||||
})
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
logger.LogTrace("loaded {Count} domain events", domainEvents.Count);
|
|
||||||
|
|
||||||
foreach (var domainEvent in domainEvents)
|
|
||||||
{
|
|
||||||
logger.LogTrace(
|
|
||||||
"publishing {Type} domain event {Id}",
|
|
||||||
domainEvent.GetType().Name,
|
|
||||||
domainEvent.EventId
|
|
||||||
);
|
|
||||||
await publisher.Publish(domainEvent, cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,23 +0,0 @@
|
||||||
using Femto.Modules.Auth.Data;
|
|
||||||
using MediatR;
|
|
||||||
|
|
||||||
namespace Femto.Modules.Auth.Infrastructure;
|
|
||||||
|
|
||||||
internal class SaveChangesPipelineBehaviour<TRequest, TResponse>(AuthContext context)
|
|
||||||
: IPipelineBehavior<TRequest, TResponse>
|
|
||||||
where TRequest : notnull
|
|
||||||
{
|
|
||||||
public async Task<TResponse> Handle(
|
|
||||||
TRequest request,
|
|
||||||
RequestHandlerDelegate<TResponse> next,
|
|
||||||
CancellationToken cancellationToken
|
|
||||||
)
|
|
||||||
{
|
|
||||||
var response = await next(cancellationToken);
|
|
||||||
|
|
||||||
if (context.ChangeTracker.HasChanges())
|
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue