From 1c3416c1da710c883b06efc57d89cfae53b0e8cc Mon Sep 17 00:00:00 2001 From: DenBond7 Date: Wed, 26 Aug 2020 12:26:17 +0300 Subject: [PATCH] Improved handling errors during the token refreshing.| #716 --- .../accounts/FlowcryptAccountAuthenticator.kt | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/accounts/FlowcryptAccountAuthenticator.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/accounts/FlowcryptAccountAuthenticator.kt index fb6da237d6..9fda7dbe02 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/accounts/FlowcryptAccountAuthenticator.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/accounts/FlowcryptAccountAuthenticator.kt @@ -68,18 +68,25 @@ class FlowcryptAccountAuthenticator(val context: Context) : AbstractAccountAuthe putString(AccountManager.KEY_ERROR_MESSAGE, "Refresh token is wrong or was corrupted!") } } - val refreshToken = KeyStoreCryptoManager.decrypt(encryptedRefreshToken) - val apiService = ApiHelper.getInstance(context).retrofit.create(ApiService::class.java) - val apiResponse = apiService.refreshMicrosoftOAuth2Token(refreshToken).execute() - if (apiResponse.isSuccessful) { - val tokenResponse = apiResponse.body() - authToken = KeyStoreCryptoManager.encrypt(tokenResponse?.accessToken) - accountManager.setAuthToken(account, authTokenType, authToken) - accountManager.setUserData(account, KEY_REFRESH_TOKEN, KeyStoreCryptoManager.encrypt(tokenResponse?.refreshToken)) - accountManager.setUserData(account, KEY_EXPIRES_AT, OAuth2Helper.getExpiresAtTime(tokenResponse?.expiresIn).toString()) - } else return Bundle().apply { - putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_AUTHENTICATION) - putString(AccountManager.KEY_ERROR_MESSAGE, "Couldn't fetch an access token") + try { + val refreshToken = KeyStoreCryptoManager.decrypt(encryptedRefreshToken) + val apiService = ApiHelper.getInstance(context).retrofit.create(ApiService::class.java) + val apiResponse = apiService.refreshMicrosoftOAuth2Token(refreshToken + " 12").execute() + if (apiResponse.isSuccessful) { + val tokenResponse = apiResponse.body() + authToken = KeyStoreCryptoManager.encrypt(tokenResponse?.accessToken) + accountManager.setAuthToken(account, authTokenType, authToken) + accountManager.setUserData(account, KEY_REFRESH_TOKEN, KeyStoreCryptoManager.encrypt(tokenResponse?.refreshToken)) + accountManager.setUserData(account, KEY_EXPIRES_AT, OAuth2Helper.getExpiresAtTime(tokenResponse?.expiresIn).toString()) + } else return Bundle().apply { + putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_AUTHENTICATION) + putString(AccountManager.KEY_ERROR_MESSAGE, "Couldn't fetch an access token") + } + } catch (e: Exception) { + return Bundle().apply { + putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_AUTHENTICATION) + putString(AccountManager.KEY_ERROR_MESSAGE, e.message) + } } }