mirror of
https://github.com/rmcrackan/Libation.git
synced 2026-12-03 02:25:40 +01:00
- Upgrade AudibleApi NuGet package reference from 14.1.2 to 14.1.3 - Support Bearer access token authentication alongside ADP request signing - Proactively validate tokens and evaluate account SignInStatus (None, Modern, Legacy) - Add tri-state 'Signed in?' checkbox in Avalonia and WinForms Accounts dialogs - In ApiExtended, dynamically set Api.AuthenticationMethods based on tokens held by account - Handle legacy credential rejections gracefully and guide user to re-authenticate - In CLI, report modern vs legacy authentication status and support forced re-login - Add comprehensive unit tests and user documentation
53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
namespace AudibleUtilities;
|
|
|
|
public static class AuthenticationExceptionHelper
|
|
{
|
|
public static bool IsAuthenticationFailure(Exception ex)
|
|
{
|
|
if (ex is AggregateException aggregate)
|
|
{
|
|
return aggregate.InnerExceptions.Any(IsAuthenticationFailure)
|
|
|| (aggregate.InnerException is not null && IsAuthenticationFailure(aggregate.InnerException));
|
|
}
|
|
|
|
for (var current = ex; current is not null; current = current.InnerException)
|
|
{
|
|
if (current is AuthenticationRequiredException)
|
|
return true;
|
|
|
|
if (current is AudibleApi.NotAuthenticatedException)
|
|
return true;
|
|
|
|
if (current is InvalidOperationException { Message: var message }
|
|
&& message.Contains("ADP token is null", StringComparison.Ordinal))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Finds the <see cref="AuthenticationRequiredException"/> in <paramref name="ex"/> or its inner chain, which is
|
|
/// the one that knows which account needs a login.
|
|
/// </summary>
|
|
public static AuthenticationRequiredException? FindAuthenticationRequired(Exception ex)
|
|
{
|
|
for (var current = ex; current is not null; current = current.InnerException)
|
|
{
|
|
if (current is AuthenticationRequiredException auth)
|
|
return auth;
|
|
}
|
|
|
|
if (ex is AggregateException aggregate)
|
|
{
|
|
foreach (var inner in aggregate.InnerExceptions)
|
|
{
|
|
if (FindAuthenticationRequired(inner) is { } found)
|
|
return found;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|