blob: d96bef528ebb2670dd322662edcc45e77191454d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
using LibMatrix.Abstractions;
using LibMatrix.Federation.Extensions;
using LibMatrix.FederationTest.Services;
using LibMatrix.Homeservers;
using LibMatrix.Responses.Federation;
using Microsoft.AspNetCore.Mvc;
namespace LibMatrix.FederationTest.Controllers.Spec;
[ApiController]
[Route("_matrix/key/v2/")]
public class FederationKeysController(FederationTestConfiguration config, FederationKeyStore keyStore) {
static FederationKeysController() {
Console.WriteLine("INFO | FederationKeysController initialized.");
}
private static SignedObject<ServerKeysResponse>? _cachedServerKeysResponse;
private static SemaphoreSlim _serverKeyCacheLock = new SemaphoreSlim(1, 1);
[HttpGet("server")]
public async Task<SignedObject<ServerKeysResponse>> 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(5),
ServerName = config.ServerName,
OldVerifyKeys = [],
VerifyKeysById = new() {
{
keys.CurrentSigningKey.KeyId, new ServerKeysResponse.CurrentVerifyKey() {
Key = keys.CurrentSigningKey.PublicKey //.ToUnpaddedBase64(),
}
}
}
}.Sign(keys.CurrentSigningKey);
}
_serverKeyCacheLock.Release();
return _cachedServerKeysResponse;
}
}
|