mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-17 04:58:46 +00:00
Add E2EE messaging and VPS deployment
Add signed, encrypted messages and attachments with per-session device keys, conversation key discovery, and server-side validation. Extend CI to publish immutable container images and deploy to a VPS, with setup and protocol documentation.
This commit is contained in:
@@ -32,6 +32,7 @@ public static class ConfigurationProgramExtensions
|
||||
{
|
||||
public static void AddServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<Govor.Application.E2ee.E2eeService>();
|
||||
services.AddSingleton<IPasswordHasher, PasswordHasher>();
|
||||
services.AddSingleton<IUsernameValidator, UsernameValidator>();
|
||||
services.AddSingleton<IJwtTokenHasher, JwtTokenHasher>();
|
||||
|
||||
@@ -9,7 +9,8 @@ public static class ConfigurationSignalR
|
||||
{
|
||||
services.AddSignalR(options =>
|
||||
{
|
||||
options.MaximumReceiveMessageSize = 1_100_000;
|
||||
options.AddFilter<HubExceptionFilter>();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Security.Cryptography;
|
||||
using Govor.Application.E2ee;
|
||||
using Govor.Application.Infrastructure.Extensions;
|
||||
using Govor.Domain.Models.Messages;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Govor.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize(Roles = "User,Admin")]
|
||||
[Route("api/e2ee")]
|
||||
public sealed class E2eeController(E2eeService e2ee, ICurrentUserService user,
|
||||
ICurrentUserSessionService session) : ControllerBase
|
||||
{
|
||||
[HttpPut("device")]
|
||||
public async Task<IActionResult> Register(DeviceKey key)
|
||||
{
|
||||
try
|
||||
{
|
||||
await e2ee.RegisterAsync(user.GetCurrentUserId(), session.GetUserSessionId(), key);
|
||||
return Ok(new { registered = true });
|
||||
}
|
||||
catch (UnauthorizedAccessException) { return Forbid(); }
|
||||
catch (InvalidOperationException) { return Conflict("Device keys already exist for this session."); }
|
||||
catch (Exception ex) when (ex is CryptographicException or ArgumentException or FormatException)
|
||||
{ return BadRequest("Invalid E2EE public keys."); }
|
||||
}
|
||||
|
||||
[HttpGet("conversations/{type:int}/{conversationId:guid}/keys")]
|
||||
public async Task<IActionResult> Keys(int type, Guid conversationId)
|
||||
{
|
||||
if (type is < 0 or > 1 || conversationId == Guid.Empty) return BadRequest();
|
||||
try
|
||||
{
|
||||
return Ok(await e2ee.GetConversationKeysAsync(user.GetCurrentUserId(), conversationId, (RecipientType)type));
|
||||
}
|
||||
catch (UnauthorizedAccessException) { return Forbid(); }
|
||||
catch (Exception ex) when (ex is InvalidOperationException or CryptographicException or ArgumentException or FormatException)
|
||||
{ return Conflict("Every participant must have valid E2EE keys. Update clients and register their devices."); }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Govor.API.Common.Extensions;
|
||||
using Govor.Application.E2ee;
|
||||
using Govor.Application.Infrastructure.Extensions;
|
||||
using Govor.Application.Medias;
|
||||
using Govor.Contracts.Requests;
|
||||
@@ -49,6 +50,10 @@ public class MediaController : Controller
|
||||
if (string.IsNullOrWhiteSpace(request.MimeType))
|
||||
return BadRequest("Missing MIME type");
|
||||
|
||||
if (request.OwnerType == MediaOwnerType.Message &&
|
||||
(request.MimeType != E2eeProtocol.EncryptedMimeType || !string.IsNullOrEmpty(request.EncryptedKey)))
|
||||
return BadRequest("Message attachments must be encrypted on the client. Do not upload decryption keys.");
|
||||
|
||||
try
|
||||
{
|
||||
byte[] fileBytes = await ReadFileAsync(request.FromFile);
|
||||
|
||||
@@ -7,6 +7,7 @@ using Govor.Application.Infrastructure.Common;
|
||||
using Govor.Application.Messages;
|
||||
using Govor.Application.Reactions;
|
||||
using AutoMapper;
|
||||
using Govor.Application.E2ee;
|
||||
using Govor.Application.Messages.Parameters;
|
||||
using Govor.Contracts.Requests.SignalR;
|
||||
using Govor.Contracts.Responses.SignalR;
|
||||
@@ -241,6 +242,7 @@ public class ChatsHub : Hub
|
||||
RecipientId = result.OriginalMessage.RecipientId,
|
||||
RecipientType = result.OriginalMessage.RecipientType,
|
||||
NewEncryptedContent = request.NewEncryptedContent,
|
||||
ReplyToMessageId = result.OriginalMessage.ReplyToMessageId,
|
||||
EditedAt = editParams.EditedAt,
|
||||
};
|
||||
|
||||
@@ -294,7 +296,7 @@ public class ChatsHub : Hub
|
||||
{
|
||||
throw new ArgumentException("Message must contain content or media.");
|
||||
}
|
||||
if ((request.EncryptedContent?.Length ?? 0) > 50_000)
|
||||
if ((request.EncryptedContent?.Length ?? 0) > E2eeProtocol.MaxEnvelopeLength)
|
||||
{
|
||||
throw new ArgumentException("Message is too long.");
|
||||
}
|
||||
|
||||
@@ -210,6 +210,4 @@ app.MapSwagger()
|
||||
|
||||
app.Map("/", () => "Not for browsers");
|
||||
|
||||
app.Run();
|
||||
|
||||
public partial class Program { }
|
||||
app.Run();
|
||||
Reference in New Issue
Block a user