mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
6d1c53beeb
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.
109 lines
3.7 KiB
C#
109 lines
3.7 KiB
C#
using Govor.Application.Friends;
|
|
using Govor.Application.Infrastructure.Extensions;
|
|
using Govor.Application.Users.UserSessions.Crypto;
|
|
using Govor.Contracts.Requests;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Govor.API.Controllers.Authentication;
|
|
|
|
[RequireHttps]
|
|
[ApiController]
|
|
[Route("api/session")]
|
|
[Authorize(Roles = "Admin, User")]
|
|
public class SessionKeysController : Controller
|
|
{
|
|
private readonly ILogger<SessionKeysController> _logger;
|
|
private readonly IFriendshipService _friendshipService;
|
|
private readonly ICurrentUserSessionService _currentSession;
|
|
private readonly ISessionKeyAttacher _sessionKeyAttacher;
|
|
private readonly ISessionKeysReader _sessionKeysReader;
|
|
private readonly IOneTimePreKeysRotator _oneTimePreKeysRotator;
|
|
private readonly ICurrentUserService _currentUser;
|
|
|
|
public SessionKeysController(
|
|
ILogger<SessionKeysController> logger,
|
|
IFriendshipService friendshipService,
|
|
ICurrentUserSessionService currentSession,
|
|
ISessionKeyAttacher sessionKeyAttacher,
|
|
ISessionKeysReader sessionKeysReader,
|
|
ICurrentUserService currentUser)
|
|
{
|
|
_logger = logger;
|
|
_friendshipService = friendshipService;
|
|
_currentSession = currentSession;
|
|
_sessionKeyAttacher = sessionKeyAttacher;
|
|
_sessionKeysReader = sessionKeysReader;
|
|
_currentUser = currentUser;
|
|
}
|
|
|
|
[HttpPost("keys")]
|
|
public async Task<IActionResult> UploadSessionKeys([FromBody] UploadKeysRequest request)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
if(request.OneTimePreKeys.Count > 100)
|
|
return BadRequest("Too many one time pre keys");
|
|
|
|
var sessionId = _currentSession.GetUserSessionId();
|
|
|
|
await _sessionKeyAttacher.AttachKeysAsync(sessionId,
|
|
request.IdentityKey,
|
|
request.SignedPreKey,
|
|
request.SignedPreKeySignature,
|
|
request.OneTimePreKeys);
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[HttpGet("users/{userId}/keys")]
|
|
public async Task<IActionResult> GetUserPublicKeys(Guid userId)
|
|
{
|
|
var requesterId = _currentUser.GetCurrentUserId();
|
|
|
|
if (!(await _friendshipService.GetFriendsAsync(userId)).Select(u => u.Id).Contains(requesterId))
|
|
return Forbid("You can only access keys of your friends");
|
|
|
|
var keys = await _sessionKeysReader.GetAllActiveKeysAsync(userId);
|
|
|
|
return Ok(keys);
|
|
}
|
|
|
|
[HttpPost("keys/rotate")]
|
|
public async Task<IActionResult> RotateOneTimePreKeys([FromBody] RotateOneTimePreKeysRequest request)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
if (request.NewOneTimePreKeys.Count > 100)
|
|
return BadRequest("Too many new one time pre keys");
|
|
|
|
var sessionId = _currentSession.GetUserSessionId();
|
|
|
|
await _oneTimePreKeysRotator.RotateOneTimePreKeysAsync(sessionId, request.NewOneTimePreKeys);
|
|
|
|
return Ok("One-Time PreKeys rotated successfully.");
|
|
}
|
|
|
|
[HttpGet("keys/remaining")]
|
|
public async Task<IActionResult> GetRemainingOneTimePreKeysCount()
|
|
{
|
|
var sessionId = _currentSession.GetUserSessionId();
|
|
|
|
var count = await _sessionKeysReader.GetRemainingOneTimePreKeysCountAsync(sessionId);
|
|
|
|
return Ok(new { remaining = count });
|
|
}
|
|
|
|
[HttpPost("keys/{preKeyId}/used")]
|
|
public async Task<IActionResult> MarkOneTimePreKeyAsUsed([FromRoute] Guid preKeyId)
|
|
{
|
|
var sessionId = _currentSession.GetUserSessionId();
|
|
|
|
await _oneTimePreKeysRotator.MarkOneTimePreKeyAsUsedAsync(sessionId, preKeyId);
|
|
|
|
return Ok("Marked as used.");
|
|
}
|
|
|
|
} |