Skip to content

Commit

Permalink
Provide support for authentication action type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Aug 12, 2024
1 parent 8f81737 commit c43f87c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public static ActionExecutionResponseProcessor getActionExecutionResponseProcess
switch (actionType) {
case PRE_ISSUE_ACCESS_TOKEN:
return actionInvocationResponseProcessors.get(ActionType.PRE_ISSUE_ACCESS_TOKEN);
case AUTHENTICATION:
return actionInvocationResponseProcessors.get(ActionType.AUTHENTICATION);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.action.execution.exception.ActionExecutionException;
Expand Down Expand Up @@ -82,6 +83,8 @@ public boolean isExecutionEnabled(ActionType actionType) {
switch (actionType) {
case PRE_ISSUE_ACCESS_TOKEN:
return IdentityUtil.isPreIssueAccessTokenActionTypeEnabled();
case AUTHENTICATION:
return IdentityUtil.isAuthenticationActionTypeEnabled();
default:
return false;
}
Expand All @@ -90,13 +93,26 @@ public boolean isExecutionEnabled(ActionType actionType) {
public ActionExecutionStatus execute(ActionType actionType, Map<String, Object> eventContext, String tenantDomain)
throws ActionExecutionException {

return execute(actionType, null, eventContext, tenantDomain);
}

public ActionExecutionStatus execute(ActionType actionType, String actionId, Map<String, Object> eventContext,
String tenantDomain) throws ActionExecutionException {

try {
List<Action> actions = getActionsByActionType(actionType, tenantDomain);
validateActions(actions, actionType);
Action action;
if (StringUtils.isNotBlank(actionId)) {
List<Action> actions = getActionsByActionType(actionType, tenantDomain);
validateActions(actions, actionType);
// As of now only one action is allowed.
action = actions.get(0);
} else {
action = getActionsByActionId(actionId, tenantDomain);
}

ActionExecutionRequest actionRequest = buildActionExecutionRequest(actionType, eventContext);
ActionExecutionResponseProcessor actionExecutionResponseProcessor = getResponseProcessor(actionType);

Action action = actions.get(0); // As of now only one action is allowed.
return Optional.ofNullable(action)
.filter(activeAction -> activeAction.getStatus() == Action.Status.ACTIVE)
.map(activeAction -> executeAction(activeAction, actionRequest, eventContext,
Expand All @@ -121,6 +137,16 @@ private List<Action> getActionsByActionType(ActionType actionType, String tenant
}
}

private Action getActionsByActionId(String actionId, String tenantDomain) throws ActionExecutionRuntimeException {

try {
return ActionExecutionServiceComponentHolder.getInstance().getActionManagementService()
.getActionByActionId(actionId, tenantDomain);
} catch (ActionMgtException e) {
throw new ActionExecutionRuntimeException("Error occurred while retrieving actions.", e);
}
}

private void validateActions(List<Action> actions, ActionType actionType) throws ActionExecutionException {

if (CollectionUtils.isEmpty(actions)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
*/
public enum ActionType {
PRE_ISSUE_ACCESS_TOKEN,
AUTHENTICATION
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
public enum Operation {
ADD("add"),
REMOVE("remove"),
REPLACE("replace");
REPLACE("replace"),
REDIRECT("redirect");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.identity.action.management;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.action.management.constant.ActionMgtConstants;
Expand Down Expand Up @@ -189,6 +190,9 @@ private String getActionTypeFromPath(String actionType) throws ActionMgtClientEx
*/
private void validateMaxActionsPerType(String actionType, String tenantDomain) throws ActionMgtException {

if (StringUtils.equals(Action.ActionTypes.AUTHENTICATION.getActionType(), actionType)) {
return;
}
Map<String, Integer> actionsCountPerType = getActionsCountPerType(tenantDomain);
if (actionsCountPerType.containsKey(actionType) &&
actionsCountPerType.get(actionType) >= IdentityUtil.getMaximumActionsPerActionType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public enum ActionTypes {
"preRegistration",
"PRE_REGISTRATION",
"Pre Registration.",
"Configure an extension point for modifying user registration via a custom service.");
"Configure an extension point for modifying user registration via a custom service."),
AUTHENTICATION(
"authentication",
"AUTHENTICATION",
"Authentication.",
"Configure an extension point for user authentication via a custom service."),;

private final String pathParam;
private final String actionType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public class IdentityCoreConstants {
// Actions constants.
public static final String MAXIMUM_ACTIONS_PER_TYPE_PROPERTY = "Actions.MaximumActionsPerType";
public static final String PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_PROPERTY = "Actions.Types.PreIssueAccessToken.Enable";

public static final String AUTHENTICATION_ACTION_TYPE_ENABLE_PROPERTY = "Actions.Types.PreIssueAccessToken.Enable";

public static final int DEFAULT_MAXIMUM_ACTIONS_PER_TYPE = 1;
public static final boolean DEFAULT_PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_VALUE = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,16 @@ public static boolean isPreIssueAccessTokenActionTypeEnabled() {
IdentityCoreConstants.DEFAULT_PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_VALUE);
}

/**
* Get Pre Issue Access Token Action Type enabled status.
*
* @return Whether the Pre Issue Access Token Action type is enabled or not.
*/
public static boolean isAuthenticationActionTypeEnabled() {

return isActionTypeEnabled(IdentityCoreConstants.AUTHENTICATION_ACTION_TYPE_ENABLE_PROPERTY, false);
}

/**
* Check whether a given action type is enabled or not.
*
Expand Down

0 comments on commit c43f87c

Please sign in to comment.