Skip to content

Commit

Permalink
Update APIs to support custom authentication management.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Nov 13, 2024
1 parent 6d3a143 commit fa4caea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ public enum ErrorMessage {
ERROR_CODE_ERROR_LISTING_TRUSTED_TOKEN_ISSUERS("60021",
"Unable to list existing trusted token issuers.",
"Server encountered an error while listing the trusted token issuers."),
ERROR_CODE_ENDPOINT_PROVIDED_FOR_SYSTEM_AUTH("60039", "No endpoint configuration is allowed " +
"for system defined authenticators.", "No endpoint configuration must be " +
ERROR_CODE_ENDPOINT_PROVIDED_FOR_SYSTEM_AUTH("60039", "Invalid Request.",
"No endpoint configuration must be " +
"provided for the system defined federated authenticators %s."),
ERROR_CODE_PROPERTIES_PROVIDED_FOR_USER_AUTH("60040", "No properties are allowed for " +
"user defined authenticators.", "No properties must be provided for the user defined " +
ERROR_CODE_PROPERTIES_PROVIDED_FOR_USER_AUTH("60040", "Invalid Request.",
"No properties must be provided for the user defined " +
"federated authenticators %s."),
ERROR_CODE_NO_ENDPOINT_PROVIDED("60041", "No endpoint provided.", "Endpoint " +
ERROR_CODE_NO_ENDPOINT_PROVIDED("60041", "Invalid Request.", "Endpoint " +
"configuration must be provided for the user defined federated authenticators %s."),
ERROR_CODE_NON_DECODABLE_AUTH_ID("60042", "Non-decodable authenticator ID.",
"Unable to decode the provided authenticator ID %s."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1777,10 +1778,10 @@ private void updateFederatedAuthenticatorConfig(IdentityProvider idp, FederatedA
String authenticatorName = getDecodedAuthenticatorName(authenticator.getAuthenticatorId());
String definedByType;
if (isNewFederatedAuthenticator) {
definedByType = resolveDefinedByTypeForCreateFederatedAuthenticator(
definedByType = resolveDefinedByTypeToCreateFederatedAuthenticator(
authenticator.getDefinedBy().toString()).toString();
} else {
definedByType = resolveDefinedByTypeForUpdateFederatedAuthenticator(authenticatorName).toString();
definedByType = resolveDefinedByTypeToUpdateFederatedAuthenticator(authenticatorName).toString();
}

if (DefinedByType.SYSTEM.toString().equals(definedByType)) {
Expand All @@ -1794,8 +1795,9 @@ private void updateFederatedAuthenticatorConfig(IdentityProvider idp, FederatedA
builder.enabled(authenticator.getIsEnabled());
builder.displayName(getDisplayNameOfAuthenticator(authenticatorName));
builder.endpoint(authenticator.getEndpoint());
List<Property> properties = authenticator.getProperties().stream().map(propertyToInternal)
.collect(Collectors.toList());
List<Property> properties = Optional.ofNullable(authenticator.getProperties())
.map(props -> props.stream().map(propertyToInternal).collect(Collectors.toList()))
.orElse(null);
builder.properties(properties);
FederatedAuthenticatorConfig authConfig = builder.build();

Expand Down Expand Up @@ -2848,7 +2850,7 @@ private FederatedAuthenticatorConfig updateFederatedAuthenticatorConfig(String f
FederatedAuthenticatorPUTRequest authenticator) throws IdentityProviderManagementClientException {

String authenticatorName = getDecodedAuthenticatorName(federatedAuthenticatorId);
String definedByType = resolveDefinedByTypeForUpdateFederatedAuthenticator(authenticatorName).toString();
String definedByType = resolveDefinedByTypeToUpdateFederatedAuthenticator(authenticatorName).toString();
if (DefinedByType.SYSTEM.toString().equals(definedByType)) {
validateAuthenticatorProperties(authenticatorName, authenticator.getProperties());
}
Expand All @@ -2860,14 +2862,15 @@ private FederatedAuthenticatorConfig updateFederatedAuthenticatorConfig(String f
builder.enabled(authenticator.getIsEnabled());
builder.displayName(getDisplayNameOfAuthenticator(authenticatorName));
builder.endpoint(authenticator.getEndpoint());
List<Property> properties = authenticator.getProperties().stream().map(propertyToInternal)
.collect(Collectors.toList());
List<Property> properties = Optional.ofNullable(authenticator.getProperties())
.map(props -> props.stream().map(propertyToInternal).collect(Collectors.toList()))
.orElse(null);
builder.properties(properties);

return builder.build();
}

private DefinedByType resolveDefinedByTypeForCreateFederatedAuthenticator(String definedByType) {
private DefinedByType resolveDefinedByTypeToCreateFederatedAuthenticator(String definedByType) {

/* For new federated authenticators:
If 'definedByType' is not null, use the value provided in the request payload. If not, default to SYSTEM. */
Expand All @@ -2877,7 +2880,7 @@ private DefinedByType resolveDefinedByTypeForCreateFederatedAuthenticator(String
return DefinedByType.SYSTEM;
}

private DefinedByType resolveDefinedByTypeForUpdateFederatedAuthenticator(String authenticatorName) {
private DefinedByType resolveDefinedByTypeToUpdateFederatedAuthenticator(String authenticatorName) {

/* For existing federated authenticators, disregard any value provided in the request payload.
Instead, resolve and retrieve the 'definedBy' type of the corresponding existing authenticator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.wso2.carbon.identity.api.server.idp.common.Constants;
import org.wso2.carbon.identity.api.server.idp.v1.model.Endpoint;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig;
Expand Down Expand Up @@ -74,14 +73,6 @@ private static void validateSystemDefinedFederatedAuthenticatorModel(Builder bui
throw new IdentityProviderManagementClientException(error.getCode(), String.format(error.getDescription(),
builder.authenticatorName));
}

// Check if there is an authenticator registered in the system for the given authenticator ID.
if (ApplicationAuthenticatorService.getInstance()
.getFederatedAuthenticatorByName(builder.authenticatorName) == null) {
Constants.ErrorMessage error = Constants.ErrorMessage.ERROR_CODE_NO_SYSTEM_AUTHENTICATOR_FOUND;
throw new IdentityProviderManagementClientException(error.getCode(),
String.format(error.getDescription(), builder.authenticatorName));
}
}

private static UserDefinedFederatedAuthenticatorConfig createUserDefinedFederatedAuthenticator(Builder builder)
Expand All @@ -106,7 +97,7 @@ private static void validateUserDefinedFederatedAuthenticatorModel(Builder build
throws IdentityProviderManagementClientException {

// The User-defined authenticator configs must not have properties configurations; throw an error if they do.
if (builder.properties == null || !builder.properties.isEmpty()) {
if (builder.properties != null) {
Constants.ErrorMessage error = Constants.ErrorMessage.ERROR_CODE_PROPERTIES_PROVIDED_FOR_USER_AUTH;
throw new IdentityProviderManagementClientException(error.getCode(),
String.format(error.getDescription(), builder.authenticatorName));
Expand Down

0 comments on commit fa4caea

Please sign in to comment.