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 GameServices.Auth; public sealed class AuthHandler : IClientMessageHandler { private readonly ILogger _log; private readonly IAuthService _authService; private readonly ISessionRegistry _sessions; private readonly IMessageSerializer _serializer; private readonly HandlerContext _ctx; public AuthHandler( ILogger log, IAuthService authService, ISessionRegistry sessions, IMessageSerializer serializer) { _log = log; _authService = authService; _sessions = sessions; _serializer = serializer; _ctx = new HandlerContext(serializer); } public IReadOnlyCollection SupportedOpCodes => new[] { OpCodes.AUTH_REGISTER, OpCodes.AUTH_LOGIN, OpCodes.AUTH_WITH_TOKEN }; public async Task HandleAsync(int opCode, IConnection connection, ReadOnlyMemory 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 payload, CancellationToken ct) { var request = _serializer.Deserialize(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 payload, CancellationToken ct) { var request = _serializer.Deserialize(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 payload, CancellationToken ct) { var request = _serializer.Deserialize(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); } }