From 2e8aa30daa4a33fa33622bccb344dfc24483e320 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Thu, 9 Nov 2023 07:36:02 +0100 Subject: Fix sync --- MxApiExtensions/Services/AuthenticationService.cs | 31 +++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'MxApiExtensions/Services/AuthenticationService.cs') diff --git a/MxApiExtensions/Services/AuthenticationService.cs b/MxApiExtensions/Services/AuthenticationService.cs index 0dcc8b1..7430fcd 100644 --- a/MxApiExtensions/Services/AuthenticationService.cs +++ b/MxApiExtensions/Services/AuthenticationService.cs @@ -1,3 +1,5 @@ +using ArcaneLibs.Extensions; +using LibMatrix; using LibMatrix.Services; using MxApiExtensions.Classes.LibMatrix; @@ -17,7 +19,7 @@ public class AuthenticationService(ILogger logger, MxApiE token = _request.Query["access_token"]; } - if (token == null && fail) { + if (string.IsNullOrWhiteSpace(token) && fail) { throw new MxApiMatrixException { ErrorCode = "M_MISSING_TOKEN", Error = "Missing access token" @@ -29,7 +31,7 @@ public class AuthenticationService(ILogger logger, MxApiE public async Task GetMxidFromToken(string? token = null, bool fail = true) { token ??= GetToken(fail); - if (token == null) { + if (string.IsNullOrWhiteSpace(token)) { if (fail) { throw new MxApiMatrixException { ErrorCode = "M_MISSING_TOKEN", @@ -44,15 +46,34 @@ public class AuthenticationService(ILogger logger, MxApiE _tokenMap = (await File.ReadAllLinesAsync("token_map")) .Select(l => l.Split('\t')) .ToDictionary(l => l[0], l => l[1]); + + //THIS IS BROKEN, DO NOT USE! + // foreach (var (mapToken, mapUser) in _tokenMap) { + // try { + // var hs = await homeserverProviderService.GetAuthenticatedWithToken(mapUser.Split(':', 2)[1], mapToken); + // } + // catch (MatrixException e) { + // if (e is { ErrorCode: "M_UNKNOWN_TOKEN" }) _tokenMap[mapToken] = ""; + // } + // catch { + // // ignored + // } + // } + // _tokenMap.RemoveAll((x, y) => string.IsNullOrWhiteSpace(y)); + // await File.WriteAllTextAsync("token_map", _tokenMap.Aggregate("", (x, y) => $"{y.Key}\t{y.Value}\n")); } + if (_tokenMap.TryGetValue(token, out var mxid)) return mxid; var lookupTasks = new Dictionary>(); foreach (var homeserver in config.AuthHomeservers) { - lookupTasks.Add(homeserver, GetMxidFromToken(token, homeserver)); - await lookupTasks[homeserver].WaitAsync(TimeSpan.FromMilliseconds(250)); - if(lookupTasks[homeserver].IsCompletedSuccessfully && !string.IsNullOrWhiteSpace(lookupTasks[homeserver].Result)) break; + try { + lookupTasks.Add(homeserver, GetMxidFromToken(token, homeserver)); + await lookupTasks[homeserver].WaitAsync(TimeSpan.FromMilliseconds(500)); + if (lookupTasks[homeserver].IsCompletedSuccessfully && !string.IsNullOrWhiteSpace(lookupTasks[homeserver].Result)) break; + } + catch {} } await Task.WhenAll(lookupTasks.Values); -- cgit 1.5.1