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

Restrict not implemented action types from Action Mangement APIs #663

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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 @@ -47,7 +47,12 @@ public enum ErrorMessage {
"Authentication property values cannot be empty."),
ERROR_NO_ACTION_FOUND_ON_GIVEN_ACTION_TYPE_AND_ID("60004",
"Action is not found.",
"No action is found for given action id and action type");
"No action is found for given action id and action type"),

// Server errors.
ERROR_NOT_IMPLEMENTED_ACTION_TYPE("65001",
"Unable to perform the operation.",
"The requested action type is not currently supported by the server.");

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.core.Response;

import static org.wso2.carbon.identity.api.server.action.management.v1.constants.ActionMgtEndpointConstants.ErrorMessage.ERROR_EMPTY_ACTION_ENDPOINT_AUTHENTICATION_PROPERTIES;
import static org.wso2.carbon.identity.api.server.action.management.v1.constants.ActionMgtEndpointConstants.ErrorMessage.ERROR_INVALID_ACTION_ENDPOINT_AUTHENTICATION_PROPERTIES;
import static org.wso2.carbon.identity.api.server.action.management.v1.constants.ActionMgtEndpointConstants.ErrorMessage.ERROR_INVALID_ACTION_ENDPOINT_AUTH_TYPE;
import static org.wso2.carbon.identity.api.server.action.management.v1.constants.ActionMgtEndpointConstants.ErrorMessage.ERROR_NOT_IMPLEMENTED_ACTION_TYPE;
import static org.wso2.carbon.identity.api.server.action.management.v1.constants.ActionMgtEndpointConstants.ErrorMessage.ERROR_NO_ACTION_FOUND_ON_GIVEN_ACTION_TYPE_AND_ID;

/**
Expand All @@ -56,10 +59,19 @@
public class ServerActionManagementService {

private static final Log LOG = LogFactory.getLog(ServerActionManagementService.class);
private static final Set<String> NOT_IMPLEMENTED_ACTION_TYPES = new HashSet<>();

static {
NOT_IMPLEMENTED_ACTION_TYPES.add(Action.ActionTypes.PRE_UPDATE_PASSWORD.getPathParam());
NOT_IMPLEMENTED_ACTION_TYPES.add(Action.ActionTypes.PRE_UPDATE_PROFILE.getPathParam());
NOT_IMPLEMENTED_ACTION_TYPES.add(Action.ActionTypes.PRE_REGISTRATION.getPathParam());
NOT_IMPLEMENTED_ACTION_TYPES.add(Action.ActionTypes.AUTHENTICATION.getPathParam());
}

public ActionResponse createAction(String actionType, ActionModel actionModel) {

try {
handleNotImplementedActionTypes(actionType);
return buildActionResponse(ActionManagementServiceHolder.getActionManagementService()
.addAction(actionType, buildAction(actionModel),
CarbonContext.getThreadLocalCarbonContext().getTenantDomain()));
Expand All @@ -71,6 +83,7 @@ public ActionResponse createAction(String actionType, ActionModel actionModel) {
public List<ActionResponse> getActionsByActionType(String actionType) {

try {
handleNotImplementedActionTypes(actionType);
List<Action> actions = ActionManagementServiceHolder.getActionManagementService()
.getActionsByActionType(actionType,
CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
Expand All @@ -88,6 +101,7 @@ public List<ActionResponse> getActionsByActionType(String actionType) {
public ActionResponse getActionByActionId(String actionType, String actionId) {

try {
handleNotImplementedActionTypes(actionType);
Action action = ActionManagementServiceHolder.getActionManagementService()
.getActionByActionId(actionType, actionId,
CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
Expand All @@ -106,6 +120,7 @@ public ActionResponse getActionByActionId(String actionType, String actionId) {
public ActionResponse updateAction(String actionType, String actionId, ActionUpdateModel actionUpdateModel) {

try {
handleNotImplementedActionTypes(actionType);
return buildActionResponse(ActionManagementServiceHolder.getActionManagementService()
.updateAction(actionType, actionId, buildUpdatingAction(actionUpdateModel),
CarbonContext.getThreadLocalCarbonContext().getTenantDomain()));
Expand All @@ -117,6 +132,7 @@ public ActionResponse updateAction(String actionType, String actionId, ActionUpd
public void deleteAction(String actionType, String actionId) {

try {
handleNotImplementedActionTypes(actionType);
ActionManagementServiceHolder.getActionManagementService().deleteAction(actionType, actionId,
CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
} catch (ActionMgtException e) {
Expand All @@ -127,6 +143,7 @@ public void deleteAction(String actionType, String actionId) {
public ActionBasicResponse activateAction(String actionType, String actionId) {

try {
handleNotImplementedActionTypes(actionType);
return buildActionBasicResponse(ActionManagementServiceHolder.getActionManagementService()
.activateAction(actionType, actionId,
CarbonContext.getThreadLocalCarbonContext().getTenantDomain()));
Expand All @@ -138,6 +155,7 @@ public ActionBasicResponse activateAction(String actionType, String actionId) {
public ActionBasicResponse deactivateAction(String actionType, String actionId) {

try {
handleNotImplementedActionTypes(actionType);
return buildActionBasicResponse(ActionManagementServiceHolder.getActionManagementService()
.deactivateAction(actionType, actionId,
CarbonContext.getThreadLocalCarbonContext().getTenantDomain()));
Expand All @@ -158,12 +176,14 @@ public List<ActionTypesResponseItem> getActionTypes() {
List<ActionTypesResponseItem> actionTypesResponseItems = new ArrayList<>();
for (Action.ActionTypes actionType : Action.ActionTypes.values()) {

actionTypesResponseItems.add(new ActionTypesResponseItem()
.type(ActionType.valueOf(actionType.getActionType()))
.displayName(actionType.getDisplayName())
.description(actionType.getDescription())
.count(actionsCountPerType.getOrDefault(actionType.getActionType(), 0))
.self(ActionMgtEndpointUtil.buildURIForActionType(actionType.getActionType())));
if (!NOT_IMPLEMENTED_ACTION_TYPES.contains(actionType.getPathParam())) {
actionTypesResponseItems.add(new ActionTypesResponseItem()
.type(ActionType.valueOf(actionType.getActionType()))
.displayName(actionType.getDisplayName())
.description(actionType.getDescription())
.count(actionsCountPerType.getOrDefault(actionType.getActionType(), 0))
.self(ActionMgtEndpointUtil.buildURIForActionType(actionType.getActionType())));
}
}

return actionTypesResponseItems;
Expand All @@ -176,6 +196,7 @@ public ActionResponse updateActionEndpointAuthentication(String actionType, Stri
AuthenticationTypeProperties authenticationTypeProperties) {

try {
handleNotImplementedActionTypes(actionType);
Authentication authentication = buildAuthentication(getAuthTypeFromPath(authType),
authenticationTypeProperties.getProperties());
return buildActionResponse(ActionManagementServiceHolder.getActionManagementService()
Expand Down Expand Up @@ -352,4 +373,17 @@ private Authentication.Type getAuthTypeFromPath(String authType) {
.orElseThrow(() -> ActionMgtEndpointUtil.handleException(Response.Status.BAD_REQUEST,
ERROR_INVALID_ACTION_ENDPOINT_AUTH_TYPE));
}

/**
* Handle not implemented action types.
*
* @param actionType Action type.
*/
private void handleNotImplementedActionTypes(String actionType) {

if (NOT_IMPLEMENTED_ACTION_TYPES.contains(actionType)) {
throw ActionMgtEndpointUtil.handleException(Response.Status.NOT_IMPLEMENTED,
ERROR_NOT_IMPLEMENTED_ACTION_TYPE);
}
}
}
Loading