diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-12-23 12:07:34 +0100 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-12-23 12:07:34 +0100 |
commit | a847c172069c27fccd95fc593a1b0c630a620d4f (patch) | |
tree | 78fe3f1d20af02738421eb020cedd417b51b8305 /ModAS.Server | |
parent | Basic test (diff) | |
download | ModAS-a847c172069c27fccd95fc593a1b0c630a620d4f.tar.xz |
Transactions test
Diffstat (limited to 'ModAS.Server')
-rw-r--r-- | ModAS.Server/AppServiceRegistration.cs | 4 | ||||
-rw-r--r-- | ModAS.Server/Controllers/DebugController.cs | 2 | ||||
-rw-r--r-- | ModAS.Server/Controllers/TransactionsController.cs | 15 | ||||
-rw-r--r-- | ModAS.Server/Program.cs | 4 | ||||
-rw-r--r-- | ModAS.Server/Services/AuthenticatedHomeserverProviderService.cs | 16 |
5 files changed, 30 insertions, 11 deletions
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<IActionResult> 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<IHttpContextAccessor, HttpContextAccessor>(); builder.Services.AddSingleton<ModASConfiguration>(); -builder.Services.AddScoped<AuthenticationService>(); -builder.Services.AddScoped<AuthenticatedHomeserverProviderService>(); +builder.Services.AddSingleton<AuthenticationService>(); +builder.Services.AddSingleton<AuthenticatedHomeserverProviderService>(); // builder.Services.AddScoped<UserContextService>(); builder.Services.AddSingleton<TieredStorageService>(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<string, AuthenticatedHomeserverGeneric> KnownUsers { get; set; } = new(); + public ConcurrentDictionary<string, AuthenticatedHomeserverGeneric> KnownUsers { get; set; } = new(); public async Task<AuthenticatedHomeserverGeneric> 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<AuthenticatedHomeserverGeneric> GetHomeserver() { |