From 90e815a4213a9764a1baead229a1372c932ef6ea Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 12 Apr 2024 22:36:51 -0400 Subject: [PATCH 1/6] Fix auth header added to all lykosauthapi routes --- .../Api/TokenAuthHeaderHandler.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs b/StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs index 25cdaa6f..3efe6847 100644 --- a/StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs +++ b/StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs @@ -22,8 +22,7 @@ public class TokenAuthHeaderHandler : DelegatingHandler .HandleResult( r => r.StatusCode is HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden - && r.RequestMessage?.Headers.Authorization - is { Scheme: "Bearer", Parameter: not null } + && r.RequestMessage?.Headers.Authorization is { Scheme: "Bearer", Parameter: not null } ) .RetryAsync( async (result, _) => @@ -35,9 +34,7 @@ public class TokenAuthHeaderHandler : DelegatingHandler "Refreshing access token for status ({StatusCode})", result.Result.StatusCode ); - var (newToken, _) = await tokenProvider - .RefreshTokensAsync() - .ConfigureAwait(false); + var (newToken, _) = await tokenProvider.RefreshTokensAsync().ConfigureAwait(false); Logger.Info( "Access token refreshed: {OldToken} -> {NewToken}", @@ -46,10 +43,6 @@ public class TokenAuthHeaderHandler : DelegatingHandler ); } ); - - // InnerHandler must be left as null when using DI, but must be assigned a value when - // using RestService.For - // InnerHandler = new HttpClientHandler(); } protected override Task SendAsync( @@ -59,9 +52,17 @@ public class TokenAuthHeaderHandler : DelegatingHandler { return policy.ExecuteAsync(async () => { - var accessToken = await tokenProvider.GetAccessTokenAsync().ConfigureAwait(false); + // Only add if Authorization is already set to Bearer and access token is not empty + // this allows some routes to not use the access token + if (request.Headers.Authorization is { Scheme: "Bearer" }) + { + var accessToken = await tokenProvider.GetAccessTokenAsync().ConfigureAwait(false); - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + if (!string.IsNullOrWhiteSpace(accessToken)) + { + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + } + } return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); }); From d3b37229dae439f36288ff790f56f7ce780f4f08 Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 12 Apr 2024 22:38:32 -0400 Subject: [PATCH 2/6] Fix token refresh when access token is empty --- StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs b/StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs index 3efe6847..b042d6b7 100644 --- a/StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs +++ b/StabilityMatrix.Core/Api/TokenAuthHeaderHandler.cs @@ -22,7 +22,8 @@ public class TokenAuthHeaderHandler : DelegatingHandler .HandleResult( r => r.StatusCode is HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden - && r.RequestMessage?.Headers.Authorization is { Scheme: "Bearer", Parameter: not null } + && r.RequestMessage?.Headers.Authorization is { Scheme: "Bearer", Parameter: { } param } + && !string.IsNullOrWhiteSpace(param) ) .RetryAsync( async (result, _) => From a75908e4ef2495ffb7c007bec1ddcb43e0133104 Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 12 Apr 2024 22:47:54 -0400 Subject: [PATCH 3/6] Improve login error message --- .../ViewModels/Dialogs/LykosLoginViewModel.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/LykosLoginViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/LykosLoginViewModel.cs index 471d052b..7dd14a8b 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/LykosLoginViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/LykosLoginViewModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Net; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; @@ -90,7 +91,15 @@ public partial class LykosLoginViewModel : TaskDialogViewModelBase } catch (ApiException e) { - LoginError = new AppException("Failed to login", $"{e.StatusCode} - {e.Message}"); + LoginError = e.StatusCode switch + { + HttpStatusCode.Unauthorized + => new AppException( + "Incorrect email or password", + "Please try again or reset your password" + ), + _ => new AppException("Failed to login", $"{e.StatusCode} - {e.Message}") + }; } } From 01cc2f89a7930d2aaa472392689926347b32d9ae Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 12 Apr 2024 22:48:16 -0400 Subject: [PATCH 4/6] Add LykosLoginDialog max width --- StabilityMatrix.Avalonia/Views/Dialogs/LykosLoginDialog.axaml | 1 + 1 file changed, 1 insertion(+) diff --git a/StabilityMatrix.Avalonia/Views/Dialogs/LykosLoginDialog.axaml b/StabilityMatrix.Avalonia/Views/Dialogs/LykosLoginDialog.axaml index 98ab8228..0f0de8a5 100644 --- a/StabilityMatrix.Avalonia/Views/Dialogs/LykosLoginDialog.axaml +++ b/StabilityMatrix.Avalonia/Views/Dialogs/LykosLoginDialog.axaml @@ -14,6 +14,7 @@ xmlns:mdxaml="https://github.com/whistyun/Markdown.Avalonia.Tight" xmlns:ctxt="clr-namespace:ColorTextBlock.Avalonia;assembly=ColorTextBlock.Avalonia" Focusable="True" + MaxWidth="400" d:DataContext="{x:Static mocks:DesignData.LykosLoginViewModel}" d:DesignHeight="350" d:DesignWidth="400" From 128ef132293371c25c343a6a7b9432a631ac5481 Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 12 Apr 2024 22:50:40 -0400 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c644343d..163fed1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2 ## v2.10.1 ### Fixed - Fixed package launch not working when environment variable `SETUPTOOLS_USE_DISTUTILS` is set due to conflict with a default environment variable. User environment variables will now correctly override any default environment variables. +- Fixed "No refresh token found" error when failing to login with Lykos account in some cases. ## v2.10.0 ### Added From 64c55ec3799efe8b83196d4eb946d1c9b22c646c Mon Sep 17 00:00:00 2001 From: Ionite Date: Fri, 12 Apr 2024 22:52:02 -0400 Subject: [PATCH 6/6] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 163fed1f..76a4992d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2.0.0.html). ## v2.10.1 +### Changed +- Improved error message when logging in with a Lykos account fails due to incorrect email or password. ### Fixed - Fixed package launch not working when environment variable `SETUPTOOLS_USE_DISTUTILS` is set due to conflict with a default environment variable. User environment variables will now correctly override any default environment variables. - Fixed "No refresh token found" error when failing to login with Lykos account in some cases.