misc
This commit is contained in:
parent
fbc6f562e3
commit
d7e0c59559
31 changed files with 249 additions and 57 deletions
|
@ -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;
|
||||
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.Login;
|
||||
|
||||
public record LoginCommand(string Username, string Password) : ICommand<LoginResult>;
|
|
@ -0,0 +1,28 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
using Femto.Modules.Auth.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Femto.Modules.Auth.Application.Interface.Login;
|
||||
|
||||
internal class LoginCommandHandler(AuthContext context)
|
||||
: ICommandHandler<LoginCommand, LoginResult>
|
||||
{
|
||||
public async Task<LoginResult> Handle(LoginCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var user = await context.Users.SingleOrDefaultAsync(
|
||||
u => u.Username == request.Username,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
if (user is null)
|
||||
throw new DomainError("invalid credentials");
|
||||
|
||||
if (!user.HasPassword(request.Password))
|
||||
throw new DomainError("invalid credentials");
|
||||
|
||||
var session = user.StartNewSession();
|
||||
|
||||
return new(new Session(session.Id, session.Expires), 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<RegisterResult>;
|
|
@ -0,0 +1,35 @@
|
|||
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, RegisterResult>
|
||||
{
|
||||
public async Task<RegisterResult> 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 user = new UserIdentity(request.Username);
|
||||
|
||||
user.SetPassword(request.Password);
|
||||
|
||||
var session = user.StartNewSession();
|
||||
|
||||
await context.AddAsync(user, cancellationToken);
|
||||
|
||||
code.Redeem(user.Id);
|
||||
|
||||
return new(new Session(session.Id, session.Expires), new UserInfo(user));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
|
||||
namespace Femto.Modules.Auth.Application.Interface.ValidateSession;
|
||||
|
||||
/// <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>;
|
|
@ -0,0 +1,34 @@
|
|||
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.Interface.ValidateSession;
|
||||
|
||||
internal class ValidateSessionCommandHandler(AuthContext context)
|
||||
: ICommandHandler<ValidateSessionCommand, ValidateSessionResult>
|
||||
{
|
||||
public async Task<ValidateSessionResult> Handle(
|
||||
ValidateSessionCommand request,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var user = await context.Users.SingleOrDefaultAsync(
|
||||
u => u.Sessions.Any(s => s.Id == request.SessionId && s.Expires > now),
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
if (user is null)
|
||||
throw new InvalidSessionError();
|
||||
|
||||
var session = user.PossiblyRefreshSession(request.SessionId);
|
||||
|
||||
return new ValidateSessionResult(
|
||||
new Session(session.Id, session.Expires),
|
||||
new UserInfo(user)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue