From 05289f61d7bd0650ef511cc92a8a657c493dce30 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 31 Dec 2023 16:38:20 +0100 Subject: Clean up swagger, clean up auth code --- ModAS.Server/Controllers/HomeController.cs | 38 +++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'ModAS.Server/Controllers/HomeController.cs') diff --git a/ModAS.Server/Controllers/HomeController.cs b/ModAS.Server/Controllers/HomeController.cs index eb17966..48432d5 100644 --- a/ModAS.Server/Controllers/HomeController.cs +++ b/ModAS.Server/Controllers/HomeController.cs @@ -1,5 +1,13 @@ using System.Diagnostics; +using System.Net.Http.Headers; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Web; using Microsoft.AspNetCore.Mvc; +using ModAS.Server.Controllers.AppService; +using MxApiExtensions.Services; namespace ModAS.Server.Controllers; @@ -7,10 +15,38 @@ namespace ModAS.Server.Controllers; /// Manages the visual homepage. /// [ApiController] -public class HomeController : Controller { +public class HomeController(AppServiceRegistration asr, ModASConfiguration config) : Controller { + private const string ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + /// [HttpGet("/_matrix/_modas")] + [ApiExplorerSettings(IgnoreApi = true)] //hide from swagger public IActionResult Index() { return LocalRedirect("/index.html"); } + + [HttpGet("/_matrix/_modas/version")] + public IActionResult Version() { + return Ok(new { + Version = Modas.Server.Version.VersionString + }); + } + + [HttpGet("/_matrix/_modas/ping")] + public async Task Ping() { + var txn = new PingController.TransactionIdContainer() { + TransactionId = RandomNumberGenerator.GetString(ValidChars, 32) + }; + var url = $"{config.HomeserverUrl}/_matrix/client/v1/appservice/{HttpUtility.UrlEncode(asr.Id)}/ping"; + var hrm = new HttpRequestMessage(HttpMethod.Post, url) { + Content = new StringContent(JsonSerializer.Serialize(txn), Encoding.UTF8, "application/json"), + Headers = { + Authorization = new AuthenticationHeaderValue("Bearer", asr.AppServiceToken) + } + }; + var req = await new HttpClient().SendAsync(hrm); + var resp = await req.Content.ReadFromJsonAsync(); + resp!["tnxId"] = txn.TransactionId; + return Ok(resp); + } } \ No newline at end of file -- cgit 1.4.1