Skip to content

Commit

Permalink
[ELY-1815] Unable to set custom AUTHENTICATION_TIMEOUT value
Browse files Browse the repository at this point in the history
  • Loading branch information
lvydra committed Feb 27, 2024
1 parent 2bbdcfc commit 962457d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.wildfly.security.sasl.util.AbstractDelegatingSaslServerFactory;
import org.wildfly.security.sasl.util.AuthenticationCompleteCallbackSaslServerFactory;
import org.wildfly.security.sasl.util.AuthenticationTimeoutSaslServerFactory;
import org.wildfly.security.sasl.util.PropertiesSaslServerFactory;
import org.wildfly.security.sasl.util.SaslMechanismInformation;
import org.wildfly.security.sasl.util.SecurityIdentitySaslServerFactory;
import org.wildfly.security.sasl.util.SetMechanismInformationSaslServerFactory;
Expand Down Expand Up @@ -121,6 +122,7 @@ public static Builder builder() {
public static final class Builder extends AbstractMechanismAuthenticationFactory.Builder<SaslServer, SaslServerFactory, SaslException> {

private ScheduledExecutorService scheduledExecutorService;
private Map<String, Object> properties;

/**
* Construct a new instance.
Expand All @@ -143,6 +145,11 @@ public Builder setFactory(final SaslServerFactory factory) {
return this;
}

public Builder setProperties(final Map<String, Object> properties) {
this.properties = properties;
return this;
}

/**
* Set the scheduled executor service.
*
Expand Down Expand Up @@ -174,6 +181,10 @@ public SaslAuthenticationFactory build() {
}
factory = new AuthenticationTimeoutSaslServerFactory(factory, this.scheduledExecutorService);

if (this.properties != null && this.properties.size() > 0) {
factory = new PropertiesSaslServerFactory(factory, properties);
}

return new SaslAuthenticationFactory(getSecurityDomain(), getMechanismConfigurationSelector(), factory);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
package org.wildfly.security.sasl.test;

import static java.security.AccessController.doPrivileged;
import static org.wildfly.security.sasl.WildFlySasl.AUTHENTICATION_TIMEOUT;

import java.net.URI;
import java.net.URISyntaxException;
import java.security.PrivilegedAction;
import java.security.Provider;
import java.security.Security;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;

Expand All @@ -53,12 +54,8 @@
import org.wildfly.security.sasl.SaslMechanismSelector;
import org.wildfly.security.sasl.digest.DigestServerFactory;
import org.wildfly.security.sasl.digest.WildFlyElytronSaslDigestProvider;
import org.wildfly.security.sasl.util.AuthenticationTimeoutSaslServerFactory;
import org.wildfly.security.sasl.util.SaslMechanismInformation;

import mockit.Mock;
import mockit.MockUp;

/**
* Tests a successful authentication timeout for a custom executor service and the default executor service.
*
Expand All @@ -75,28 +72,8 @@ public class SaslAuthenticationTimeoutTest {
WildFlyElytronPasswordProvider.getInstance()
};

/*
* Unable to set custom AUTHENTICATION_TIMEOUT using a property SaslServer factory (see ELY-1815), so using mock
* function to avoid using default timeout of 150 sec
*/
private static void mockGetTimeout() {
Class<?> classToMock;
try {
classToMock = Class.forName("org.wildfly.security.sasl.util.AuthenticationTimeoutSaslServerFactory", true, AuthenticationTimeoutSaslServerFactory.class.getClassLoader());
} catch (ClassNotFoundException e) {
throw new NoClassDefFoundError(e.getMessage());
}
new MockUp<Object>(classToMock) {
@Mock
private long getTimeout(final Map<String, ?> props) {
return 3;
}
};
}

@BeforeClass
public static void registerPasswordProvider() {
mockGetTimeout();
for (Provider provider : providers) {
Security.insertProviderAt(provider, 1);
}
Expand All @@ -121,12 +98,14 @@ public void testSuccessfulTimeout() throws Exception {
INSTANCE.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);

try {

HashMap<String, Object> properties = new HashMap<>();
properties.put(AUTHENTICATION_TIMEOUT, "3");
SaslServer server = new SaslServerBuilder(DigestServerFactory.class, DIGEST)
.setUserName("George")
.setPassword("gpwd".toCharArray())
.setProtocol("TestProtocol")
.setServerName("TestServer")
.setProperties(properties)
.setScheduledExecutorService(INSTANCE)
.addMechanismRealm("TestRealm")
.build();
Expand All @@ -153,12 +132,14 @@ public void testSuccessfulTimeout() throws Exception {
public void testSuccessfulTimeout_DefaultExecuterService() throws Exception {

try {

HashMap<String, Object> properties = new HashMap<>();
properties.put(AUTHENTICATION_TIMEOUT, "3");
SaslServer server = new SaslServerBuilder(DigestServerFactory.class, DIGEST)
.setUserName("George")
.setPassword("gpwd".toCharArray())
.setProtocol("TestProtocol")
.setServerName("TestServer")
.setProperties(properties)
.addMechanismRealm("TestRealm")
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import org.wildfly.security.sasl.util.ChannelBindingSaslServerFactory;
import org.wildfly.security.sasl.util.CredentialSaslServerFactory;
import org.wildfly.security.sasl.util.KeyManagerCredentialSaslServerFactory;
import org.wildfly.security.sasl.util.PropertiesSaslServerFactory;
import org.wildfly.security.sasl.util.ProtocolSaslServerFactory;
import org.wildfly.security.sasl.util.SecurityProviderSaslServerFactory;
import org.wildfly.security.sasl.util.ServerNameSaslServerFactory;
Expand Down Expand Up @@ -377,11 +376,8 @@ public SaslServer build() throws IOException {
if (factory == null && providerSupplier != null) {
factory = new SecurityProviderSaslServerFactory(providerSupplier);
}
if (properties != null && properties.size() > 0) {
if (properties.containsKey(WildFlySasl.REALM_LIST)) {
factory = new AvailableRealmsSaslServerFactory(factory);
}
factory = new PropertiesSaslServerFactory(factory, properties);
if (properties != null && properties.size() > 0 && properties.containsKey(WildFlySasl.REALM_LIST)) {
factory = new AvailableRealmsSaslServerFactory(factory);
}
if (bindingTypeAndData != null) {
factory = new ChannelBindingSaslServerFactory(factory, bindingTypeAndData.key, bindingTypeAndData.value);
Expand All @@ -403,6 +399,9 @@ public SaslServer build() throws IOException {
}
final SaslAuthenticationFactory.Builder builder = SaslAuthenticationFactory.builder();
builder.setFactory(factory);
if (properties != null && properties.size() > 0) {
builder.setProperties(properties);
}
builder.setSecurityDomain(securityDomain);
if (scheduledExecutorService != null) {
builder.setScheduledExecutorService(scheduledExecutorService);
Expand Down

0 comments on commit 962457d

Please sign in to comment.