From 923453ad3f5abb6a90918e1b7c2be5071a23f995 Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 14 Jul 2022 17:06:31 +0200 Subject: [PATCH] Don't remove OAuth state on temporary errors --- .../k9/backends/RealOAuth2TokenProvider.kt | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/app/k9mail/src/main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt b/app/k9mail/src/main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt index 3fc93963c..48402de42 100644 --- a/app/k9mail/src/main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt +++ b/app/k9mail/src/main/java/com/fsck/k9/backends/RealOAuth2TokenProvider.kt @@ -5,10 +5,13 @@ import com.fsck.k9.Account import com.fsck.k9.mail.AuthenticationFailedException import com.fsck.k9.mail.oauth.OAuth2TokenProvider import com.fsck.k9.preferences.AccountManager +import java.io.IOException import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit import net.openid.appauth.AuthState import net.openid.appauth.AuthorizationException +import net.openid.appauth.AuthorizationException.AuthorizationRequestErrors +import net.openid.appauth.AuthorizationException.GeneralErrors import net.openid.appauth.AuthorizationService class RealOAuth2TokenProvider( @@ -42,21 +45,26 @@ class RealOAuth2TokenProvider( latch.await(timeoutMillis, TimeUnit.MILLISECONDS) - if (exception != null) { + val authException = exception + if (authException == GeneralErrors.NETWORK_ERROR || + authException == GeneralErrors.SERVER_ERROR || + authException == AuthorizationRequestErrors.SERVER_ERROR || + authException == AuthorizationRequestErrors.TEMPORARILY_UNAVAILABLE + ) { + throw IOException("Error while fetching an access token", authException) + } else if (authException != null) { account.oAuthState = null accountManager.saveAccount(account) - } else if (token != oldAccessToken) { - requestFreshToken = false - account.oAuthState = authState.jsonSerializeString() - accountManager.saveAccount(account) - } - exception?.let { authException -> throw AuthenticationFailedException( message = "Failed to fetch an access token", throwable = authException, messageFromServer = authException.error ) + } else if (token != oldAccessToken) { + requestFreshToken = false + account.oAuthState = authState.jsonSerializeString() + accountManager.saveAccount(account) } return token ?: throw AuthenticationFailedException("Failed to fetch an access token")