Further optimizing webapp authenticator valve implementation

revert-70aa11f8
prabathabey 9 years ago
parent 28a2e918e7
commit 262e53ddcc

@ -112,7 +112,8 @@
org.apache.commons.pool.impl, org.apache.commons.pool.impl,
org.apache.http.client, org.apache.http.client,
org.apache.http.conn, org.apache.http.conn,
org.apache.http.impl.client org.apache.http.impl.client,
org.apache.http.impl.conn
</Import-Package> </Import-Package>
</instructions> </instructions>
</configuration> </configuration>

@ -31,6 +31,9 @@ import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub; import org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub;
import org.wso2.carbon.webapp.authenticator.framework.authenticator.oauth.OAuthConstants; import org.wso2.carbon.webapp.authenticator.framework.authenticator.oauth.OAuthConstants;
import org.wso2.carbon.webapp.authenticator.framework.authenticator.oauth.OAuthTokenValidationException; import org.wso2.carbon.webapp.authenticator.framework.authenticator.oauth.OAuthTokenValidationException;
@ -52,12 +55,10 @@ public class OAuthTokenValidationStubFactory implements PoolableObjectFactory {
this.url = url; this.url = url;
this.basicAuthHeader = new String(Base64.encodeBase64((adminUsername + ":" + adminPassword).getBytes())); this.basicAuthHeader = new String(Base64.encodeBase64((adminUsername + ":" + adminPassword).getBytes()));
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.getParams().setDefaultMaxConnectionsPerHost( connectionManager.setDefaultMaxPerRoute(Integer.parseInt(properties.getProperty("MaxConnectionsPerHost")));
Integer.parseInt(properties.getProperty("MaxConnectionsPerHost"))); connectionManager.setMaxTotal(Integer.parseInt(properties.getProperty("MaxTotalConnections")));
connectionManager.getParams().setMaxTotalConnections( this.httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
Integer.parseInt(properties.getProperty("MaxTotalConnections")));
this.httpClient = new DefaultHttpClient((ClientConnectionManager) connectionManager);
} }
@Override @Override

@ -33,6 +33,11 @@ public class BasicAuthAuthenticator implements WebappAuthenticator {
private static final String BASIC_AUTH_AUTHENTICATOR = "BasicAuth"; private static final String BASIC_AUTH_AUTHENTICATOR = "BasicAuth";
@Override
public void init() {
}
@Override @Override
public boolean canHandle(Request request) { public boolean canHandle(Request request) {
MessageBytes authorization = MessageBytes authorization =

@ -26,6 +26,11 @@ public class CertificateAuthenticator implements WebappAuthenticator {
private static final String CERTIFICATE_AUTHENTICATOR = "CertificateAuth"; private static final String CERTIFICATE_AUTHENTICATOR = "CertificateAuth";
private static final String CERTIFICATE_VERIFICATION_HEADER = "certificate-verification-header"; private static final String CERTIFICATE_VERIFICATION_HEADER = "certificate-verification-header";
@Override
public void init() {
}
@Override @Override
public boolean canHandle(Request request) { public boolean canHandle(Request request) {
String certVerificationHeader = request.getContext().findParameter(CERTIFICATE_VERIFICATION_HEADER); String certVerificationHeader = request.getContext().findParameter(CERTIFICATE_VERIFICATION_HEADER);

@ -52,6 +52,11 @@ public class JWTAuthenticator implements WebappAuthenticator {
private static final String JWT_AUTHENTICATOR = "JWT"; private static final String JWT_AUTHENTICATOR = "JWT";
private static final String JWT_ASSERTION_HEADER = "X-JWT-Assertion"; private static final String JWT_ASSERTION_HEADER = "X-JWT-Assertion";
@Override
public void init() {
}
@Override @Override
public boolean canHandle(Request request) { public boolean canHandle(Request request) {
String authorizationHeader = request.getHeader(JWTAuthenticator.JWT_ASSERTION_HEADER); String authorizationHeader = request.getHeader(JWTAuthenticator.JWT_ASSERTION_HEADER);

@ -52,16 +52,33 @@ public class OAuthAuthenticator implements WebappAuthenticator {
private static final Log log = LogFactory.getLog(OAuthAuthenticator.class); private static final Log log = LogFactory.getLog(OAuthAuthenticator.class);
public OAuthAuthenticator() { @Override
public void init() {
if (properties == null) {
throw new IllegalArgumentException("Required properties needed to initialize OAuthAuthenticator are " +
"not provided");
}
String url = properties.getProperty("TokenValidationEndpointUrl"); String url = properties.getProperty("TokenValidationEndpointUrl");
if (url == null || url.isEmpty()) {
throw new IllegalArgumentException("OAuth token validation endpoint url is not provided");
}
String adminUsername = properties.getProperty("Username"); String adminUsername = properties.getProperty("Username");
if (adminUsername == null) {
throw new IllegalArgumentException("Username to connect to the OAuth token validation endpoint is " +
"not provided");
}
String adminPassword = properties.getProperty("Password"); String adminPassword = properties.getProperty("Password");
if (adminPassword == null) {
throw new IllegalArgumentException("Password to connect to the OAuth token validation endpoint is " +
"not provided");
}
boolean isRemote = Boolean.parseBoolean(properties.getProperty("IsRemote")); boolean isRemote = Boolean.parseBoolean(properties.getProperty("IsRemote"));
Properties validatorProperties = new Properties(); Properties validatorProperties = new Properties();
validatorProperties.setProperty("MaxTotalConnections", properties.getProperty("MaxTotalConnections")); validatorProperties.setProperty("MaxTotalConnections", properties.getProperty("MaxTotalConnections"));
validatorProperties.setProperty("MaxConnectionsPerHost", properties.getProperty("MaxTotalConnectionsPerHost")); validatorProperties.setProperty("MaxConnectionsPerHost", properties.getProperty("MaxConnectionsPerHost"));
this.tokenValidator = OAuthValidatorFactory.getNewValidator(url, adminUsername, adminPassword, isRemote, validatorProperties); this.tokenValidator =
OAuthValidatorFactory.getNewValidator(url, adminUsername, adminPassword, isRemote, validatorProperties);
} }
@Override @Override

@ -30,6 +30,8 @@ public interface WebappAuthenticator {
SUCCESS, FAILURE, CONTINUE SUCCESS, FAILURE, CONTINUE
} }
void init();
boolean canHandle(Request request); boolean canHandle(Request request);
AuthenticationInfo authenticate(Request request, Response response); AuthenticationInfo authenticate(Request request, Response response);

@ -18,10 +18,7 @@
*/ */
package org.wso2.carbon.webapp.authenticator.framework.config; package org.wso2.carbon.webapp.authenticator.framework.config;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List; import java.util.List;
@XmlRootElement(name = "Authenticator") @XmlRootElement(name = "Authenticator")
@ -55,6 +52,10 @@ public class AuthenticatorConfig {
return params; return params;
} }
public void setParams(List<Parameter> params) {
this.params = params;
}
@XmlRootElement(name = "Parameter") @XmlRootElement(name = "Parameter")
public static class Parameter { public static class Parameter {
private String name; private String name;
@ -69,7 +70,7 @@ public class AuthenticatorConfig {
this.name = name; this.name = name;
} }
@XmlElement(name = "Value") @XmlValue
public String getValue() { public String getValue() {
return value; return value;
} }

@ -80,13 +80,14 @@ public class WebappAuthenticatorFrameworkServiceComponent {
for (AuthenticatorConfig config : WebappAuthenticatorConfig.getInstance().getAuthenticators()) { for (AuthenticatorConfig config : WebappAuthenticatorConfig.getInstance().getAuthenticators()) {
WebappAuthenticator authenticator = (WebappAuthenticator) Class.forName(config.getClassName()). WebappAuthenticator authenticator = (WebappAuthenticator) Class.forName(config.getClassName()).
newInstance(); newInstance();
if (config.getParams() != null || !config.getParams().isEmpty()) { if (config.getParams() != null && !config.getParams().isEmpty()) {
Properties properties = new Properties(); Properties properties = new Properties();
for (AuthenticatorConfig.Parameter param : config.getParams()) { for (AuthenticatorConfig.Parameter param : config.getParams()) {
properties.setProperty(param.getName(), param.getValue()); properties.setProperty(param.getName(), param.getValue());
} }
authenticator.setProperties(properties); authenticator.setProperties(properties);
} }
authenticator.init();
repository.addAuthenticator(authenticator); repository.addAuthenticator(authenticator);
} }
AuthenticatorFrameworkDataHolder.getInstance().setWebappAuthenticatorRepository(repository); AuthenticatorFrameworkDataHolder.getInstance().setWebappAuthenticatorRepository(repository);
@ -99,7 +100,7 @@ public class WebappAuthenticatorFrameworkServiceComponent {
log.debug("Web Application Authenticator Framework Bundle has been started successfully"); log.debug("Web Application Authenticator Framework Bundle has been started successfully");
} }
} catch (Throwable e) { } catch (Throwable e) {
log.error("Error occurred while initializing the bundle", e); log.error("Error occurred while initializing the bundle", e);
} }
} }

@ -3,6 +3,14 @@
<Authenticator> <Authenticator>
<Name>OAuth</Name> <Name>OAuth</Name>
<ClassName>org.wso2.carbon.webapp.authenticator.framework.authenticator.OAuthAuthenticator</ClassName> <ClassName>org.wso2.carbon.webapp.authenticator.framework.authenticator.OAuthAuthenticator</ClassName>
<Parameters>
<Parameter Name="TokenValidationEndpointUrl">https://localhost:9443</Parameter>
<Parameter Name="Username">admin</Parameter>
<Parameter Name="Password">admin</Parameter>
<Parameter Name="IsRemote">true</Parameter>
<Parameter Name="MaxConnectionsPerHost">10000</Parameter>
<Parameter Name="MaxTotalConnections">10000</Parameter>
</Parameters>
</Authenticator> </Authenticator>
<Authenticator> <Authenticator>
<Name>BasicAuth</Name> <Name>BasicAuth</Name>

Loading…
Cancel
Save