Add networking foundation — 9-project C# solution (compiles clean)
Implements the modular networking layer for SOR Server: - SOR.Networking.Abstractions: pure interfaces (ITransport, IClientConnection, IMessageSerializer, IMessageDispatcher, IMessageHandler, IEventBus, IClientPush) - SOR.Networking.WebSocket: ITransport + IClientConnection over System.Net.WebSockets - SOR.Networking.Nats: IEventBus over NATS.Net (INatsConnection) - SOR.Networking.Redis: IClientPush over Redis Pub/Sub - SOR.Shared: ServiceResult<T>, RedisKeys, NatsSubjects, ServerMessage, JSON serializer - SOR.Shared.Proto: auth.proto with gRPC AuthService contract - SOR.Gateway: accept loop, auth handshake, dispatcher, Redis push subscriber - SOR.Auth: stub gRPC handler (placeholder responses) - SOR.Host: multi-service runner via ENABLED_SERVICES env var; wires all DI All concrete implementations are wired exclusively in SOR.Host — every transport, broker, and push implementation can be swapped by changing only Program.cs. dotnet build SOR.slnx → 0 errors, 0 warnings Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
namespace SOR.Gateway.Auth;
|
||||
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SOR.Shared.Proto;
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
public sealed class GatewayAuthValidator : IGatewayAuthValidator
|
||||
{
|
||||
// AuthService.AuthServiceClient is the generated client stub from auth.proto.
|
||||
// It wraps a gRPC channel and provides typed RPC methods (ValidateTokenAsync, etc.).
|
||||
private readonly AuthService.AuthServiceClient _grpcClient;
|
||||
private readonly ILogger<GatewayAuthValidator> _logger;
|
||||
|
||||
public GatewayAuthValidator(
|
||||
AuthService.AuthServiceClient grpcClient,
|
||||
ILogger<GatewayAuthValidator> logger)
|
||||
{
|
||||
_grpcClient = grpcClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<(bool Valid, string UserId, string DisplayName)> ValidateAsync(
|
||||
string sessionToken,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Call the Auth service's ValidateToken RPC.
|
||||
// This is a synchronous RPC (request → response) over gRPC.
|
||||
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)
|
||||
{
|
||||
// gRPC exception: Auth service is unreachable, overloaded, or returned an error.
|
||||
// Treat as invalid — never accept a connection that cannot be validated.
|
||||
_logger.LogWarning(ex,
|
||||
"Auth gRPC call failed during token validation: {Status}", ex.Status.StatusCode);
|
||||
return (false, string.Empty, string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user