Search in sources :

Example 1 with SASLXTOKENMechanism

use of com.xabber.xmpp.smack.SASLXTOKENMechanism in project xabber-android by redsolution.

the class ConnectionBuilder method build.

@NonNull
public static XMPPTCPConnection build(AccountJid account, @NonNull final ConnectionSettings connectionSettings) {
    XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
    builder.setXmppDomain(connectionSettings.getServerName());
    if (connectionSettings.isCustomHostAndPort()) {
        setCustomHost(connectionSettings, builder);
        builder.setPort(connectionSettings.getPort());
    }
    builder.setDebuggerEnabled(true);
    builder.setSecurityMode(connectionSettings.getTlsMode().getSecurityMode());
    builder.setCompressionEnabled(connectionSettings.useCompression());
    builder.setSendPresence(false);
    builder.setUsernameAndPassword(connectionSettings.getUserName(), connectionSettings.getPassword());
    builder.setResource(connectionSettings.getResource());
    builder.setProxyInfo(getProxyInfo(connectionSettings));
    try {
        LogManager.i(LOG_TAG, "SettingsManager.securityCheckCertificate: " + SettingsManager.securityCheckCertificate());
        if (SettingsManager.securityCheckCertificate()) {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            MemorizingTrustManager mtm = CertificateManager.getInstance().getNewMemorizingTrustManager(account);
            sslContext.init(null, new X509TrustManager[] { mtm }, new java.security.SecureRandom());
            builder.setCustomSSLContext(sslContext);
            builder.setHostnameVerifier(mtm.wrapHostnameVerifier(new CustomDomainVerifier()));
        } else {
            TLSUtils.acceptAllCertificates(builder);
            builder.setHostnameVerifier(new AllowAllHostnameVerifier());
        }
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        LogManager.exception(LOG_TAG, e);
    }
    // if account have token
    if (connectionSettings.getToken() != null && !connectionSettings.getToken().isEmpty() && connectionSettings.getPassword() != null && connectionSettings.getPassword().isEmpty()) {
        // then enable only SASLXOauth2Mechanism
        builder.addEnabledSaslMechanism(SASLXOauth2Mechanism.NAME);
        // and set token as password
        builder.setUsernameAndPassword(connectionSettings.getUserName(), connectionSettings.getToken());
    }
    // X-TOKEN
    if (connectionSettings.getXToken() != null && !connectionSettings.getXToken().isExpired()) {
        LogManager.d(LOG_TAG, "Authorization with x-token");
        SASLAuthentication.registerSASLMechanism(new SASLXTOKENMechanism());
        builder.addEnabledSaslMechanism(SASLXTOKENMechanism.NAME);
        builder.setUsernameAndPassword(connectionSettings.getUserName(), connectionSettings.getXToken().getToken());
    }
    LogManager.i(LOG_TAG, "new XMPPTCPConnection " + connectionSettings.getServerName());
    return new XMPPTCPConnection(builder.build());
}
Also used : SASLXTOKENMechanism(com.xabber.xmpp.smack.SASLXTOKENMechanism) XMPPTCPConnection(com.xabber.xmpp.smack.XMPPTCPConnection) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) XMPPTCPConnectionConfiguration(com.xabber.xmpp.smack.XMPPTCPConnectionConfiguration) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) MemorizingTrustManager(de.duenndns.ssl.MemorizingTrustManager) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)1 SASLXTOKENMechanism (com.xabber.xmpp.smack.SASLXTOKENMechanism)1 XMPPTCPConnection (com.xabber.xmpp.smack.XMPPTCPConnection)1 XMPPTCPConnectionConfiguration (com.xabber.xmpp.smack.XMPPTCPConnectionConfiguration)1 MemorizingTrustManager (de.duenndns.ssl.MemorizingTrustManager)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SSLContext (javax.net.ssl.SSLContext)1 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)1