From 256b46e823f5d2cd3e2184cdfcef2d682acb7533 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Wed, 19 Nov 2025 00:36:03 -0600 Subject: [PATCH] Fix storage of authentication token --- .../AuthenticationService.cs | 17 +++++------------ .../CommandLineService.cs | 8 +++----- .../Services/WindowService.cs | 6 +----- .../UI/Components/RedirectToLogin.razor | 2 +- .../UI/Windows/MainWindow.razor | 2 +- .../Abstractions/ITokenProvider.cs | 5 +++-- LANCommander.SDK/Helpers/ApiRequestBuilder.cs | 4 ++-- .../Models/Settings/AuthenticationSettings.cs | 5 +---- .../Providers/ServerConfigurationProvider.cs | 2 +- LANCommander.SDK/Providers/TokenProvider.cs | 18 +++++++++--------- LANCommander.SDK/Rpc/Clients/RpcClient.Base.cs | 2 +- .../Services/AuthenticationClient.cs | 12 +++--------- LANCommander.SDK/Services/ConnectionClient.cs | 2 +- .../AuthenticationService.cs | 4 ++-- 14 files changed, 34 insertions(+), 55 deletions(-) diff --git a/LANCommander.Launcher.Services/AuthenticationService.cs b/LANCommander.Launcher.Services/AuthenticationService.cs index 2f8d2a8b..9ba442d8 100644 --- a/LANCommander.Launcher.Services/AuthenticationService.cs +++ b/LANCommander.Launcher.Services/AuthenticationService.cs @@ -45,11 +45,7 @@ public class AuthenticationService( { using (var op = logger.BeginDebugOperation("Logging in using stored credentials")) { - await Login(settingsProvider.CurrentValue.Authentication.ServerAddress, new SDK.Models.AuthToken - { - AccessToken = settingsProvider.CurrentValue.Authentication.AccessToken, - RefreshToken = settingsProvider.CurrentValue.Authentication.RefreshToken, - }); + await Login(settingsProvider.CurrentValue.Authentication.ServerAddress, settingsProvider.CurrentValue.Authentication.Token); op.Complete(); } @@ -77,7 +73,7 @@ public class AuthenticationService( { await connectionClient.UpdateServerAddressAsync(serverAddress.ToString()); - tokenProvider.SetToken(token.AccessToken); + tokenProvider.SetToken(token); if (await authenticationClient.ValidateTokenAsync()) { @@ -87,8 +83,6 @@ public class AuthenticationService( settingsProvider.Update(s => { s.Authentication.ServerAddress = serverAddress; - s.Authentication.AccessToken = token.AccessToken; - s.Authentication.RefreshToken = token.RefreshToken; }); await using var scope = scopeFactory.CreateAsyncScope(); @@ -124,7 +118,6 @@ public class AuthenticationService( settingsProvider.Update(s => { s.Authentication.ServerAddress = connectionClient.GetServerAddress(); - s.Authentication.AccessToken = tokenProvider.GetToken(); }); await using var scope = scopeFactory.CreateAsyncScope(); @@ -191,14 +184,14 @@ public class AuthenticationService( { var token = tokenProvider.GetToken(); - if (string.IsNullOrEmpty(token)) + if (string.IsNullOrEmpty(token.AccessToken)) return null; try { var handler = new JwtSecurityTokenHandler(); - return handler.ReadToken(token) as JwtSecurityToken; + return handler.ReadToken(token.AccessToken) as JwtSecurityToken; } catch { @@ -208,7 +201,7 @@ public class AuthenticationService( public bool HasStoredCredentials() { - if (string.IsNullOrEmpty(tokenProvider.GetToken())) + if (string.IsNullOrEmpty(tokenProvider.GetToken().AccessToken)) return false; var decodedToken = DecodeToken(); diff --git a/LANCommander.Launcher.Services/CommandLineService.cs b/LANCommander.Launcher.Services/CommandLineService.cs index 8d8ca85e..be791fea 100644 --- a/LANCommander.Launcher.Services/CommandLineService.cs +++ b/LANCommander.Launcher.Services/CommandLineService.cs @@ -256,11 +256,10 @@ namespace LANCommander.Launcher.Services await client.Connection.UpdateServerAddressAsync(options.ServerAddress); var token = await client.Authentication.AuthenticateAsync(options.Username, options.Password, client.Connection.GetServerAddress()); - + client.Settings.Update(s => { - s.Authentication.AccessToken = token.AccessToken; - s.Authentication.RefreshToken = token.RefreshToken; + s.Authentication.Token = token; s.Authentication.ServerAddress = client.Connection.GetServerAddress(); }); @@ -278,8 +277,7 @@ namespace LANCommander.Launcher.Services client.Settings.Update(s => { - s.Authentication.AccessToken = String.Empty; - s.Authentication.RefreshToken = String.Empty; + s.Authentication.Token = null; s.Authentication.OfflineModeEnabled = false; }); } diff --git a/LANCommander.Launcher/Services/WindowService.cs b/LANCommander.Launcher/Services/WindowService.cs index 19f6a777..ecc85b0c 100644 --- a/LANCommander.Launcher/Services/WindowService.cs +++ b/LANCommander.Launcher/Services/WindowService.cs @@ -64,12 +64,8 @@ internal static class WindowService if (appHook != null) appHook(app); - - var settingsProvider = app.Services.GetService>(); - var tokenProvider = app.Services.GetService(); - var connectionClient = app.Services.GetService(); - tokenProvider?.SetToken(settingsProvider.CurrentValue.Authentication.AccessToken); + var connectionClient = app.Services.GetService(); connectionClient.ConnectAsync().Wait(); diff --git a/LANCommander.Launcher/UI/Components/RedirectToLogin.razor b/LANCommander.Launcher/UI/Components/RedirectToLogin.razor index 2ab2295f..e95cf191 100644 --- a/LANCommander.Launcher/UI/Components/RedirectToLogin.razor +++ b/LANCommander.Launcher/UI/Components/RedirectToLogin.razor @@ -6,7 +6,7 @@ { var currentUri = new Uri(NavigationManager.Uri); - if (String.IsNullOrEmpty(Client.Settings.CurrentValue.Authentication.AccessToken) && currentUri.LocalPath != "/Authenticate") + if (String.IsNullOrEmpty(Client.Settings.CurrentValue.Authentication.Token?.AccessToken) && currentUri.LocalPath != "/Authenticate") NavigationManager.NavigateTo("/Authenticate", true); } } diff --git a/LANCommander.Launcher/UI/Windows/MainWindow.razor b/LANCommander.Launcher/UI/Windows/MainWindow.razor index f7474dfd..bdd98306 100644 --- a/LANCommander.Launcher/UI/Windows/MainWindow.razor +++ b/LANCommander.Launcher/UI/Windows/MainWindow.razor @@ -102,7 +102,7 @@ ConnectionClient.OnOfflineModeEnabled += OnOfflineModeChanged; ConnectionClient.OnConnect += OnConnect; - if (!await Client.Authentication.ValidateTokenAsync()) + if (await Client.Authentication.ValidateTokenAsync()) { await AuthenticationService.Login(); diff --git a/LANCommander.SDK/Abstractions/ITokenProvider.cs b/LANCommander.SDK/Abstractions/ITokenProvider.cs index 279ce7bd..8aeea461 100644 --- a/LANCommander.SDK/Abstractions/ITokenProvider.cs +++ b/LANCommander.SDK/Abstractions/ITokenProvider.cs @@ -1,9 +1,10 @@ using System.Threading.Tasks; +using LANCommander.SDK.Models; namespace LANCommander.SDK.Abstractions; public interface ITokenProvider { - void SetToken(string token); - string GetToken(); + void SetToken(AuthToken token); + AuthToken GetToken(); } \ No newline at end of file diff --git a/LANCommander.SDK/Helpers/ApiRequestBuilder.cs b/LANCommander.SDK/Helpers/ApiRequestBuilder.cs index 86491e3d..b8aacfa6 100644 --- a/LANCommander.SDK/Helpers/ApiRequestBuilder.cs +++ b/LANCommander.SDK/Helpers/ApiRequestBuilder.cs @@ -21,7 +21,7 @@ public class ApiRequestBuilder( ITokenProvider tokenProvider, ISettingsProvider settingsProvider) { - private string _token { get; set; } = tokenProvider.GetToken(); + private AuthToken _token { get; set; } = tokenProvider.GetToken(); private bool _ignoreVersion { get; set; } private object _body { get; set; } private string _route { get; set; } @@ -49,7 +49,7 @@ public class ApiRequestBuilder( public ApiRequestBuilder UseAuthenticationToken() { - _request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token); + _request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken); return this; } diff --git a/LANCommander.SDK/Models/Settings/AuthenticationSettings.cs b/LANCommander.SDK/Models/Settings/AuthenticationSettings.cs index 7738abd6..9a9867d3 100644 --- a/LANCommander.SDK/Models/Settings/AuthenticationSettings.cs +++ b/LANCommander.SDK/Models/Settings/AuthenticationSettings.cs @@ -8,9 +8,6 @@ public class AuthenticationSettings { [YamlMember(typeof(string))] public Uri ServerAddress { get; set; } - [YamlMember(ScalarStyle = ScalarStyle.Plain)] - public string AccessToken { get; set; } - [YamlMember(ScalarStyle = ScalarStyle.Plain)] - public string RefreshToken { get; set; } + public AuthToken Token { get; set; } public bool OfflineModeEnabled { get; set; } } \ No newline at end of file diff --git a/LANCommander.SDK/Providers/ServerConfigurationProvider.cs b/LANCommander.SDK/Providers/ServerConfigurationProvider.cs index 1a47317a..c6f5a1eb 100644 --- a/LANCommander.SDK/Providers/ServerConfigurationProvider.cs +++ b/LANCommander.SDK/Providers/ServerConfigurationProvider.cs @@ -58,7 +58,7 @@ public sealed class ServerConfigurationProvider : ConfigurationProvider var request = new HttpRequestMessage(HttpMethod.Get, settings.Authentication.ServerAddress.Join("/api/Settings")); - request.Headers.Add("Authorization", $"Bearer {settings.Authentication.AccessToken}"); + request.Headers.Add("Authorization", $"Bearer {settings.Authentication.Token.AccessToken}"); var response = await _httpClient.SendAsync(request, cancellationToken); diff --git a/LANCommander.SDK/Providers/TokenProvider.cs b/LANCommander.SDK/Providers/TokenProvider.cs index 5b31cbcf..3bdb77ad 100644 --- a/LANCommander.SDK/Providers/TokenProvider.cs +++ b/LANCommander.SDK/Providers/TokenProvider.cs @@ -1,18 +1,18 @@ using LANCommander.SDK.Abstractions; +using LANCommander.SDK.Models; namespace LANCommander.SDK.Providers; -public class TokenProvider : ITokenProvider +public class TokenProvider(ISettingsProvider settingsProvider) : ITokenProvider { - private string _token { get; set; } - - public void SetToken(string token) + public void SetToken(AuthToken token) { - _token = token; + settingsProvider.Update(s => + { + s.Authentication.Token = token; + s.Authentication.OfflineModeEnabled = false; + }); } - public string GetToken() - { - return _token; - } + AuthToken ITokenProvider.GetToken() => settingsProvider.CurrentValue.Authentication.Token; } \ No newline at end of file diff --git a/LANCommander.SDK/Rpc/Clients/RpcClient.Base.cs b/LANCommander.SDK/Rpc/Clients/RpcClient.Base.cs index 42cf9504..8cecc206 100644 --- a/LANCommander.SDK/Rpc/Clients/RpcClient.Base.cs +++ b/LANCommander.SDK/Rpc/Clients/RpcClient.Base.cs @@ -20,7 +20,7 @@ internal partial class RpcSubscriber(ITokenProvider tokenProvider) : IRpcSubscri _connection = new HubConnectionBuilder() .WithUrl(serverAddress.Join("rpc"), options => { - options.AccessTokenProvider = () => Task.FromResult(tokenProvider.GetToken()); + options.AccessTokenProvider = () => Task.FromResult(tokenProvider.GetToken().AccessToken); }) .Build(); diff --git a/LANCommander.SDK/Services/AuthenticationClient.cs b/LANCommander.SDK/Services/AuthenticationClient.cs index 316c3fd1..7a2364a0 100644 --- a/LANCommander.SDK/Services/AuthenticationClient.cs +++ b/LANCommander.SDK/Services/AuthenticationClient.cs @@ -60,7 +60,7 @@ public class AuthenticationClient( Expiration = result.Data.Expiration }; - tokenProvider.SetToken(token.AccessToken); + tokenProvider.SetToken(token); await configRefresher.RefreshAsync(); @@ -103,12 +103,6 @@ public class AuthenticationClient( } tokenProvider.SetToken(null); - settingsProvider.Update(s => - { - s.Authentication.AccessToken = null; - s.Authentication.RefreshToken = null; - s.Authentication.OfflineModeEnabled = false; - }); } public async Task RegisterAsync(string username, string password, string passwordConfirmation) @@ -138,7 +132,7 @@ public class AuthenticationClient( switch (result.Response.StatusCode) { case HttpStatusCode.OK: - tokenProvider.SetToken(result.Data.AccessToken); + tokenProvider.SetToken(result.Data); return; case HttpStatusCode.BadRequest: @@ -163,7 +157,7 @@ public class AuthenticationClient( { logger?.LogTrace("Validating token"); - if (String.IsNullOrWhiteSpace(tokenProvider.GetToken())) + if (String.IsNullOrWhiteSpace(tokenProvider.GetToken()?.AccessToken)) { logger?.LogError("Token is empty"); return false; diff --git a/LANCommander.SDK/Services/ConnectionClient.cs b/LANCommander.SDK/Services/ConnectionClient.cs index f27fabac..43c87c0b 100644 --- a/LANCommander.SDK/Services/ConnectionClient.cs +++ b/LANCommander.SDK/Services/ConnectionClient.cs @@ -27,7 +27,7 @@ public class ConnectionClient( public bool IsConfigured() { - return HasServerAddress() && !String.IsNullOrEmpty(tokenProvider.GetToken()); + return HasServerAddress() && !String.IsNullOrEmpty(tokenProvider.GetToken()?.AccessToken); } public bool IsOfflineMode() diff --git a/LANCommander.Server.Services/AuthenticationService.cs b/LANCommander.Server.Services/AuthenticationService.cs index 3b5bb6a2..4945c5cb 100644 --- a/LANCommander.Server.Services/AuthenticationService.cs +++ b/LANCommander.Server.Services/AuthenticationService.cs @@ -38,7 +38,7 @@ namespace LANCommander.Server.Services if (scripts.Any()) { - tokenProvider.SetToken(token.AccessToken); + tokenProvider.SetToken(token); foreach (var script in scripts) { @@ -190,7 +190,7 @@ namespace LANCommander.Server.Services if (scripts.Any()) { - tokenProvider.SetToken(token.AccessToken); + tokenProvider.SetToken(token); foreach (var script in scripts) {