diff options
Diffstat (limited to 'MatrixRoomUtils.Core/Services')
3 files changed, 67 insertions, 8 deletions
diff --git a/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs b/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs index 8a22d33..870e0d4 100644 --- a/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs +++ b/MatrixRoomUtils.Core/Services/HomeserverProviderService.cs @@ -1,7 +1,11 @@ using System.Net.Http.Headers; using System.Net.Http.Json; +using System.Text.Json; +using System.Text.Json.Serialization; using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Responses; using Microsoft.Extensions.Logging; + namespace MatrixRoomUtils.Core.Services; public class HomeserverProviderService { @@ -9,7 +13,8 @@ public class HomeserverProviderService { private readonly ILogger<HomeserverProviderService> _logger; private readonly HomeserverResolverService _homeserverResolverService; - public HomeserverProviderService(TieredStorageService tieredStorageService, ILogger<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) { + public HomeserverProviderService(TieredStorageService tieredStorageService, + ILogger<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) { Console.WriteLine("Homeserver provider service instantiated!"); _tieredStorageService = tieredStorageService; _logger = logger; @@ -18,9 +23,11 @@ public class HomeserverProviderService { $"New HomeserverProviderService created with TieredStorageService<{string.Join(", ", tieredStorageService.GetType().GetProperties().Select(x => x.Name))}>!"); } - public async Task<AuthenticatedHomeServer> GetAuthenticatedWithToken(string homeserver, string accessToken) { + public async Task<AuthenticatedHomeServer> GetAuthenticatedWithToken(string homeserver, string accessToken, + string? overrideFullDomain = null) { var hs = new AuthenticatedHomeServer(_tieredStorageService, homeserver, accessToken); - hs.FullHomeServerDomain = await _homeserverResolverService.ResolveHomeserverFromWellKnown(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(5); @@ -29,4 +36,48 @@ public class HomeserverProviderService { hs.WhoAmI = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!; 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(5); + 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; } = ""; + } + } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs b/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs index f6363ab..526a261 100644 --- a/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs +++ b/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs @@ -6,11 +6,12 @@ using Microsoft.Extensions.Logging; namespace MatrixRoomUtils.Core.Services; public class HomeserverResolverService { - private readonly HttpClient _httpClient; + private readonly MatrixHttpClient _httpClient = new MatrixHttpClient(); private readonly ILogger<HomeserverResolverService> _logger; - public HomeserverResolverService(HttpClient httpClient, ILogger<HomeserverResolverService> logger) { - _httpClient = httpClient; + private static Dictionary<string, object> _wellKnownCache = new(); + + public HomeserverResolverService(ILogger<HomeserverResolverService> logger) { _logger = logger; } @@ -22,6 +23,12 @@ public class HomeserverResolverService { } private async Task<string> _resolveHomeserverFromWellKnown(string homeserver) { + if(homeserver is null) throw new ArgumentNullException(nameof(homeserver)); + if (_wellKnownCache.ContainsKey(homeserver)) { + if (_wellKnownCache[homeserver] is SemaphoreSlim s) await s.WaitAsync(); + if (_wellKnownCache[homeserver] is string p) return p; + } + _wellKnownCache[homeserver] = new SemaphoreSlim(1); string? result = null; _logger.LogInformation($"Attempting to resolve homeserver: {homeserver}"); if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; @@ -34,6 +41,7 @@ public class HomeserverResolverService { if(result is not null) { _logger.LogInformation($"Resolved homeserver: {homeserver} -> {result}"); + _wellKnownCache.TryAdd(homeserver, result); return result; } diff --git a/MatrixRoomUtils.Core/Services/ServiceInstaller.cs b/MatrixRoomUtils.Core/Services/ServiceInstaller.cs index 4a831c1..ef9238d 100644 --- a/MatrixRoomUtils.Core/Services/ServiceInstaller.cs +++ b/MatrixRoomUtils.Core/Services/ServiceInstaller.cs @@ -9,14 +9,14 @@ public static class ServiceInstaller { if (!services.Any(x => x.ServiceType == typeof(TieredStorageService))) throw new Exception("[MRUCore/DI] No TieredStorageService has been registered!"); //Add config - if(config != null) + if(config is not null) services.AddSingleton(config); else { services.AddSingleton(new RoryLibMatrixConfiguration()); } //Add services services.AddScoped<HomeserverProviderService>(); - services.AddScoped<HomeserverResolverService>(); + services.AddSingleton<HomeserverResolverService>(); services.AddScoped<HttpClient>(); return services; } |