Switch Authentication to asynchronous streaming calls (#16)

* add base grpc service and swap auth service to streaming

* remove Authorize from hub itself

* remove unused usings

* heave files server to net 7, add exception handling in grpc auth stream

Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
This commit is contained in:
rootdarkarchon
2022-10-13 16:55:23 +02:00
committed by GitHub
parent d37c1208fe
commit c98e2b2dd6
20 changed files with 313 additions and 159 deletions

View File

@@ -4,7 +4,7 @@ using System.Threading.Tasks;
namespace MareSynchronosServices.Authentication;
public class FailedAuthorization : IDisposable
internal class FailedAuthorization : IDisposable
{
private int failedAttempts = 1;
public int FailedAttempts => failedAttempts;

View File

@@ -14,7 +14,7 @@ using Microsoft.Extensions.Logging;
namespace MareSynchronosServices.Authentication;
public class SecretKeyAuthenticationHandler
internal class SecretKeyAuthenticationHandler
{
private readonly ILogger<SecretKeyAuthenticationHandler> logger;
private readonly MareMetrics metrics;
@@ -60,7 +60,7 @@ public class SecretKeyAuthenticationHandler
if (string.IsNullOrEmpty(secretKey))
{
metrics.IncCounter(MetricsAPI.CounterAuthenticationFailures);
return new AuthReply() { Success = false, Uid = string.Empty };
return new AuthReply() { Success = false, Uid = new UidMessage() { Uid = string.Empty } };
}
lock (failedAuthLock)
@@ -86,7 +86,7 @@ public class SecretKeyAuthenticationHandler
}, token);
logger.LogWarning("TempBan {ip} for authorization spam", ip);
return new AuthReply() { Success = false, Uid = string.Empty };
return new AuthReply() { Success = false, Uid = new UidMessage() { Uid = string.Empty } };
}
}
@@ -115,7 +115,7 @@ public class SecretKeyAuthenticationHandler
}
}
return new AuthReply() { Success = false, Uid = string.Empty };
return new AuthReply() { Success = false, Uid = new UidMessage() { Uid = string.Empty } };
}
metrics.IncCounter(MetricsAPI.CounterAuthenticationCacheHits);
@@ -152,7 +152,7 @@ public class SecretKeyAuthenticationHandler
}
metrics.IncCounter(MetricsAPI.CounterAuthenticationFailures);
return new AuthReply() { Success = false, Uid = string.Empty };
return new AuthReply() { Success = false, Uid = new UidMessage() { Uid = string.Empty } };
}
lock (authDictLock)
@@ -163,7 +163,7 @@ public class SecretKeyAuthenticationHandler
metrics.IncCounter(MetricsAPI.CounterAuthenticationSuccesses);
return new AuthReply() { Success = true, Uid = uid };
return new AuthReply() { Success = true, Uid = new UidMessage() { Uid = uid } };
}
public SecretKeyAuthenticationHandler(IConfiguration configuration, ILogger<SecretKeyAuthenticationHandler> logger, MareMetrics metrics)

View File

@@ -16,7 +16,7 @@ using System.Threading.Tasks;
namespace MareSynchronosServices;
public class CleanupService : IHostedService, IDisposable
internal class CleanupService : IHostedService, IDisposable
{
private readonly MareMetrics metrics;
private readonly SecretKeyAuthenticationHandler _authService;

View File

@@ -1,6 +1,5 @@
using MareSynchronosShared.Protos;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
@@ -101,10 +100,10 @@ internal class IdentityHandler
{
identChanges[serverId] = new ConcurrentQueue<IdentChange>();
}
}
internal record ServerIdentity
{
public string ServerId { get; set; } = string.Empty;
public string CharacterIdent { get; set; } = string.Empty;
internal record ServerIdentity
{
public string ServerId { get; set; } = string.Empty;
public string CharacterIdent { get; set; } = string.Empty;
}
}

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace MareSynchronosServices.Services;
public class AuthenticationService : AuthService.AuthServiceBase
internal class AuthenticationService : AuthService.AuthServiceBase
{
private readonly ILogger<AuthenticationService> _logger;
private readonly MareDbContext _dbContext;
@@ -20,12 +20,16 @@ public class AuthenticationService : AuthService.AuthServiceBase
_authHandler = authHandler;
}
public override async Task<AuthReply> Authorize(AuthRequest request, ServerCallContext context)
public override async Task Authorize(IAsyncStreamReader<AuthRequest> requestStream, IServerStreamWriter<AuthReply> responseStream, ServerCallContext context)
{
return await _authHandler.AuthenticateAsync(_dbContext, request.Ip, request.SecretKey);
await foreach (var input in requestStream.ReadAllAsync(context.CancellationToken).ConfigureAwait(false))
{
var response = await _authHandler.AuthenticateAsync(_dbContext, input.Ip, input.SecretKey).ConfigureAwait(false);
await responseStream.WriteAsync(response, context.CancellationToken).ConfigureAwait(false);
}
}
public override Task<Empty> RemoveAuth(RemoveAuthRequest request, ServerCallContext context)
public override Task<Empty> RemoveAuth(UidMessage request, ServerCallContext context)
{
_logger.LogInformation("Removing Authentication for {uid}", request.Uid);
_authHandler.RemoveAuthentication(request.Uid);