summary refs log tree commit diff
path: root/ModAS.Server/Services/AuthenticationService.cs
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/AuthenticationService.cs
parentRoom query (diff)
downloadModAS-c5b72e6f002a637d542068be88d70936150c8818.tar.xz
Add auth, start of commit script
Diffstat (limited to 'ModAS.Server/Services/AuthenticationService.cs')
-rw-r--r--ModAS.Server/Services/AuthenticationService.cs31
1 files changed, 14 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