Skip to content

Commit

Permalink
Add fed acc link API
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaminduR committed Jan 22, 2025
1 parent 79055b1 commit b01fc95
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
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 userIdFederatedAssociationsPost(@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) 2025, WSO2 LLC. (http://www.wso2.com) 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 @@ -418,6 +418,49 @@ paths:
$ref: '#/definitions/Error'
tags:
- admin
post:
summary: Create federated user association
description: |
This API is used to create federated user associations. <br>
<b>Permission required:</b>
* /permission/admin/manage/identity/user/association/create
<b>Scope required:</b>
* internal_user_association_create
parameters:
- name: user-id
in: path
required: true
description: user id
type: string
- name: association
in: body
description: User details to be associated.
required: true
schema:
$ref: '#/definitions/FederatedAssociationRequest'
responses:
201:
description: Successfully created
schema:
$ref: '#/definitions/FederatedAssociation'
400:
description: Bad Request
schema:
$ref: '#/definitions/Error'
401:
description: Unauthorized
schema:
$ref: '#/definitions/Error'
403:
description: Resource Forbidden
schema:
$ref: '#/definitions/Error'
500:
description: Server Error
schema:
$ref: '#/definitions/Error'
tags:
- admin

/{user-id}/federated-associations/{id}:
delete:
Expand Down Expand Up @@ -520,6 +563,20 @@ definitions:
type: string
example: [email protected]
#-----------------------------------------------------
# The Federated Association Request object
#-----------------------------------------------------
FederatedAssociationRequest:
type: object
properties:
idp:
type: string
example: exampleIdP
description: Name of the IdP
federatedUserId:
type: string
example: [email protected]
description: User identifier in the federated IdP
#-----------------------------------------------------
# The Federated Identity Provider Response object
#-----------------------------------------------------
Idp:
Expand Down

0 comments on commit b01fc95

Please sign in to comment.