Skip to content

Commit

Permalink
Handled the auth failure error.| #716
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Aug 26, 2020
1 parent 31ab716 commit b636360
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.flowcrypt.email.BuildConfig
import com.flowcrypt.email.R
import com.flowcrypt.email.api.oauth.OAuth2Helper
import com.flowcrypt.email.api.retrofit.ApiHelper
import com.flowcrypt.email.api.retrofit.ApiService
Expand Down Expand Up @@ -65,7 +66,7 @@ class FlowcryptAccountAuthenticator(val context: Context) : AbstractAccountAuthe
if (encryptedRefreshToken.isNullOrEmpty()) {
return Bundle().apply {
putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_ARGUMENTS)
putString(AccountManager.KEY_ERROR_MESSAGE, "Refresh token is wrong or was corrupted!")
putString(AccountManager.KEY_ERROR_MESSAGE, context.getString(R.string.refrech_token_was_corrupted))
}
}
try {
Expand All @@ -80,7 +81,7 @@ class FlowcryptAccountAuthenticator(val context: Context) : AbstractAccountAuthe
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")
putString(AccountManager.KEY_ERROR_MESSAGE, context.getString(R.string.could_not_fetch_access_token))
}
} catch (e: Exception) {
return Bundle().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.flowcrypt.email.jetpack.workmanager

import android.accounts.AuthenticatorException
import android.content.Context
import android.net.Uri
import android.text.TextUtils
Expand Down Expand Up @@ -123,13 +124,12 @@ class MessagesSenderWorker(context: Context, params: WorkerParameters) : Corouti
return@withContext Result.success()
} catch (e: UserRecoverableAuthException) {
e.printStackTrace()
val account = roomDatabase.accountDao().getActiveAccountSuspend()
roomDatabase.msgDao().changeMsgsStateSuspend(
account = account?.email,
label = JavaEmailConstants.FOLDER_OUTBOX,
oldValue = MessageState.QUEUED.value,
newValues = MessageState.AUTH_FAILURE.value
)
markMsgsWithAuthFailureState(roomDatabase)

return@withContext Result.failure()
} catch (e: AuthenticatorException) {
e.printStackTrace()
markMsgsWithAuthFailureState(roomDatabase)

return@withContext Result.failure()
} catch (e: Exception) {
Expand All @@ -149,6 +149,16 @@ class MessagesSenderWorker(context: Context, params: WorkerParameters) : Corouti
}
}

private suspend fun markMsgsWithAuthFailureState(roomDatabase: FlowCryptRoomDatabase) {
val account = roomDatabase.accountDao().getActiveAccountSuspend()
roomDatabase.msgDao().changeMsgsStateSuspend(
account = account?.email,
label = JavaEmailConstants.FOLDER_OUTBOX,
oldValue = MessageState.QUEUED.value,
newValues = MessageState.AUTH_FAILURE.value
)
}

private fun genForegroundInfo(account: AccountEntity): ForegroundInfo {
val title = applicationContext.getString(R.string.sending_email)
val notification = NotificationCompat.Builder(applicationContext,
Expand Down Expand Up @@ -519,8 +529,8 @@ class MessagesSenderWorker(context: Context, params: WorkerParameters) : Corouti
/**
* The [DataSource] realization for a file which received from [Uri]
*/
private class AttachmentInfoDataSource internal constructor(private val context: Context,
private val att: AttachmentInfo) : DataSource {
private class AttachmentInfoDataSource(private val context: Context,
private val att: AttachmentInfo) : DataSource {

override fun getInputStream(): InputStream? {
val inputStream: InputStream? = if (att.uri == null) {
Expand Down
2 changes: 2 additions & 0 deletions FlowCrypt/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,6 @@
<string name="oauth_error">OAuth error</string>
<string name="loading_oauth_server_configuration">Loading OAuth server configuration&#8230;</string>
<string name="open_side_menu_and_do_logout">Please open the side menu and do logout to remove %1$s</string>
<string name="refrech_token_was_corrupted">The refresh token is wrong or was corrupted!</string>
<string name="could_not_fetch_access_token">Couldn\'t fetch an access token. Correct credentials needed. Please logout and login again.</string>
</resources>

0 comments on commit b636360

Please sign in to comment.