Skip to content

Commit

Permalink
Update to new request builder and processor contracts.
Browse files Browse the repository at this point in the history
  • Loading branch information
malithie committed Feb 17, 2025
1 parent 740ad7a commit ed111f7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import org.wso2.carbon.identity.action.execution.ActionExecutionRequestBuilder;
import org.wso2.carbon.identity.action.execution.exception.ActionExecutionRequestBuilderException;
import org.wso2.carbon.identity.action.execution.model.ActionExecutionRequest;
import org.wso2.carbon.identity.action.execution.model.ActionExecutionRequestContext;
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.action.execution.model.AllowedOperation;
import org.wso2.carbon.identity.action.execution.model.Event;
import org.wso2.carbon.identity.action.execution.model.FlowContext;
import org.wso2.carbon.identity.action.execution.model.Operation;
import org.wso2.carbon.identity.action.execution.model.Organization;
import org.wso2.carbon.identity.action.execution.model.Request;
Expand Down Expand Up @@ -80,11 +82,12 @@ public ActionType getSupportedActionType() {
}

@Override
public ActionExecutionRequest buildActionExecutionRequest(Map<String, Object> eventContext)
public ActionExecutionRequest buildActionExecutionRequest(FlowContext flowContext,
ActionExecutionRequestContext actionExecutionContext)
throws ActionExecutionRequestBuilderException {

OAuthTokenReqMessageContext tokenMessageContext =
(OAuthTokenReqMessageContext) eventContext.get("tokenMessageContext");
flowContext.getValue("tokenMessageContext", OAuthTokenReqMessageContext.class);

Map<String, Object> additionalClaimsToAddToToken = getAdditionalClaimsToAddToToken(tokenMessageContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@
import org.wso2.carbon.identity.action.execution.ActionExecutionLogConstants;
import org.wso2.carbon.identity.action.execution.ActionExecutionResponseProcessor;
import org.wso2.carbon.identity.action.execution.exception.ActionExecutionResponseProcessorException;
import org.wso2.carbon.identity.action.execution.model.ActionExecutionResponseContext;
import org.wso2.carbon.identity.action.execution.model.ActionExecutionStatus;
import org.wso2.carbon.identity.action.execution.model.ActionInvocationErrorResponse;
import org.wso2.carbon.identity.action.execution.model.ActionInvocationFailureResponse;
import org.wso2.carbon.identity.action.execution.model.ActionInvocationSuccessResponse;
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.action.execution.model.Error;
import org.wso2.carbon.identity.action.execution.model.ErrorStatus;
import org.wso2.carbon.identity.action.execution.model.Event;
import org.wso2.carbon.identity.action.execution.model.FailedStatus;
import org.wso2.carbon.identity.action.execution.model.Failure;
import org.wso2.carbon.identity.action.execution.model.FlowContext;
import org.wso2.carbon.identity.action.execution.model.PerformableOperation;
import org.wso2.carbon.identity.action.execution.model.Success;
import org.wso2.carbon.identity.action.execution.model.SuccessStatus;
Expand Down Expand Up @@ -80,15 +81,16 @@ public ActionType getSupportedActionType() {
}

@Override
public ActionExecutionStatus<Success> processSuccessResponse(Map<String, Object> eventContext, Event event,
ActionInvocationSuccessResponse
actionInvocationSuccessResponse)
public ActionExecutionStatus<Success> processSuccessResponse(FlowContext flowContext,
ActionExecutionResponseContext
<ActionInvocationSuccessResponse>
responseContext)
throws ActionExecutionResponseProcessorException {

OAuthTokenReqMessageContext tokenMessageContext =
(OAuthTokenReqMessageContext) eventContext.get("tokenMessageContext");
PreIssueAccessTokenEvent preIssueAccessTokenEvent = (PreIssueAccessTokenEvent) event;
List<PerformableOperation> operationsToPerform = actionInvocationSuccessResponse.getOperations();
flowContext.getValue("tokenMessageContext", OAuthTokenReqMessageContext.class);
PreIssueAccessTokenEvent preIssueAccessTokenEvent = (PreIssueAccessTokenEvent) responseContext.getActionEvent();
List<PerformableOperation> operationsToPerform = responseContext.getActionInvocationResponse().getOperations();

AccessToken requestAccessToken = preIssueAccessTokenEvent.getAccessToken();
AccessToken.Builder responseAccessTokenBuilder = preIssueAccessTokenEvent.getAccessToken().copy();
Expand Down Expand Up @@ -120,7 +122,7 @@ public ActionExecutionStatus<Success> processSuccessResponse(Map<String, Object>
AccessToken responseAccessToken = responseAccessTokenBuilder.build();
updateTokenMessageContext(tokenMessageContext, responseAccessToken);

return new SuccessStatus.Builder().setResponseContext(eventContext).build();
return new SuccessStatus.Builder().setResponseContext(flowContext.getContextData()).build();
}

private void logOperationExecutionResults(ActionType actionType,
Expand Down Expand Up @@ -164,10 +166,13 @@ private void logOperationExecutionResults(ActionType actionType,
}

@Override
public ActionExecutionStatus<Failure> processFailureResponse(Map<String, Object> eventContext, Event actionEvent,
ActionInvocationFailureResponse failureResponse) throws
ActionExecutionResponseProcessorException {
public ActionExecutionStatus<Failure> processFailureResponse(FlowContext flowContext,
ActionExecutionResponseContext
<ActionInvocationFailureResponse>
responseContext)
throws ActionExecutionResponseProcessorException {

ActionInvocationFailureResponse failureResponse = responseContext.getActionInvocationResponse();
handleInvalidErrorCodes(failureResponse.getFailureReason());
return new FailedStatus(new Failure(failureResponse.getFailureReason(),
failureResponse.getFailureDescription()));
Expand Down Expand Up @@ -207,9 +212,9 @@ private boolean isServerError(String errorCode) {
}

@Override
public ActionExecutionStatus<Error> processErrorResponse(Map<String, Object> map, Event event,
ActionInvocationErrorResponse
actionInvocationErrorResponse)
public ActionExecutionStatus<Error> processErrorResponse(FlowContext flowContext,
ActionExecutionResponseContext
<ActionInvocationErrorResponse> responseContext)
throws ActionExecutionResponseProcessorException {

/*
Expand All @@ -220,7 +225,7 @@ public ActionExecutionStatus<Error> processErrorResponse(Map<String, Object> map
* However, currently this value is not propagated by the endpoint to comply with OAuth specification.
*/
return new ErrorStatus(new Error(OAuth2ErrorCodes.SERVER_ERROR,
actionInvocationErrorResponse.getErrorDescription()));
responseContext.getActionInvocationResponse().getErrorDescription()));
}

private void updateTokenMessageContext(OAuthTokenReqMessageContext tokenMessageContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.action.execution.model.Error;
import org.wso2.carbon.identity.action.execution.model.Failure;
import org.wso2.carbon.identity.action.execution.model.FlowContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.base.IdentityConstants;
Expand Down Expand Up @@ -76,13 +77,10 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;

import static org.wso2.carbon.identity.oauth.common.OAuthConstants.OAUTH_APP;
import static org.wso2.carbon.identity.oauth.common.OAuthConstants.RENEW_TOKEN_WITHOUT_REVOKING_EXISTING_ENABLE_CONFIG;
Expand Down Expand Up @@ -614,15 +612,13 @@ private ActionExecutionStatus<?> executePreIssueAccessTokenActions(
ActionExecutionStatus<?> executionStatus = null;
if (checkExecutePreIssueAccessTokensActions(tokenReqMessageContext)) {

Map<String, Object> additionalProperties = new HashMap<>();
Consumer<Map<String, Object>> mapInitializer =
map -> map.put("tokenMessageContext", tokenReqMessageContext);
mapInitializer.accept(additionalProperties);
FlowContext flowContext = FlowContext.create().add("tokenMessageContext", tokenReqMessageContext);

try {
executionStatus = OAuthComponentServiceHolder.getInstance().getActionExecutorService()
.execute(ActionType.PRE_ISSUE_ACCESS_TOKEN, additionalProperties,
IdentityTenantUtil.getTenantDomain(IdentityTenantUtil.getLoginTenantId()));
.execute(ActionType.PRE_ISSUE_ACCESS_TOKEN, flowContext,
IdentityTenantUtil.getTenantDomain(IdentityTenantUtil.getLoginTenantId()));

if (log.isDebugEnabled()) {
log.debug(String.format(
"Invoked pre issue access token action for clientID: %s grant types: %s. Status: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.action.execution.model.Error;
import org.wso2.carbon.identity.action.execution.model.Failure;
import org.wso2.carbon.identity.action.execution.model.FlowContext;
import org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException;
import org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException;
import org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException;
Expand Down Expand Up @@ -77,12 +78,9 @@

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -843,14 +841,11 @@ private ActionExecutionStatus<?> executePreIssueAccessTokenActions(

setCustomizedAccessTokenAttributesToMessageContext(refreshTokenValidationDataDO, tokenReqMessageContext);

Map<String, Object> additionalProperties = new HashMap<>();
Consumer<Map<String, Object>> mapInitializer =
map -> map.put("tokenMessageContext", tokenReqMessageContext);
mapInitializer.accept(additionalProperties);
FlowContext flowContext = FlowContext.create().add("tokenMessageContext", tokenReqMessageContext);

try {
executionStatus = OAuthComponentServiceHolder.getInstance().getActionExecutorService()
.execute(ActionType.PRE_ISSUE_ACCESS_TOKEN, additionalProperties,
.execute(ActionType.PRE_ISSUE_ACCESS_TOKEN, flowContext,
IdentityTenantUtil.getTenantDomain(IdentityTenantUtil.getLoginTenantId()));
if (log.isDebugEnabled()) {
log.debug(String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.action.execution.exception.ActionExecutionRequestBuilderException;
import org.wso2.carbon.identity.action.execution.model.ActionExecutionRequest;
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.action.execution.model.AllowedOperation;
import org.wso2.carbon.identity.action.execution.model.FlowContext;
import org.wso2.carbon.identity.action.execution.model.Header;
import org.wso2.carbon.identity.action.execution.model.Operation;
import org.wso2.carbon.identity.action.execution.model.Param;
Expand Down Expand Up @@ -146,20 +146,13 @@ public void testGetSupportedActionType() {
Assert.assertEquals(actionType, ActionType.PRE_ISSUE_ACCESS_TOKEN);
}

@DataProvider(name = "BuildTokenRequestMessageContext")
public Object[][] buildTokenRequestMessageContext() {

return new Object[][]{
{mockTokenMessageContext()},
};
}

@Test(dataProvider = "BuildTokenRequestMessageContext")
public void testBuildActionExecutionRequest(Map<String, Object> eventContext)
@Test
public void testBuildActionExecutionRequest()
throws ActionExecutionRequestBuilderException {

ActionExecutionRequest actionExecutionRequest = preIssueAccessTokenRequestBuilder.
buildActionExecutionRequest(eventContext);
buildActionExecutionRequest(
FlowContext.create().add("tokenMessageContext", getMockTokenMessageContext()), null);
Assert.assertNotNull(actionExecutionRequest);
Assert.assertEquals(actionExecutionRequest.getActionType(), ActionType.PRE_ISSUE_ACCESS_TOKEN);
assertEvent((PreIssueAccessTokenEvent) actionExecutionRequest.getEvent(), getExpectedEvent());
Expand Down Expand Up @@ -254,21 +247,11 @@ private void assertAllowedOperations(List<AllowedOperation> actual, List<Allowed
}
}

/**
* Mock the token message context for testing.
*
* @return A map representing the event context.
*/
private Map<String, Object> mockTokenMessageContext() {

Map<String, Object> eventContext = new HashMap<>();
private OAuthTokenReqMessageContext getMockTokenMessageContext() {

OAuth2AccessTokenReqDTO tokenReqDTO = mockTokenRequestDTO();
AuthenticatedUser authenticatedUser = mockAuthenticatedUser();
OAuthTokenReqMessageContext tokenMessageContext = mockMessageContext(tokenReqDTO, authenticatedUser);
eventContext.put("tokenMessageContext", tokenMessageContext);

return eventContext;
return mockMessageContext(tokenReqDTO, authenticatedUser);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@
<carbon.kernel.registry.imp.pkg.version.range>[1.0.1, 2.0.0)</carbon.kernel.registry.imp.pkg.version.range>

<!-- Carbon Identity Framework version -->
<carbon.identity.framework.version>7.7.221</carbon.identity.framework.version>
<carbon.identity.framework.version>7.7.261</carbon.identity.framework.version>
<carbon.identity.framework.imp.pkg.version.range>[5.25.234, 8.0.0)
</carbon.identity.framework.imp.pkg.version.range>
<identity.oauth.xacml.version.range>[2.0.0, 3.0.0)</identity.oauth.xacml.version.range>
Expand Down

0 comments on commit ed111f7

Please sign in to comment.