From 26491676faee56011d9c255d78f0565e4b14f907 Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Sat, 22 Feb 2014 17:18:31 -0500 Subject: [PATCH] Retrict use of AuthenticationFailedException In AccountSetupCheckSettings.onCreate(Bundle), the account settings are checked. If an AuthenticationFailedException occurs, A dialog saying "Username or password incorrect." pops up. We don't want to say this if the cause is not related to an incorrect user name or password. Instead we want to say the more generic "Cannot connect to server" which pops up for other exception types. This commit attempts to eliminate the use of AuthenticationFailedException in instances where it could not be due to "Username or password incorrect." --- src/com/fsck/k9/mail/Authentication.java | 14 ++--- src/com/fsck/k9/mail/store/ImapStore.java | 51 ++++++++----------- src/com/fsck/k9/mail/store/Pop3Store.java | 20 ++++---- .../fsck/k9/mail/transport/SmtpTransport.java | 6 +-- 4 files changed, 43 insertions(+), 48 deletions(-) diff --git a/src/com/fsck/k9/mail/Authentication.java b/src/com/fsck/k9/mail/Authentication.java index a4a9b4fbf..2872b1b58 100644 --- a/src/com/fsck/k9/mail/Authentication.java +++ b/src/com/fsck/k9/mail/Authentication.java @@ -17,21 +17,21 @@ public class Authentication { * @param b64Nonce The nonce as base64-encoded string. * @return The CRAM-MD5 response. * - * @throws AuthenticationFailedException If something went wrong. + * @throws MessagingException If something went wrong. * * @see Authentication#computeCramMd5Bytes(String, String, byte[]) */ public static String computeCramMd5(String username, String password, String b64Nonce) - throws AuthenticationFailedException { + throws MessagingException { try { byte[] b64NonceBytes = b64Nonce.getBytes(US_ASCII); byte[] b64CRAM = computeCramMd5Bytes(username, password, b64NonceBytes); return new String(b64CRAM, US_ASCII); - } catch (AuthenticationFailedException e) { + } catch (MessagingException e) { throw e; } catch (Exception e) { - throw new AuthenticationFailedException("This shouldn't happen", e); + throw new MessagingException("This shouldn't happen", e); } } @@ -44,12 +44,12 @@ public class Authentication { * @param b64Nonce The nonce as base64-encoded byte array. * @return The CRAM-MD5 response as byte array. * - * @throws AuthenticationFailedException If something went wrong. + * @throws MessagingException If something went wrong. * * @see RFC 2195 */ public static byte[] computeCramMd5Bytes(String username, String password, byte[] b64Nonce) - throws AuthenticationFailedException { + throws MessagingException { try { byte[] nonce = Base64.decodeBase64(b64Nonce); @@ -79,7 +79,7 @@ public class Authentication { return b64CRAM; } catch (Exception e) { - throw new AuthenticationFailedException("Something went wrong during CRAM-MD5 computation", e); + throw new MessagingException("Something went wrong during CRAM-MD5 computation", e); } } } diff --git a/src/com/fsck/k9/mail/store/ImapStore.java b/src/com/fsck/k9/mail/store/ImapStore.java index 0d4d077bc..f557a852e 100644 --- a/src/com/fsck/k9/mail/store/ImapStore.java +++ b/src/com/fsck/k9/mail/store/ImapStore.java @@ -2537,39 +2537,32 @@ public class ImapStore extends Store { mOut = new BufferedOutputStream(mOut, 1024); - try { - switch (mSettings.getAuthType()) { - case CRAM_MD5: - if (hasCapability(CAPABILITY_AUTH_CRAM_MD5)) { - authCramMD5(); - } else { - throw new MessagingException( - "Server doesn't support encrypted passwords using CRAM-MD5."); - } - break; - - case PLAIN: - if (hasCapability(CAPABILITY_AUTH_PLAIN)) { - saslAuthPlain(); - } else if (!hasCapability(CAPABILITY_LOGINDISABLED)) { - login(); - } else { - throw new MessagingException( - "Server doesn't support unencrypted passwords using AUTH=PLAIN and LOGIN is disabled."); - } - break; - - default: + switch (mSettings.getAuthType()) { + case CRAM_MD5: + if (hasCapability(CAPABILITY_AUTH_CRAM_MD5)) { + authCramMD5(); + } else { throw new MessagingException( - "Unhandled authentication method found in the server settings (bug)."); + "Server doesn't support encrypted passwords using CRAM-MD5."); } - authSuccess = true; - } catch (ImapException ie) { - throw new AuthenticationFailedException(ie.getAlertText(), ie); + break; - } catch (MessagingException me) { - throw new AuthenticationFailedException(null, me); + case PLAIN: + if (hasCapability(CAPABILITY_AUTH_PLAIN)) { + saslAuthPlain(); + } else if (!hasCapability(CAPABILITY_LOGINDISABLED)) { + login(); + } else { + throw new MessagingException( + "Server doesn't support unencrypted passwords using AUTH=PLAIN and LOGIN is disabled."); + } + break; + + default: + throw new MessagingException( + "Unhandled authentication method found in the server settings (bug)."); } + authSuccess = true; if (K9.DEBUG) { Log.d(K9.LOG_TAG, CAPABILITY_COMPRESS_DEFLATE + " = " + hasCapability(CAPABILITY_COMPRESS_DEFLATE)); } diff --git a/src/com/fsck/k9/mail/store/Pop3Store.java b/src/com/fsck/k9/mail/store/Pop3Store.java index bdc0ef5bf..361f4258a 100644 --- a/src/com/fsck/k9/mail/store/Pop3Store.java +++ b/src/com/fsck/k9/mail/store/Pop3Store.java @@ -377,21 +377,23 @@ public class Pop3Store extends Store { } if (mAuthType == AuthType.CRAM_MD5) { + String b64Nonce = executeSimpleCommand("AUTH CRAM-MD5").replace("+ ", ""); + + String b64CRAM = Authentication.computeCramMd5(mUsername, mPassword, b64Nonce); try { - String b64Nonce = executeSimpleCommand("AUTH CRAM-MD5").replace("+ ", ""); - - String b64CRAM = Authentication.computeCramMd5(mUsername, mPassword, b64Nonce); executeSimpleCommand(b64CRAM); - - } catch (MessagingException me) { - throw new AuthenticationFailedException(null, me); + } catch (Pop3ErrorResponse e) { + throw new AuthenticationFailedException( + "POP3 CRAM-MD5 authentication failed: " + + e.getMessage(), e); } } else { + executeSimpleCommand(USER_COMMAND + " " + mUsername); try { - executeSimpleCommand(USER_COMMAND + " " + mUsername); executeSimpleCommand(PASS_COMMAND + " " + mPassword, true); - } catch (MessagingException me) { - throw new AuthenticationFailedException(null, me); + } catch (Pop3ErrorResponse e) { + throw new AuthenticationFailedException( + "POP3 login authentication failed: " + e.getMessage(), e); } } diff --git a/src/com/fsck/k9/mail/transport/SmtpTransport.java b/src/com/fsck/k9/mail/transport/SmtpTransport.java index d7478c89b..47ed37e36 100644 --- a/src/com/fsck/k9/mail/transport/SmtpTransport.java +++ b/src/com/fsck/k9/mail/transport/SmtpTransport.java @@ -714,7 +714,7 @@ public class SmtpTransport extends Transport { List respList = executeSimpleCommand("AUTH CRAM-MD5"); if (respList.size() != 1) { - throw new AuthenticationFailedException("Unable to negotiate CRAM-MD5"); + throw new MessagingException("Unable to negotiate CRAM-MD5"); } String b64Nonce = respList.get(0); @@ -722,8 +722,8 @@ public class SmtpTransport extends Transport { try { executeSimpleCommand(b64CRAMString, true); - } catch (MessagingException me) { - throw new AuthenticationFailedException("Unable to negotiate MD5 CRAM"); + } catch (NegativeSmtpReplyException exception) { + throw new AuthenticationFailedException(exception.getMessage(), exception); } }