Files
Libation/Source/AudibleUtilities/AccountCredentialStatus.cs
William Tanksley Jr 4a70c41f65 feat(auth): upgrade AudibleApi to 14.1.3 and support Bearer tokens with graceful legacy fallback (#2148)
- 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
2026-11-12 18:42:09 -07:00

74 lines
2.8 KiB
C#

using AudibleApi.Common;
using Dinah.Core;
namespace AudibleUtilities;
/// <summary>Authentication status of an account.</summary>
public enum AccountSignInStatus
{
/// <summary>Account has not been authenticated, or is marked for re-registration.</summary>
NotSignedIn,
/// <summary>Account is authenticated with legacy tokens (e.g. ADP signature only, or obsolete registration), but missing modern Bearer credentials.</summary>
Legacy,
/// <summary>Account is authenticated with modern credentials (both ADP signature and Bearer token, and modern device profile).</summary>
Modern
}
/// <summary>Describes whether an account has usable stored Audible tokens.</summary>
public static class AccountCredentialStatus
{
/// <summary>
/// Modern Audible operations require both ADP request signing and Bearer token headers.
/// </summary>
public const ApiAuthenticationMethod RequiredAuthenticationMethods =
ApiAuthenticationMethod.AdpSignature | ApiAuthenticationMethod.BearerToken;
/// <summary>
/// Evaluates the sign-in status of an account: Modern, Legacy, or NotSignedIn.
/// </summary>
public static AccountSignInStatus GetSignInStatus(Account? account)
{
if (account is null || account.RequiresReRegistration || account.IdentityTokens is not { IsValid: true } tokens)
return AccountSignInStatus.NotSignedIn;
if (!tokens.HasRequiredTokens(RequiredAuthenticationMethods) || account.HasObsoleteDeviceRegistration())
return AccountSignInStatus.Legacy;
return AccountSignInStatus.Modern;
}
/// <summary>
/// True when identity tokens are absent, carry no refresh token, or lack usable credentials,
/// meaning the account was never fully logged in or its credentials were cleared, rather than
/// merely holding an expired access token or legacy tokens.
/// </summary>
public static bool LooksLikeMissingCredentials(Account? account, ApiAuthenticationMethod? requiredAuth = null)
{
if (account is null || account.RequiresReRegistration)
return true;
if (account.IdentityTokens is not { } tokens)
return true;
if (tokens.IsValid)
return false;
// without a refresh token there is nothing to renew from, so this needs a fresh login rather than a retry
return string.IsNullOrWhiteSpace(tokens.RefreshToken?.Reveal());
}
/// <summary>
/// Account label for dialogs shown to the person who owns the account. Not for logs: those get attached to
/// public issue reports, which is what <see cref="Account.MaskedLogEntry"/> is for.
/// </summary>
public static string FormatAccountLabel(Account? account)
{
if (account is null)
return "an Audible account";
return string.IsNullOrWhiteSpace(account.AccountName) || account.AccountName.EqualsInsensitive(account.AccountId)
? $"'{account.AccountId}'"
: $"'{account.AccountName}' ({account.AccountId})";
}
}