namespace SOR.Gateway.Auth; using Microsoft.Extensions.Logging; using SOR.Shared.Proto; /// /// IGatewayAuthValidator implementation that calls the Auth gRPC service. /// /// This is the only place in SOR.Gateway where the generated gRPC client type appears. /// All other Gateway code uses IGatewayAuthValidator (the interface). /// /// The gRPC client (AuthService.AuthServiceClient) is injected via DI. /// It is registered in ServiceRegistration.cs using AddGrpcClient{T}(), which /// integrates gRPC client lifetime management with ASP.NET Core's HttpClientFactory. /// /// Lifetime: Singleton (gRPC client factory handles connection pooling internally). /// public sealed class GatewayAuthValidator : IGatewayAuthValidator { private readonly AuthService.AuthServiceClient _grpcClient; private readonly ILogger _logger; public GatewayAuthValidator( AuthService.AuthServiceClient grpcClient, ILogger logger) { _grpcClient = grpcClient; _logger = logger; } /// public async Task<(bool Valid, string UserId, string DisplayName)> ValidateAsync( string sessionToken, CancellationToken cancellationToken = default) { try { var response = await _grpcClient.ValidateTokenAsync( new ValidateTokenRequest { SessionToken = sessionToken }, cancellationToken: cancellationToken); if (!response.Valid) { _logger.LogDebug("Token validation failed: token is invalid or expired"); return (false, string.Empty, string.Empty); } return (true, response.UserId, response.DisplayName); } catch (Grpc.Core.RpcException ex) { _logger.LogWarning(ex, "Auth gRPC call failed during token validation: {Status}", ex.Status.StatusCode); return (false, string.Empty, string.Empty); } } /// public async Task<(bool Success, string ErrorCode, string SessionToken, string UserId, string DisplayName)> LoginAsync(string email, string password, CancellationToken cancellationToken = default) { try { var response = await _grpcClient.LoginAsync( new LoginRequest { Email = email, Password = password }, cancellationToken: cancellationToken); if (!response.Success) return (false, response.ErrorCode, string.Empty, string.Empty, string.Empty); // Token is returned; fetch display name via ValidateToken (it includes it). var validate = await ValidateAsync(response.SessionToken, cancellationToken); return (true, string.Empty, response.SessionToken, validate.UserId, validate.DisplayName); } catch (Grpc.Core.RpcException ex) { _logger.LogWarning(ex, "Auth gRPC call failed during login: {Status}", ex.Status.StatusCode); return (false, "AUTH_SERVICE_UNAVAILABLE", string.Empty, string.Empty, string.Empty); } } /// public async Task<(bool Success, string ErrorCode, string SessionToken, string UserId, string DisplayName)> RegisterAsync(string email, string password, string displayName, CancellationToken cancellationToken = default) { try { var response = await _grpcClient.RegisterAsync( new RegisterRequest { Email = email, Password = password, DisplayName = displayName }, cancellationToken: cancellationToken); if (!response.Success) return (false, response.ErrorCode, string.Empty, string.Empty, string.Empty); var validate = await ValidateAsync(response.SessionToken, cancellationToken); return (true, string.Empty, response.SessionToken, validate.UserId, validate.DisplayName); } catch (Grpc.Core.RpcException ex) { _logger.LogWarning(ex, "Auth gRPC call failed during register: {Status}", ex.Status.StatusCode); return (false, "AUTH_SERVICE_UNAVAILABLE", string.Empty, string.Empty, string.Empty); } } }