Code style fixes
This commit is contained in:
parent
79c1f921f9
commit
74017c926e
10 changed files with 146 additions and 150 deletions
|
@ -1,61 +1,60 @@
|
|||
package com.fsck.k9.mail.oauth;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import com.fsck.k9.mail.AuthenticationFailedException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface OAuth2TokenProvider {
|
||||
|
||||
/**
|
||||
* A default timeout value to use when fetching tokens.
|
||||
*/
|
||||
public static final int OAUTH2_TIMEOUT = 30000;
|
||||
int OAUTH2_TIMEOUT = 30000;
|
||||
|
||||
|
||||
/**
|
||||
* @return Accounts suitable for OAuth 2.0 token provision.
|
||||
*/
|
||||
List<String> getAccounts();
|
||||
|
||||
/**
|
||||
* Provides an asynchronous response to an
|
||||
* {@link OAuth2TokenProvider#authorizeAPI(String, Activity, OAuth2TokenProviderAuthCallback)} request
|
||||
*/
|
||||
interface OAuth2TokenProviderAuthCallback {
|
||||
|
||||
void success();
|
||||
|
||||
void failure(AuthorizationException e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request API authorization. This is a foreground action that may produce a dialog to interact with.
|
||||
* @param username Username
|
||||
* @param activity The responsible activity
|
||||
* @param callback A callback to process the asynchronous response
|
||||
*
|
||||
* @param username
|
||||
* Username
|
||||
* @param activity
|
||||
* The responsible activity
|
||||
* @param callback
|
||||
* A callback to process the asynchronous response
|
||||
*/
|
||||
void authorizeAPI(String username, Activity activity,
|
||||
OAuth2TokenProviderAuthCallback callback);
|
||||
void authorizeApi(String username, Activity activity, OAuth2TokenProviderAuthCallback callback);
|
||||
|
||||
/**
|
||||
* Fetch a token. No guarantees are provided for validity.
|
||||
* @param username Username
|
||||
* @return Token string
|
||||
* @throws AuthenticationFailedException
|
||||
*/
|
||||
String getToken(String username, long timeoutMillis) throws AuthenticationFailedException;
|
||||
|
||||
/**
|
||||
* Invalidate the token for this username.
|
||||
* Note that the token should always be invalidated on credential failure.
|
||||
* However invalidating a token every single time is not recommended.
|
||||
*
|
||||
* Invalidating a token and then failure with a new token
|
||||
* should be treated as a permanent failure.
|
||||
*
|
||||
* @param username
|
||||
* <p>
|
||||
* Note that the token should always be invalidated on credential failure. However invalidating a token every
|
||||
* single time is not recommended.
|
||||
* <p>
|
||||
* Invalidating a token and then failure with a new token should be treated as a permanent failure.
|
||||
*/
|
||||
void invalidateToken(String username);
|
||||
|
||||
|
||||
/**
|
||||
* Provides an asynchronous response to an
|
||||
* {@link OAuth2TokenProvider#authorizeApi(String, Activity, OAuth2TokenProviderAuthCallback)} request.
|
||||
*/
|
||||
interface OAuth2TokenProviderAuthCallback {
|
||||
void success();
|
||||
void failure(AuthorizationException e);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
package com.fsck.k9.mail.oauth;
|
||||
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.fsck.k9.mail.K9MailLib;
|
||||
import com.fsck.k9.mail.filter.Base64;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
|
||||
|
||||
|
||||
/**
|
||||
* Parses Google's Error/Challenge responses
|
||||
* See: https://developers.google.com/gmail/xoauth2_protocol#error_response
|
||||
|
@ -17,21 +18,24 @@ import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
|
|||
public class XOAuth2ChallengeParser {
|
||||
public static final String BAD_RESPONSE = "400";
|
||||
|
||||
|
||||
public static boolean shouldRetry(String response, String host) {
|
||||
String decodedResponse = Base64.decode(response);
|
||||
|
||||
if (K9MailLib.isDebug())
|
||||
Log.v(LOG_TAG, "Challenge response: "+ decodedResponse);
|
||||
if (K9MailLib.isDebug()) {
|
||||
Log.v(LOG_TAG, "Challenge response: " + decodedResponse);
|
||||
}
|
||||
|
||||
try {
|
||||
JSONObject json = new JSONObject(decodedResponse);
|
||||
if (!BAD_RESPONSE.equals(json.getString("status"))) {
|
||||
String status = json.getString("status");
|
||||
if (!BAD_RESPONSE.equals(status)) {
|
||||
return false;
|
||||
}
|
||||
} catch (JSONException jsonException) {
|
||||
Log.e(LOG_TAG, "Error decoding JSON response from:"
|
||||
+ host + ". Response was:" + decodedResponse);
|
||||
Log.e(LOG_TAG, "Error decoding JSON response from: " + host + ". Response was: " + decodedResponse);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.fsck.k9.mail.store;
|
||||
|
||||
import android.accounts.AccountManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
|
||||
|
@ -16,8 +19,6 @@ import com.fsck.k9.mail.store.pop3.Pop3Store;
|
|||
import com.fsck.k9.mail.store.webdav.WebDavHttpClient;
|
||||
import com.fsck.k9.mail.store.webdav.WebDavStore;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class RemoteStore extends Store {
|
||||
public static final int SOCKET_CONNECT_TIMEOUT = 30000;
|
||||
|
@ -41,8 +42,7 @@ public abstract class RemoteStore extends Store {
|
|||
* Get an instance of a remote mail store.
|
||||
*/
|
||||
public synchronized static Store getInstance(Context context, StoreConfig storeConfig,
|
||||
OAuth2TokenProvider oAuth2TokenProvider)
|
||||
throws MessagingException {
|
||||
OAuth2TokenProvider oAuth2TokenProvider) throws MessagingException {
|
||||
String uri = storeConfig.getStoreUri();
|
||||
|
||||
if (uri.startsWith("local")) {
|
||||
|
@ -53,18 +53,14 @@ public abstract class RemoteStore extends Store {
|
|||
if (store == null) {
|
||||
if (uri.startsWith("imap")) {
|
||||
store = new ImapStore(
|
||||
storeConfig,
|
||||
new DefaultTrustedSocketFactory(context),
|
||||
(ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE),
|
||||
oAuth2TokenProvider
|
||||
);
|
||||
storeConfig,
|
||||
new DefaultTrustedSocketFactory(context),
|
||||
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE),
|
||||
oAuth2TokenProvider);
|
||||
} else if (uri.startsWith("pop3")) {
|
||||
store = new Pop3Store(storeConfig,
|
||||
new DefaultTrustedSocketFactory(context));
|
||||
store = new Pop3Store(storeConfig, new DefaultTrustedSocketFactory(context));
|
||||
} else if (uri.startsWith("webdav")) {
|
||||
store = new WebDavStore(storeConfig,
|
||||
new WebDavHttpClient.WebDavHttpClientFactory());
|
||||
store = new WebDavStore(storeConfig, new WebDavHttpClient.WebDavHttpClientFactory());
|
||||
}
|
||||
|
||||
if (store != null) {
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package com.fsck.k9.mail.store.imap;
|
||||
|
||||
|
||||
class Commands {
|
||||
public static final String IDLE = "IDLE";
|
||||
public static final String NAMESPACE = "NAMESPACE";
|
||||
public static final String CAPABILITY = "CAPABILITY";
|
||||
public static final String COMPRESS_DEFLATE = "COMPRESS DEFLATE";
|
||||
public static final String STARTTLS = "STARTTLS";
|
||||
public static final String AUTHENTICATE_XOAUTH2="AUTHENTICATE XOAUTH2";
|
||||
public static final String AUTHENTICATE_XOAUTH2 = "AUTHENTICATE XOAUTH2";
|
||||
public static final String AUTHENTICATE_CRAM_MD5 = "AUTHENTICATE CRAM-MD5";
|
||||
public static final String AUTHENTICATE_PLAIN = "AUTHENTICATE PLAIN";
|
||||
public static final String AUTHENTICATE_EXTERNAL = "AUTHENTICATE EXTERNAL";
|
||||
|
|
|
@ -332,9 +332,7 @@ class ImapConnection {
|
|||
case XOAUTH2:
|
||||
if (oauthTokenProvider == null) {
|
||||
throw new MessagingException("No OAuthToken Provider available.");
|
||||
}
|
||||
if (hasCapability(Capabilities.AUTH_XOAUTH2)
|
||||
&& hasCapability(Capabilities.SASL_IR)) {
|
||||
} else if (hasCapability(Capabilities.AUTH_XOAUTH2) && hasCapability(Capabilities.SASL_IR)) {
|
||||
authXoauth2withSASLIR();
|
||||
} else {
|
||||
throw new MessagingException("Server doesn't support SASL XOAUTH2.");
|
||||
|
@ -413,27 +411,24 @@ class ImapConnection {
|
|||
}
|
||||
|
||||
private void attemptXOAuth2() throws MessagingException, IOException {
|
||||
String token = oauthTokenProvider.getToken(settings.getUsername(),
|
||||
OAuth2TokenProvider.OAUTH2_TIMEOUT);
|
||||
String tag = sendSaslIrCommand(Commands.AUTHENTICATE_XOAUTH2,
|
||||
Authentication.computeXoauth(settings.getUsername(), token), true);
|
||||
String token = oauthTokenProvider.getToken(settings.getUsername(), OAuth2TokenProvider.OAUTH2_TIMEOUT);
|
||||
String authString = Authentication.computeXoauth(settings.getUsername(), token);
|
||||
String tag = sendSaslIrCommand(Commands.AUTHENTICATE_XOAUTH2, authString, true);
|
||||
|
||||
extractCapabilities(
|
||||
responseParser.readStatusResponse(tag, Commands.AUTHENTICATE_XOAUTH2, getLogId(),
|
||||
new UntaggedHandler() {
|
||||
@Override
|
||||
public void handleAsyncUntaggedResponse(ImapResponse response) throws IOException {
|
||||
handleXOAuthUntaggedResponse(response);
|
||||
|
||||
}
|
||||
})
|
||||
);
|
||||
List<ImapResponse> responses = responseParser.readStatusResponse(tag, Commands.AUTHENTICATE_XOAUTH2, getLogId(),
|
||||
new UntaggedHandler() {
|
||||
@Override
|
||||
public void handleAsyncUntaggedResponse(ImapResponse response) throws IOException {
|
||||
handleXOAuthUntaggedResponse(response);
|
||||
}
|
||||
});
|
||||
|
||||
extractCapabilities(responses);
|
||||
}
|
||||
|
||||
private void handleXOAuthUntaggedResponse(ImapResponse response) throws IOException {
|
||||
if (response.isString(0)) {
|
||||
retryXoauth2WithNewToken = XOAuth2ChallengeParser.shouldRetry(
|
||||
response.getString(0), settings.getHost());
|
||||
retryXoauth2WithNewToken = XOAuth2ChallengeParser.shouldRetry(response.getString(0), settings.getHost());
|
||||
}
|
||||
|
||||
if (response.isContinuationRequested()) {
|
||||
|
@ -727,7 +722,7 @@ class ImapConnection {
|
|||
open();
|
||||
|
||||
String tag = Integer.toString(nextCommandTag++);
|
||||
String commandToSend = tag + " " + command + " " + initialClientResponse+ "\r\n";
|
||||
String commandToSend = tag + " " + command + " " + initialClientResponse + "\r\n";
|
||||
outputStream.write(commandToSend.getBytes());
|
||||
outputStream.flush();
|
||||
|
||||
|
@ -735,7 +730,7 @@ class ImapConnection {
|
|||
if (sensitive && !K9MailLib.isDebugSensitive()) {
|
||||
Log.v(LOG_TAG, getLogId() + ">>> [Command Hidden, Enable Sensitive Debug Logging To Show]");
|
||||
} else {
|
||||
Log.v(LOG_TAG, getLogId() + ">>> " + tag + " " + command+ " " + initialClientResponse);
|
||||
Log.v(LOG_TAG, getLogId() + ">>> " + tag + " " + command + " " + initialClientResponse);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -76,10 +76,8 @@ public class ImapStore extends RemoteStore {
|
|||
return ImapStoreUriCreator.create(server);
|
||||
}
|
||||
|
||||
public ImapStore(StoreConfig storeConfig,
|
||||
TrustedSocketFactory trustedSocketFactory,
|
||||
ConnectivityManager connectivityManager,
|
||||
OAuth2TokenProvider oauthTokenProvider) throws MessagingException {
|
||||
public ImapStore(StoreConfig storeConfig, TrustedSocketFactory trustedSocketFactory,
|
||||
ConnectivityManager connectivityManager, OAuth2TokenProvider oauthTokenProvider) throws MessagingException {
|
||||
super(storeConfig, trustedSocketFactory);
|
||||
|
||||
ImapStoreSettings settings;
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.fsck.k9.mail.store.imap;
|
||||
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
|
@ -88,7 +86,7 @@ class ImapStoreUriDecoder {
|
|||
// Last field (password/certAlias) is empty.
|
||||
// For imports e.g.: PLAIN:username: or username:
|
||||
// Or XOAUTH2 where it's a valid config - XOAUTH:username:
|
||||
if(userInfoParts.length > 1) {
|
||||
if (userInfoParts.length > 1) {
|
||||
authenticationType = AuthType.valueOf(userInfoParts[0]);
|
||||
username = decodeUtf8(userInfoParts[1]);
|
||||
} else {
|
||||
|
@ -136,6 +134,5 @@ class ImapStoreUriDecoder {
|
|||
|
||||
return new ImapStoreSettings(host, port, connectionSecurity, authenticationType, username,
|
||||
password, clientCertificateAlias, autoDetectNamespace, pathPrefix);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,37 +1,58 @@
|
|||
|
||||
package com.fsck.k9.mail.transport;
|
||||
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fsck.k9.mail.*;
|
||||
import com.fsck.k9.mail.Address;
|
||||
import com.fsck.k9.mail.AuthType;
|
||||
import com.fsck.k9.mail.Authentication;
|
||||
import com.fsck.k9.mail.AuthenticationFailedException;
|
||||
import com.fsck.k9.mail.CertificateValidationException;
|
||||
import com.fsck.k9.mail.ConnectionSecurity;
|
||||
import com.fsck.k9.mail.K9MailLib;
|
||||
import com.fsck.k9.mail.Message;
|
||||
import com.fsck.k9.mail.Message.RecipientType;
|
||||
import com.fsck.k9.mail.MessagingException;
|
||||
import com.fsck.k9.mail.ServerSettings;
|
||||
import com.fsck.k9.mail.Transport;
|
||||
import com.fsck.k9.mail.filter.Base64;
|
||||
import com.fsck.k9.mail.filter.EOLConvertingOutputStream;
|
||||
import com.fsck.k9.mail.filter.LineWrapOutputStream;
|
||||
import com.fsck.k9.mail.filter.PeekableInputStream;
|
||||
import com.fsck.k9.mail.filter.SmtpDataStuffing;
|
||||
import com.fsck.k9.mail.internet.CharsetSupport;
|
||||
import com.fsck.k9.mail.CertificateValidationException;
|
||||
import com.fsck.k9.mail.oauth.OAuth2TokenProvider;
|
||||
import com.fsck.k9.mail.oauth.XOAuth2ChallengeParser;
|
||||
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
|
||||
import com.fsck.k9.mail.store.StoreConfig;
|
||||
|
||||
import javax.net.ssl.SSLException;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.*;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.*;
|
||||
|
||||
import static com.fsck.k9.mail.CertificateValidationException.Reason.MissingCapability;
|
||||
import static com.fsck.k9.mail.K9MailLib.DEBUG_PROTOCOL_SMTP;
|
||||
import static com.fsck.k9.mail.K9MailLib.LOG_TAG;
|
||||
import static com.fsck.k9.mail.CertificateValidationException.Reason.MissingCapability;
|
||||
|
||||
public class SmtpTransport extends Transport {
|
||||
public static final int SMTP_CONTINUE_REQUEST = 334;
|
||||
|
@ -194,8 +215,7 @@ public class SmtpTransport extends Transport {
|
|||
private boolean retryXoauthWithNewToken;
|
||||
|
||||
public SmtpTransport(StoreConfig storeConfig, TrustedSocketFactory trustedSocketFactory,
|
||||
OAuth2TokenProvider oauth2TokenProvider)
|
||||
throws MessagingException {
|
||||
OAuth2TokenProvider oauth2TokenProvider) throws MessagingException {
|
||||
ServerSettings settings;
|
||||
try {
|
||||
settings = decodeUri(storeConfig.getTransportUri());
|
||||
|
@ -881,13 +901,12 @@ public class SmtpTransport extends Transport {
|
|||
}
|
||||
|
||||
private void attemptXoauth2(String username) throws MessagingException, IOException {
|
||||
CommandResponse response = executeSimpleCommandWithResponse("AUTH XOAUTH2 " +
|
||||
Authentication.computeXoauth(username,
|
||||
oauthTokenProvider.getToken(username, OAuth2TokenProvider.OAUTH2_TIMEOUT)),
|
||||
true);
|
||||
if(response.replyCode == SMTP_CONTINUE_REQUEST) {
|
||||
retryXoauthWithNewToken = XOAuth2ChallengeParser.shouldRetry(
|
||||
response.message, mHost);
|
||||
String token = oauthTokenProvider.getToken(username, OAuth2TokenProvider.OAUTH2_TIMEOUT);
|
||||
String authString = Authentication.computeXoauth(username, token);
|
||||
CommandResponse response = executeSimpleCommandWithResponse("AUTH XOAUTH2 " + authString, true);
|
||||
|
||||
if (response.replyCode == SMTP_CONTINUE_REQUEST) {
|
||||
retryXoauthWithNewToken = XOAuth2ChallengeParser.shouldRetry(response.message, mHost);
|
||||
|
||||
//Per Google spec, respond to challenge with empty response
|
||||
executeSimpleCommandWithResponse("", false);
|
||||
|
|
|
@ -14,12 +14,10 @@ import com.fsck.k9.mail.ConnectionSecurity;
|
|||
import com.fsck.k9.mail.K9MailLib;
|
||||
import com.fsck.k9.mail.MessagingException;
|
||||
import com.fsck.k9.mail.XOAuth2ChallengeParserTest;
|
||||
import com.fsck.k9.mail.filter.Base64;
|
||||
import com.fsck.k9.mail.helpers.TestTrustedSocketFactory;
|
||||
import com.fsck.k9.mail.oauth.OAuth2TokenProvider;
|
||||
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
|
||||
import com.fsck.k9.mail.store.imap.mockserver.MockImapServer;
|
||||
import com.fsck.k9.mail.helpers.TestTrustedSocketFactory;
|
||||
|
||||
import okio.ByteString;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -49,7 +47,10 @@ public class ImapConnectionTest {
|
|||
private static final String PASSWORD = "123456";
|
||||
private static final int SOCKET_CONNECT_TIMEOUT = 10000;
|
||||
private static final int SOCKET_READ_TIMEOUT = 10000;
|
||||
|
||||
private static final String XOAUTH_STRING = ByteString.encodeUtf8(
|
||||
"user=" + USERNAME + "\001auth=Bearer token\001\001").base64();
|
||||
private static final String XOAUTH_STRING_RETRY = ByteString.encodeUtf8(
|
||||
"user=" + USERNAME + "\001auth=Bearer token2\001\001").base64();
|
||||
|
||||
|
||||
private TrustedSocketFactory socketFactory;
|
||||
|
@ -82,7 +83,7 @@ public class ImapConnectionTest {
|
|||
server.output("* OK [CAPABILITY IMAP4 IMAP4REV1 AUTH=PLAIN]");
|
||||
server.expect("1 AUTHENTICATE PLAIN");
|
||||
server.output("+");
|
||||
server.expect(ByteString.encodeUtf8("\000" + USERNAME+ "\000" + PASSWORD).base64());
|
||||
server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64());
|
||||
server.output("1 OK Success");
|
||||
server.expect("2 LIST \"\" \"\"");
|
||||
server.output("* LIST () \"/\" foo/bar");
|
||||
|
@ -102,7 +103,7 @@ public class ImapConnectionTest {
|
|||
preAuthenticationDialog(server, "AUTH=PLAIN");
|
||||
server.expect("2 AUTHENTICATE PLAIN");
|
||||
server.output("+");
|
||||
server.expect(ByteString.encodeUtf8("\000" + USERNAME+ "\000" + PASSWORD).base64());
|
||||
server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64());
|
||||
server.output("2 OK Success");
|
||||
simplePostAuthenticationDialog(server);
|
||||
ImapConnection imapConnection = startServerAndCreateImapConnection(server);
|
||||
|
@ -160,7 +161,7 @@ public class ImapConnectionTest {
|
|||
preAuthenticationDialog(server, "AUTH=PLAIN");
|
||||
server.expect("2 AUTHENTICATE PLAIN");
|
||||
server.output("+");
|
||||
server.expect(ByteString.encodeUtf8("\000" + USERNAME+ "\000" + PASSWORD).base64());
|
||||
server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64());
|
||||
server.output("2 NO Login Failure");
|
||||
server.expect("3 LOGIN \"" + USERNAME + "\" \"" + PASSWORD + "\"");
|
||||
server.output("3 OK LOGIN completed");
|
||||
|
@ -180,7 +181,7 @@ public class ImapConnectionTest {
|
|||
preAuthenticationDialog(server, "AUTH=PLAIN");
|
||||
server.expect("2 AUTHENTICATE PLAIN");
|
||||
server.output("+");
|
||||
server.expect(ByteString.encodeUtf8("\000" + USERNAME+ "\000" + PASSWORD).base64());
|
||||
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");
|
||||
|
@ -276,13 +277,10 @@ public class ImapConnectionTest {
|
|||
@Test
|
||||
public void open_authXoauthWithSaslIr() throws Exception {
|
||||
settings.setAuthType(AuthType.XOAUTH2);
|
||||
when(oAuth2TokenProvider.getToken("user", OAuth2TokenProvider.OAUTH2_TIMEOUT))
|
||||
.thenReturn("token");
|
||||
when(oAuth2TokenProvider.getToken("user", OAuth2TokenProvider.OAUTH2_TIMEOUT)).thenReturn("token");
|
||||
MockImapServer server = new MockImapServer();
|
||||
preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2");
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token\001\001"
|
||||
).base64());
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING);
|
||||
server.output("2 OK Success");
|
||||
simplePostAuthenticationDialog(server);
|
||||
ImapConnection imapConnection = startServerAndCreateImapConnection(server);
|
||||
|
@ -300,10 +298,8 @@ public class ImapConnectionTest {
|
|||
.thenReturn("token").thenReturn("token2");
|
||||
MockImapServer server = new MockImapServer();
|
||||
preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2");
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token\001\001"
|
||||
).base64());
|
||||
server.output("+ "+ XOAuth2ChallengeParserTest.STATUS_401_RESPONSE);
|
||||
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);
|
||||
|
@ -325,15 +321,11 @@ public class ImapConnectionTest {
|
|||
.thenReturn("token").thenReturn("token2");
|
||||
MockImapServer server = new MockImapServer();
|
||||
preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2");
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token\001\001"
|
||||
).base64());
|
||||
server.output("+ "+XOAuth2ChallengeParserTest.STATUS_400_RESPONSE);
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING);
|
||||
server.output("+ " + XOAuth2ChallengeParserTest.STATUS_400_RESPONSE);
|
||||
server.expect("");
|
||||
server.output("2 NO SASL authentication failed");
|
||||
server.expect("3 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token2\001\001"
|
||||
).base64());
|
||||
server.expect("3 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING_RETRY);
|
||||
server.output("3 OK Success");
|
||||
simplePostAuthenticationDialog(server, "4");
|
||||
ImapConnection imapConnection = startServerAndCreateImapConnection(server);
|
||||
|
@ -355,15 +347,11 @@ public class ImapConnectionTest {
|
|||
.thenReturn("token").thenReturn("token2");
|
||||
MockImapServer server = new MockImapServer();
|
||||
preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2");
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token\001\001"
|
||||
).base64());
|
||||
server.output("+ "+XOAuth2ChallengeParserTest.INVALID_RESPONSE);
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING);
|
||||
server.output("+ " + XOAuth2ChallengeParserTest.INVALID_RESPONSE);
|
||||
server.expect("");
|
||||
server.output("2 NO SASL authentication failed");
|
||||
server.expect("3 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token2\001\001"
|
||||
).base64());
|
||||
server.expect("3 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING_RETRY);
|
||||
server.output("3 OK Success");
|
||||
simplePostAuthenticationDialog(server, "4");
|
||||
ImapConnection imapConnection = startServerAndCreateImapConnection(server);
|
||||
|
@ -385,15 +373,11 @@ public class ImapConnectionTest {
|
|||
.thenReturn("token").thenReturn("token2");
|
||||
MockImapServer server = new MockImapServer();
|
||||
preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2");
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token\001\001"
|
||||
).base64());
|
||||
server.output("+ "+XOAuth2ChallengeParserTest.MISSING_STATUS_RESPONSE);
|
||||
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 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token2\001\001"
|
||||
).base64());
|
||||
server.expect("3 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING_RETRY);
|
||||
server.output("3 OK Success");
|
||||
simplePostAuthenticationDialog(server, "4");
|
||||
ImapConnection imapConnection = startServerAndCreateImapConnection(server);
|
||||
|
@ -415,15 +399,11 @@ public class ImapConnectionTest {
|
|||
.thenReturn("token").thenReturn("token2");
|
||||
MockImapServer server = new MockImapServer();
|
||||
preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2");
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token\001\001"
|
||||
).base64());
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING);
|
||||
server.output("+ r3j3krj3irj3oir3ojo");
|
||||
server.expect("");
|
||||
server.output("2 NO SASL authentication failed");
|
||||
server.expect("3 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token2\001\001"
|
||||
).base64());
|
||||
server.expect("3 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING_RETRY);
|
||||
server.output("+ 433ba3a3a");
|
||||
server.expect("");
|
||||
server.output("3 NO SASL authentication failed");
|
||||
|
@ -447,9 +427,7 @@ public class ImapConnectionTest {
|
|||
.thenReturn("token");
|
||||
MockImapServer server = new MockImapServer();
|
||||
preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2");
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 "+ByteString.encodeUtf8(
|
||||
"user=user\001auth=Bearer token\001\001"
|
||||
).base64());
|
||||
server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING);
|
||||
server.output("2 OK [CAPABILITY IMAP4REV1 IDLE XM-GM-EXT-1]");
|
||||
simplePostAuthenticationDialog(server);
|
||||
ImapConnection imapConnection = startServerAndCreateImapConnection(server);
|
||||
|
|
|
@ -19,6 +19,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapNoAuth() {
|
||||
String uri = "imap://user:pass@server/";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.PLAIN, settings.authenticationType);
|
||||
|
@ -30,6 +31,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapNoPassword() {
|
||||
String uri = "imap://user:@server/";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.PLAIN, settings.authenticationType);
|
||||
|
@ -41,6 +43,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapPlainNoPassword() {
|
||||
String uri = "imap://PLAIN:user:@server/";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.PLAIN, settings.authenticationType);
|
||||
|
@ -52,6 +55,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapExternalAuth() {
|
||||
String uri = "imap://EXTERNAL:user:clientCertAlias@server/";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.EXTERNAL, settings.authenticationType);
|
||||
|
@ -64,6 +68,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapXOAuth2() {
|
||||
String uri = "imap://XOAUTH2:user:@server/";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.XOAUTH2, settings.authenticationType);
|
||||
|
@ -88,6 +93,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapTLS() {
|
||||
String uri = "imap+ssl+://PLAIN:user:pass@server/";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(ConnectionSecurity.SSL_TLS_REQUIRED, settings.connectionSecurity);
|
||||
|
@ -97,10 +103,10 @@ public class ImapStoreUriTest {
|
|||
assertEquals("server", settings.host);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDecodeStoreUriImapAllExtras() {
|
||||
String uri = "imap://PLAIN:user:pass@server:143/0%7CcustomPathPrefix";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.PLAIN, settings.authenticationType);
|
||||
|
@ -115,6 +121,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapNoExtras() {
|
||||
String uri = "imap://PLAIN:user:pass@server:143/";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.PLAIN, settings.authenticationType);
|
||||
|
@ -128,6 +135,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapPrefixOnly() {
|
||||
String uri = "imap://PLAIN:user:pass@server:143/customPathPrefix";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.PLAIN, settings.authenticationType);
|
||||
|
@ -142,6 +150,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapEmptyPrefix() {
|
||||
String uri = "imap://PLAIN:user:pass@server:143/0%7C";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.PLAIN, settings.authenticationType);
|
||||
|
@ -156,6 +165,7 @@ public class ImapStoreUriTest {
|
|||
@Test
|
||||
public void testDecodeStoreUriImapAutodetectAndPrefix() {
|
||||
String uri = "imap://PLAIN:user:pass@server:143/1%7CcustomPathPrefix";
|
||||
|
||||
ServerSettings settings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals(AuthType.PLAIN, settings.authenticationType);
|
||||
|
@ -172,7 +182,6 @@ public class ImapStoreUriTest {
|
|||
Map<String, String> extra = new HashMap<String, String>();
|
||||
extra.put("autoDetectNamespace", "false");
|
||||
extra.put("pathPrefix", "customPathPrefix");
|
||||
|
||||
ServerSettings settings = new ServerSettings(ServerSettings.Type.IMAP, "server", 143,
|
||||
ConnectionSecurity.NONE, AuthType.PLAIN, "user", "pass", null, extra);
|
||||
|
||||
|
@ -186,7 +195,6 @@ public class ImapStoreUriTest {
|
|||
Map<String, String> extra = new HashMap<String, String>();
|
||||
extra.put("autoDetectNamespace", "false");
|
||||
extra.put("pathPrefix", "");
|
||||
|
||||
ServerSettings settings = new ServerSettings(ServerSettings.Type.IMAP, "server", 143,
|
||||
ConnectionSecurity.NONE, AuthType.PLAIN, "user", "pass", null, extra);
|
||||
|
||||
|
@ -228,6 +236,7 @@ public class ImapStoreUriTest {
|
|||
assertEquals("imap://PLAIN:user%2540doma%253An:p%2540ssw%253Ard%2525@server:143/1%7C", uri);
|
||||
|
||||
ServerSettings outSettings = RemoteStore.decodeStoreUri(uri);
|
||||
|
||||
assertEquals("user@doma:n", outSettings.username);
|
||||
assertEquals("p@ssw:rd%", outSettings.password);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue