Skip to content

Commit

Permalink
LoginDialog and AccountCreationWizard: extract configureConnectionTls()
Browse files Browse the repository at this point in the history
  • Loading branch information
stokito committed Aug 24, 2024
1 parent 1d58ed1 commit 6ee0092
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
29 changes: 16 additions & 13 deletions core/src/main/java/org/jivesoftware/AccountCreationWizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,22 @@ private XMPPConnection getConnection() throws SmackException, IOException, XMPPE
{
builder.setHost( localPreferences.getXmppHost() );
}

configureConnectionTls(builder, securityMode, useDirectTls, hostPortConfigured, serverName);

final XMPPTCPConnectionConfiguration configuration = builder.build();

final AbstractXMPPConnection connection = new XMPPTCPConnection( configuration );
connection.setParsingExceptionCallback( new ExceptionLoggingCallback() );
try {
connection.connect();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}

return connection;
}

private void configureConnectionTls(XMPPTCPConnectionConfiguration.Builder builder, ConnectionConfiguration.SecurityMode securityMode, boolean useDirectTls, boolean hostPortConfigured, String serverName) throws SmackException.SmackMessageException {
if (securityMode != ConnectionConfiguration.SecurityMode.disabled) {
if (!useDirectTls) {
// This use STARTTLS which starts initially plain connection to upgrade it to TLS, it use the same port as
Expand Down Expand Up @@ -398,18 +413,6 @@ private XMPPConnection getConnection() throws SmackException, IOException, XMPPE
builder.setSecurityMode( ConnectionConfiguration.SecurityMode.ifpossible );
}
}

final XMPPTCPConnectionConfiguration configuration = builder.build();

final AbstractXMPPConnection connection = new XMPPTCPConnection( configuration );
connection.setParsingExceptionCallback( new ExceptionLoggingCallback() );
try {
connection.connect();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}

return connection;
}

/**
Expand Down
61 changes: 32 additions & 29 deletions core/src/main/java/org/jivesoftware/LoginDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,38 @@ protected XMPPTCPConnectionConfiguration retrieveConnectionConfiguration() {
if (localPref.isProxyEnabled()) {
builder.setProxyInfo(proxyInfo);
}
configureConnectionTls(builder, securityMode, useDirectTls, hostPortConfigured, loginServer);

// SPARK-1747: Don't use the GSS-API SASL mechanism when SSO is disabled.
SASLAuthentication.unregisterSASLMechanism(SASLGSSAPIMechanism.class.getName());
SASLAuthentication.unregisterSASLMechanism(SASLGSSAPIv3CompatMechanism.class.getName());

// Add the mechanism only when SSO is enabled (which allows us to register the correct one).
if (localPref.isSSOEnabled()) {
// SPARK-1740: Register a mechanism that's compatible with Smack 3, when requested.
if (localPref.isSaslGssapiSmack3Compatible()) {
// SPARK-1747: Don't use the GSSAPI mechanism when SSO is disabled.
SASLAuthentication.registerSASLMechanism(new SASLGSSAPIv3CompatMechanism());
} else {
SASLAuthentication.registerSASLMechanism(new SASLGSSAPIMechanism());
}
}

if (localPref.isLoginAnonymously() && !localPref.isSSOEnabled()) {
//later login() is called without arguments
builder.performSaslAnonymousAuthentication();
}

// TODO These were used in Smack 3. Find Smack 4 alternative.
// config.setRosterLoadedAtLogin(true);
// if(ModelUtil.hasLength(localPref.getTrustStorePath())) {
// config.setTruststorePath(localPref.getTrustStorePath());
// config.setTruststorePassword(localPref.getTrustStorePassword());
// }
return builder.build();
}

private void configureConnectionTls(XMPPTCPConnectionConfiguration.Builder builder, ConnectionConfiguration.SecurityMode securityMode, boolean useDirectTls, boolean hostPortConfigured, String serverName) {
if (securityMode != ConnectionConfiguration.SecurityMode.disabled) {
if (localPref.isDisableHostnameVerification()) {
TLSUtils.disableHostnameVerificationForTlsCertificates(builder);
Expand All @@ -311,7 +342,7 @@ protected XMPPTCPConnectionConfiguration retrieveConnectionConfiguration() {
// SMACK 4.1.9 does not support XEP-0368, and does not apply a port change, if the host is not changed too.
// Here, we force the host to be set (by doing a DNS lookup), and force the port to 5223 (which is the
// default 'old-style' SSL port).
DnsName serverNameDnsName = DnsName.from(loginServer);
DnsName serverNameDnsName = DnsName.from(serverName);
java.util.List<InetAddress> resolvedAddresses = DNSUtil.getDNSResolver().lookupHostAddress(serverNameDnsName, null, DnssecMode.disabled);
if (resolvedAddresses.isEmpty()) {
throw new RuntimeException("Could not resolve " + serverNameDnsName);
Expand All @@ -328,34 +359,6 @@ protected XMPPTCPConnectionConfiguration retrieveConnectionConfiguration() {
}
SASLAuthentication.registerSASLMechanism(new SASLExternalMechanism());
}

// SPARK-1747: Don't use the GSS-API SASL mechanism when SSO is disabled.
SASLAuthentication.unregisterSASLMechanism(SASLGSSAPIMechanism.class.getName());
SASLAuthentication.unregisterSASLMechanism(SASLGSSAPIv3CompatMechanism.class.getName());

// Add the mechanism only when SSO is enabled (which allows us to register the correct one).
if (localPref.isSSOEnabled()) {
// SPARK-1740: Register a mechanism that's compatible with Smack 3, when requested.
if (localPref.isSaslGssapiSmack3Compatible()) {
// SPARK-1747: Don't use the GSSAPI mechanism when SSO is disabled.
SASLAuthentication.registerSASLMechanism(new SASLGSSAPIv3CompatMechanism());
} else {
SASLAuthentication.registerSASLMechanism(new SASLGSSAPIMechanism());
}
}

if (localPref.isLoginAnonymously() && !localPref.isSSOEnabled()) {
//later login() is called without arguments
builder.performSaslAnonymousAuthentication();
}

// TODO These were used in Smack 3. Find Smack 4 alternative.
// config.setRosterLoadedAtLogin(true);
// if(ModelUtil.hasLength(localPref.getTrustStorePath())) {
// config.setTruststorePath(localPref.getTrustStorePath());
// config.setTruststorePassword(localPref.getTrustStorePassword());
// }
return builder.build();
}

/**
Expand Down

0 comments on commit 6ee0092

Please sign in to comment.