From 11957f1e478f884cc1f100debb4bbc8290c0901c Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 21 Dec 2015 11:00:42 +0530 Subject: [PATCH 1/3] Pooling OAuth token validator stub initialization --- .../pom.xml | 28 +++++ .../OAuthTokenValidationStubFactory.java | 115 ++++++++++++++++-- .../framework/WebappAuthenticatorFactory.java | 3 +- .../oauth/impl/RemoteOAuthValidator.java | 67 ++++++---- .../test/WebappAuthenticatorConfigTest.java | 64 ++++++++++ .../WebappAuthenticatorFrameworkUtilTest.java | 106 ++++++++++++++++ .../etc/webapp-authenticator-config.xml | 28 +++++ .../src/test/resources/log4j.properties | 32 +++++ .../src/test/resources/testng.xml | 37 ++++++ pom.xml | 18 ++- 10 files changed, 458 insertions(+), 40 deletions(-) create mode 100644 components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/test/WebappAuthenticatorConfigTest.java create mode 100644 components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/test/WebappAuthenticatorFrameworkUtilTest.java create mode 100644 components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/config/etc/webapp-authenticator-config.xml create mode 100644 components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/log4j.properties create mode 100644 components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/testng.xml diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/pom.xml b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/pom.xml index 3414eea46f..84a78d27d4 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/pom.xml +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/pom.xml @@ -118,6 +118,18 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + + file:src/test/resources/log4j.properties + + + src/test/resources/testng.xml + + + @@ -182,6 +194,22 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.common + + org.apache.httpcomponents.wso2 + httpclient + + + commons-httpclient.wso2 + commons-httpclient + + + org.testng + testng + + + commons-pool.wso2 + commons-pool + diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/Utils/OAuthTokenValidationStubFactory.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/Utils/OAuthTokenValidationStubFactory.java index 88ceb34aeb..95fbc86a44 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/Utils/OAuthTokenValidationStubFactory.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/Utils/OAuthTokenValidationStubFactory.java @@ -24,15 +24,14 @@ import org.apache.axis2.client.ServiceClient; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.commons.codec.binary.Base64; import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpConnectionManager; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; +import org.apache.commons.httpclient.params.HttpConnectionManagerParams; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.pool.PoolableObjectFactory; -import org.apache.http.client.HttpClient; -import org.apache.http.conn.ClientConnectionManager; -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.conn.HttpClientConnectionManager; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub; import org.wso2.carbon.webapp.authenticator.framework.authenticator.oauth.OAuthConstants; @@ -46,19 +45,94 @@ public class OAuthTokenValidationStubFactory implements PoolableObjectFactory { private String url; private String basicAuthHeader; - private static final Log log = LogFactory.getLog(OAuthTokenValidationStubFactory.class); - private HttpClient httpClient; + private static final Log log = LogFactory.getLog(OAuthTokenValidationStubFactory.class); + public OAuthTokenValidationStubFactory(String url, String adminUsername, String adminPassword, Properties properties) { + this.validateUrl(url); this.url = url; + + this.validateCredentials(adminUsername, adminPassword); this.basicAuthHeader = new String(Base64.encodeBase64((adminUsername + ":" + adminPassword).getBytes())); + HttpConnectionManager connectionManager = this.createConnectionManager(properties); + this.httpClient = new HttpClient(connectionManager); + } + + /** + * Creates an instance of MultiThreadedHttpConnectionManager using HttpClient 3.x APIs + * + * @param properties Properties to configure MultiThreadedHttpConnectionManager + * @return An instance of properly configured MultiThreadedHttpConnectionManager + */ + private HttpConnectionManager createConnectionManager(Properties properties) { + HttpConnectionManagerParams params = new HttpConnectionManagerParams(); + if (properties == null || properties.isEmpty()) { + throw new IllegalArgumentException("Parameters required to initialize HttpClient instances " + + "associated with OAuth token validation service stub are not provided"); + } + String maxConnectionsPerHostParam = properties.getProperty("MaxConnectionsPerHost"); + if (maxConnectionsPerHostParam == null || maxConnectionsPerHostParam.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("MaxConnectionsPerHost parameter is not explicitly defined. Therefore, the default, " + + "which is 2, will be used"); + } + } else { + params.setDefaultMaxConnectionsPerHost(Integer.parseInt(maxConnectionsPerHostParam)); + } + + String maxTotalConnectionsParam = properties.getProperty("MaxTotalConnections"); + if (maxTotalConnectionsParam == null || maxTotalConnectionsParam.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("MaxTotalConnections parameter is not explicitly defined. Therefore, the default, " + + "which is 10, will be used"); + } + } else { + params.setMaxTotalConnections(Integer.parseInt(maxTotalConnectionsParam)); + } + HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); + connectionManager.setParams(params); + return connectionManager; + } + + /** + * Creates an instance of PoolingHttpClientConnectionManager using HttpClient 4.x APIs + * + * @param properties Properties to configure PoolingHttpClientConnectionManager + * @return An instance of properly configured PoolingHttpClientConnectionManager + */ + private HttpClientConnectionManager createClientConnectionManager(Properties properties) { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); - connectionManager.setDefaultMaxPerRoute(Integer.parseInt(properties.getProperty("MaxConnectionsPerHost"))); - connectionManager.setMaxTotal(Integer.parseInt(properties.getProperty("MaxTotalConnections"))); - this.httpClient = HttpClients.custom().setConnectionManager(connectionManager).build(); + if (properties != null) { + String maxConnectionsPerHostParam = properties.getProperty("MaxConnectionsPerHost"); + if (maxConnectionsPerHostParam == null || maxConnectionsPerHostParam.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("MaxConnectionsPerHost parameter is not explicitly defined. Therefore, the default, " + + "which is 2, will be used"); + } + } else { + connectionManager.setDefaultMaxPerRoute(Integer.parseInt(maxConnectionsPerHostParam)); + } + + String maxTotalConnectionsParam = properties.getProperty("MaxTotalConnections"); + if (maxTotalConnectionsParam == null || maxTotalConnectionsParam.isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("MaxTotalConnections parameter is not explicitly defined. Therefore, the default, " + + "which is 10, will be used"); + } + } else { + connectionManager.setMaxTotal(Integer.parseInt(maxTotalConnectionsParam)); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Properties, i.e. MaxTotalConnections/MaxConnectionsPerHost, required to tune the " + + "HttpClient used in OAuth token validation service stub instances are not provided. " + + "Therefore, the defaults, 2/10 respectively, will be used"); + } + } + return connectionManager; } @Override @@ -88,7 +162,6 @@ public class OAuthTokenValidationStubFactory implements PoolableObjectFactory { if (o instanceof OAuth2TokenValidationServiceStub) { OAuth2TokenValidationServiceStub stub = (OAuth2TokenValidationServiceStub) o; stub._getServiceClient().cleanupTransport(); - stub._getServiceClient().setOptions(null); } } @@ -111,10 +184,28 @@ public class OAuthTokenValidationStubFactory implements PoolableObjectFactory { options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true"); client.setOptions(options); } catch (AxisFault axisFault) { - throw new OAuthTokenValidationException("Exception occurred while creating the " + + throw new OAuthTokenValidationException("Error occurred while creating the " + "OAuth2TokenValidationServiceStub.", axisFault); } return stub; } + private void validateUrl(String url) { + if (url == null || url.isEmpty()) { + throw new IllegalArgumentException("Url provided as the endpoint of the OAuth token validation service " + + "is null"); + } + } + + private void validateCredentials(String adminUsername, String adminPassword) { + if (adminUsername == null || adminUsername.isEmpty()) { + throw new IllegalArgumentException("An appropriate username required to initialize OAuth token " + + "validation service stub factory hasn't been provided"); + } + if (adminPassword == null || adminPassword.isEmpty()) { + throw new IllegalArgumentException("An appropriate password required to initialize OAuth token " + + "validation service stub factory hasn't been provided"); + } + } + } diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticatorFactory.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticatorFactory.java index 9613b18c00..c211e74e9b 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticatorFactory.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticatorFactory.java @@ -26,7 +26,8 @@ import java.util.Map; public class WebappAuthenticatorFactory { public static WebappAuthenticator getAuthenticator(String authScheme) { - return AuthenticatorFrameworkDataHolder.getInstance().getWebappAuthenticatorRepository().getAuthenticator(authScheme); + return AuthenticatorFrameworkDataHolder.getInstance().getWebappAuthenticatorRepository(). + getAuthenticator(authScheme); } public static WebappAuthenticator getAuthenticator(Request request) { diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/oauth/impl/RemoteOAuthValidator.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/oauth/impl/RemoteOAuthValidator.java index 58eb3a0611..3811d64807 100755 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/oauth/impl/RemoteOAuthValidator.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/oauth/impl/RemoteOAuthValidator.java @@ -59,36 +59,21 @@ public class RemoteOAuthValidator implements OAuth2TokenValidator { } @Override - public OAuthValidationResponse validateToken(String accessToken, String resource) throws - OAuthTokenValidationException { - OAuth2TokenValidationRequestDTO validationRequest = new OAuth2TokenValidationRequestDTO(); - OAuth2TokenValidationRequestDTO_OAuth2AccessToken oauthToken = - new OAuth2TokenValidationRequestDTO_OAuth2AccessToken(); - oauthToken.setTokenType(OAuthConstants.BEARER_TOKEN_TYPE); - oauthToken.setIdentifier(accessToken); - validationRequest.setAccessToken(oauthToken); - - //Set the resource context param. This will be used in scope validation. - OAuth2TokenValidationRequestDTO_TokenValidationContextParam resourceContextParam = new - OAuth2TokenValidationRequestDTO_TokenValidationContextParam(); - resourceContextParam.setKey(OAuthConstants.RESOURCE_KEY); - resourceContextParam.setValue(resource); - - OAuth2TokenValidationRequestDTO_TokenValidationContextParam[] tokenValidationContextParams = - new OAuth2TokenValidationRequestDTO_TokenValidationContextParam[1]; - tokenValidationContextParams[0] = resourceContextParam; - validationRequest.setContext(tokenValidationContextParams); - - OAuth2TokenValidationResponseDTO tokenValidationResponse; + public OAuthValidationResponse validateToken(String accessToken, + String resource) throws OAuthTokenValidationException { OAuth2TokenValidationServiceStub stub = null; + OAuth2TokenValidationResponseDTO validationResponse; try { + OAuth2TokenValidationRequestDTO validationRequest = this.createValidationRequest(accessToken, resource); stub = (OAuth2TokenValidationServiceStub) stubs.borrowObject(); - tokenValidationResponse = stub. + validationResponse = stub. findOAuthConsumerIfTokenIsValid(validationRequest).getAccessTokenValidationResponse(); } catch (RemoteException e) { throw new OAuthTokenValidationException("Remote Exception occurred while invoking the Remote " + "IS server for OAuth2 token validation.", e); } catch (Exception e) { + /* In this particular instance, generic exceptions are caught as enforced by the pooling library + used to pool stubs created to invoke OAuth token validation service */ throw new OAuthTokenValidationException("Error occurred while borrowing an oauth token validation " + "service stub from the pool", e); } finally { @@ -99,19 +84,49 @@ public class RemoteOAuthValidator implements OAuth2TokenValidator { " stub pool", e); } } - boolean isValid = tokenValidationResponse.getValid(); + + if (validationResponse == null) { + if (log.isDebugEnabled()) { + log.debug("Response returned by the OAuth token validation service is null"); + } + return null; + } + String userName; String tenantDomain; + boolean isValid = validationResponse.getValid(); if (isValid) { userName = MultitenantUtils.getTenantAwareUsername( - tokenValidationResponse.getAuthorizedUser()); - tenantDomain = MultitenantUtils.getTenantDomain(tokenValidationResponse.getAuthorizedUser()); + validationResponse.getAuthorizedUser()); + tenantDomain = MultitenantUtils.getTenantDomain(validationResponse.getAuthorizedUser()); } else { OAuthValidationResponse oAuthValidationResponse = new OAuthValidationResponse(); - oAuthValidationResponse.setErrorMsg(tokenValidationResponse.getErrorMsg()); + oAuthValidationResponse.setErrorMsg(validationResponse.getErrorMsg()); return oAuthValidationResponse; } return new OAuthValidationResponse(userName, tenantDomain, isValid); } + private OAuth2TokenValidationRequestDTO createValidationRequest(String accessToken, String resource) { + OAuth2TokenValidationRequestDTO validationRequest = new OAuth2TokenValidationRequestDTO(); + OAuth2TokenValidationRequestDTO_OAuth2AccessToken oauthToken = + new OAuth2TokenValidationRequestDTO_OAuth2AccessToken(); + oauthToken.setTokenType(OAuthConstants.BEARER_TOKEN_TYPE); + oauthToken.setIdentifier(accessToken); + validationRequest.setAccessToken(oauthToken); + + //Set the resource context param. This will be used in scope validation. + OAuth2TokenValidationRequestDTO_TokenValidationContextParam resourceContextParam = new + OAuth2TokenValidationRequestDTO_TokenValidationContextParam(); + resourceContextParam.setKey(OAuthConstants.RESOURCE_KEY); + resourceContextParam.setValue(resource); + + OAuth2TokenValidationRequestDTO_TokenValidationContextParam[] tokenValidationContextParams = + new OAuth2TokenValidationRequestDTO_TokenValidationContextParam[1]; + tokenValidationContextParams[0] = resourceContextParam; + validationRequest.setContext(tokenValidationContextParams); + + return validationRequest; + } + } diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/test/WebappAuthenticatorConfigTest.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/test/WebappAuthenticatorConfigTest.java new file mode 100644 index 0000000000..8ea931a8eb --- /dev/null +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/test/WebappAuthenticatorConfigTest.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.webapp.authenticator.framework.test; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.utils.ServerConstants; +import org.wso2.carbon.webapp.authenticator.framework.AuthenticatorFrameworkException; +import org.wso2.carbon.webapp.authenticator.framework.config.AuthenticatorConfig; +import org.wso2.carbon.webapp.authenticator.framework.config.WebappAuthenticatorConfig; + +import java.util.List; + +public class WebappAuthenticatorConfigTest { + + @BeforeClass + public void init() { + System.setProperty(ServerConstants.CARBON_CONFIG_DIR_PATH, "src/test/resources/config"); + } + + @Test + public void testConfigInitialization() { + try { + WebappAuthenticatorConfig.init(); + + WebappAuthenticatorConfig config = WebappAuthenticatorConfig.getInstance(); + Assert.assertNotNull(config); + + List authConfigs = config.getAuthenticators(); + Assert.assertNotNull(authConfigs); + } catch (AuthenticatorFrameworkException e) { + Assert.fail("Error occurred while testing webapp authenticator config initialization", e); + } catch (Throwable e) { + Assert.fail("Unexpected error has been encountered while testing webapp authenticator config " + + "initialization", e); + } + } + + @AfterClass + public void cleanup() { + System.setProperty(ServerConstants.CARBON_CONFIG_DIR_PATH, ""); + } + +} diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/test/WebappAuthenticatorFrameworkUtilTest.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/test/WebappAuthenticatorFrameworkUtilTest.java new file mode 100644 index 0000000000..1ad1975b08 --- /dev/null +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/test/WebappAuthenticatorFrameworkUtilTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.webapp.authenticator.framework.test; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.commons.pool.ObjectPool; +import org.apache.commons.pool.impl.GenericObjectPool; +import org.testng.Assert; +import org.testng.annotations.Test; +import org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub; +import org.wso2.carbon.webapp.authenticator.framework.Utils.OAuthTokenValidationStubFactory; + +import java.util.Properties; + +public class WebappAuthenticatorFrameworkUtilTest { + + private static final Log log = LogFactory.getLog(WebappAuthenticatorFrameworkUtilTest.class); + + private static final String TOKEN_VALIDATION_SERVICE_URL = "https://localhost:9443"; + private static final String ADMIN_USERNAME = "admin"; + private static final String ADMIN_PASSWORD = "admin"; + private static final Properties PROPERTIES = new Properties(); + + static { + PROPERTIES.setProperty("MaxTotalConnections", "100"); + PROPERTIES.setProperty("MaxConnectionsPerHost", "100"); + } + + @Test + public void testOAuthTokenValidatorStubPool() { + ObjectPool stubs = null; + OAuth2TokenValidationServiceStub stub = null; + + try { + stubs = new GenericObjectPool( + new OAuthTokenValidationStubFactory( + TOKEN_VALIDATION_SERVICE_URL, ADMIN_USERNAME, ADMIN_PASSWORD, PROPERTIES)); + + stub = (OAuth2TokenValidationServiceStub) stubs.borrowObject(); + Assert.assertNotNull(stub); + } catch (Exception e) { + String msg = "Error occurred while borrowing an oauth validator service stub instance from the pool"; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + if (stubs != null) { + try { + if (stub != null) { + stubs.returnObject(stub); + } + } catch (Exception e) { + log.warn("Error occurred while returning oauth validator service stub instance to the pool", e); + } + + /* Checks if the stub instance used above has been properly returned to the pool */ + Assert.assertEquals(stubs.getNumIdle(), 1); + /* Verifies that there's no hanging connections after the operation performed above */ + Assert.assertEquals(stubs.getNumActive(), 0); + + try { + stubs.close(); + } catch (Exception e) { + log.warn("Error occurred while closing the object pool", e); + } + } + } + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testStubFactoryInitWithInvalidHttpClientProperties() { + new OAuthTokenValidationStubFactory(TOKEN_VALIDATION_SERVICE_URL, null, ADMIN_PASSWORD, PROPERTIES); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testStubFactoryInitWithInvalidUsername() { + new OAuthTokenValidationStubFactory(TOKEN_VALIDATION_SERVICE_URL, null, ADMIN_PASSWORD, PROPERTIES); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testStubFactoryInitWithInvalidPassword() { + new OAuthTokenValidationStubFactory(TOKEN_VALIDATION_SERVICE_URL, ADMIN_USERNAME, null, PROPERTIES); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testStubFactoryInitWithInvalidUrl() { + new OAuthTokenValidationStubFactory(null, ADMIN_USERNAME, ADMIN_PASSWORD, PROPERTIES); + } + +} diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/config/etc/webapp-authenticator-config.xml b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/config/etc/webapp-authenticator-config.xml new file mode 100644 index 0000000000..5099328df7 --- /dev/null +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/config/etc/webapp-authenticator-config.xml @@ -0,0 +1,28 @@ + + + + OAuth + org.wso2.carbon.webapp.authenticator.framework.authenticator.OAuthAuthenticator + + https://localhost:9443 + admin + admin + true + 10000 + 10000 + + + + BasicAuth + org.wso2.carbon.webapp.authenticator.framework.authenticator.BasicAuthAuthenticator + + + JWT + org.wso2.carbon.webapp.authenticator.framework.authenticator.JWTAuthenticator + + + CertificateAuth + org.wso2.carbon.webapp.authenticator.framework.authenticator.CertificateAuthenticator + + + diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/log4j.properties b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/log4j.properties new file mode 100644 index 0000000000..a625c80cd5 --- /dev/null +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/log4j.properties @@ -0,0 +1,32 @@ +# +# Copyright 2009 WSO2, Inc. (http://wso2.com) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# +# This is the log4j configuration file used by WSO2 Carbon +# +# IMPORTANT : Please do not remove or change the names of any +# of the Appenders defined here. The layout pattern & log file +# can be changed using the WSO2 Carbon Management Console, and those +# settings will override the settings in this file. +# + +log4j.rootLogger=ERROR, STD_OUT + +# Redirect log messages to console +log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender +log4j.appender.STD_OUT.Target=System.out +log4j.appender.STD_OUT.layout=org.apache.log4j.PatternLayout +log4j.appender.STD_OUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/testng.xml b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/testng.xml new file mode 100644 index 0000000000..8b9832e2e6 --- /dev/null +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/resources/testng.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 238e9a596d..5e9edd5c3b 100644 --- a/pom.xml +++ b/pom.xml @@ -1263,12 +1263,21 @@ neethi ${neethi.version} - commons-pool.wso2 commons-pool ${commons.pool.wso2.version} + + org.apache.httpcomponents.wso2 + httpclient + ${httpcomponents.httpclient.version} + + + commons-httpclient.wso2 + commons-httpclient + ${commons.httpclient.version} + @@ -1380,6 +1389,11 @@ build-helper-maven-plugin 1.8 + + org.apache.maven.plugins + maven-surefire-plugin + 2.18 + @@ -1564,6 +1578,8 @@ github-scm 1.5.6.wso2v1 + 4.2.3.wso2v1 + 3.1.0.wso2v2 From 0cdc139a0b8203efee18f96b54f264e8b31da461 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 21 Dec 2015 11:57:50 +0530 Subject: [PATCH 2/3] Adding updated webapp authenticator config --- .../oauth/impl/RemoteOAuthValidator.java | 29 ++++++++++++------- .../conf/webapp-authenticator-config.xml | 8 +++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/oauth/impl/RemoteOAuthValidator.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/oauth/impl/RemoteOAuthValidator.java index 5175899397..7700941382 100755 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/oauth/impl/RemoteOAuthValidator.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/oauth/impl/RemoteOAuthValidator.java @@ -43,27 +43,33 @@ public class RemoteOAuthValidator implements OAuth2TokenValidator { private static final Log log = LogFactory.getLog(RemoteOAuthValidator.class); public RemoteOAuthValidator(String hostURL, String adminUserName, String adminPassword, Properties properties) { - this.stubs = new GenericObjectPool(new OAuthTokenValidationStubFactory(hostURL, adminUserName, adminPassword, properties)); + this.stubs = + new GenericObjectPool(new OAuthTokenValidationStubFactory( + hostURL, adminUserName, adminPassword, properties)); } - public OAuthValidationResponse validateToken(String accessToken, String resource) throws OAuthTokenValidationException { + public OAuthValidationResponse validateToken(String accessToken, + String resource) throws OAuthTokenValidationException { OAuth2TokenValidationServiceStub stub = null; OAuth2TokenValidationResponseDTO validationResponse; try { OAuth2TokenValidationRequestDTO validationRequest = createValidationRequest(accessToken, resource); stub = (OAuth2TokenValidationServiceStub) this.stubs.borrowObject(); - validationResponse = stub.findOAuthConsumerIfTokenIsValid(validationRequest).getAccessTokenValidationResponse(); + validationResponse = + stub.findOAuthConsumerIfTokenIsValid(validationRequest).getAccessTokenValidationResponse(); } catch (RemoteException e) { - throw new OAuthTokenValidationException("Remote Exception occurred while invoking the Remote IS server for OAuth2 token validation.", e); + throw new OAuthTokenValidationException("Remote Exception occurred while invoking the Remote " + + "IS server for OAuth2 token validation.", e); } catch (Exception e) { - throw new OAuthTokenValidationException("Error occurred while borrowing an oauth token validation service stub from the pool", e); + throw new OAuthTokenValidationException("Error occurred while borrowing an oauth token validation " + + "service stub from the pool", e); } finally { try { this.stubs.returnObject(stub); } catch (Exception e) { - log.warn("Error occurred while returning the object back to the oauth token validation service stub pool", e); + log.warn("Error occurred while returning the object back to the oauth token validation service " + + "stub pool", e); } - } if (validationResponse == null) { @@ -89,18 +95,21 @@ public class RemoteOAuthValidator implements OAuth2TokenValidator { private OAuth2TokenValidationRequestDTO createValidationRequest(String accessToken, String resource) { OAuth2TokenValidationRequestDTO validationRequest = new OAuth2TokenValidationRequestDTO(); - OAuth2TokenValidationRequestDTO_OAuth2AccessToken oauthToken = new OAuth2TokenValidationRequestDTO_OAuth2AccessToken(); + OAuth2TokenValidationRequestDTO_OAuth2AccessToken oauthToken = + new OAuth2TokenValidationRequestDTO_OAuth2AccessToken(); oauthToken.setTokenType("bearer"); oauthToken.setIdentifier(accessToken); validationRequest.setAccessToken(oauthToken); - OAuth2TokenValidationRequestDTO_TokenValidationContextParam resourceContextParam = new OAuth2TokenValidationRequestDTO_TokenValidationContextParam(); + OAuth2TokenValidationRequestDTO_TokenValidationContextParam resourceContextParam = + new OAuth2TokenValidationRequestDTO_TokenValidationContextParam(); resourceContextParam.setKey("resource"); resourceContextParam.setValue(resource); - OAuth2TokenValidationRequestDTO_TokenValidationContextParam[] tokenValidationContextParams = new OAuth2TokenValidationRequestDTO_TokenValidationContextParam[1]; + OAuth2TokenValidationRequestDTO_TokenValidationContextParam[] tokenValidationContextParams = + new OAuth2TokenValidationRequestDTO_TokenValidationContextParam[1]; tokenValidationContextParams[0] = resourceContextParam; validationRequest.setContext(tokenValidationContextParams); diff --git a/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/conf/webapp-authenticator-config.xml b/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/conf/webapp-authenticator-config.xml index 067d8cd3ce..5507b059e1 100644 --- a/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/conf/webapp-authenticator-config.xml +++ b/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/conf/webapp-authenticator-config.xml @@ -3,6 +3,14 @@ OAuth org.wso2.carbon.webapp.authenticator.framework.authenticator.OAuthAuthenticator + + true + https://localhost:9443 + admin + admin + 100 + 100 + BasicAuth From 67e1bfb012ff51b514f59953e6d1585255af391f Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 21 Dec 2015 12:23:59 +0530 Subject: [PATCH 3/3] Making attribute names schema compliant and code cleanup --- .../framework/config/AuthenticatorConfig.java | 9 +++++---- ...WebappAuthenticatorFrameworkServiceComponent.java | 3 ++- .../resources/conf/webapp-authenticator-config.xml | 12 ++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/config/AuthenticatorConfig.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/config/AuthenticatorConfig.java index 0fed4f50e2..a5aab6319f 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/config/AuthenticatorConfig.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/config/AuthenticatorConfig.java @@ -46,8 +46,8 @@ public class AuthenticatorConfig { this.className = className; } - @XmlElementWrapper(name="Parameters", nillable=true) - @XmlElement(name="Parameter", nillable=false) + @XmlElementWrapper(name = "Parameters", nillable = true) + @XmlElement(name = "Parameter", nillable = false) public List getParams() { return this.params; } @@ -55,12 +55,13 @@ public class AuthenticatorConfig { public void setParams(List params) { this.params = params; } - @XmlRootElement(name="Parameter") + + @XmlRootElement(name = "Parameter") public static class Parameter { private String name; private String value; - @XmlAttribute(name="Name") + @XmlAttribute(name = "Name") public String getName() { return this.name; } diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/internal/WebappAuthenticatorFrameworkServiceComponent.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/internal/WebappAuthenticatorFrameworkServiceComponent.java index 926a6eed51..c81ae13f4b 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/internal/WebappAuthenticatorFrameworkServiceComponent.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/internal/WebappAuthenticatorFrameworkServiceComponent.java @@ -78,7 +78,8 @@ public class WebappAuthenticatorFrameworkServiceComponent { WebappAuthenticatorConfig.init(); WebappAuthenticatorRepository repository = new WebappAuthenticatorRepository(); for (AuthenticatorConfig config : WebappAuthenticatorConfig.getInstance().getAuthenticators()) { - WebappAuthenticator authenticator = (WebappAuthenticator)Class.forName(config.getClassName()).newInstance(); + WebappAuthenticator authenticator = + (WebappAuthenticator) Class.forName(config.getClassName()).newInstance(); if ((config.getParams() != null) && (!config.getParams().isEmpty())) { Properties properties = new Properties(); diff --git a/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/conf/webapp-authenticator-config.xml b/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/conf/webapp-authenticator-config.xml index 5507b059e1..a9e0c9d832 100644 --- a/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/conf/webapp-authenticator-config.xml +++ b/features/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework.server.feature/src/main/resources/conf/webapp-authenticator-config.xml @@ -4,12 +4,12 @@ OAuth org.wso2.carbon.webapp.authenticator.framework.authenticator.OAuthAuthenticator - true - https://localhost:9443 - admin - admin - 100 - 100 + true + https://localhost:9443 + admin + admin + 100 + 100