From 7e40421d0eaee613be5b807502eb25fafebde5b1 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 4 Sep 2023 02:18:47 +0200 Subject: Added a lot of utilities --- .../AuthenticatedHomeserverProviderService.cs | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs (limited to 'MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs') diff --git a/MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs b/MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs new file mode 100644 index 0000000..96b0254 --- /dev/null +++ b/MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs @@ -0,0 +1,35 @@ +using LibMatrix.Homeservers; +using LibMatrix.Services; + +namespace MxApiExtensions.Services; + +public class AuthenticatedHomeserverProviderService { + private readonly AuthenticationService _authenticationService; + private readonly HomeserverProviderService _homeserverProviderService; + + public AuthenticatedHomeserverProviderService(AuthenticationService authenticationService, HomeserverProviderService homeserverProviderService) { + _authenticationService = authenticationService; + _homeserverProviderService = homeserverProviderService; + } + + public async Task GetHomeserver() { + var token = _authenticationService.GetToken(); + if (token == null) { + throw new MxApiMatrixException { + ErrorCode = "M_MISSING_TOKEN", + Error = "Missing access token" + }; + } + + var mxid = await _authenticationService.GetMxidFromToken(token); + if (mxid == "@anonymous:*") { + throw new MxApiMatrixException { + ErrorCode = "M_MISSING_TOKEN", + Error = "Missing access token" + }; + } + + var hsCanonical = string.Join(":", mxid.Split(':').Skip(1)); + return await _homeserverProviderService.GetAuthenticatedWithToken(hsCanonical, token); + } +} -- cgit 1.5.1