This commit is contained in:
Stanley Dimant
2025-01-12 15:00:39 +01:00
parent 149b496ea7
commit 90b77d95d8
4 changed files with 15 additions and 11 deletions

View File

@@ -8,7 +8,7 @@ using MareSynchronosShared.Utils.Configuration;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using StackExchange.Redis.Extensions.Core.Abstractions; using StackExchange.Redis;
using System.Globalization; using System.Globalization;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims; using System.Security.Claims;
@@ -23,14 +23,14 @@ public abstract class AuthControllerBase : Controller
protected readonly IConfigurationService<AuthServiceConfiguration> Configuration; protected readonly IConfigurationService<AuthServiceConfiguration> Configuration;
protected readonly IDbContextFactory<MareDbContext> MareDbContextFactory; protected readonly IDbContextFactory<MareDbContext> MareDbContextFactory;
protected readonly SecretKeyAuthenticatorService SecretKeyAuthenticatorService; protected readonly SecretKeyAuthenticatorService SecretKeyAuthenticatorService;
private readonly IRedisDatabase _redis; private readonly IDatabase _redis;
private readonly GeoIPService _geoIPProvider; private readonly GeoIPService _geoIPProvider;
protected AuthControllerBase(ILogger logger, protected AuthControllerBase(ILogger logger,
IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContextFactory, IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContextFactory,
SecretKeyAuthenticatorService secretKeyAuthenticatorService, SecretKeyAuthenticatorService secretKeyAuthenticatorService,
IConfigurationService<AuthServiceConfiguration> configuration, IConfigurationService<AuthServiceConfiguration> configuration,
IRedisDatabase redisDb, GeoIPService geoIPProvider) IDatabase redisDb, GeoIPService geoIPProvider)
{ {
Logger = logger; Logger = logger;
HttpAccessor = accessor; HttpAccessor = accessor;
@@ -72,7 +72,7 @@ public abstract class AuthControllerBase : Controller
return Unauthorized("Your Mare account is banned from using the service."); return Unauthorized("Your Mare account is banned from using the service.");
} }
var existingIdent = await _redis.GetAsync<string>("UID:" + authResult.Uid); var existingIdent = await _redis.StringGetAsync("UID:" + authResult.Uid);
if (!string.IsNullOrEmpty(existingIdent)) if (!string.IsNullOrEmpty(existingIdent))
{ {
Logger.LogWarning("Authenticate:DUPLICATE:{id}:{ident}", authResult.Uid, charaIdent); Logger.LogWarning("Authenticate:DUPLICATE:{id}:{ident}", authResult.Uid, charaIdent);

View File

@@ -8,7 +8,7 @@ using MareSynchronosShared.Utils.Configuration;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using StackExchange.Redis.Extensions.Core.Abstractions; using StackExchange.Redis;
namespace MareSynchronosAuthService.Controllers; namespace MareSynchronosAuthService.Controllers;
@@ -19,7 +19,7 @@ public class JwtController : AuthControllerBase
IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContextFactory, IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContextFactory,
SecretKeyAuthenticatorService secretKeyAuthenticatorService, SecretKeyAuthenticatorService secretKeyAuthenticatorService,
IConfigurationService<AuthServiceConfiguration> configuration, IConfigurationService<AuthServiceConfiguration> configuration,
IRedisDatabase redisDb, GeoIPService geoIPProvider) IDatabase redisDb, GeoIPService geoIPProvider)
: base(logger, accessor, mareDbContextFactory, secretKeyAuthenticatorService, : base(logger, accessor, mareDbContextFactory, secretKeyAuthenticatorService,
configuration, redisDb, geoIPProvider) configuration, redisDb, geoIPProvider)
{ {

View File

@@ -9,7 +9,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using StackExchange.Redis; using StackExchange.Redis;
using StackExchange.Redis.Extensions.Core.Abstractions;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Globalization; using System.Globalization;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
@@ -30,7 +29,7 @@ public class OAuthController : AuthControllerBase
IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContext, IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContext,
SecretKeyAuthenticatorService secretKeyAuthenticatorService, SecretKeyAuthenticatorService secretKeyAuthenticatorService,
IConfigurationService<AuthServiceConfiguration> configuration, IConfigurationService<AuthServiceConfiguration> configuration,
IRedisDatabase redisDb, GeoIPService geoIPProvider) IDatabase redisDb, GeoIPService geoIPProvider)
: base(logger, accessor, mareDbContext, secretKeyAuthenticatorService, : base(logger, accessor, mareDbContext, secretKeyAuthenticatorService,
configuration, redisDb, geoIPProvider) configuration, redisDb, geoIPProvider)
{ {

View File

@@ -17,6 +17,7 @@ using MareSynchronosShared.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Prometheus; using Prometheus;
using MareSynchronosShared.Utils.Configuration; using MareSynchronosShared.Utils.Configuration;
using StackExchange.Redis.Extensions.Core.Abstractions;
namespace MareSynchronosAuthService; namespace MareSynchronosAuthService;
@@ -179,8 +180,10 @@ public class Startup
var endpoint = options.EndPoints[0]; var endpoint = options.EndPoints[0];
string address = ""; string address = "";
int port = 0; int port = 0;
if (endpoint is DnsEndPoint dnsEndPoint) { address = dnsEndPoint.Host; port = dnsEndPoint.Port; } if (endpoint is DnsEndPoint dnsEndPoint) { address = dnsEndPoint.Host; port = dnsEndPoint.Port; }
if (endpoint is IPEndPoint ipEndPoint) { address = ipEndPoint.Address.ToString(); port = ipEndPoint.Port; } if (endpoint is IPEndPoint ipEndPoint) { address = ipEndPoint.Address.ToString(); port = ipEndPoint.Port; }
/*
var redisConfiguration = new RedisConfiguration() var redisConfiguration = new RedisConfiguration()
{ {
AbortOnConnectFail = true, AbortOnConnectFail = true,
@@ -203,11 +206,13 @@ public class Startup
MaxValueLength = 1024, MaxValueLength = 1024,
PoolSize = mareConfig.GetValue(nameof(ServerConfiguration.RedisPool), 50), PoolSize = mareConfig.GetValue(nameof(ServerConfiguration.RedisPool), 50),
SyncTimeout = options.SyncTimeout, SyncTimeout = options.SyncTimeout,
}; };*/
var muxer = ConnectionMultiplexer.Connect(options);
var db = muxer.GetDatabase();
services.AddSingleton<IDatabase>(db);
_logger.LogInformation("Setting up Redis to connect to {host}:{port}", address, port); _logger.LogInformation("Setting up Redis to connect to {host}:{port}", address, port);
services.AddStackExchangeRedisExtensions<SystemTextJsonSerializer>(redisConfiguration);
} }
private void ConfigureConfigServices(IServiceCollection services) private void ConfigureConfigServices(IServiceCollection services)
{ {