diff --git a/Source/AudibleUtilities/ApiExtended.cs b/Source/AudibleUtilities/ApiExtended.cs index 71a3af58..8d1c9611 100644 --- a/Source/AudibleUtilities/ApiExtended.cs +++ b/Source/AudibleUtilities/ApiExtended.cs @@ -7,6 +7,7 @@ using Polly; using Polly.Retry; using System.Diagnostics; using System.Globalization; +using System.IO; using System.Threading.Channels; namespace AudibleUtilities; @@ -114,6 +115,41 @@ public class ApiExtended await InteractiveLoginGate.WaitAsync(); try { + // Re-read disk state first: another concurrent caller may have completed login while we waited on the gate. + if (File.Exists(AudibleApiStorage.AccountsSettingsFile)) + { + Account? reloaded; + using (var persister = AudibleApiStorage.GetAccountsSettingsPersister()) + reloaded = persister.AccountsSettings.GetAccount(account.AccountId, locale.Name); + + if (reloaded is not null) + { + // Sync IdentityTokens before RequiresReRegistration: any setter on `account` + // triggers an attached caller AccountsSettingsPersister to flush its in-memory copy. + if (reloaded.IdentityTokens is not null) + account.IdentityTokens = reloaded.IdentityTokens; + account.RequiresReRegistration = reloaded.RequiresReRegistration; + } + + if (!account.RequiresReRegistration + && AccountCredentialStatus.GetSignInStatus(account) == AccountSignInStatus.Modern) + { + try + { + var cachedApi = await EzApiCreator.GetApiAsync( + locale, + AudibleApiStorage.AccountsSettingsFile, + account.GetIdentityTokensJsonPath(), + storeLocale); + return new ApiExtended(cachedApi, account); + } + catch (Exception retryEx) + { + Serilog.Log.Logger.Debug(retryEx, "Non-interactive retry after InteractiveLoginGate failed for {Account}; proceeding with interactive login.", account.MaskedLogEntry); + } + } + } + Serilog.Log.Logger.Information("{@DebugInfo}", new { LoginType = nameof(ILoginChoiceEager), @@ -141,13 +177,7 @@ public class ApiExtended forceLogin: missingRequiredTokens || account.RequiresReRegistration, requiredAuthMethods: AccountCredentialStatus.RequiredAuthenticationMethods); - account.RequiresReRegistration = false; - using (var persister = AudibleApiStorage.GetAccountsSettingsPersister()) - { - var matching = persister.AccountsSettings.GetAccount(account.AccountId, account.Locale?.Name); - if (matching is not null) - matching.RequiresReRegistration = false; - } + SyncAccountFromDiskAfterLogin(account); // login happens against the account's own marketplace, so the api it hands back reads that one. // re-create once the tokens exist if some other marketplace is the one actually wanted. @@ -167,6 +197,28 @@ public class ApiExtended } } + /// + /// Reloads newly persisted tokens onto the in-memory before clearing + /// , so an open caller persister cannot flush stale + /// invalidated tokens back over the new ones on disk. + /// + internal static void SyncAccountFromDiskAfterLogin(Account account, string? accountsSettingsFile = null) + { + using (var persister = accountsSettingsFile is null + ? AudibleApiStorage.GetAccountsSettingsPersister() + : new AccountsSettingsPersister(accountsSettingsFile)) + { + var matching = persister.AccountsSettings.GetAccount(account.AccountId, account.Locale?.Name); + if (matching is not null) + { + if (matching.IdentityTokens is not null) + account.IdentityTokens = matching.IdentityTokens; + matching.RequiresReRegistration = false; + } + } + account.RequiresReRegistration = false; + } + private static AsyncRetryPolicy policy { get; } = Policy.Handle() // 2 retries == 3 total diff --git a/Source/_Tests/AudibleUtilities.Tests/ApiExtendedTokenSyncTests.cs b/Source/_Tests/AudibleUtilities.Tests/ApiExtendedTokenSyncTests.cs new file mode 100644 index 00000000..a8ea7c0b --- /dev/null +++ b/Source/_Tests/AudibleUtilities.Tests/ApiExtendedTokenSyncTests.cs @@ -0,0 +1,110 @@ +using AudibleApi; +using AudibleApi.Authorization; +using AudibleApi.Common; +using AudibleApi.Cryptography; +using Dinah.Core.Security; +using LibationFileManager; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.IO; + +namespace AudibleUtilities.Tests; + +[TestClass] +[DoNotParallelize] +public class ApiExtendedTokenSyncTests +{ + private const string SamplePrivateKey = @" +-----BEGIN RSA PRIVATE KEY----- +MIIEpgIBAAKCAQEA5nPbGSVDmlEH2tJa6kz/P2HI8IeirhfPHdmi+X/nsb9i3WNf +tmEdZxfK26IValQDXvBH17a1gr0HD6pYse1XsV2w0HxiW1RW+ZnjL8/fzPdkSOb+ +4xKlqRopCueBSdDGgAF06spZ3IeHLfEFOJX4dO1Y73p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gbWaGuBHfkwFAoGBAMOLg1MRxgKRMKavk6pU3EfyP3+J5XemWCDI +1WLtbgV5uClNmzmxBBGypQHs7jbzKPtHpULn5kB+HzdVb0clG8ZDsK7u6s5OF0pO +sBS+oVl7rF/eSeFcFhUYP26ZhsbWo3z/bERuj926VO2AxDPRTsP5o3pQPGZhY0V9 +irGgbUJrAoGBAOseS3J4BqYM4R3Hr7cRAhvzSjIkeTcDF1zTOa4FZDHBxZ6g2PNq +8ekhtfn1zPczsPTF1vNuqEISKLxaPkVPiw0mtaZQjVwpF/IOxMNjWVLp6oJf8Mm2 +BxlXqPnQ4mG66oqSFQgDEmFdMhRb2of6xL1gYYL62C80G2T7QtmPfSab +-----END RSA PRIVATE KEY----- +"; + + private const string OldAdp = "{enc:OLD}{key:1234}{iv:56789}{name:QURQVG9rZW5FbmNyeXB0aW9uS2V5}{serial:Mg==}"; + private const string NewAdp = "{enc:NEW}{key:1234}{iv:56789}{name:QURQVG9rZW5FbmNyeXB0aW9uS2V5}{serial:Mg==}"; + + [TestInitialize] + public void Initialize() => Configuration.CreateMockInstance(); + + [TestMethod] + public void SyncAccountFromDiskAfterLogin_preserves_new_tokens_when_caller_persister_is_open() + { + var file = Path.Combine(Path.GetTempPath(), $"libation_sync_test_{Guid.NewGuid():N}.json"); + try + { + var seed = new AccountsSettings(); + seed.Add(new Account("u@x.com") { IdentityTokens = MakeIdentity(OldAdp), RequiresReRegistration = true }); + using (new AccountsSettingsPersister(seed, file)) { } + + // Caller (e.g. UtilityExtensions.GetApiAsync) holds this persister open for the entire CreateAsync call. + using var callerPersister = new AccountsSettingsPersister(file); + var account = callerPersister.AccountsSettings.GetAccount("u@x.com", "us")!; + + // CreateAsync invalidates both in-memory and on-disk tokens when RequiresReRegistration is true. + account.IdentityTokens?.Invalidate(); + using (var p = new AccountsSettingsPersister(file)) + p.AccountsSettings.GetAccount(account.AccountId, "us")?.IdentityTokens?.Invalidate(); + + // Interactive EzApiCreator.GetApiAsync writes a new Identity at the account's JSONPath on disk. + _ = new IdentityPersister(MakeIdentity(NewAdp), file, account.GetIdentityTokensJsonPath()); + + // Post-login sync must load the new tokens before clearing RequiresReRegistration. + ApiExtended.SyncAccountFromDiskAfterLogin(account, file); + + Assert.IsFalse(account.RequiresReRegistration); + Assert.IsNotNull(account.IdentityTokens?.AdpToken); + + using var verifyPersister = new AccountsSettingsPersister(file); + var persisted = verifyPersister.AccountsSettings.GetAccount(account.AccountId, "us")!; + Assert.IsFalse(persisted.RequiresReRegistration); + Assert.IsNotNull(persisted.IdentityTokens?.AdpToken); + } + finally + { + if (File.Exists(file)) + File.Delete(file); + } + } + + private static Identity MakeIdentity(string adp) + { + var id = new Identity(Localization.Get("us")); + id.Update( + new PrivateKey(SamplePrivateKey), + new AdpToken(adp), + new AccessToken("Atna|x", DateTime.UtcNow.AddHours(1)), + new RefreshToken("Atnr|y"), + new List> { new("session-id", new SecretString("c")) }, + deviceSerialNumber: "0123456789abcdef0123", + deviceType: AudibleApi.Resources.DeviceType, + amazonAccountId: "amzn", + deviceName: "dev", + storeAuthenticationCookie: new SecretString("s")); + return id; + } +}