mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
fix(stores): drop expired Epic and Amazon tokens when a refresh fails (#804)
A failed token refresh returned the stored access token even after it had expired, so the next request failed with a generic error instead of asking to sign in again. The stored token is now returned only while it is still valid. The Epic docs now say that achievements are sent with Epic's launcher user agent, which the achievements service requires.
This commit is contained in:
1 parent
7ad3c60efe
commit
c8ac2d1e91
4 files changed
+17
-9
No files matched your search
@@ -70,6 +70,9 @@ object AmazonCredentialStore {
|
||||
private fun fresh(a: Account): Boolean =
|
||||
a.expiresAt - System.currentTimeMillis() > REFRESH_MARGIN_MS || a.refreshToken.isEmpty()
|
||||
|
||||
private fun unexpired(a: Account): String? =
|
||||
a.accessToken.takeIf { a.expiresAt <= 0 || a.expiresAt > System.currentTimeMillis() }
|
||||
|
||||
private fun refreshLocked(context: Context, a: Account): String? {
|
||||
if (a.refreshToken.isEmpty()) return null
|
||||
val answer = AmazonNative.call("auth.refresh", JSONObject().put("refreshToken", a.refreshToken))
|
||||
@@ -90,7 +93,7 @@ object AmazonCredentialStore {
|
||||
synchronized(refreshLock) {
|
||||
val again = load(context) ?: return null
|
||||
if (fresh(again)) return again.accessToken
|
||||
return refreshLocked(context, again) ?: again.accessToken
|
||||
return refreshLocked(context, again) ?: unexpired(again)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +85,9 @@ object EpicCredentialStore {
|
||||
private fun fresh(c: Credentials): Boolean =
|
||||
c.expiresAt - System.currentTimeMillis() > REFRESH_MARGIN_MS || c.refreshToken.isNullOrEmpty()
|
||||
|
||||
private fun unexpired(c: Credentials): String? =
|
||||
c.accessToken.takeIf { c.expiresAt <= 0 || c.expiresAt > System.currentTimeMillis() }
|
||||
|
||||
@JvmStatic
|
||||
fun getValidAccessToken(context: Context): String? {
|
||||
val current = load(context) ?: return null
|
||||
@@ -96,7 +99,7 @@ object EpicCredentialStore {
|
||||
val result = answer?.let(TokenResult::from)
|
||||
if (result == null) {
|
||||
Log.w(TAG, "token refresh failed: ${answer?.optString("error").orEmpty().ifEmpty { "no answer" }}")
|
||||
return again.accessToken
|
||||
return unexpired(again)
|
||||
}
|
||||
save(context, result)
|
||||
return result.accessToken
|
||||
|
||||
Reference in new issue
Block a user