diff --git a/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/AuthenticationType.java b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/AuthenticationType.java new file mode 100644 index 0000000000..5da5dea38b --- /dev/null +++ b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/AuthenticationType.java @@ -0,0 +1,166 @@ +/* + * 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.api.server.configs.v1.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.validation.constraints.*; + + +import io.swagger.annotations.*; +import java.util.Objects; +import javax.validation.Valid; +import javax.xml.bind.annotation.*; + +public class AuthenticationType { + + +@XmlType(name="TypeEnum") +@XmlEnum(String.class) +public enum TypeEnum { + + @XmlEnumValue("NONE") NONE(String.valueOf("NONE")), @XmlEnumValue("BEARER") BEARER(String.valueOf("BEARER")), @XmlEnumValue("API_KEY") API_KEY(String.valueOf("API_KEY")), @XmlEnumValue("BASIC") BASIC(String.valueOf("BASIC")); + + + private String value; + + TypeEnum(String v) { + value = v; + } + + public String value() { + return value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + public static TypeEnum fromValue(String value) { + for (TypeEnum b : TypeEnum.values()) { + if (b.value.equals(value)) { + return b; + } + } + throw new IllegalArgumentException("Unexpected value '" + value + "'"); + } +} + + private TypeEnum type; + private Map properties = new HashMap<>(); + + + /** + **/ + public AuthenticationType type(TypeEnum type) { + + this.type = type; + return this; + } + + @ApiModelProperty(example = "BASIC", required = true, value = "") + @JsonProperty("type") + @Valid + @NotNull(message = "Property type cannot be null.") + + public TypeEnum getType() { + return type; + } + public void setType(TypeEnum type) { + this.type = type; + } + + /** + **/ + public AuthenticationType properties(Map properties) { + + this.properties = properties; + return this; + } + + @ApiModelProperty(example = "{\"username\":\"auth_username\",\"password\":\"auth_password\"}", required = true, value = "") + @JsonProperty("properties") + @Valid + @NotNull(message = "Property properties cannot be null.") + + public Map getProperties() { + return properties; + } + public void setProperties(Map properties) { + this.properties = properties; + } + + + public AuthenticationType putPropertiesItem(String key, Object propertiesItem) { + this.properties.put(key, propertiesItem); + return this; + } + + + + @Override + public boolean equals(java.lang.Object o) { + + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AuthenticationType authenticationType = (AuthenticationType) o; + return Objects.equals(this.type, authenticationType.type) && + Objects.equals(this.properties, authenticationType.properties); + } + + @Override + public int hashCode() { + return Objects.hash(type, properties); + } + + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + sb.append("class AuthenticationType {\n"); + + sb.append(" type: ").append(toIndentedString(type)).append("\n"); + sb.append(" properties: ").append(toIndentedString(properties)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(java.lang.Object o) { + + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n"); + } +} + diff --git a/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/Authenticator.java b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/Authenticator.java index 744e6dbe18..10df96afa5 100644 --- a/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/Authenticator.java +++ b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/Authenticator.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.List; import org.wso2.carbon.identity.api.server.configs.v1.model.AuthenticatorProperty; +import org.wso2.carbon.identity.api.server.configs.v1.model.Endpoint; import javax.validation.constraints.*; @@ -109,6 +110,8 @@ public static TypeEnum fromValue(String value) { private List properties = null; + private List endpoint = null; + /** **/ @@ -274,6 +277,32 @@ public Authenticator addPropertiesItem(AuthenticatorProperty propertiesItem) { return this; } + /** + **/ + public Authenticator endpoint(List endpoint) { + + this.endpoint = endpoint; + return this; + } + + @ApiModelProperty(value = "") + @JsonProperty("endpoint") + @Valid + public List getEndpoint() { + return endpoint; + } + public void setEndpoint(List endpoint) { + this.endpoint = endpoint; + } + + public Authenticator addEndpointItem(Endpoint endpointItem) { + if (this.endpoint == null) { + this.endpoint = new ArrayList<>(); + } + this.endpoint.add(endpointItem); + return this; + } + @Override @@ -293,12 +322,13 @@ public boolean equals(java.lang.Object o) { Objects.equals(this.definedBy, authenticator.definedBy) && Objects.equals(this.type, authenticator.type) && Objects.equals(this.tags, authenticator.tags) && - Objects.equals(this.properties, authenticator.properties); + Objects.equals(this.properties, authenticator.properties) && + Objects.equals(this.endpoint, authenticator.endpoint); } @Override public int hashCode() { - return Objects.hash(id, name, displayName, isEnabled, definedBy, type, tags, properties); + return Objects.hash(id, name, displayName, isEnabled, definedBy, type, tags, properties, endpoint); } @Override @@ -315,6 +345,7 @@ public String toString() { sb.append(" type: ").append(toIndentedString(type)).append("\n"); sb.append(" tags: ").append(toIndentedString(tags)).append("\n"); sb.append(" properties: ").append(toIndentedString(properties)).append("\n"); + sb.append(" endpoint: ").append(toIndentedString(endpoint)).append("\n"); sb.append("}"); return sb.toString(); } diff --git a/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/Endpoint.java b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/Endpoint.java new file mode 100644 index 0000000000..e07fb7851e --- /dev/null +++ b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/gen/java/org/wso2/carbon/identity/api/server/configs/v1/model/Endpoint.java @@ -0,0 +1,120 @@ +/* + * 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.api.server.configs.v1.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonCreator; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.identity.api.server.configs.v1.model.AuthenticationType; +import javax.validation.constraints.*; + + +import io.swagger.annotations.*; +import java.util.Objects; +import javax.validation.Valid; +import javax.xml.bind.annotation.*; + +public class Endpoint { + + private String uri; + private AuthenticationType authentication; + + /** + **/ + public Endpoint uri(String uri) { + + this.uri = uri; + return this; + } + + @ApiModelProperty(example = "https://abc.com/token", value = "") + @JsonProperty("uri") + @Valid @Pattern(regexp="^https?://.+") + public String getUri() { + return uri; + } + public void setUri(String uri) { + this.uri = uri; + } + + /** + **/ + public Endpoint authentication(AuthenticationType authentication) { + + this.authentication = authentication; + return this; + } + + @ApiModelProperty(value = "") + @JsonProperty("authentication") + @Valid + public AuthenticationType getAuthentication() { + return authentication; + } + public void setAuthentication(AuthenticationType authentication) { + this.authentication = authentication; + } + + + + @Override + public boolean equals(java.lang.Object o) { + + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Endpoint endpoint = (Endpoint) o; + return Objects.equals(this.uri, endpoint.uri) && + Objects.equals(this.authentication, endpoint.authentication); + } + + @Override + public int hashCode() { + return Objects.hash(uri, authentication); + } + + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + sb.append("class Endpoint {\n"); + + sb.append(" uri: ").append(toIndentedString(uri)).append("\n"); + sb.append(" authentication: ").append(toIndentedString(authentication)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(java.lang.Object o) { + + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n"); + } +} + diff --git a/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/main/java/org/wso2/carbon/identity/api/server/configs/v1/core/ServerConfigManagementService.java b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/main/java/org/wso2/carbon/identity/api/server/configs/v1/core/ServerConfigManagementService.java index 023adfee91..f20a9c0812 100644 --- a/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/main/java/org/wso2/carbon/identity/api/server/configs/v1/core/ServerConfigManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/main/java/org/wso2/carbon/identity/api/server/configs/v1/core/ServerConfigManagementService.java @@ -29,6 +29,8 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.identity.action.management.model.AuthProperty; +import org.wso2.carbon.identity.action.management.model.EndpointConfig; import org.wso2.carbon.identity.api.server.common.ContextLoader; import org.wso2.carbon.identity.api.server.common.error.APIError; import org.wso2.carbon.identity.api.server.common.error.ErrorResponse; @@ -40,6 +42,7 @@ import org.wso2.carbon.identity.api.server.configs.v1.function.CORSConfigurationToCORSConfig; import org.wso2.carbon.identity.api.server.configs.v1.function.DCRConnectorUtil; import org.wso2.carbon.identity.api.server.configs.v1.function.JWTConnectorUtil; +import org.wso2.carbon.identity.api.server.configs.v1.model.AuthenticationType; import org.wso2.carbon.identity.api.server.configs.v1.model.Authenticator; import org.wso2.carbon.identity.api.server.configs.v1.model.AuthenticatorListItem; import org.wso2.carbon.identity.api.server.configs.v1.model.AuthenticatorProperty; @@ -47,6 +50,7 @@ import org.wso2.carbon.identity.api.server.configs.v1.model.CORSPatch; import org.wso2.carbon.identity.api.server.configs.v1.model.DCRConfig; import org.wso2.carbon.identity.api.server.configs.v1.model.DCRPatch; +import org.wso2.carbon.identity.api.server.configs.v1.model.Endpoint; import org.wso2.carbon.identity.api.server.configs.v1.model.ImpersonationConfiguration; import org.wso2.carbon.identity.api.server.configs.v1.model.ImpersonationPatch; import org.wso2.carbon.identity.api.server.configs.v1.model.InboundAuthPassiveSTSConfig; @@ -74,9 +78,11 @@ import org.wso2.carbon.identity.application.common.model.Property; import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.ServiceProvider; +import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants; import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil; import org.wso2.carbon.identity.application.mgt.ApplicationConstants; +import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants; import org.wso2.carbon.identity.core.ServiceURLBuilder; import org.wso2.carbon.identity.core.URLBuilderException; import org.wso2.carbon.identity.cors.mgt.core.exception.CORSManagementServiceClientException; @@ -100,6 +106,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -774,23 +781,55 @@ private Authenticator buildAuthenticatorResponse(LocalAuthenticatorConfig config authenticator.setName(config.getName()); authenticator.setDisplayName(config.getDisplayName()); authenticator.setIsEnabled(config.isEnabled()); - authenticator.definedBy(Authenticator.DefinedByEnum.valueOf(config.getDefinedByType().toString())); if (config instanceof RequestPathAuthenticatorConfig) { authenticator.setType(Authenticator.TypeEnum.REQUEST_PATH); + authenticator.setDefinedBy(Authenticator.DefinedByEnum.SYSTEM); } else { authenticator.setType(Authenticator.TypeEnum.LOCAL); + if (AuthenticatorPropertyConstants.DefinedByType.SYSTEM == config.getDefinedByType()) { + authenticator.setDefinedBy(Authenticator.DefinedByEnum.SYSTEM); + List authenticatorProperties = + Arrays.stream(config.getProperties()).map(propertyToExternal) + .collect(Collectors.toList()); + authenticator.setProperties(authenticatorProperties); + } else { + authenticator.setDefinedBy(Authenticator.DefinedByEnum.USER); + resolveEndpointConfiguration(authenticator, config); + } } String[] tags = config.getTags(); if (ArrayUtils.isNotEmpty(tags)) { authenticator.setTags(Arrays.asList(tags)); } - List authenticatorProperties = - Arrays.stream(config.getProperties()).map(propertyToExternal) - .collect(Collectors.toList()); - authenticator.setProperties(authenticatorProperties); return authenticator; } + private void resolveEndpointConfiguration(Authenticator authenticator, LocalAuthenticatorConfig config) { + + try { + UserDefinedLocalAuthenticatorConfig userDefinedConfig = (UserDefinedLocalAuthenticatorConfig) config; + EndpointConfig endpointConfig = userDefinedConfig.getEndpointConfig().getEndpointConfig(); + + AuthenticationType authenticationType = new AuthenticationType(); + authenticationType.setType(AuthenticationType.TypeEnum.fromValue(endpointConfig + .getAuthentication().getType().toString())); + Map authenticatorProperties = new HashMap<>(); + for (AuthProperty prop: endpointConfig.getAuthentication().getProperties()) { + authenticatorProperties.put(prop.getName(), prop.getValue()); + } + authenticationType.setProperties(authenticatorProperties); + + Endpoint endpoint = new Endpoint(); + endpoint.setAuthentication(authenticationType); + endpoint.setUri(userDefinedConfig.getEndpointConfig().getEndpointConfig().getUri()); + authenticator.addEndpointItem(endpoint); + } catch (ClassCastException e) { + log.error("Error while casting the Authenticator Config to UserDefinedLocalAuthenticatorConfig", e); + throw handleException(Response.Status.INTERNAL_SERVER_ERROR, Constants.ErrorMessage + .ERROR_CODE_ERROR_RETRIEVING_AUTHENTICATOR, null); + } + } + private Function propertyToExternal = property -> { AuthenticatorProperty authenticatorProperty = new AuthenticatorProperty(); diff --git a/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/main/resources/configs.yaml b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/main/resources/configs.yaml index fa508a56ff..9170160492 100644 --- a/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/main/resources/configs.yaml +++ b/components/org.wso2.carbon.identity.api.server.configs/org.wso2.carbon.identity.api.server.configs.v1/src/main/resources/configs.yaml @@ -1242,9 +1242,42 @@ components: type: array items: $ref: '#/components/schemas/AuthenticatorProperty' + endpoint: + type: array + items: + $ref: '#/components/schemas/Endpoint' required: - name - displayName + Endpoint: + type: object + properties: + uri: + type: string + example: https://abc.com/token + pattern: '^https?://.+' + authentication: + $ref: '#/components/schemas/AuthenticationType' + AuthenticationType: + type: object + required: + - type + - properties + properties: + type: + type: string + enum: + - NONE + - BEARER + - API_KEY + - BASIC + example: BASIC + properties: + type: object + additionalProperties: true + example: + username: "auth_username" + password: "auth_password" AuthenticatorProperty: required: - key