about summary refs log tree commit diff
path: root/Utilities/LibMatrix.FederationTest/Controllers/TestController.cs
blob: 4a6bc874ccca1cf93e506b514b6a1671c63603bc (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
44
45
46
47
48
49
50
51
52
using System.Text.Json.Nodes;
using ArcaneLibs.Extensions;
using LibMatrix.Extensions;
using LibMatrix.Federation;
using LibMatrix.Federation.Utilities;
using LibMatrix.FederationTest.Services;
using LibMatrix.Homeservers;
using Microsoft.AspNetCore.Mvc;

namespace LibMatrix.FederationTest.Controllers;

[ApiController]
public class TestController(FederationTestConfiguration config, FederationKeyStore keyStore) : ControllerBase {
    static TestController() {
        Console.WriteLine("INFO | TestController initialized.");
    }

    [HttpGet("/test")]
    public async Task<JsonObject> GetTest() {
        var hc = new MatrixHttpClient() {
            BaseAddress = new Uri("https://matrix.rory.gay")
        };

        var keyId = new ServerKeysResponse.VersionedKeyId() {
            Algorithm = "ed25519",
            KeyId = "0"
        };

        var signatureData = new XMatrixAuthorizationScheme.XMatrixRequestSignature() {
                Method = "GET",
                Uri = "/_matrix/federation/v1/user/devices/@emma:rory.gay",
                OriginServerName = config.ServerName,
                DestinationServerName = "rory.gay"
            }
            .Sign(config.ServerName, keyId, keyStore.GetCurrentSigningKey().privateKey);

        var signature = signatureData.Signatures[config.ServerName][keyId];
        var headerValue = new XMatrixAuthorizationScheme.XMatrixAuthorizationHeader() {
            Origin = config.ServerName,
            Destination = "rory.gay",
            Key = keyId,
            Signature = signature
        }.ToHeaderValue();

        var req = new HttpRequestMessage(HttpMethod.Get, "/_matrix/federation/v1/user/devices/@emma:rory.gay");
        req.Headers.Add("Authorization", headerValue);

        var response = await hc.SendAsync(req);
        var content = await response.Content.ReadFromJsonAsync<JsonObject>();
        return content!;
    }
}