Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new client to call organization discovery configuration API #6099

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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.identity.mgt.endpoint.util.client;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementEndpointUtil;
import org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementServiceUtil;
import org.wso2.carbon.utils.HTTPClientUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

public class OrganizationDiscoveryConfigDataRetrievalClient {

private static final String CLIENT = "Client ";
private static final String ORG_DISCOVERY_CONFIG_ENDPOINT = "/api/server/v1/organization-configs/discovery";
private static final String PROPERTIES = "properties";
private static final String VALUE = "value";
private static final String KEY = "key";
private static final String AUTHORIZATION_HEADER = "Authorization";

public Map<String, String> getDiscoveryConfiguration(String tenantDomain)
throws OrganizationDiscoveryConfigDataRetrievalClientException {

Map<String, String> organizationDiscoveryConfig = new HashMap<>();

try (CloseableHttpClient httpClient = HTTPClientUtils.createClientWithCustomVerifier().build()) {
HttpGet request = new HttpGet(getOrganizationDiscoveryConfigEndpoint(tenantDomain));
setAuthorizationHeader(request);

try (CloseableHttpResponse httpResponse = httpClient.execute(request)) {
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
JSONObject configObject = new JSONObject(new JSONTokener(new InputStreamReader(
httpResponse.getEntity().getContent())));

if (configObject.has(PROPERTIES) && configObject.get(PROPERTIES) instanceof JSONArray) {
JSONArray properties = configObject.getJSONArray(PROPERTIES);
for (int i = 0; i < properties.length(); i++) {
JSONObject property = properties.getJSONObject(i);
organizationDiscoveryConfig.put(property.getString(KEY), property.getString(VALUE));
}
}
}
return organizationDiscoveryConfig;

} finally {
request.releaseConnection();
}
} catch (IOException e) {
throw new OrganizationDiscoveryConfigDataRetrievalClientException("Error while retrieving organization " +
"discovery configuration for tenant: " + tenantDomain, e);
}

}

private String getOrganizationDiscoveryConfigEndpoint(String tenantDomain)
throws OrganizationDiscoveryConfigDataRetrievalClientException {

try {
return IdentityManagementEndpointUtil.getBasePath(tenantDomain, ORG_DISCOVERY_CONFIG_ENDPOINT);
} catch (ApiException e) {
throw new OrganizationDiscoveryConfigDataRetrievalClientException("Error while building url for context: " +
ORG_DISCOVERY_CONFIG_ENDPOINT);
}
}

private void setAuthorizationHeader(HttpRequestBase httpMethod) {

String toEncode = IdentityManagementServiceUtil.getInstance().getAppName() + ":"
+ String.valueOf(IdentityManagementServiceUtil.getInstance().getAppPassword());
byte[] encoding = Base64.encodeBase64(toEncode.getBytes());
String authHeader = new String(encoding, Charset.defaultCharset());
httpMethod.addHeader(AUTHORIZATION_HEADER, CLIENT + authHeader);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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.identity.mgt.endpoint.util.client;

public class OrganizationDiscoveryConfigDataRetrievalClientException extends Exception {

/**
* Client Exception with error message.
*
* @param message Error message.
*/
public OrganizationDiscoveryConfigDataRetrievalClientException(String message) {
super(message);
}

/**
* Client exception with message and a throwable.
*
* @param message Error message.
* @param throwable Throwable.
*/
public OrganizationDiscoveryConfigDataRetrievalClientException(String message, Throwable throwable) {
super(message, throwable);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. 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.identity.mgt.endpoint.util;

import org.mockito.MockedStatic;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.mgt.endpoint.util.client.OrganizationDiscoveryConfigDataRetrievalClient;
import org.wso2.carbon.utils.HTTPClientUtils;

import java.io.IOException;
import java.util.Map;

import static org.mockito.Mockito.*;

public class OrganizationDiscoveryConfigDataRetrievalClientTest extends RetrievalClientBaseTest {

private final OrganizationDiscoveryConfigDataRetrievalClient orgDiscoveryConfigDataRetrievalClient =
new OrganizationDiscoveryConfigDataRetrievalClient();

@BeforeTest
public void setMockData() throws IOException {

setMockJsonResponse(readResource("OrganizationDiscoveryConfigResponse.json"));
}

@Test
public void testGetDiscoveryConfiguration() throws Exception {

try (MockedStatic<IdentityManagementServiceUtil> identityMgtServiceUtil = mockStatic(
IdentityManagementServiceUtil.class);
MockedStatic<HTTPClientUtils> httpclientUtil = mockStatic(HTTPClientUtils.class)) {
identityMgtServiceUtil.when(IdentityManagementServiceUtil::getInstance)
.thenReturn(identityManagementServiceUtil);
httpclientUtil.when(HTTPClientUtils::createClientWithCustomVerifier).thenReturn(httpClientBuilder);
Map<String, String> result =
orgDiscoveryConfigDataRetrievalClient.getDiscoveryConfiguration(SUPER_TENANT_DOMAIN);
Assert.assertEquals(result.size(), 2);
Assert.assertEquals(result.get("emailDomain.enable"), "true");
Assert.assertEquals(result.get("emailDomainBasedSelfSignup.enable"), "true");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"properties": [
{
"key": "emailDomain.enable",
"value": "true"
},
{
"key": "emailDomainBasedSelfSignup.enable",
"value": "true"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<class name="org.wso2.carbon.identity.mgt.endpoint.util.ConfiguredAuthenticatorsRetrievalClientTest"/>
<class name="org.wso2.carbon.identity.mgt.endpoint.util.IdentityProviderDataRetrievalClientTest"/>
<class name="org.wso2.carbon.identity.mgt.endpoint.util.ValidationConfigurationRetrievalClientTest"/>
<class name="org.wso2.carbon.identity.mgt.endpoint.util.OrganizationDiscoveryConfigDataRetrievalClientTest"/>
</classes>
</test>
</suite>
Loading