Files
Govor/Govor.Application/Users/UserSessions/Crypto/SessionKeyAttacher.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

72 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 SessionKeyAttacher : ISessionKeyAttacher
{
private readonly ILogger<SessionKeyAttacher> _logger;
private readonly ICurrentUserService _current;
private readonly GovorDbContext _context;
public SessionKeyAttacher(ILogger<SessionKeyAttacher> logger, ICurrentUserService current, GovorDbContext context)
{
_logger = logger;
_current = current;
_context = context;
}
public async Task AttachKeysAsync(
Guid sessionId,
byte[] identityKey,
byte[] signedPreKey,
byte[] signedPreKeySignature,
IEnumerable<byte[]> oneTimePreKeys)
{
var session = await _context.UserSessions
.Include(s => s.CryptoSession)
.ThenInclude(c => c.OneTimePreKeys)
.FirstOrDefaultAsync(s => s.Id == sessionId);
if (session == null)
throw new ArgumentException("Session not found", nameof(sessionId));
if (session.CryptoSession != null)
throw new InvalidOperationException("Keys are already attached to this session");
if(session.UserId != _current.GetCurrentUserId())
throw new InvalidOperationException("You cannot attach keys to this session");
var cryptoSessionId = Guid.NewGuid();
var cryptoSession = new UserCryptoSession
{
Id = cryptoSessionId,
UserSessionId = sessionId,
PublicIdentityKey = identityKey,
SignedPreKey = new SignedPreKey()
{
Id = Guid.NewGuid(),
UserCryptoSessionId = cryptoSessionId,
PublicSignedPreKey = signedPreKey,
SignedPreKeySignature = signedPreKeySignature,
},
OneTimePreKeys = oneTimePreKeys.Select(key => new OneTimePreKey
{
Id = Guid.NewGuid(),
PublicKey = key,
IsUsed = false,
UploadedAt = DateTime.UtcNow
}).ToList()
};
session.CryptoSession = cryptoSession;
await _context.SaveChangesAsync();
_logger.LogInformation("Attached keys to session {SessionId}", sessionId);
}
}