From 8a756489789a1f5bf7ce6e31ac4bb867e38ee52d Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 20 Jun 2025 21:15:16 +0200 Subject: Oops, fix project path for federation test server --- .../Controllers/FederationKeysController.cs | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Utilities/LibMatrix.FederationTest/Controllers/FederationKeysController.cs (limited to 'Utilities/LibMatrix.FederationTest/Controllers/FederationKeysController.cs') diff --git a/Utilities/LibMatrix.FederationTest/Controllers/FederationKeysController.cs b/Utilities/LibMatrix.FederationTest/Controllers/FederationKeysController.cs new file mode 100644 index 0000000..33d0b99 --- /dev/null +++ b/Utilities/LibMatrix.FederationTest/Controllers/FederationKeysController.cs @@ -0,0 +1,41 @@ +using LibMatrix.Federation.Extensions; +using LibMatrix.Federation.Utilities; +using LibMatrix.FederationTest.Services; +using LibMatrix.Homeservers; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.FederationTest.Controllers; + +[ApiController] +[Route("_matrix/key/v2/")] +public class FederationKeysController(FederationTestConfiguration config, FederationKeyStore keyStore) { + static FederationKeysController() { + Console.WriteLine("INFO | FederationKeysController initialized."); + } + + private static SignedObject? _cachedServerKeysResponse; + private static SemaphoreSlim _serverKeyCacheLock = new SemaphoreSlim(1, 1); + + [HttpGet("server")] + public async Task> GetServerKeys() { + await _serverKeyCacheLock.WaitAsync(); + if (_cachedServerKeysResponse == null || _cachedServerKeysResponse.TypedContent.ValidUntil < DateTime.Now + TimeSpan.FromSeconds(30)) { + var keys = keyStore.GetCurrentSigningKey(); + _cachedServerKeysResponse = new ServerKeysResponse() { + ValidUntil = DateTime.Now + TimeSpan.FromMinutes(1), + ServerName = config.ServerName, + OldVerifyKeys = [], + VerifyKeysById = new() { + { + new() { Algorithm = "ed25519", KeyId = "0" }, new ServerKeysResponse.CurrentVerifyKey() { + Key = keys.publicKey.ToUnpaddedBase64(), + } + } + } + }.Sign(config.ServerName, new ServerKeysResponse.VersionedKeyId() { Algorithm = "ed25519", KeyId = "0" }, keys.privateKey); + } + _serverKeyCacheLock.Release(); + + return _cachedServerKeysResponse; + } +} \ No newline at end of file -- cgit 1.5.1