summary refs log tree commit diff
path: root/MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-04 02:18:47 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-04 02:18:47 +0200
commit7e40421d0eaee613be5b807502eb25fafebde5b1 (patch)
treeaf84e4c39fd0c3cfa05e1b66fb566d5cda59607f /MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs
parentCode cleanup (diff)
downloadMxApiExtensions-7e40421d0eaee613be5b807502eb25fafebde5b1.tar.xz
Added a lot of utilities
Diffstat (limited to 'MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs')
-rw-r--r--MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs35
1 files changed, 35 insertions, 0 deletions
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<AuthenticatedHomeserverGeneric> 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);
+    }
+}