summary refs log tree commit diff
path: root/ModAS.Server/Services
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2023-12-31 12:00:40 +0100
committerRory& <root@rory.gay>2023-12-31 12:00:40 +0100
commitc5b72e6f002a637d542068be88d70936150c8818 (patch)
treec7d7a5c99329e88bce47b60b566b8398c0dd4a68 /ModAS.Server/Services
parentRoom query (diff)
downloadModAS-c5b72e6f002a637d542068be88d70936150c8818.tar.xz
Add auth, start of commit script
Diffstat (limited to 'ModAS.Server/Services')
-rw-r--r--ModAS.Server/Services/AuthenticationService.cs31
-rw-r--r--ModAS.Server/Services/ModASConfiguration.cs5
-rw-r--r--ModAS.Server/Services/PingTask.cs9
3 files changed, 28 insertions, 17 deletions
diff --git a/ModAS.Server/Services/AuthenticationService.cs b/ModAS.Server/Services/AuthenticationService.cs

index 27e12ad..8efc08c 100644 --- a/ModAS.Server/Services/AuthenticationService.cs +++ b/ModAS.Server/Services/AuthenticationService.cs
@@ -1,20 +1,28 @@ +using System.Net.Http.Headers; using LibMatrix; using LibMatrix.Services; +using MxApiExtensions.Extensions; using MxApiExtensions.Services; namespace ModAS.Server.Services; -public class AuthenticationService(ILogger<AuthenticationService> logger, ModASConfiguration config, IHttpContextAccessor request, HomeserverProviderService homeserverProviderService) { +public class AuthenticationService( + ILogger<AuthenticationService> logger, + ModASConfiguration config, + IHttpContextAccessor request, + HomeserverProviderService homeserverProviderService) { private readonly HttpRequest _request = request.HttpContext!.Request; private static Dictionary<string, string> _tokenMap = new(); internal string? GetToken(bool fail = true) { - string? token; - if (_request.Headers.TryGetValue("Authorization", out var tokens)) { - token = tokens.FirstOrDefault()?[7..]; + //_request.GetTypedHeaders().Get<AuthenticationHeaderValue>("Authorization")?.Parameter != asr.HomeserverToken + + string? token = null; + if (_request.GetTypedHeaders().TryGet<AuthenticationHeaderValue>("Authorization", out var authHeader) && !string.IsNullOrWhiteSpace(authHeader?.Parameter)) { + token = authHeader.Parameter; } - else { + else if (_request.Query.ContainsKey("access_token")) { token = _request.Query["access_token"]; } @@ -47,18 +55,13 @@ public class AuthenticationService(ILogger<AuthenticationService> logger, ModASC .ToDictionary(l => l[0], l => l[1]); } - if (_tokenMap.TryGetValue(token, out var mxid)) return mxid; - var lookupTasks = new Dictionary<string, Task<string?>>(); - - logger.LogInformation("Looking up mxid for token {}", token); var hs = await homeserverProviderService.GetAuthenticatedWithToken(config.ServerName, token, config.HomeserverUrl); try { var res = hs.WhoAmI.UserId; logger.LogInformation("Got mxid {} for token {}", res, token); - await SaveMxidForToken(token, mxid); return res; } @@ -70,10 +73,4 @@ public class AuthenticationService(ILogger<AuthenticationService> logger, ModASC throw; } } - - - public async Task SaveMxidForToken(string token, string mxid) { - _tokenMap.Add(token, mxid); - await File.AppendAllLinesAsync("token_map", new[] { $"{token}\t{mxid}" }); - } -} +} \ No newline at end of file diff --git a/ModAS.Server/Services/ModASConfiguration.cs b/ModAS.Server/Services/ModASConfiguration.cs
index 063e838..90f8e9e 100644 --- a/ModAS.Server/Services/ModASConfiguration.cs +++ b/ModAS.Server/Services/ModASConfiguration.cs
@@ -1,5 +1,8 @@ namespace MxApiExtensions.Services; +/// <summary> +/// Configuration for ModAS. +/// </summary> public class ModASConfiguration { public ModASConfiguration(IConfiguration configuration) { configuration.GetRequiredSection("ModAS").Bind(this); @@ -7,4 +10,6 @@ public class ModASConfiguration { public string ServerName { get; set; } public string HomeserverUrl { get; set; } + + public Dictionary<string, List<string>> Roles { get; set; } } \ No newline at end of file diff --git a/ModAS.Server/Services/PingTask.cs b/ModAS.Server/Services/PingTask.cs new file mode 100644
index 0000000..99a8f40 --- /dev/null +++ b/ModAS.Server/Services/PingTask.cs
@@ -0,0 +1,9 @@ +namespace ModAS.Server.Services; + +public class PingTask : IHostedService, IDisposable { + public Task StartAsync(CancellationToken cancellationToken) => throw new NotImplementedException(); + + public Task StopAsync(CancellationToken cancellationToken) => throw new NotImplementedException(); + + public void Dispose() => throw new NotImplementedException(); +} \ No newline at end of file