Search in sources :

Example 1 with OAuth2TokenProvider

use of com.fsck.k9.mail.oauth.OAuth2TokenProvider in project k-9 by k9mail.

the class RemoteStore method getInstance.

/**
     * Get an instance of a remote mail store.
     */
public static synchronized Store getInstance(Context context, StoreConfig storeConfig) throws MessagingException {
    String uri = storeConfig.getStoreUri();
    if (uri.startsWith("local")) {
        throw new RuntimeException("Asked to get non-local Store object but given LocalStore URI");
    }
    Store store = sStores.get(uri);
    if (store == null) {
        if (uri.startsWith("imap")) {
            OAuth2TokenProvider oAuth2TokenProvider = null;
            store = new ImapStore(storeConfig, new DefaultTrustedSocketFactory(context), (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE), oAuth2TokenProvider);
        } else if (uri.startsWith("pop3")) {
            store = new Pop3Store(storeConfig, new DefaultTrustedSocketFactory(context));
        } else if (uri.startsWith("webdav")) {
            store = new WebDavStore(storeConfig, new WebDavHttpClient.WebDavHttpClientFactory());
        }
        if (store != null) {
            sStores.put(uri, store);
        }
    }
    if (store == null) {
        throw new MessagingException("Unable to locate an applicable Store for " + uri);
    }
    return store;
}
Also used : Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) DefaultTrustedSocketFactory(com.fsck.k9.mail.ssl.DefaultTrustedSocketFactory) MessagingException(com.fsck.k9.mail.MessagingException) OAuth2TokenProvider(com.fsck.k9.mail.oauth.OAuth2TokenProvider) ConnectivityManager(android.net.ConnectivityManager) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Store(com.fsck.k9.mail.Store) WebDavStore(com.fsck.k9.mail.store.webdav.WebDavStore) ImapStore(com.fsck.k9.mail.store.imap.ImapStore) ImapStore(com.fsck.k9.mail.store.imap.ImapStore) WebDavStore(com.fsck.k9.mail.store.webdav.WebDavStore)

Example 2 with OAuth2TokenProvider

use of com.fsck.k9.mail.oauth.OAuth2TokenProvider in project k-9 by k9mail.

the class ImapConnectionTest method open_authXoauthWithSaslIrInvalidatesAndRetriesNewTokenOnMissingStatusJsonResponse.

@Test
public void open_authXoauthWithSaslIrInvalidatesAndRetriesNewTokenOnMissingStatusJsonResponse() throws Exception {
    settings.setAuthType(AuthType.XOAUTH2);
    when(oAuth2TokenProvider.getToken("user", OAuth2TokenProvider.OAUTH2_TIMEOUT)).thenReturn("token").thenReturn("token2");
    MockImapServer server = new MockImapServer();
    preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2");
    server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING);
    server.output("+ " + XOAuth2ChallengeParserTest.MISSING_STATUS_RESPONSE);
    server.expect("");
    server.output("2 NO SASL authentication failed");
    server.expect("3 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING_RETRY);
    server.output("3 OK Success");
    simplePostAuthenticationDialog(server, "4");
    ImapConnection imapConnection = startServerAndCreateImapConnection(server);
    imapConnection.open();
    server.verifyConnectionStillOpen();
    server.verifyInteractionCompleted();
    InOrder inOrder = inOrder(oAuth2TokenProvider);
    inOrder.verify(oAuth2TokenProvider).getToken("user", OAuth2TokenProvider.OAUTH2_TIMEOUT);
    inOrder.verify(oAuth2TokenProvider).invalidateToken("user");
    inOrder.verify(oAuth2TokenProvider).getToken("user", OAuth2TokenProvider.OAUTH2_TIMEOUT);
}
Also used : InOrder(org.mockito.InOrder) MockImapServer(com.fsck.k9.mail.store.imap.mockserver.MockImapServer) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Example 3 with OAuth2TokenProvider

use of com.fsck.k9.mail.oauth.OAuth2TokenProvider in project k-9 by k9mail.

the class SmtpTransportTest method open_withXoauth2Extension_shouldInvalidateAndRetryOnInvalidJsonResponse.

@Test
public void open_withXoauth2Extension_shouldInvalidateAndRetryOnInvalidJsonResponse() throws Exception {
    MockSmtpServer server = new MockSmtpServer();
    server.output("220 localhost Simple Mail Transfer Service Ready");
    server.expect("EHLO localhost");
    server.output("250-localhost Hello client.localhost");
    server.output("250 AUTH XOAUTH2");
    server.expect("AUTH XOAUTH2 dXNlcj11c2VyAWF1dGg9QmVhcmVyIG9sZFRva2VuAQE=");
    server.output("334 " + XOAuth2ChallengeParserTest.INVALID_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("AUTH XOAUTH2 dXNlcj11c2VyAWF1dGg9QmVhcmVyIG5ld1Rva2VuAQE=");
    server.output("235 2.7.0 Authentication successful");
    SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.XOAUTH2, ConnectionSecurity.NONE);
    transport.open();
    InOrder inOrder = inOrder(oAuth2TokenProvider);
    inOrder.verify(oAuth2TokenProvider).getToken(eq(USERNAME), anyInt());
    inOrder.verify(oAuth2TokenProvider).invalidateToken(USERNAME);
    inOrder.verify(oAuth2TokenProvider).getToken(eq(USERNAME), anyInt());
    server.verifyConnectionStillOpen();
    server.verifyInteractionCompleted();
}
Also used : InOrder(org.mockito.InOrder) MockSmtpServer(com.fsck.k9.mail.transport.mockServer.MockSmtpServer) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Example 4 with OAuth2TokenProvider

use of com.fsck.k9.mail.oauth.OAuth2TokenProvider in project k-9 by k9mail.

the class SmtpTransportTest method startServerAndCreateSmtpTransport.

private SmtpTransport startServerAndCreateSmtpTransport(MockSmtpServer server, AuthType authenticationType, ConnectionSecurity connectionSecurity, String password) throws IOException, MessagingException {
    server.start();
    String host = server.getHost();
    int port = server.getPort();
    ServerSettings serverSettings = new ServerSettings(Type.SMTP, host, port, connectionSecurity, authenticationType, USERNAME, password, CLIENT_CERTIFICATE_ALIAS);
    String uri = SmtpTransport.createUri(serverSettings);
    StoreConfig storeConfig = createStoreConfigWithTransportUri(uri);
    return new TestSmtpTransport(storeConfig, socketFactory, oAuth2TokenProvider);
}
Also used : ServerSettings(com.fsck.k9.mail.ServerSettings) StoreConfig(com.fsck.k9.mail.store.StoreConfig) Matchers.anyString(org.mockito.Matchers.anyString)

Example 5 with OAuth2TokenProvider

use of com.fsck.k9.mail.oauth.OAuth2TokenProvider in project k-9 by k9mail.

the class SmtpTransportTest method open_withXoauth2Extension_shouldInvalidateAndRetryOn400Response.

@Test
public void open_withXoauth2Extension_shouldInvalidateAndRetryOn400Response() throws Exception {
    MockSmtpServer server = new MockSmtpServer();
    server.output("220 localhost Simple Mail Transfer Service Ready");
    server.expect("EHLO localhost");
    server.output("250-localhost Hello client.localhost");
    server.output("250 AUTH XOAUTH2");
    server.expect("AUTH XOAUTH2 dXNlcj11c2VyAWF1dGg9QmVhcmVyIG9sZFRva2VuAQE=");
    server.output("334 " + XOAuth2ChallengeParserTest.STATUS_400_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("AUTH XOAUTH2 dXNlcj11c2VyAWF1dGg9QmVhcmVyIG5ld1Rva2VuAQE=");
    server.output("235 2.7.0 Authentication successful");
    SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.XOAUTH2, ConnectionSecurity.NONE);
    transport.open();
    InOrder inOrder = inOrder(oAuth2TokenProvider);
    inOrder.verify(oAuth2TokenProvider).getToken(eq(USERNAME), anyInt());
    inOrder.verify(oAuth2TokenProvider).invalidateToken(USERNAME);
    inOrder.verify(oAuth2TokenProvider).getToken(eq(USERNAME), anyInt());
    server.verifyConnectionStillOpen();
    server.verifyInteractionCompleted();
}
Also used : InOrder(org.mockito.InOrder) MockSmtpServer(com.fsck.k9.mail.transport.mockServer.MockSmtpServer) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Aggregations

XOAuth2ChallengeParserTest (com.fsck.k9.mail.XOAuth2ChallengeParserTest)10 Test (org.junit.Test)10 InOrder (org.mockito.InOrder)8 MockSmtpServer (com.fsck.k9.mail.transport.mockServer.MockSmtpServer)5 OAuth2TokenProvider (com.fsck.k9.mail.oauth.OAuth2TokenProvider)4 ConnectivityManager (android.net.ConnectivityManager)3 StoreConfig (com.fsck.k9.mail.store.StoreConfig)3 MockImapServer (com.fsck.k9.mail.store.imap.mockserver.MockImapServer)3 Before (org.junit.Before)3 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)2 TestTrustedSocketFactory (com.fsck.k9.mail.helpers.TestTrustedSocketFactory)2 MessagingException (com.fsck.k9.mail.MessagingException)1 ServerSettings (com.fsck.k9.mail.ServerSettings)1 Store (com.fsck.k9.mail.Store)1 DefaultTrustedSocketFactory (com.fsck.k9.mail.ssl.DefaultTrustedSocketFactory)1 TrustedSocketFactory (com.fsck.k9.mail.ssl.TrustedSocketFactory)1 ImapStore (com.fsck.k9.mail.store.imap.ImapStore)1 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)1 WebDavStore (com.fsck.k9.mail.store.webdav.WebDavStore)1 Matchers.anyString (org.mockito.Matchers.anyString)1