Search in sources :

Example 6 with AuthType

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

the class AccountSetupOutgoing method updateViewFromAuthType.

/**
     * Shows/hides password field and client certificate spinner
     */
private void updateViewFromAuthType() {
    AuthType authType = getSelectedAuthType();
    boolean isAuthTypeExternal = (AuthType.EXTERNAL == authType);
    if (isAuthTypeExternal) {
        // hide password fields, show client certificate fields
        mPasswordView.setVisibility(View.GONE);
        mPasswordLabelView.setVisibility(View.GONE);
        mClientCertificateLabelView.setVisibility(View.VISIBLE);
        mClientCertificateSpinner.setVisibility(View.VISIBLE);
    } else {
        // show password fields, hide client certificate fields
        mPasswordView.setVisibility(View.VISIBLE);
        mPasswordLabelView.setVisibility(View.VISIBLE);
        mClientCertificateLabelView.setVisibility(View.GONE);
        mClientCertificateSpinner.setVisibility(View.GONE);
    }
}
Also used : AuthType(com.fsck.k9.mail.AuthType)

Example 7 with AuthType

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

the class AccountSetupOutgoing method initializeViewListeners.

/**
     * Called at the end of either {@code onCreate()} or
     * {@code onRestoreInstanceState()}, after the views have been initialized,
     * so that the listeners are not triggered during the view initialization.
     * This avoids needless calls to {@code validateFields()} which is called
     * immediately after this is called.
     */
private void initializeViewListeners() {
    /*
         * Updates the port when the user changes the security type. This allows
         * us to show a reasonable default which the user can change.
         */
    mSecurityTypeView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            /*
                 * We keep our own record of the spinner state so we
                 * know for sure that onItemSelected() was called
                 * because of user input, not because of spinner
                 * state initialization. This assures that the port
                 * will not be replaced with a default value except
                 * on user input.
                 */
            if (mCurrentSecurityTypeViewPosition != position) {
                updatePortFromSecurityType();
                boolean isInsecure = (ConnectionSecurity.NONE == getSelectedSecurity());
                boolean isAuthExternal = (AuthType.EXTERNAL == getSelectedAuthType());
                boolean loginNotRequired = !mRequireLoginView.isChecked();
                /*
                     * If the user selects ConnectionSecurity.NONE, a
                     * warning would normally pop up if the authentication
                     * is AuthType.EXTERNAL (i.e., using client
                     * certificates). But such a warning is irrelevant if
                     * login is not required. So to avoid such a warning
                     * (generated in validateFields()) under those
                     * conditions, we change the (irrelevant) authentication
                     * method to PLAIN.
                     */
                if (isInsecure && isAuthExternal && loginNotRequired) {
                    OnItemSelectedListener onItemSelectedListener = mAuthTypeView.getOnItemSelectedListener();
                    mAuthTypeView.setOnItemSelectedListener(null);
                    mCurrentAuthTypeViewPosition = mAuthTypeAdapter.getAuthPosition(AuthType.PLAIN);
                    mAuthTypeView.setSelection(mCurrentAuthTypeViewPosition, false);
                    mAuthTypeView.setOnItemSelectedListener(onItemSelectedListener);
                    updateViewFromAuthType();
                }
                validateFields();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        /* unused */
        }
    });
    mAuthTypeView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (mCurrentAuthTypeViewPosition == position) {
                return;
            }
            updateViewFromAuthType();
            validateFields();
            AuthType selection = getSelectedAuthType();
            // Have the user select (or confirm) the client certificate
            if (AuthType.EXTERNAL == selection) {
                // This may again invoke validateFields()
                mClientCertificateSpinner.chooseCertificate();
            } else {
                mPasswordView.requestFocus();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        /* unused */
        }
    });
    mRequireLoginView.setOnCheckedChangeListener(this);
    mClientCertificateSpinner.setOnClientCertificateChangedListener(clientCertificateChangedListener);
    mUsernameView.addTextChangedListener(validationTextWatcher);
    mPasswordView.addTextChangedListener(validationTextWatcher);
    mServerView.addTextChangedListener(validationTextWatcher);
    mPortView.addTextChangedListener(validationTextWatcher);
}
Also used : OnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) AuthType(com.fsck.k9.mail.AuthType) OnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) View(android.view.View)

Example 8 with AuthType

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

the class AccountSetupOutgoing method onNext.

protected void onNext() {
    ConnectionSecurity securityType = getSelectedSecurity();
    String uri;
    String username = null;
    String password = null;
    String clientCertificateAlias = null;
    AuthType authType = null;
    if (mRequireLoginView.isChecked()) {
        username = mUsernameView.getText().toString();
        authType = getSelectedAuthType();
        if (AuthType.EXTERNAL == authType) {
            clientCertificateAlias = mClientCertificateSpinner.getAlias();
        } else {
            password = mPasswordView.getText().toString();
        }
    }
    String newHost = mServerView.getText().toString();
    int newPort = Integer.parseInt(mPortView.getText().toString());
    ServerSettings server = new ServerSettings(Type.SMTP, newHost, newPort, securityType, authType, username, password, clientCertificateAlias);
    uri = Transport.createTransportUri(server);
    mAccount.deleteCertificate(newHost, newPort, CheckDirection.OUTGOING);
    mAccount.setTransportUri(uri);
    AccountSetupCheckSettings.actionCheckSettings(this, mAccount, CheckDirection.OUTGOING);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) ServerSettings(com.fsck.k9.mail.ServerSettings) AuthType(com.fsck.k9.mail.AuthType)

Example 9 with AuthType

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

the class ImapStoreUriDecoder method decode.

/**
     * Decodes an ImapStore URI.
     *
     * <p>Possible forms:</p>
     * <pre>
     * imap://auth:user:password@server:port ConnectionSecurity.NONE
     * imap+tls+://auth:user:password@server:port ConnectionSecurity.STARTTLS_REQUIRED
     * imap+ssl+://auth:user:password@server:port ConnectionSecurity.SSL_TLS_REQUIRED
     * </pre>
     *
     * NOTE: this method expects the userinfo part of the uri to be encoded twice, due to a bug in
     * {@link ImapStoreUriCreator#create(ServerSettings)}.
     *
     * @param uri the store uri.
     */
public static ImapStoreSettings decode(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    AuthType authenticationType = null;
    String username = null;
    String password = null;
    String clientCertificateAlias = null;
    String pathPrefix = null;
    boolean autoDetectNamespace = true;
    URI imapUri;
    try {
        imapUri = new URI(uri);
    } catch (URISyntaxException use) {
        throw new IllegalArgumentException("Invalid ImapStore URI", use);
    }
    String scheme = imapUri.getScheme();
    /*
         * Currently available schemes are:
         * imap
         * imap+tls+
         * imap+ssl+
         *
         * The following are obsolete schemes that may be found in pre-existing
         * settings from earlier versions or that may be found when imported. We
         * continue to recognize them and re-map them appropriately:
         * imap+tls
         * imap+ssl
         */
    if (scheme.equals("imap")) {
        connectionSecurity = ConnectionSecurity.NONE;
        port = Type.IMAP.defaultPort;
    } else if (scheme.startsWith("imap+tls")) {
        connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
        port = Type.IMAP.defaultPort;
    } else if (scheme.startsWith("imap+ssl")) {
        connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
        port = Type.IMAP.defaultTlsPort;
    } else {
        throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }
    host = imapUri.getHost();
    if (imapUri.getPort() != -1) {
        port = imapUri.getPort();
    }
    if (imapUri.getUserInfo() != null) {
        String userinfo = imapUri.getUserInfo();
        String[] userInfoParts = userinfo.split(":");
        if (userinfo.endsWith(":")) {
            // Or XOAUTH2 where it's a valid config - XOAUTH:username:
            if (userInfoParts.length > 1) {
                authenticationType = AuthType.valueOf(userInfoParts[0]);
                username = decodeUtf8(userInfoParts[1]);
            } else {
                authenticationType = AuthType.PLAIN;
                username = decodeUtf8(userInfoParts[0]);
            }
        } else if (userInfoParts.length == 2) {
            // Old/standard style of encoding - PLAIN auth only:
            // username:password
            authenticationType = AuthType.PLAIN;
            username = decodeUtf8(userInfoParts[0]);
            password = decodeUtf8(userInfoParts[1]);
        } else if (userInfoParts.length == 3) {
            // Standard encoding
            // PLAIN:username:password
            // EXTERNAL:username:certAlias
            authenticationType = AuthType.valueOf(userInfoParts[0]);
            username = decodeUtf8(userInfoParts[1]);
            if (AuthType.EXTERNAL == authenticationType) {
                clientCertificateAlias = decodeUtf8(userInfoParts[2]);
            } else {
                password = decodeUtf8(userInfoParts[2]);
            }
        }
    }
    String path = imapUri.getPath();
    if (path != null && path.length() > 1) {
        // Strip off the leading "/"
        String cleanPath = path.substring(1);
        if (cleanPath.length() >= 2 && cleanPath.charAt(1) == '|') {
            autoDetectNamespace = cleanPath.charAt(0) == '1';
            if (!autoDetectNamespace) {
                pathPrefix = cleanPath.substring(2);
            }
        } else {
            if (cleanPath.length() > 0) {
                pathPrefix = cleanPath;
                autoDetectNamespace = false;
            }
        }
    }
    return new ImapStoreSettings(host, port, connectionSecurity, authenticationType, username, password, clientCertificateAlias, autoDetectNamespace, pathPrefix);
}
Also used : ConnectionSecurity(com.fsck.k9.mail.ConnectionSecurity) AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 10 with AuthType

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

the class ImapStoreUriCreator method create.

/**
     * Creates an ImapStore URI with the supplied settings.
     *
     * @param server
     *         The {@link ServerSettings} object that holds the server settings.
     *
     * @return An ImapStore URI that holds the same information as the {@code server} parameter.
     *
     * @see com.fsck.k9.mail.store.StoreConfig#getStoreUri()
     * @see ImapStore#decodeUri(String)
     */
public static String create(ServerSettings server) {
    String userEnc = encodeUtf8(server.username);
    String passwordEnc = (server.password != null) ? encodeUtf8(server.password) : "";
    String clientCertificateAliasEnc = (server.clientCertificateAlias != null) ? encodeUtf8(server.clientCertificateAlias) : "";
    String scheme;
    switch(server.connectionSecurity) {
        case SSL_TLS_REQUIRED:
            scheme = "imap+ssl+";
            break;
        case STARTTLS_REQUIRED:
            scheme = "imap+tls+";
            break;
        default:
        case NONE:
            scheme = "imap";
            break;
    }
    AuthType authType = server.authenticationType;
    String userInfo;
    if (authType == AuthType.EXTERNAL) {
        userInfo = authType.name() + ":" + userEnc + ":" + clientCertificateAliasEnc;
    } else {
        userInfo = authType.name() + ":" + userEnc + ":" + passwordEnc;
    }
    try {
        Map<String, String> extra = server.getExtra();
        String path;
        if (extra != null) {
            boolean autoDetectNamespace = Boolean.TRUE.toString().equals(extra.get(ImapStoreSettings.AUTODETECT_NAMESPACE_KEY));
            String pathPrefix = (autoDetectNamespace) ? null : extra.get(ImapStoreSettings.PATH_PREFIX_KEY);
            path = "/" + (autoDetectNamespace ? "1" : "0") + "|" + ((pathPrefix == null) ? "" : pathPrefix);
        } else {
            path = "/1|";
        }
        return new URI(scheme, userInfo, server.host, server.port, path, null, null).toString();
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Can't create ImapStore URI", e);
    }
}
Also used : AuthType(com.fsck.k9.mail.AuthType) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

AuthType (com.fsck.k9.mail.AuthType)14 ServerSettings (com.fsck.k9.mail.ServerSettings)7 ConnectionSecurity (com.fsck.k9.mail.ConnectionSecurity)6 URISyntaxException (java.net.URISyntaxException)6 URI (java.net.URI)5 OnItemSelectedListener (android.widget.AdapterView.OnItemSelectedListener)4 View (android.view.View)2 HashMap (java.util.HashMap)2 SharedPreferences (android.content.SharedPreferences)1 Account (com.fsck.k9.Account)1 Preferences (com.fsck.k9.Preferences)1 Store (com.fsck.k9.mail.Store)1 RemoteStore (com.fsck.k9.mail.store.RemoteStore)1 StoreConfig (com.fsck.k9.mail.store.StoreConfig)1 InvalidSettingValueException (com.fsck.k9.preferences.Settings.InvalidSettingValueException)1 Map (java.util.Map)1 Matchers.anyString (org.mockito.Matchers.anyString)1