Always refresh token, regardless of expiration date.

This commit is contained in:
Michael Bucari-Tovo 2022-06-10 19:34:49 -06:00
parent 52193933b2
commit 60e96572ff

View File

@ -45,7 +45,6 @@ namespace AudibleUtilities
[JsonProperty("activation_bytes")] [JsonProperty("activation_bytes")]
public string ActivationBytes { get; private set; } public string ActivationBytes { get; private set; }
[JsonIgnore] [JsonIgnore]
public Dictionary<string, string> WebsiteCookies public Dictionary<string, string> WebsiteCookies
{ {
@ -67,7 +66,6 @@ namespace AudibleUtilities
private set => _expires = new DateTimeOffset(value).ToUnixTimeMilliseconds() / 1000d; private set => _expires = new DateTimeOffset(value).ToUnixTimeMilliseconds() / 1000d;
} }
[JsonIgnore] public ISystemDateTime SystemDateTime { get; } = new SystemDateTime(); [JsonIgnore] public ISystemDateTime SystemDateTime { get; } = new SystemDateTime();
[JsonIgnore] public Locale Locale => Localization.Get(LocaleCode); [JsonIgnore] public Locale Locale => Localization.Get(LocaleCode);
[JsonIgnore] public string DeviceSerialNumber => DeviceInfo.DeviceSerialNumber; [JsonIgnore] public string DeviceSerialNumber => DeviceInfo.DeviceSerialNumber;
@ -83,11 +81,6 @@ namespace AudibleUtilities
public Task<PrivateKey> GetPrivateKeyAsync() public Task<PrivateKey> GetPrivateKeyAsync()
=> Task.FromResult(new PrivateKey(DevicePrivateKey)); => Task.FromResult(new PrivateKey(DevicePrivateKey));
} }
public partial class StoreAuthenticationCookie
{
[JsonProperty("cookie")]
public string Cookie { get; set; }
}
public partial class CustomerInfo public partial class CustomerInfo
{ {
@ -124,31 +117,36 @@ namespace AudibleUtilities
public static Mkb79Auth FromJson(string json) public static Mkb79Auth FromJson(string json)
=> JsonConvert.DeserializeObject<Mkb79Auth>(json, Converter.Settings); => JsonConvert.DeserializeObject<Mkb79Auth>(json, Converter.Settings);
public string ToJson()
=> JObject.Parse(JsonConvert.SerializeObject(this, Converter.Settings)).ToString(Formatting.Indented);
public async Task<Account> ToAccountAsync() public async Task<Account> ToAccountAsync()
{ {
var api = new Api(this);
if ((DateTime.Now - AccessTokenExpires).TotalMinutes >= 59)
{
var authorize = new Authorize(Locale);
var newToken = await authorize.RefreshAccessTokenAsync(new RefreshToken(RefreshToken));
AccessToken = newToken.TokenValue;
AccessTokenExpires = newToken.Expires;
}
var email = await api.GetEmailAsync();
var account = new Account(email);
var privateKey = await GetPrivateKeyAsync(); var privateKey = await GetPrivateKeyAsync();
var adpToken = await GetAdpTokenAsync(); var adpToken = await GetAdpTokenAsync();
var accessToken = await GetAccessTokenAsync(); var accessToken = await GetAccessTokenAsync();
var refreshToken = new RefreshToken(RefreshToken);
var cookies = WebsiteCookies.Select(c => new KeyValuePair<string, string>(c.Key, c.Value)); var cookies = WebsiteCookies.Select(c => new KeyValuePair<string, string>(c.Key, c.Value));
account.IdentityTokens = new Identity(Locale); var authorize = new Authorize(Locale);
var newToken = await authorize.RefreshAccessTokenAsync(refreshToken);
AccessToken = newToken.TokenValue;
AccessTokenExpires = newToken.Expires;
var api = new Api(this);
var email = await api.GetEmailAsync();
var account = new Account(email)
{
DecryptKey = ActivationBytes,
AccountName = $"{email} - {Locale.Name}",
IdentityTokens = new Identity(Locale)
};
account.IdentityTokens.Update( account.IdentityTokens.Update(
privateKey, privateKey,
adpToken, accessToken, adpToken,
new RefreshToken(RefreshToken), accessToken,
refreshToken,
cookies, cookies,
DeviceSerialNumber, DeviceSerialNumber,
DeviceType, DeviceType,
@ -156,9 +154,6 @@ namespace AudibleUtilities
DeviceInfo.DeviceName, DeviceInfo.DeviceName,
StoreAuthenticationCookie); StoreAuthenticationCookie);
account.DecryptKey = ActivationBytes;
account.AccountName = $"{email} - {Locale.Name}";
return account; return account;
} }
@ -185,7 +180,7 @@ namespace AudibleUtilities
DevicePrivateKey = account.IdentityTokens.PrivateKey, DevicePrivateKey = account.IdentityTokens.PrivateKey,
AccessTokenExpires = account.IdentityTokens.ExistingAccessToken.Expires, AccessTokenExpires = account.IdentityTokens.ExistingAccessToken.Expires,
LocaleCode = account.Locale.CountryCode, LocaleCode = account.Locale.CountryCode,
RefreshToken = account.IdentityTokens.RefreshToken.Value, RefreshToken = account.IdentityTokens.RefreshToken.Value,
StoreAuthenticationCookie = account.IdentityTokens.StoreAuthenticationCookie, StoreAuthenticationCookie = account.IdentityTokens.StoreAuthenticationCookie,
WebsiteCookies = new(account.IdentityTokens.Cookies.ToKeyValuePair()), WebsiteCookies = new(account.IdentityTokens.Cookies.ToKeyValuePair()),
}; };