diff options
Diffstat (limited to 'MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs')
-rw-r--r-- | MxApiExtensions/Services/AuthenticatedHomeserverProviderService.cs | 35 |
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); + } +} |