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 tags in the idp authenticator responses. #758

Open
wants to merge 2 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
Expand Up @@ -588,33 +588,10 @@ public FederatedAuthenticatorListResponse getFederatedAuthenticators(String idpI
listResponse = new FederatedAuthenticatorListResponse();
FederatedAuthenticatorConfig[] fedAuthConfigs = idP.getFederatedAuthenticatorConfigs();
if (fedAuthConfigs != null) {
List<FederatedAuthenticatorListItem> fedAuthList = new ArrayList<>();
String defaultAuthenticator = null;
for (FederatedAuthenticatorConfig config : fedAuthConfigs) {
String fedAuthId = base64URLEncode(config.getName());
FederatedAuthenticatorListItem listItem = new FederatedAuthenticatorListItem();
listItem.setAuthenticatorId(fedAuthId);
listItem.setName(config.getName());
listItem.setIsEnabled(config.isEnabled());
listItem.setDefinedBy(
FederatedAuthenticatorListItem.DefinedByEnum.valueOf(config.getDefinedByType().toString()));
FederatedAuthenticatorConfig federatedAuthenticatorConfig =
ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(
config.getName());
if (federatedAuthenticatorConfig != null) {
String[] tags = federatedAuthenticatorConfig.getTags();
if (ArrayUtils.isNotEmpty(tags)) {
listItem.setTags(Arrays.asList(tags));
}
}
listItem.setSelf(
ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + IDP_PATH_COMPONENT +
"/%s/federated-authenticators/%s", idpId, fedAuthId)).toString());
fedAuthList.add(listItem);
if (idP.getDefaultAuthenticatorConfig() != null) {
defaultAuthenticator = base64URLEncode(idP.getDefaultAuthenticatorConfig().getName());
}
}
List<FederatedAuthenticatorListItem> fedAuthList = FederatedAuthenticatorConfigBuilderFactory.build(
fedAuthConfigs, idP.getResourceId());
String defaultAuthenticator = (idP.getDefaultAuthenticatorConfig() != null ? base64URLEncode(idP
.getDefaultAuthenticatorConfig().getName()) : null);
listResponse.setDefaultAuthenticatorId(defaultAuthenticator);
listResponse.setAuthenticators(fedAuthList);
}
Expand Down Expand Up @@ -2428,30 +2405,8 @@ private FederatedAuthenticatorListResponse createFederatedAuthenticatorResponse(

FederatedAuthenticatorConfig[] fedAuthConfigs = idp.getFederatedAuthenticatorConfigs();
FederatedAuthenticatorListResponse fedAuthIDPResponse = new FederatedAuthenticatorListResponse();
List<FederatedAuthenticatorListItem> authenticators = new ArrayList<>();
for (FederatedAuthenticatorConfig fedAuthConfig : fedAuthConfigs) {
FederatedAuthenticatorListItem fedAuthListItem = new FederatedAuthenticatorListItem();
fedAuthListItem.setAuthenticatorId(base64URLEncode(fedAuthConfig.getName()));
fedAuthListItem.setName(fedAuthConfig.getName());
fedAuthListItem.setIsEnabled(fedAuthConfig.isEnabled());
fedAuthListItem.setDefinedBy(FederatedAuthenticatorListItem.DefinedByEnum.valueOf(
fedAuthConfig.getDefinedByType().toString()));
FederatedAuthenticatorConfig federatedAuthenticatorConfig =
ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(
fedAuthConfig.getName());
if (federatedAuthenticatorConfig != null) {
String[] tags = federatedAuthenticatorConfig.getTags();
if (ArrayUtils.isNotEmpty(tags)) {
fedAuthListItem.setTags(Arrays.asList(tags));
}
}
fedAuthListItem.setSelf(
ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT + IDP_PATH_COMPONENT +
"/%s/federated-authenticators/%s", idp.getResourceId(),
base64URLEncode(fedAuthConfig.getName())))
.toString());
authenticators.add(fedAuthListItem);
}
List<FederatedAuthenticatorListItem> authenticators =
FederatedAuthenticatorConfigBuilderFactory.build(fedAuthConfigs, idp.getResourceId());
fedAuthIDPResponse.setDefaultAuthenticatorId(idp.getDefaultAuthenticatorConfig() != null ? base64URLEncode(idp
.getDefaultAuthenticatorConfig().getName()) : null);
fedAuthIDPResponse.setAuthenticators(authenticators);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@

package org.wso2.carbon.identity.api.server.idp.v1.impl;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.api.server.common.ContextLoader;
import org.wso2.carbon.identity.api.server.idp.common.Constants;
import org.wso2.carbon.identity.api.server.idp.common.IdentityProviderServiceHolder;
import org.wso2.carbon.identity.api.server.idp.v1.model.AuthenticationType;
import org.wso2.carbon.identity.api.server.idp.v1.model.Endpoint;
import org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticator;
import org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorListItem;
import org.wso2.carbon.identity.api.server.idp.v1.model.FederatedAuthenticatorPUTRequest;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
Expand All @@ -38,6 +39,7 @@
import org.wso2.carbon.idp.mgt.IdentityProviderManagementServerException;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
Expand All @@ -47,7 +49,10 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.api.server.common.Constants.V1_API_PATH_COMPONENT;
import static org.wso2.carbon.identity.api.server.common.Util.base64URLEncode;
import static org.wso2.carbon.identity.api.server.idp.common.Constants.GOOGLE_PRIVATE_KEY;
import static org.wso2.carbon.identity.api.server.idp.common.Constants.IDP_PATH_COMPONENT;

/**
* The factory class for building federated authenticator configuration related models.
Expand Down Expand Up @@ -118,15 +123,9 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config)

federatedAuthenticator.setName(config.getName());
federatedAuthenticator.setIsEnabled(config.isEnabled());

FederatedAuthenticatorConfig federatedAuthenticatorConfig =
ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(
config.getName());
if (federatedAuthenticatorConfig != null) {
String[] tags = federatedAuthenticatorConfig.getTags();
if (ArrayUtils.isNotEmpty(tags)) {
federatedAuthenticator.setTags(Arrays.asList(tags));
}
List<String> tags = resolveAuthenticatorTags(config);
if (tags.isEmpty()) {
federatedAuthenticator.setTags(tags);
}

if (DefinedByType.SYSTEM == config.getDefinedByType()) {
Expand All @@ -141,6 +140,38 @@ public static FederatedAuthenticator build(FederatedAuthenticatorConfig config)

return federatedAuthenticator;
}

/**
* Builds a list of FederatedAuthenticatorListItem instances based on the given array of
* FederatedAuthenticatorConfig.
*
* @param fedAuthConfigs Array of FederatedAuthenticatorConfig instances.
* @param idpResourceId Identity provider resource ID.
* @return List of FederatedAuthenticatorListItem instances.
Thisara-Welmilla marked this conversation as resolved.
Show resolved Hide resolved
*/
public static List<FederatedAuthenticatorListItem> build(FederatedAuthenticatorConfig[] fedAuthConfigs,
String idpResourceId) {

List<FederatedAuthenticatorListItem> authenticators = new ArrayList<>();
for (FederatedAuthenticatorConfig config : fedAuthConfigs) {
FederatedAuthenticatorListItem authenticatorListItem = new FederatedAuthenticatorListItem();
authenticatorListItem.setAuthenticatorId(base64URLEncode(config.getName()));
authenticatorListItem.setName(config.getName());
authenticatorListItem.setIsEnabled(config.isEnabled());
authenticatorListItem.definedBy(FederatedAuthenticatorListItem.DefinedByEnum.valueOf(
config.getDefinedByType().toString()));
List<String> tags = resolveAuthenticatorTags(config);
if (tags.isEmpty()) {
Thisara-Welmilla marked this conversation as resolved.
Show resolved Hide resolved
authenticatorListItem.setTags(tags);
}
authenticatorListItem.setSelf(ContextLoader.buildURIForBody(String.format(V1_API_PATH_COMPONENT +
IDP_PATH_COMPONENT + "/%s/federated-authenticators/%s", idpResourceId,
base64URLEncode(config.getName()))).toString());
authenticators.add(authenticatorListItem);
}

return authenticators;
}

private static FederatedAuthenticatorConfig createFederatedAuthenticatorConfig(Config config)
throws IdentityProviderManagementClientException {
Expand Down Expand Up @@ -435,6 +466,21 @@ private static void resolveEndpointConfiguration(FederatedAuthenticator authenti
}
}

private static List<String> resolveAuthenticatorTags(FederatedAuthenticatorConfig config) {

/* If the authenticator is defined by the user, return the tags of the authenticator config. Otherwise, return
the tags of the system registered federated authenticator template.
*/
if (DefinedByType.USER == config.getDefinedByType()) {
return Arrays.asList(config.getTags());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If configs.getTags() equals to null, this can throw a NPE

}

FederatedAuthenticatorConfig federatedAuthenticatorConfig =
ApplicationAuthenticatorService.getInstance().getFederatedAuthenticatorByName(config.getName());
return federatedAuthenticatorConfig != null ? Arrays.asList(federatedAuthenticatorConfig.getTags())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above aomment

: new ArrayList<>();
}

/**
* Config class to build FederatedAuthenticatorConfig.
*/
Expand Down
Loading