diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-11-09 07:36:02 +0100 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-11-09 07:36:02 +0100 |
commit | 2e8aa30daa4a33fa33622bccb344dfc24483e320 (patch) | |
tree | ee08ce4e83382b81a1fbabaac85c763971408bbe /MxApiExtensions/Services/AuthenticationService.cs | |
parent | Fix some null checks (diff) | |
download | MxApiExtensions-2e8aa30daa4a33fa33622bccb344dfc24483e320.tar.xz |
Fix sync
Diffstat (limited to 'MxApiExtensions/Services/AuthenticationService.cs')
-rw-r--r-- | MxApiExtensions/Services/AuthenticationService.cs | 31 |
1 files changed, 26 insertions, 5 deletions
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<AuthenticationService> 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<AuthenticationService> logger, MxApiE public async Task<string> 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<AuthenticationService> 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<string, Task<string?>>(); 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); |