Modular Message Service

This commit is contained in:
dingo
2025-11-10 13:18:13 -05:00
parent a45cc41f4a
commit 53a8ebb1d6
5 changed files with 200 additions and 91 deletions
+114
View File
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Server.Networking;
using Server.Networking.Handling;
using Server.Networking.Messages;
namespace Server.Services.Auth;
public sealed class AuthHandler : IClientMessageHandler
{
private readonly ILogger<AuthHandler> _log;
private readonly IAuthService _authService;
private readonly ISessionRegistry _sessions;
private readonly IMessageSerializer _serializer;
private readonly HandlerContext _ctx;
public AuthHandler(
ILogger<AuthHandler> log,
IAuthService authService,
ISessionRegistry sessions,
IMessageSerializer serializer)
{
_log = log;
_authService = authService;
_sessions = sessions;
_serializer = serializer;
_ctx = new HandlerContext(serializer);
}
public IReadOnlyCollection<int> SupportedOpCodes => new[]
{
OpCodes.AUTH_REGISTER,
OpCodes.AUTH_LOGIN,
OpCodes.AUTH_WITH_TOKEN
};
public async Task HandleAsync(int opCode, IConnection connection, ReadOnlyMemory<byte> payload, CancellationToken ct)
{
switch (opCode)
{
case OpCodes.AUTH_REGISTER:
await HandleRegisterAsync(connection, payload, ct);
break;
case OpCodes.AUTH_LOGIN:
await HandleLoginAsync(connection, payload, ct);
break;
case OpCodes.AUTH_WITH_TOKEN:
await HandleAuthWithTokenAsync(connection, payload, ct);
break;
default:
_log.LogWarning("AuthHandler received unsupported opcode {Op}", opCode);
break;
}
}
private async Task HandleRegisterAsync(IConnection connection, ReadOnlyMemory<byte> payload, CancellationToken ct)
{
var request = _serializer.Deserialize<AuthRegisterRequest>(payload);
var (ok, error) = await _authService.RegisterAsync(request.Username, request.Password, request.Email, ct);
string? uiMessage = error switch
{
"invalid_username" => "Username must be 3–32 characters.",
"weak_password" => "Password must be at least 8 characters.",
"username_taken" => "That username is already in use.",
"email_taken" => "That email is already registered.",
_ => null
};
await _ctx.ReplyObjectAsync(connection, OpCodes.AUTH_REGISTER_RESULT,
new AuthRegisterResponse(ok, error, uiMessage), ct);
}
private async Task HandleLoginAsync(IConnection connection, ReadOnlyMemory<byte> payload, CancellationToken ct)
{
var request = _serializer.Deserialize<AuthLoginRequest>(payload);
var ip = connection.RemoteEndPoint?.ToString();
var userAgent = "gameclient";
var (ok, error, token, accountId) =
await _authService.LoginAsync(request.UsernameOrEmail, request.Password, ip, userAgent, TimeSpan.FromHours(24), ct);
if (ok && token is not null)
{
_sessions.Bind(connection.Id, accountId, token);
_log.LogInformation("Account {AccountId} logged in on {Conn}", accountId, connection.Id);
}
// If you want to include BannedUntilUtc, extend the service to return it
await _ctx.ReplyObjectAsync(connection, OpCodes.AUTH_LOGIN_RESULT,
new AuthLoginResponse(ok, error, token, accountId, null), ct);
}
private async Task HandleAuthWithTokenAsync(IConnection connection, ReadOnlyMemory<byte> payload, CancellationToken ct)
{
var request = _serializer.Deserialize<AuthWithTokenRequest>(payload);
var (ok, error, accountId) = await _authService.ValidateTokenAsync(request.Token, ct);
if (ok)
{
_sessions.Bind(connection.Id, accountId, request.Token);
_log.LogInformation("Account {AccountId} authenticated via token on {Conn}", accountId, connection.Id);
}
await _ctx.ReplyObjectAsync(connection, OpCodes.AUTH_WITH_TOKEN_RESULT,
new AuthWithTokenResponse(ok, error, accountId), ct);
}
}