private void handlePermanentFailure(NegativeSmtpReplyException negativeResponse) throws AuthenticationFailedException { throw new AuthenticationFailedException(negativeResponse.getMessage(), negativeResponse); }
showErrorDialog( R.string.account_setup_failed_dlg_auth_message_fmt, afe.getMessage() == null ? "" : afe.getMessage()); } catch (CertificateValidationException cve) { handleCertificateValidationException(cve);
@Test public void open_withXoauth2Extension_shouldThrowOnFailure_fetchingToken() throws Exception { MockSmtpServer server = new MockSmtpServer(); server.output("220 localhost Simple Mail Transfer Service Ready"); server.expect("EHLO [127.0.0.1]"); server.output("250-localhost Hello client.localhost"); server.output("250 AUTH XOAUTH2"); server.expect("QUIT"); server.output("221 BYE"); when(oAuth2TokenProvider.getToken(anyString(), anyLong())) .thenThrow(new AuthenticationFailedException("Failed to fetch token")); SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.XOAUTH2, ConnectionSecurity.NONE); try { transport.open(); fail("Exception expected"); } catch (AuthenticationFailedException e) { assertEquals("Failed to fetch token", e.getMessage()); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
private AuthenticationFailedException handlePermanentXoauth2Failure(NegativeImapResponseException e) { Timber.v(e, "Permanent failure during XOAUTH2"); return new AuthenticationFailedException(e.getMessage(), e, e.getAlertText()); }
@Test public void open_withXoauth2Extension_shouldThrowOn401Response() throws Exception { MockSmtpServer server = new MockSmtpServer(); server.output("220 localhost Simple Mail Transfer Service Ready"); server.expect("EHLO [127.0.0.1]"); server.output("250-localhost Hello client.localhost"); server.output("250 AUTH XOAUTH2"); server.expect("AUTH XOAUTH2 dXNlcj11c2VyAWF1dGg9QmVhcmVyIG9sZFRva2VuAQE="); server.output("334 "+ XOAuth2ChallengeParserTest.STATUS_401_RESPONSE); server.expect(""); server.output("535-5.7.1 Username and Password not accepted. Learn more at"); server.output("535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 hx9sm5317360pbc.68"); server.expect("QUIT"); server.output("221 BYE"); SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.XOAUTH2, ConnectionSecurity.NONE); try { transport.open(); fail("Exception expected"); } catch (AuthenticationFailedException e) { assertEquals( "5.7.1 Username and Password not accepted. Learn more at " + "5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 hx9sm5317360pbc.68", e.getMessage()); } InOrder inOrder = inOrder(oAuth2TokenProvider); inOrder.verify(oAuth2TokenProvider).getToken(eq(USERNAME), anyLong()); inOrder.verify(oAuth2TokenProvider).invalidateToken(USERNAME); server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
private void authPlain() throws MessagingException { executeSimpleCommand("AUTH PLAIN"); try { byte[] encodedBytes = Base64.encodeBase64(("\000" + settings.getUsername() + "\000" + settings.getPassword()).getBytes()); executeSimpleCommand(new String(encodedBytes), true); } catch (Pop3ErrorResponse e) { throw new AuthenticationFailedException( "POP3 SASL auth PLAIN authentication failed: " + e.getMessage(), e); } }
"Username and Password not accepted. " + "Learn more at http://support.google.com/mail/bin/answer.py?answer=14257 hx9sm5317360pbc.68", e.getMessage());
private void authAPOP(String serverGreeting) throws MessagingException { // regex based on RFC 2449 (3.) "Greeting" String timestamp = serverGreeting.replaceFirst( "^\\+OK *(?:\\[[^\\]]+\\])?[^<]*(<[^>]*>)?[^<]*$", "$1"); if ("".equals(timestamp)) { throw new MessagingException( "APOP authentication is not supported"); } MessageDigest md; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new MessagingException( "MD5 failure during POP3 auth APOP", e); } byte[] digest = md.digest((timestamp + settings.getPassword()).getBytes()); String hexDigest = Hex.encodeHex(digest); try { executeSimpleCommand("APOP " + settings.getUsername() + " " + hexDigest, true); } catch (Pop3ErrorResponse e) { throw new AuthenticationFailedException( "POP3 APOP authentication failed: " + e.getMessage(), e); } }
@Test public void open_authXoauthWithSaslIrThrowsExeptionOn401Response() throws Exception { settings.setAuthType(AuthType.XOAUTH2); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2"); server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING); server.output("+ " + XOAuth2ChallengeParserTest.STATUS_401_RESPONSE); server.expect(""); server.output("2 NO SASL authentication failed"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail(); } catch (AuthenticationFailedException e) { assertEquals("Command: AUTHENTICATE XOAUTH2; response: #2# [NO, SASL authentication failed]", e.getMessage()); } }
private void saslAuthCramMD5() throws MessagingException, IOException { List<String> respList = executeCommand("AUTH CRAM-MD5").results; if (respList.size() != 1) { throw new MessagingException("Unable to negotiate CRAM-MD5"); } String b64Nonce = respList.get(0); String b64CRAMString = Authentication.computeCramMd5(username, password, b64Nonce); try { executeSensitiveCommand(b64CRAMString); } catch (NegativeSmtpReplyException exception) { if (exception.getReplyCode() == SMTP_AUTHENTICATION_FAILURE_ERROR_CODE) { throw new AuthenticationFailedException(exception.getMessage(), exception); } else { throw exception; } } }
@Test public void open_authCramMd5WithAuthenticationFailure_shouldThrow() throws Exception { settings.setAuthType(AuthType.CRAM_MD5); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=CRAM-MD5"); server.expect("2 AUTHENTICATE CRAM-MD5"); server.output("+ " + ByteString.encodeUtf8("<0000.000000000@example.org>").base64()); server.expect("dXNlciA2ZjdiOTcyYjk5YTI4NDk4OTRhN2YyMmE3MGRhZDg0OQ=="); server.output("2 NO Who are you?"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail("Expected exception"); } catch (AuthenticationFailedException e) { //FIXME: improve exception message assertThat(e.getMessage(), containsString("Who are you?")); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
private void saslAuthPlain() throws MessagingException, IOException { String data = Base64.encode("\000" + username + "\000" + password); try { executeSensitiveCommand("AUTH PLAIN %s", data); } catch (NegativeSmtpReplyException exception) { if (exception.getReplyCode() == SMTP_AUTHENTICATION_FAILURE_ERROR_CODE) { throw new AuthenticationFailedException("AUTH PLAIN failed (" + exception.getMessage() + ")"); } else { throw exception; } } }
@Test public void open_authXoauthWithSaslIrWithOldTokenThrowsExceptionIfRetryFails() throws Exception { settings.setAuthType(AuthType.XOAUTH2); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2"); server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING); server.output("+ r3j3krj3irj3oir3ojo"); server.expect(""); server.output("2 NO SASL authentication failed"); server.expect("3 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING_RETRY); server.output("+ 433ba3a3a"); server.expect(""); server.output("3 NO SASL authentication failed"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail(); } catch (AuthenticationFailedException e) { assertEquals("Command: AUTHENTICATE XOAUTH2; response: #3# [NO, SASL authentication failed]", e.getMessage()); } }
private void authCramMD5() throws MessagingException { String b64Nonce = executeSimpleCommand("AUTH CRAM-MD5").replace("+ ", ""); String b64CRAM = Authentication.computeCramMd5(settings.getUsername(), settings.getPassword(), b64Nonce); try { executeSimpleCommand(b64CRAM, true); } catch (Pop3ErrorResponse e) { throw new AuthenticationFailedException( "POP3 CRAM-MD5 authentication failed: " + e.getMessage(), e); } }
"5.7.1 Username and Password not accepted. Learn more at " + "5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 hx9sm5317360pbc.68", e.getMessage());
private MessagingException handleAuthenticationFailure(NegativeImapResponseException e) { ImapResponse lastResponse = e.getLastResponse(); String responseCode = ResponseCodeExtractor.getResponseCode(lastResponse); // If there's no response code we simply assume it was an authentication failure. if (responseCode == null || responseCode.equals(ResponseCodeExtractor.AUTHENTICATION_FAILED)) { if (e.wasByeResponseReceived()) { close(); } return new AuthenticationFailedException(e.getMessage()); } else { close(); return e; } }
@Test public void open_authPlainAndLoginFallbackWithAuthenticationFailure_shouldThrow() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=PLAIN"); server.expect("2 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("2 NO Login Failure"); server.expect("3 LOGIN \"" + USERNAME + "\" \"" + PASSWORD + "\""); server.output("3 NO Go away"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail("Expected exception"); } catch (AuthenticationFailedException e) { //FIXME: improve exception message assertThat(e.getMessage(), containsString("Go away")); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
private void login() throws MessagingException { executeSimpleCommand(USER_COMMAND + " " + settings.getUsername()); try { executeSimpleCommand(PASS_COMMAND + " " + settings.getPassword(), true); } catch (Pop3ErrorResponse e) { throw new AuthenticationFailedException( "POP3 login authentication failed: " + e.getMessage(), e); } }
@Test public void open_authPlainWithByeResponseAndConnectionClose_shouldThrowAuthenticationFailedException() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=PLAIN"); server.expect("2 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("* BYE Go away"); server.output("2 NO Login Failure"); server.closeConnection(); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail("Expected exception"); } catch (AuthenticationFailedException e) { //FIXME: improve exception message assertThat(e.getMessage(), containsString("Login Failure")); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
@Override public String getToken(String username, long timeoutMillis) throws AuthenticationFailedException { assertEquals(USERNAME, username); assertEquals(OAUTH2_TIMEOUT, timeoutMillis); switch (invalidationCount) { case 0: { return XOAUTH_TOKEN; } case 1: { return XOAUTH_ANOTHER_TOKEN; } default: { throw new AuthenticationFailedException("Ran out of auth tokens. invalidateToken() called too often?"); } } }