about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverProviderService.cs
blob: 61c449aa5d4c19cac19bfb9c344b55bd4565dd37 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using LibMatrix.Extensions;
using LibMatrix.Responses;
using Microsoft.Extensions.Logging;

namespace LibMatrix.Services;

public class HomeserverProviderService {
    private readonly TieredStorageService _tieredStorageService;
    private readonly ILogger<HomeserverProviderService> _logger;
    private readonly HomeserverResolverService _homeserverResolverService;

    public HomeserverProviderService(TieredStorageService tieredStorageService,
        ILogger<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) {
        Console.WriteLine("Homeserver provider service instantiated!");
        _tieredStorageService = tieredStorageService;
        _logger = logger;
        _homeserverResolverService = homeserverResolverService;
        logger.LogDebug(
            $"New HomeserverProviderService created with TieredStorageService<{string.Join(", ", tieredStorageService.GetType().GetProperties().Select(x => x.Name))}>!");
    }

    private static Dictionary<string, SemaphoreSlim> _authenticatedHomeserverSemaphore = new();
    private static Dictionary<string, AuthenticatedHomeServer> _authenticatedHomeServerCache = new();

    public async Task<AuthenticatedHomeServer> GetAuthenticatedWithToken(string homeserver, string accessToken,
        string? overrideFullDomain = null) {
        SemaphoreSlim sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver+accessToken, _ => new SemaphoreSlim(1, 1));
        await sem.WaitAsync();
        if (_authenticatedHomeServerCache.ContainsKey(homeserver+accessToken)) {
            sem.Release();
            return _authenticatedHomeServerCache[homeserver+accessToken];
        }

        var hs = new AuthenticatedHomeServer(_tieredStorageService, homeserver, accessToken);
        hs.FullHomeServerDomain = overrideFullDomain ??
                                  await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver);
        hs._httpClient.Dispose();
        hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.FullHomeServerDomain) };
        hs._httpClient.Timeout = TimeSpan.FromSeconds(120);
        hs._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

        hs.WhoAmI = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!;

        _authenticatedHomeServerCache[homeserver+accessToken] = hs;
        sem.Release();

        return hs;
    }

    public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? overrideFullDomain = null) {
        var hs = new RemoteHomeServer(homeserver);
        hs.FullHomeServerDomain = overrideFullDomain ??
                                  await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver);
        hs._httpClient.Dispose();
        hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.FullHomeServerDomain) };
        hs._httpClient.Timeout = TimeSpan.FromSeconds(120);
        return hs;
    }

    public async Task<LoginResponse> Login(string homeserver, string user, string password,
        string? overrideFullDomain = null) {
        var hs = await GetRemoteHomeserver(homeserver, overrideFullDomain);
        var payload = new LoginRequest {
            Identifier = new() { User = user },
            Password = password
        };
        var resp = await hs._httpClient.PostAsJsonAsync("/_matrix/client/v3/login", payload);
        var data = await resp.Content.ReadFromJsonAsync<LoginResponse>();
        return data!;
    }

    private class LoginRequest {
        [JsonPropertyName("type")]
        public string Type { get; set; } = "m.login.password";

        [JsonPropertyName("identifier")]
        public LoginIdentifier Identifier { get; set; } = new();

        [JsonPropertyName("password")]
        public string Password { get; set; } = "";

        [JsonPropertyName("initial_device_display_name")]
        public string InitialDeviceDisplayName { get; set; } = "Rory&::LibMatrix";

        public class LoginIdentifier {
            [JsonPropertyName("type")]
            public string Type { get; set; } = "m.id.user";

            [JsonPropertyName("user")]
            public string User { get; set; } = "";
        }
    }
}