From cedebe4ddaf9698736c2257511ded6afe5977d3f Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Tue, 30 Aug 2022 14:23:55 +0600 Subject: [PATCH 1/2] 5356-Not_show_unwanted_oauth2_failure_notification issue: https://gitlab.e.foundation/e/backlog/-/issues/5356 --- .../k9/controller/MessagingController.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java index eb78dfe033..933258aba6 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -681,6 +681,22 @@ public class MessagingController { notificationController.showAuthenticationErrorNotification(account, incoming); } + public void handleAuthenticationFailure(Account account, boolean incoming, Exception exception) { + if (account.shouldMigrateToOAuth()) { + migrateAccountToOAuth(account); + } + + if (shouldShowErrorNotification(exception)) { + notificationController.showAuthenticationErrorNotification(account, incoming); + } + } + + // on network switch, sometime mail app failed to authenticate with auth2 accounts, but other operations works perfectly. + // It is ignorable, because oauth2 authentication is handled via accountManager + private boolean shouldShowErrorNotification(Exception exception) { + return !(exception instanceof AuthenticationFailedException && exception.getLocalizedMessage().equalsIgnoreCase("Command: AUTHENTICATE OAUTHBEARER; response: #2# [NO, [AUTHENTICATIONFAILED], Invalid credentials (Failure)]")); + } + private void migrateAccountToOAuth(Account account) { account.setIncomingServerSettings(account.getIncomingServerSettings().newAuthenticationType(AuthType.XOAUTH2)); account.setOutgoingServerSettings(account.getOutgoingServerSettings().newAuthenticationType(AuthType.XOAUTH2)); @@ -691,7 +707,7 @@ public class MessagingController { public void handleException(Account account, Exception exception) { if (exception instanceof AuthenticationFailedException) { - handleAuthenticationFailure(account, true); + handleAuthenticationFailure(account, true, exception); } else { notifyUserIfCertificateProblem(account, exception, true); } @@ -1601,7 +1617,7 @@ public class MessagingController { lastFailure = e; wasPermanentFailure = false; - handleAuthenticationFailure(account, false); + handleAuthenticationFailure(account, false, e); handleSendFailure(account, localFolder, message, e); } catch (CertificateValidationException e) { outboxStateRepository.decrementSendAttempts(messageId); @@ -2781,7 +2797,7 @@ public class MessagingController { syncFailed = true; if (exception instanceof AuthenticationFailedException) { - handleAuthenticationFailure(account, true); + handleAuthenticationFailure(account, true, exception); } else { notifyUserIfCertificateProblem(account, exception, true); } -- GitLab From 562850d2db4f773ac8bb73d96e0082d912586ece Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Thu, 1 Sep 2022 19:12:39 +0600 Subject: [PATCH 2/2] ignore all oauth2 authentionFailedExceptions --- .../com/fsck/k9/controller/MessagingController.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java index 933258aba6..8419ef4ff4 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -686,15 +686,19 @@ public class MessagingController { migrateAccountToOAuth(account); } - if (shouldShowErrorNotification(exception)) { + if (shouldShowErrorNotification(account, exception)) { notificationController.showAuthenticationErrorNotification(account, incoming); } } // on network switch, sometime mail app failed to authenticate with auth2 accounts, but other operations works perfectly. // It is ignorable, because oauth2 authentication is handled via accountManager - private boolean shouldShowErrorNotification(Exception exception) { - return !(exception instanceof AuthenticationFailedException && exception.getLocalizedMessage().equalsIgnoreCase("Command: AUTHENTICATE OAUTHBEARER; response: #2# [NO, [AUTHENTICATIONFAILED], Invalid credentials (Failure)]")); + private boolean shouldShowErrorNotification(Account account, Exception exception) { + if (account.getIncomingServerSettings().authenticationType != AuthType.XOAUTH2) { + return true; + } + + return !(exception instanceof AuthenticationFailedException); } private void migrateAccountToOAuth(Account account) { -- GitLab