Files
Govor/Govor.Application/Users/UserSessions/Crypto/OneTimePreKeysRotator.cs
T
Artemy 6d1c53beeb Refactor: migrate Core -> Domain and reorganize projects
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
2026-07-16 19:27:45 +07:00

69 lines
2.4 KiB
C#

using Govor.Application.Infrastructure.Extensions;
using Govor.Domain.Models.Users.Crypto;
using Govor.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Govor.Application.Users.UserSessions.Crypto;
public class OneTimePreKeysRotator : IOneTimePreKeysRotator
{
private readonly ILogger<SessionKeyAttacher> _logger;
private readonly ICurrentUserService _current;
private readonly GovorDbContext _context;
public OneTimePreKeysRotator(ILogger<SessionKeyAttacher> logger, ICurrentUserService current, GovorDbContext context)
{
_logger = logger;
_current = current;
_context = context;
}
public async Task RotateOneTimePreKeysAsync(Guid sessionId, IEnumerable<byte[]> newOneTimePreKeys)
{
var cryptoSession = await _context.UserCryptoSessions
.Include(c => c.OneTimePreKeys)
.FirstOrDefaultAsync(c => c.UserSessionId == sessionId);
if (cryptoSession == null)
throw new ArgumentException("Crypto session not found", nameof(sessionId));
// Удаляем все использованные ключи
var usedKeys = cryptoSession.OneTimePreKeys.Where(k => k.IsUsed).ToList();
if (usedKeys.Any())
{
_context.OneTimePreKeys.RemoveRange(usedKeys);
}
// Добавляем новые ключи (возможно, стоит ограничить количество)
foreach (var key in newOneTimePreKeys)
{
cryptoSession.OneTimePreKeys.Add(new OneTimePreKey
{
Id = Guid.NewGuid(),
PublicKey = key,
IsUsed = false,
UploadedAt = DateTime.UtcNow
});
}
await _context.SaveChangesAsync();
}
public async Task MarkOneTimePreKeyAsUsedAsync(Guid sessionId, Guid oneTimePreKeyId)
{
var key = await _context.OneTimePreKeys
.Include(k => k.UserCryptoSession)
.FirstOrDefaultAsync(k => k.Id == oneTimePreKeyId && k.UserCryptoSession.UserSessionId == sessionId);
if (key == null)
throw new ArgumentException("One-Time PreKey not found for this session", nameof(oneTimePreKeyId));
if (key.IsUsed)
return; // Уже помечен
key.IsUsed = true;
await _context.SaveChangesAsync();
}
}