private AuthenticationFailedException handlePermanentXoauth2Failure(NegativeImapResponseException e) { Timber.v(e, "Permanent failure during XOAUTH2"); return new AuthenticationFailedException(e.getMessage(), e, e.getAlertText()); }
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; } }
private List<ImapResponse> saslAuthExternal() throws IOException, MessagingException { try { String command = Commands.AUTHENTICATE_EXTERNAL + " " + Base64.encode(settings.getUsername()); return executeSimpleCommand(command, false); } catch (NegativeImapResponseException e) { /* * Provide notification to the user of a problem authenticating * using client certificates. We don't use an * AuthenticationFailedException because that would trigger a * "Username or password incorrect" notification in * AccountSetupCheckSettings. */ throw new CertificateValidationException(e.getMessage()); } }
public String getAlertText() { if (alertText == null) { ImapResponse lastResponse = getLastResponse(); alertText = AlertResponse.getAlertText(lastResponse); } return alertText; }
@Test public void readStatusResponse_withNoResponseAndAlertText_shouldThrowWithAlertText() throws Exception { ImapResponseParser parser = createParser("1 NO [ALERT] Access denied\r\n"); try { parser.readStatusResponse("1", "COMMAND", "[logId]", null); fail("Expected exception"); } catch (NegativeImapResponseException e) { assertEquals("Access denied", e.getAlertText()); } }
throw new NegativeImapResponseException(message, responses);
@Test public void readStatusResponse_withNoResponse_shouldThrow() throws Exception { ImapResponseParser parser = createParser("1 NO\r\n"); try { parser.readStatusResponse("1", "COMMAND", "[logId]", null); fail("Expected exception"); } catch (NegativeImapResponseException e) { assertEquals("Command: COMMAND; response: #1# [NO]", e.getMessage()); } }
@Test public void executeSingleCommand_withNoResponse_shouldThrowNegativeImapResponseException() throws Exception { MockImapServer server = new MockImapServer(); simpleOpenDialog(server, ""); server.expect("4 CREATE Folder"); server.output("4 NO Folder exists"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.executeSimpleCommand("CREATE Folder"); fail("Expected exception"); } catch (NegativeImapResponseException e) { assertEquals("Folder exists", e.getLastResponse().getString(1)); } server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_withNegativeResponseToStartTlsCommand_shouldThrow() throws Exception { settings.setAuthType(AuthType.PLAIN); settings.setConnectionSecurity(ConnectionSecurity.STARTTLS_REQUIRED); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "STARTTLS"); server.expect("2 STARTTLS"); server.output("2 NO"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail("Expected exception"); } catch (NegativeImapResponseException e) { assertEquals(e.getMessage(), "Command: STARTTLS; response: #2# [NO]"); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
@Test public void open_authPlainFailureAndDisconnect_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 [UNAVAILABLE] Maximum number of connections from user+IP exceeded"); server.closeConnection(); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail("Expected exception"); } catch (NegativeImapResponseException e) { assertThat(e.getMessage(), containsString("Maximum number of connections from user+IP exceeded")); } assertFalse(imapConnection.isConnected()); server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }