From a847c172069c27fccd95fc593a1b0c630a620d4f Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sat, 23 Dec 2023 12:07:34 +0100 Subject: Transactions test --- LibMatrix | 2 +- ModAS.Server/AppServiceRegistration.cs | 4 ++-- ModAS.Server/Controllers/DebugController.cs | 2 +- ModAS.Server/Controllers/TransactionsController.cs | 15 +++++++++++++++ ModAS.Server/Program.cs | 4 ++-- .../Services/AuthenticatedHomeserverProviderService.cs | 16 ++++++++++------ 6 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 ModAS.Server/Controllers/TransactionsController.cs diff --git a/LibMatrix b/LibMatrix index 14bcb74..314f704 160000 --- a/LibMatrix +++ b/LibMatrix @@ -1 +1 @@ -Subproject commit 14bcb748a853f7cd2afce40477bd2b0cb14ad7e6 +Subproject commit 314f7044f62b92c49abe2d5c7422c6cf3430b021 diff --git a/ModAS.Server/AppServiceRegistration.cs b/ModAS.Server/AppServiceRegistration.cs index fbf323a..63dabba 100644 --- a/ModAS.Server/AppServiceRegistration.cs +++ b/ModAS.Server/AppServiceRegistration.cs @@ -10,10 +10,10 @@ public class AppServiceRegistration { private const string ExtendedValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~+/"; [JsonPropertyName("as_token")] - public string AsToken { get; set; } = RandomNumberGenerator.GetString(ExtendedValidChars, RandomNumberGenerator.GetInt32(512, 1024)); + public string AppServiceToken { get; set; } = RandomNumberGenerator.GetString(ExtendedValidChars, RandomNumberGenerator.GetInt32(512, 1024)); [JsonPropertyName("hs_token")] - public string HsToken { get; set; } = RandomNumberGenerator.GetString(ExtendedValidChars, RandomNumberGenerator.GetInt32(512, 1024)); + public string HomeserverToken { get; set; } = RandomNumberGenerator.GetString(ExtendedValidChars, RandomNumberGenerator.GetInt32(512, 1024)); [JsonPropertyName("id")] public string Id { get; set; } = "ModAS-"+RandomNumberGenerator.GetString(ValidChars, 5); diff --git a/ModAS.Server/Controllers/DebugController.cs b/ModAS.Server/Controllers/DebugController.cs index 39bff65..d3c7ad0 100644 --- a/ModAS.Server/Controllers/DebugController.cs +++ b/ModAS.Server/Controllers/DebugController.cs @@ -40,7 +40,7 @@ public class DebugController(ModASConfiguration config, AuthenticatedHomeserverP await ahs.GetJoinedRooms(); } catch (MatrixException e) { - if(e is {ErrorCode: "M_FORBIDDEN"}) continue; + if (e is { ErrorCode: "M_FORBIDDEN" }) continue; throw; } diff --git a/ModAS.Server/Controllers/TransactionsController.cs b/ModAS.Server/Controllers/TransactionsController.cs new file mode 100644 index 0000000..8e4e018 --- /dev/null +++ b/ModAS.Server/Controllers/TransactionsController.cs @@ -0,0 +1,15 @@ +using System.IO.Pipelines; +using Microsoft.AspNetCore.Mvc; +using ModAS.Server; + +namespace WebApplication1.Controllers; + +[ApiController] +public class TransactionsController(AppServiceRegistration asr) : ControllerBase { + [HttpPut(" /_matrix/app/v1/transactions/{txnId}")] + public async Task PutTransactions(string txnId) { + if(!Request.Headers.ContainsKey("Authorization") || Request.Headers["Authorization"] != asr.HomeserverToken) return Unauthorized(); + await Request.Body.CopyToAsync(Console.OpenStandardOutput()); + return Ok(new{}); + } +} \ No newline at end of file diff --git a/ModAS.Server/Program.cs b/ModAS.Server/Program.cs index cba8d69..da44ca8 100644 --- a/ModAS.Server/Program.cs +++ b/ModAS.Server/Program.cs @@ -29,8 +29,8 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); // builder.Services.AddScoped(); builder.Services.AddSingleton(x => { diff --git a/ModAS.Server/Services/AuthenticatedHomeserverProviderService.cs b/ModAS.Server/Services/AuthenticatedHomeserverProviderService.cs index fa8e00a..1ceb095 100644 --- a/ModAS.Server/Services/AuthenticatedHomeserverProviderService.cs +++ b/ModAS.Server/Services/AuthenticatedHomeserverProviderService.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using ArcaneLibs.Extensions; using LibMatrix; using LibMatrix.Homeservers; @@ -14,14 +15,17 @@ public class AuthenticatedHomeserverProviderService( AppServiceRegistration asRegistration ) { public HttpContext? _context = request.HttpContext; - public Dictionary KnownUsers { get; set; } = new(); + public ConcurrentDictionary KnownUsers { get; set; } = new(); public async Task GetImpersonatedHomeserver(string mxid) { - if (KnownUsers.TryGetValue(mxid, out var homeserver)) return homeserver; - var hs = await homeserverProviderService.GetAuthenticatedWithToken(config.ServerName, asRegistration.AsToken, config.HomeserverUrl); - await hs.SetImpersonate(mxid); - KnownUsers.TryAdd(mxid, hs); - return hs; + if (!KnownUsers.TryGetValue(mxid, out var homeserver)) { + homeserver = await homeserverProviderService.GetAuthenticatedWithToken(config.ServerName, asRegistration.AppServiceToken, config.HomeserverUrl); + KnownUsers.TryAdd(mxid, homeserver); + } + //var hs = await homeserverProviderService.GetAuthenticatedWithToken(config.ServerName, asRegistration.AsToken, config.HomeserverUrl); + await homeserver.SetImpersonate(mxid); + // KnownUsers.TryAdd(mxid, homeserver); + return homeserver; } public async Task GetHomeserver() { -- cgit 1.4.1