♻️ Refactored auth service for better security
This commit is contained in:
@@ -571,14 +571,14 @@ public class AccountCurrentController(
|
||||
.ToListAsync();
|
||||
|
||||
var challengeDevices = devices.Select(SnAuthClientWithChallenge.FromClient).ToList();
|
||||
var deviceIds = challengeDevices.Select(x => x.Id).ToList();
|
||||
var deviceIds = challengeDevices.Select(x => x.DeviceId).ToList();
|
||||
|
||||
var authChallenges = await db.AuthChallenges
|
||||
.Where(c => c.ClientId != null && deviceIds.Contains(c.ClientId.Value))
|
||||
.GroupBy(c => c.ClientId)
|
||||
.ToDictionaryAsync(c => c.Key!.Value, c => c.ToList());
|
||||
.Where(c => deviceIds.Contains(c.DeviceId))
|
||||
.GroupBy(c => c.DeviceId)
|
||||
.ToDictionaryAsync(c => c.Key, c => c.ToList());
|
||||
foreach (var challengeDevice in challengeDevices)
|
||||
if (authChallenges.TryGetValue(challengeDevice.Id, out var challenge))
|
||||
if (authChallenges.TryGetValue(challengeDevice.DeviceId, out var challenge))
|
||||
challengeDevice.Challenges = challenge;
|
||||
|
||||
return Ok(challengeDevices);
|
||||
@@ -688,7 +688,7 @@ public class AccountCurrentController(
|
||||
if (HttpContext.Items["CurrentUser"] is not SnAccount currentUser ||
|
||||
HttpContext.Items["CurrentSession"] is not SnAuthSession currentSession) return Unauthorized();
|
||||
|
||||
var device = await db.AuthClients.FirstOrDefaultAsync(d => d.Id == currentSession.Challenge.ClientId);
|
||||
var device = await db.AuthClients.FirstOrDefaultAsync(d => d.Id == currentSession.ClientId);
|
||||
if (device is null) return NotFound();
|
||||
|
||||
try
|
||||
|
||||
@@ -508,9 +508,7 @@ public class AccountService(
|
||||
|
||||
private async Task<bool> IsDeviceActive(Guid id)
|
||||
{
|
||||
return await db.AuthSessions
|
||||
.Include(s => s.Challenge)
|
||||
.AnyAsync(s => s.Challenge.ClientId == id);
|
||||
return await db.AuthSessions.AnyAsync(s => s.ClientId == id);
|
||||
}
|
||||
|
||||
public async Task<SnAuthClient> UpdateDeviceName(SnAccount account, string deviceId, string label)
|
||||
@@ -529,8 +527,7 @@ public class AccountService(
|
||||
public async Task DeleteSession(SnAccount account, Guid sessionId)
|
||||
{
|
||||
var session = await db.AuthSessions
|
||||
.Include(s => s.Challenge)
|
||||
.ThenInclude(s => s.Client)
|
||||
.Include(s => s.Client)
|
||||
.Where(s => s.Id == sessionId && s.AccountId == account.Id)
|
||||
.FirstOrDefaultAsync();
|
||||
if (session is null) throw new InvalidOperationException("Session was not found.");
|
||||
@@ -539,11 +536,11 @@ public class AccountService(
|
||||
db.AuthSessions.Remove(session);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
if (session.Challenge.ClientId.HasValue)
|
||||
if (session.ClientId.HasValue)
|
||||
{
|
||||
if (!await IsDeviceActive(session.Challenge.ClientId.Value))
|
||||
if (!await IsDeviceActive(session.ClientId.Value))
|
||||
await pusher.UnsubscribePushNotificationsAsync(new UnsubscribePushNotificationsRequest()
|
||||
{ DeviceId = session.Challenge.Client!.DeviceId }
|
||||
{ DeviceId = session.Client!.DeviceId }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -564,15 +561,13 @@ public class AccountService(
|
||||
);
|
||||
|
||||
var sessions = await db.AuthSessions
|
||||
.Include(s => s.Challenge)
|
||||
.Where(s => s.Challenge.ClientId == device.Id && s.AccountId == account.Id)
|
||||
.Where(s => s.ClientId == device.Id && s.AccountId == account.Id)
|
||||
.ToListAsync();
|
||||
|
||||
// The current session should be included in the sessions' list
|
||||
var now = SystemClock.Instance.GetCurrentInstant();
|
||||
await db.AuthSessions
|
||||
.Include(s => s.Challenge)
|
||||
.Where(s => s.Challenge.ClientId == device.Id)
|
||||
.Where(s => s.ClientId == device.Id)
|
||||
.ExecuteUpdateAsync(p => p.SetProperty(s => s.DeletedAt, s => now));
|
||||
|
||||
db.AuthClients.Remove(device);
|
||||
|
||||
Reference in New Issue
Block a user