use dbcontext factory?
use more dbcontext factories or something idk
This commit is contained in:
@@ -23,13 +23,14 @@ public class JwtController : Controller
|
|||||||
{
|
{
|
||||||
private readonly IHttpContextAccessor _accessor;
|
private readonly IHttpContextAccessor _accessor;
|
||||||
private readonly IRedisDatabase _redis;
|
private readonly IRedisDatabase _redis;
|
||||||
private readonly MareDbContext _mareDbContext;
|
private readonly IDbContextFactory<MareDbContext> _mareDbContextFactory;
|
||||||
private readonly GeoIPService _geoIPProvider;
|
private readonly GeoIPService _geoIPProvider;
|
||||||
private readonly SecretKeyAuthenticatorService _secretKeyAuthenticatorService;
|
private readonly SecretKeyAuthenticatorService _secretKeyAuthenticatorService;
|
||||||
private readonly AccountRegistrationService _accountRegistrationService;
|
private readonly AccountRegistrationService _accountRegistrationService;
|
||||||
private readonly IConfigurationService<AuthServiceConfiguration> _configuration;
|
private readonly IConfigurationService<AuthServiceConfiguration> _configuration;
|
||||||
|
|
||||||
public JwtController(IHttpContextAccessor accessor, MareDbContext mareDbContext,
|
public JwtController(ILogger<JwtController> logger,
|
||||||
|
IHttpContextAccessor accessor, IDbContextFactory<MareDbContext> mareDbContextFactory,
|
||||||
SecretKeyAuthenticatorService secretKeyAuthenticatorService,
|
SecretKeyAuthenticatorService secretKeyAuthenticatorService,
|
||||||
AccountRegistrationService accountRegistrationService,
|
AccountRegistrationService accountRegistrationService,
|
||||||
IConfigurationService<AuthServiceConfiguration> configuration,
|
IConfigurationService<AuthServiceConfiguration> configuration,
|
||||||
@@ -38,7 +39,7 @@ public class JwtController : Controller
|
|||||||
_accessor = accessor;
|
_accessor = accessor;
|
||||||
_redis = redisDb;
|
_redis = redisDb;
|
||||||
_geoIPProvider = geoIPProvider;
|
_geoIPProvider = geoIPProvider;
|
||||||
_mareDbContext = mareDbContext;
|
_mareDbContextFactory = mareDbContextFactory;
|
||||||
_secretKeyAuthenticatorService = secretKeyAuthenticatorService;
|
_secretKeyAuthenticatorService = secretKeyAuthenticatorService;
|
||||||
_accountRegistrationService = accountRegistrationService;
|
_accountRegistrationService = accountRegistrationService;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
@@ -51,18 +52,19 @@ public class JwtController : Controller
|
|||||||
if (string.IsNullOrEmpty(auth)) return BadRequest("No Authkey");
|
if (string.IsNullOrEmpty(auth)) return BadRequest("No Authkey");
|
||||||
if (string.IsNullOrEmpty(charaIdent)) return BadRequest("No CharaIdent");
|
if (string.IsNullOrEmpty(charaIdent)) return BadRequest("No CharaIdent");
|
||||||
|
|
||||||
|
using var dbContext = await _mareDbContextFactory.CreateDbContextAsync();
|
||||||
var ip = _accessor.GetIpAddress();
|
var ip = _accessor.GetIpAddress();
|
||||||
|
|
||||||
var authResult = await _secretKeyAuthenticatorService.AuthorizeAsync(ip, auth);
|
var authResult = await _secretKeyAuthenticatorService.AuthorizeAsync(ip, auth);
|
||||||
|
|
||||||
var isBanned = await _mareDbContext.BannedUsers.AsNoTracking().AnyAsync(u => u.CharacterIdentification == charaIdent).ConfigureAwait(false);
|
var isBanned = await dbContext.BannedUsers.AsNoTracking().AnyAsync(u => u.CharacterIdentification == charaIdent).ConfigureAwait(false);
|
||||||
if (isBanned)
|
if (isBanned)
|
||||||
{
|
{
|
||||||
var authToBan = _mareDbContext.Auth.SingleOrDefault(a => a.UserUID == authResult.Uid);
|
var authToBan = dbContext.Auth.SingleOrDefault(a => a.UserUID == authResult.Uid);
|
||||||
if (authToBan != null)
|
if (authToBan != null)
|
||||||
{
|
{
|
||||||
authToBan.IsBanned = true;
|
authToBan.IsBanned = true;
|
||||||
await _mareDbContext.SaveChangesAsync().ConfigureAwait(false);
|
await dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unauthorized("Your character is banned from using the service.");
|
return Unauthorized("Your character is banned from using the service.");
|
||||||
@@ -72,37 +74,37 @@ public class JwtController : Controller
|
|||||||
if (!authResult.Success && authResult.TempBan) return Unauthorized("Due to an excessive amount of failed authentication attempts you are temporarily banned. Check your Secret Key configuration and try connecting again in 5 minutes.");
|
if (!authResult.Success && authResult.TempBan) return Unauthorized("Due to an excessive amount of failed authentication attempts you are temporarily banned. Check your Secret Key configuration and try connecting again in 5 minutes.");
|
||||||
if (authResult.Permaban)
|
if (authResult.Permaban)
|
||||||
{
|
{
|
||||||
if (!_mareDbContext.BannedUsers.Any(c => c.CharacterIdentification == charaIdent))
|
if (!dbContext.BannedUsers.Any(c => c.CharacterIdentification == charaIdent))
|
||||||
{
|
{
|
||||||
_mareDbContext.BannedUsers.Add(new Banned()
|
dbContext.BannedUsers.Add(new Banned()
|
||||||
{
|
{
|
||||||
CharacterIdentification = charaIdent,
|
CharacterIdentification = charaIdent,
|
||||||
Reason = "Autobanned CharacterIdent (" + authResult.Uid + ")",
|
Reason = "Autobanned CharacterIdent (" + authResult.Uid + ")",
|
||||||
});
|
});
|
||||||
|
|
||||||
await _mareDbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
var lodestone = await _mareDbContext.LodeStoneAuth.Include(a => a.User).FirstOrDefaultAsync(c => c.User.UID == authResult.Uid);
|
var lodestone = await dbContext.LodeStoneAuth.Include(a => a.User).FirstOrDefaultAsync(c => c.User.UID == authResult.Uid);
|
||||||
|
|
||||||
if (lodestone != null)
|
if (lodestone != null)
|
||||||
{
|
{
|
||||||
if (!_mareDbContext.BannedRegistrations.Any(c => c.DiscordIdOrLodestoneAuth == lodestone.HashedLodestoneId))
|
if (!dbContext.BannedRegistrations.Any(c => c.DiscordIdOrLodestoneAuth == lodestone.HashedLodestoneId))
|
||||||
{
|
{
|
||||||
_mareDbContext.BannedRegistrations.Add(new BannedRegistrations()
|
dbContext.BannedRegistrations.Add(new BannedRegistrations()
|
||||||
{
|
{
|
||||||
DiscordIdOrLodestoneAuth = lodestone.HashedLodestoneId,
|
DiscordIdOrLodestoneAuth = lodestone.HashedLodestoneId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!_mareDbContext.BannedRegistrations.Any(c => c.DiscordIdOrLodestoneAuth == lodestone.DiscordId.ToString()))
|
if (!dbContext.BannedRegistrations.Any(c => c.DiscordIdOrLodestoneAuth == lodestone.DiscordId.ToString()))
|
||||||
{
|
{
|
||||||
_mareDbContext.BannedRegistrations.Add(new BannedRegistrations()
|
dbContext.BannedRegistrations.Add(new BannedRegistrations()
|
||||||
{
|
{
|
||||||
DiscordIdOrLodestoneAuth = lodestone.DiscordId.ToString(),
|
DiscordIdOrLodestoneAuth = lodestone.DiscordId.ToString(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await _mareDbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unauthorized("You are permanently banned.");
|
return Unauthorized("You are permanently banned.");
|
||||||
|
|||||||
@@ -11,13 +11,13 @@ namespace MareSynchronosShared.RequirementHandlers;
|
|||||||
|
|
||||||
public class UserRequirementHandler : AuthorizationHandler<UserRequirement, HubInvocationContext>
|
public class UserRequirementHandler : AuthorizationHandler<UserRequirement, HubInvocationContext>
|
||||||
{
|
{
|
||||||
private readonly MareDbContext _dbContext;
|
private readonly IDbContextFactory<MareDbContext> _dbContextFactory;
|
||||||
private readonly ILogger<UserRequirementHandler> _logger;
|
private readonly ILogger<UserRequirementHandler> _logger;
|
||||||
private readonly IRedisDatabase _redis;
|
private readonly IRedisDatabase _redis;
|
||||||
|
|
||||||
public UserRequirementHandler(MareDbContext dbContext, ILogger<UserRequirementHandler> logger, IRedisDatabase redisDb)
|
public UserRequirementHandler(IDbContextFactory<MareDbContext> dbContextFactory, ILogger<UserRequirementHandler> logger, IRedisDatabase redisDb)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContextFactory = dbContextFactory;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_redis = redisDb;
|
_redis = redisDb;
|
||||||
}
|
}
|
||||||
@@ -36,14 +36,16 @@ public class UserRequirementHandler : AuthorizationHandler<UserRequirement, HubI
|
|||||||
|
|
||||||
if ((requirement.Requirements & UserRequirements.Administrator) is UserRequirements.Administrator)
|
if ((requirement.Requirements & UserRequirements.Administrator) is UserRequirements.Administrator)
|
||||||
{
|
{
|
||||||
var user = await _dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false);
|
using var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false);
|
||||||
|
var user = await dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false);
|
||||||
if (user == null || !user.IsAdmin) context.Fail();
|
if (user == null || !user.IsAdmin) context.Fail();
|
||||||
_logger.LogInformation("Admin {uid} authenticated", uid);
|
_logger.LogInformation("Admin {uid} authenticated", uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((requirement.Requirements & UserRequirements.Moderator) is UserRequirements.Moderator)
|
if ((requirement.Requirements & UserRequirements.Moderator) is UserRequirements.Moderator)
|
||||||
{
|
{
|
||||||
var user = await _dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false);
|
using var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false);
|
||||||
|
var user = await dbContext.Users.AsNoTracking().SingleOrDefaultAsync(b => b.UID == uid).ConfigureAwait(false);
|
||||||
if (user == null || !user.IsAdmin && !user.IsModerator) context.Fail();
|
if (user == null || !user.IsAdmin && !user.IsModerator) context.Fail();
|
||||||
_logger.LogInformation("Admin/Moderator {uid} authenticated", uid);
|
_logger.LogInformation("Admin/Moderator {uid} authenticated", uid);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user