From 6d32f94d956643fcfacdb1d53f15a5c64c5dc8e9 Mon Sep 17 00:00:00 2001 From: Thamindu Aluthwala Date: Wed, 30 Oct 2024 14:28:53 +0530 Subject: [PATCH] Add fed acc link API --- .../api/user/association/v1/UserIdApi.java | 27 ++++++++ .../user/association/v1/UserIdApiService.java | 2 + .../dto/FederatedAssociationRequestDTO.java | 69 +++++++++++++++++++ .../v1/core/UserAssociationService.java | 13 ++++ .../v1/impl/UserIdApiServiceImpl.java | 9 +++ .../src/main/resources/association.yaml | 12 ++++ 6 files changed, 132 insertions(+) create mode 100644 components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/dto/FederatedAssociationRequestDTO.java diff --git a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/UserIdApi.java b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/UserIdApi.java index c4f203819..ba3281d72 100644 --- a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/UserIdApi.java +++ b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/UserIdApi.java @@ -146,6 +146,33 @@ public Response userIdFederatedAssociationsGet(@ApiParam(value = "user id",requi return delegate.userIdFederatedAssociationsGet(userId); } + @Valid + @POST + @Path("/federated-associations") + @Consumes({ "application/json" }) + @Produces({ "application/json" }) + @io.swagger.annotations.ApiOperation(value = "Create federated user association\n", + notes = "This API is used to create federated user associations.
\n Permission required:\n * /permission/admin/manage/identity/user/association/create\n Scope required:\n * internal_user_association_create", + response = void.class) + @io.swagger.annotations.ApiResponses(value = { + @io.swagger.annotations.ApiResponse(code = 201, message = "Successfully created"), + + @io.swagger.annotations.ApiResponse(code = 400, message = "Bad Request"), + + @io.swagger.annotations.ApiResponse(code = 401, message = "Unauthorized"), + + @io.swagger.annotations.ApiResponse(code = 403, message = "Resource Forbidden"), + + @io.swagger.annotations.ApiResponse(code = 409, message = "Conflict"), + + @io.swagger.annotations.ApiResponse(code = 500, message = "Server Error") }) + + public Response meAssociationsPost(@ApiParam(value = "",required=true ) @PathParam("user-id") String userId, + @ApiParam(value = "User details to be associated." ,required=true ) @Valid FederatedAssociationRequestDTO association) { + + return delegate.userIdFederatedAssociationsPost(userId, association); + } + @Valid @DELETE @Path("/federated-associations/{id}") diff --git a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/UserIdApiService.java b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/UserIdApiService.java index 4b18b9ea2..66b0ed1ff 100644 --- a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/UserIdApiService.java +++ b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/UserIdApiService.java @@ -40,6 +40,8 @@ public abstract class UserIdApiService { public abstract Response userIdFederatedAssociationsGet(String userId); + public abstract Response userIdFederatedAssociationsPost(String userId, FederatedAssociationRequestDTO federatedAssociation); + public abstract Response userIdFederatedAssociationsIdDelete(String userId, String id); } diff --git a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/dto/FederatedAssociationRequestDTO.java b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/dto/FederatedAssociationRequestDTO.java new file mode 100644 index 000000000..b08c462f2 --- /dev/null +++ b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/gen/java/org/wso2/carbon/identity/rest/api/user/association/v1/dto/FederatedAssociationRequestDTO.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * 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. + */ + +package org.wso2.carbon.identity.rest.api.user.association.v1.dto; + +import io.swagger.annotations.*; +import com.fasterxml.jackson.annotation.*; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; + +@ApiModel(description = "") +public class FederatedAssociationRequestDTO { + + @Valid + private String idp = null; + + @Valid + private String federatedUserId = null; + + /** + **/ + @ApiModelProperty(value = "") + @JsonProperty("idp") + public String getIdp() { + return idp; + } + public void setIdp(String idp) { + this.idp = idp; + } + + /** + **/ + @ApiModelProperty(value = "") + @JsonProperty("federatedUserId") + public String getFederatedUserId() { + return federatedUserId; + } + public void setFederatedUserId(String federatedUserId) { + this.federatedUserId = federatedUserId; + } + + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + sb.append("class FederatedAssociationRequestDTO {\n"); + + sb.append(" idp: ").append(idp).append("\n"); + sb.append(" federatedUserId: ").append(federatedUserId).append("\n"); + + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/association/v1/core/UserAssociationService.java b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/association/v1/core/UserAssociationService.java index c3c10f2b8..4e9fecfa7 100644 --- a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/association/v1/core/UserAssociationService.java +++ b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/association/v1/core/UserAssociationService.java @@ -24,6 +24,7 @@ import org.wso2.carbon.identity.application.common.model.User; import org.wso2.carbon.identity.rest.api.user.association.v1.dto.AssociationUserRequestDTO; import org.wso2.carbon.identity.rest.api.user.association.v1.dto.FederatedAssociationDTO; +import org.wso2.carbon.identity.rest.api.user.association.v1.dto.FederatedAssociationRequestDTO; import org.wso2.carbon.identity.rest.api.user.association.v1.dto.IdpDTO; import org.wso2.carbon.identity.rest.api.user.association.v1.dto.UserDTO; import org.wso2.carbon.identity.rest.api.user.association.v1.util.UserAssociationServiceHolder; @@ -140,6 +141,18 @@ public void deleteFederatedUserAccountAssociation(String userId) { } } + public void addFederatedUserAccountAssociation(String userId, + FederatedAssociationRequestDTO federatedAssociationDTO) { + + try { + UserAssociationServiceHolder.getFederatedAssociationManager().createFederatedAssociation(getUser(userId), + federatedAssociationDTO.getIdp(), federatedAssociationDTO.getFederatedUserId()); + } catch (FederatedAssociationManagerException e) { + throw handleFederatedAssociationManagerException(e, "Error while adding federated user association: " + + userId); + } + } + private List getUserAssociationsDTOs(UserAccountAssociationDTO[] accountAssociationsOfUser) { List userDTOList = new ArrayList<>(); diff --git a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/association/v1/impl/UserIdApiServiceImpl.java b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/association/v1/impl/UserIdApiServiceImpl.java index e24bcb400..99670676a 100644 --- a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/association/v1/impl/UserIdApiServiceImpl.java +++ b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/association/v1/impl/UserIdApiServiceImpl.java @@ -6,6 +6,7 @@ import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.rest.api.user.association.v1.UserIdApiService; import org.wso2.carbon.identity.rest.api.user.association.v1.core.UserAssociationService; +import org.wso2.carbon.identity.rest.api.user.association.v1.dto.FederatedAssociationRequestDTO; import org.wso2.carbon.identity.rest.api.user.association.v1.util.UserAssociationServiceHolder; import javax.ws.rs.core.Response; @@ -51,6 +52,14 @@ public Response userIdFederatedAssociationsDelete(String userId) { return Response.noContent().build(); } + @Override + public Response userIdFederatedAssociationsPost(String userId, + FederatedAssociationRequestDTO federatedAssociation) { + + userAssociationService.addFederatedUserAccountAssociation(getUser(userId), federatedAssociation); + return Response.noContent().build(); + } + @Override public Response userIdFederatedAssociationsIdDelete(String userId, String id) { diff --git a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/resources/association.yaml b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/resources/association.yaml index 1eb0481d6..8c2c6f3c7 100644 --- a/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/resources/association.yaml +++ b/components/org.wso2.carbon.identity.api.user.association/org.wso2.carbon.identity.api.user.association.v1/src/main/resources/association.yaml @@ -520,6 +520,18 @@ definitions: type: string example: john@google.com #----------------------------------------------------- + # The Federated Association Request object + #----------------------------------------------------- + FederatedAssociationRequest: + type: object + properties: + idp: + type: string + example: eeqweisfhkdfikaefcqwesfceqwqas + federatedUserId: + type: string + example: john@google.com + #----------------------------------------------------- # The Federated Identity Provider Response object #----------------------------------------------------- Idp: