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 federated user account link API #227

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 @@ -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. <br>\n <b>Permission required:</b>\n * /permission/admin/manage/identity/user/association/create\n <b>Scope required:</b>\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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<UserDTO> getUserAssociationsDTOs(UserAccountAssociationDTO[] accountAssociationsOfUser) {

List<UserDTO> userDTOList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,18 @@ definitions:
type: string
example: [email protected]
#-----------------------------------------------------
# The Federated Association Request object
#-----------------------------------------------------
FederatedAssociationRequest:
type: object
properties:
idp:
type: string
example: eeqweisfhkdfikaefcqwesfceqwqas
federatedUserId:
type: string
example: [email protected]
#-----------------------------------------------------
# The Federated Identity Provider Response object
#-----------------------------------------------------
Idp:
Expand Down
Loading