Skip to content

Commit

Permalink
Merge branch 'master' into add-authentication-action
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Aug 18, 2024
2 parents 303cafd + bb3ae52 commit 3037adb
Show file tree
Hide file tree
Showing 338 changed files with 9,598 additions and 2,875 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>action-mgt</artifactId>
<version>7.3.66-SNAPSHOT</version>
<version>7.4.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
import org.wso2.carbon.identity.action.execution.model.AllowedOperation;
import org.wso2.carbon.identity.action.execution.model.PerformableOperation;
import org.wso2.carbon.identity.action.execution.util.APIClient;
import org.wso2.carbon.identity.action.execution.util.ActionExecutorConfig;
import org.wso2.carbon.identity.action.execution.util.AuthMethods;
import org.wso2.carbon.identity.action.execution.util.OperationComparator;
import org.wso2.carbon.identity.action.management.exception.ActionMgtException;
import org.wso2.carbon.identity.action.management.model.Action;
import org.wso2.carbon.identity.action.management.model.AuthProperty;
import org.wso2.carbon.identity.action.management.model.AuthType;
import org.wso2.carbon.identity.core.util.IdentityUtil;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -80,14 +80,7 @@ public static ActionExecutorServiceImpl getInstance() {
@Override
public boolean isExecutionEnabled(ActionType actionType) {

switch (actionType) {
case PRE_ISSUE_ACCESS_TOKEN:
return IdentityUtil.isPreIssueAccessTokenActionTypeEnabled();
case AUTHENTICATION:
return IdentityUtil.isPreIssueAccessTokenActionTypeEnabled();
default:
return false;
}
return ActionExecutorConfig.getInstance().isExecutionForActionTypeEnabled(actionType);
}

public ActionExecutionStatus execute(ActionType actionType, Map<String, Object> eventContext, String tenantDomain)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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.action.execution.util;

import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.core.util.IdentityUtil;

/**
* This class holds the system configurations for the Action Executor Service.
*/
public class ActionExecutorConfig {

private static final ActionExecutorConfig INSTANCE = new ActionExecutorConfig();

private static final String PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_PROPERTY =
"Actions.Types.PreIssueAccessToken.Enable";

private ActionExecutorConfig() {

}

public static ActionExecutorConfig getInstance() {

return INSTANCE;
}

public boolean isExecutionForActionTypeEnabled(ActionType actionType) {

switch (actionType) {
case PRE_ISSUE_ACCESS_TOKEN:
return isActionTypeEnabled(PRE_ISSUE_ACCESS_TOKEN_ACTION_TYPE_ENABLE_PROPERTY);
default:
return false;
}
}

private boolean isActionTypeEnabled(String actionTypePropertyName) {

boolean isActionTypeEnabled = false;
String actionTypeEnabledPropertyValue = IdentityUtil.getProperty(actionTypePropertyName);
if (StringUtils.isNotBlank(actionTypeEnabledPropertyValue)) {
return Boolean.parseBoolean(actionTypeEnabledPropertyValue);
}
return isActionTypeEnabled;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you 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.action.execution.util;

import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.core.util.IdentityUtil;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class ActionExecutorConfigTest {

private ActionExecutorConfig actionExecutorConfig;

private MockedStatic<IdentityUtil> identityUtil;

@BeforeMethod
public void setUp() {

MockitoAnnotations.openMocks(this);
actionExecutorConfig = ActionExecutorConfig.getInstance();
identityUtil = Mockito.mockStatic(IdentityUtil.class);
}

@AfterMethod
public void tearDown() {

identityUtil.close();
}

@Test
public void testIsExecutionForActionTypeEnabled_PreIssueAccessToken_Enabled() {

identityUtil.when(() -> IdentityUtil.getProperty("Actions.Types.PreIssueAccessToken.Enable"))
.thenReturn("true");
assertTrue(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.PRE_ISSUE_ACCESS_TOKEN));
}

@Test
public void testIsExecutionForActionTypeEnabled_PreIssueAccessToken_Disabled() {

identityUtil.when(() -> IdentityUtil.getProperty("Actions.Types.PreIssueAccessToken.Enable"))
.thenReturn("false");
assertFalse(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.PRE_ISSUE_ACCESS_TOKEN));
}

@Test
public void testIsExecutionForActionTypeEnabled_PreIssueAccessToken_InvalidValue() {

identityUtil.when(() -> IdentityUtil.getProperty("Actions.Types.PreIssueAccessToken.Enable"))
.thenReturn("invalid");
assertFalse(actionExecutorConfig.isExecutionForActionTypeEnabled(ActionType.PRE_ISSUE_ACCESS_TOKEN));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<class name="org.wso2.carbon.identity.action.execution.util.AuthMethodsTest"/>
<class name="org.wso2.carbon.identity.action.execution.util.OperationComparatorTest"/>
<class name="org.wso2.carbon.identity.action.execution.util.APIClientTest"/>
<class name="org.wso2.carbon.identity.action.execution.util.ActionExecutorConfigTest"/>
</classes>
</test>
<test name="action-execution-configuration-test">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>action-mgt</artifactId>
<version>7.3.66-SNAPSHOT</version>
<version>7.4.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public enum ActionTypes {
"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."),;
"AUTHENTICATION",
"Authentication.",
"Configure an extension point for user authentication via a custom service.");

private final String pathParam;
private final String actionType;
Expand Down
2 changes: 1 addition & 1 deletion components/action-mgt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>identity-framework</artifactId>
<version>7.3.66-SNAPSHOT</version>
<version>7.4.6-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>api-resource-mgt</artifactId>
<version>7.3.66-SNAPSHOT</version>
<version>7.4.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>api-resource-mgt</artifactId>
<version>7.3.66-SNAPSHOT</version>
<version>7.4.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>org.wso2.carbon.identity.api.resource.mgt</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ APIResource getAPIResourceByIdentifier(String apiResourceIdentifier, String tena
void deleteAPIScopeByScopeName(String apiResourceId, String scopeName, String tenantDomain)
throws APIResourceMgtException;

/**
* Patch scope by scope name.
*
* @param scope Scope.
* @param tenantDomain Tenant domain.
* @throws APIResourceMgtException If an error occurs while deleting API scope.
*/
default void updateScopeMetadata(Scope scope, APIResource apiResource, String tenantDomain)
throws APIResourceMgtException {
// no implementation
}

/**
* Put scopes to API resource.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ public void updateAPIResource(APIResource apiResource, List<Scope> addedScopes,
publisherProxy.publishPostUpdateAPIResource(apiResource, addedScopes, removedScopes, tenantDomain);
}

@Override
public void updateScopeMetadata(Scope scope, APIResource apiResource, String tenantDomain)
throws APIResourceMgtException {

APIResourceManagerEventPublisherProxy publisherProxy = APIResourceManagerEventPublisherProxy.getInstance();
publisherProxy.publishPreUpdateScopeMetadataWithException(scope, apiResource, tenantDomain);
CACHE_BACKED_DAO.updateScopeMetadata(scope, apiResource, IdentityTenantUtil.getTenantId(tenantDomain));
publisherProxy.publishPostUpdateScopeMetadataWithException(scope, apiResource, tenantDomain);
}

@Override
public APIResource getAPIResourceByIdentifier(String apiResourceIdentifier, String tenantDomain)
throws APIResourceMgtException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public enum ErrorMessages {
"resource properties.", "Error while retrieving API resource properties from the database."),
ERROR_CODE_ERROR_WHILE_ADDING_API_RESOURCE_PROPERTIES("65014", "Error while adding API resource " +
"properties.", "Error while adding API resource properties to the database."),
ERROR_CODE_ERROR_WHILE_UPDATING_SCOPE_METADATA("65015", "Error while updating scope metadata.",
"Error while updating scope metadata in the database."),
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public class SQLConstants {
"OR TENANT_ID IS NULL)";
public static final String UPDATE_API_RESOURCE = "UPDATE API_RESOURCE SET NAME = ?, DESCRIPTION = ?, TYPE = ?" +
" WHERE ID = ?";
public static final String UPDATE_SCOPE_METADATA = "UPDATE SCOPE SET DISPLAY_NAME = ?, DESCRIPTION = ? " +
"WHERE NAME = ? AND TENANT_ID = ?";
public static final String IS_SCOPE_EXIST_BY_ID = "SELECT ID FROM SCOPE WHERE ID = ? AND TENANT_ID = ?";
public static final String GET_SCOPE_BY_NAME = "SELECT ID, NAME, DISPLAY_NAME, DESCRIPTION, API_ID, TENANT_ID "
+ "FROM SCOPE WHERE NAME = ? AND (TENANT_ID = ? OR TENANT_ID IS NULL)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ Integer getAPIResourcesCount(Integer tenantId, List<ExpressionNode> expressionNo
void updateAPIResource(APIResource apiResource, List<Scope> addedScopes, List<String> removedScopes,
Integer tenantId) throws APIResourceMgtException;

/**
* Update the {@link Scope} for the given id.
*
* @param scope Scope.
* @param apiResource API resource.
* @param tenantId Tenant Id.
* @throws APIResourceMgtException If an error occurs while updating the API resource.
*/
default void updateScopeMetadata(Scope scope, APIResource apiResource, Integer tenantId)
throws APIResourceMgtException {
// no implementation
}

/**
* Delete the {@link APIResource} for the given id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,30 @@ public void updateAPIResource(APIResource apiResource, List<Scope> addedScopes,
}
}

@Override
public void updateScopeMetadata(Scope scope, APIResource apiResource, Integer tenantId)
throws APIResourceMgtException {

try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true);
PreparedStatement preparedStatement = dbConnection.prepareStatement(SQLConstants.UPDATE_SCOPE_METADATA)) {
try {
preparedStatement.setString(1, scope.getDisplayName());
preparedStatement.setString(2, scope.getDescription());
preparedStatement.setString(3, scope.getName());
preparedStatement.setInt(4, tenantId);
preparedStatement.executeUpdate();

IdentityDatabaseUtil.commitTransaction(dbConnection);
} catch (SQLException e) {
IdentityDatabaseUtil.rollbackTransaction(dbConnection);
throw e;
}
} catch (SQLException e) {
throw APIResourceManagementUtil.handleServerException(
APIResourceManagementConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_UPDATING_SCOPE_METADATA, e);
}
}

@Override
public void deleteAPIResourceById(String apiId, Integer tenantId) throws APIResourceMgtException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ public void updateAPIResource(APIResource apiResource, List<Scope> addedScopes,
apiResourceManagementDAO.updateAPIResource(apiResource, addedScopes, removedScopes, tenantId);
}

@Override
public void updateScopeMetadata(Scope scope, APIResource apiResource, Integer tenantId)
throws APIResourceMgtException {

clearAPIResourceCache(apiResource.getIdentifier(), apiResource.getId(), tenantId);
apiResourceManagementDAO.updateScopeMetadata(scope, apiResource, tenantId);
}

@Override
public void deleteAPIResourceById(String apiId, Integer tenantId) throws APIResourceMgtException {

Expand Down
Loading

0 comments on commit 3037adb

Please sign in to comment.