-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add service layer support for the custom local auth extensions.
- Loading branch information
1 parent
4a85b28
commit 5a98250
Showing
6 changed files
with
256 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...bon/identity/application/common/exception/AuthenticatorEndpointConfigServerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.application.common.exception; | ||
|
||
/** | ||
* Exception class for user defined local authenticator endpoint configurations related exceptions. | ||
*/ | ||
public class AuthenticatorEndpointConfigServerException extends AuthenticatorMgtServerException{ | ||
|
||
public AuthenticatorEndpointConfigServerException(String message) { | ||
|
||
super(message); | ||
} | ||
|
||
public AuthenticatorEndpointConfigServerException(String errorCode, String message, Throwable throwable) { | ||
|
||
super(errorCode, message, throwable); | ||
} | ||
} |
49 changes: 0 additions & 49 deletions
49
...va/org/wso2/carbon/identity/application/common/model/VerificationAuthenticatorConfig.java
This file was deleted.
Oops, something went wrong.
197 changes: 197 additions & 0 deletions
197
.../identity/application/common/util/UserDefinedLocalAuthenticatorEndpointConfigManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/* | ||
* 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.application.common.util; | ||
|
||
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.EndpointConfig; | ||
import org.wso2.carbon.identity.application.common.exception.AuthenticatorEndpointConfigServerException; | ||
import org.wso2.carbon.identity.application.common.model.Property; | ||
import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig; | ||
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; | ||
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants; | ||
import org.wso2.carbon.identity.core.util.IdentityTenantUtil; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants | ||
.ErrorMessages.ERROR_CODE_ENDPOINT_CONFIG_MGT; | ||
|
||
/** | ||
* This class responsible for managing authenticator endpoint configurations for the user defined Local | ||
* authenticators. | ||
*/ | ||
public class UserDefinedLocalAuthenticatorEndpointConfigManager { | ||
|
||
private static final String ACTION_ID_PROPERTY = "actionId"; | ||
|
||
/** | ||
* Create a new action for given endpoint configurations of the user defined authenticator. | ||
* | ||
* @param config The Local application authenticator configuration. | ||
* @param tenantId The id of Tenant domain. | ||
* @throws AuthenticatorEndpointConfigServerException If an error occurs while adding the action. | ||
*/ | ||
public void addEndpointConfigurations(UserDefinedLocalAuthenticatorConfig config, int tenantId) | ||
throws AuthenticatorEndpointConfigServerException { | ||
|
||
try { | ||
Action action = IdpMgtServiceComponentHolder.getInstance().getActionManagementService() | ||
.addAction(Action.ActionTypes.AUTHENTICATION.getPathParam(), | ||
buildActionToCreate(config.getName(), config.getEndpointConfig().getEndpointConfig()), | ||
IdentityTenantUtil.getTenantDomain(tenantId)); | ||
Property endpointProperty = new Property(); | ||
endpointProperty.setName(ACTION_ID_PROPERTY); | ||
endpointProperty.setValue(action.getId()); | ||
config.setProperties(new Property[]{endpointProperty}); | ||
} catch (ActionMgtException e) { | ||
throw new AuthenticatorEndpointConfigServerException(ERROR_CODE_ENDPOINT_CONFIG_MGT.getCode(), | ||
"Error occurred while adding associated action for the authenticator:" + config.getName(), e); | ||
} | ||
} | ||
|
||
/** | ||
* Updated associated action for given updated endpoint configurations of the user defined authenticator. | ||
* | ||
* @param newConfig The Local application authenticator configuration to be updated. | ||
* @param oldConfig The current Local application authenticator configuration. | ||
* @param tenantId The id of Tenant domain. | ||
* @throws AuthenticatorEndpointConfigServerException If an error occurs while updating associated action. | ||
*/ | ||
public void updateEndpointConfigurations(UserDefinedLocalAuthenticatorConfig newConfig, | ||
UserDefinedLocalAuthenticatorConfig oldConfig, int tenantId) | ||
throws AuthenticatorEndpointConfigServerException { | ||
|
||
String actionId = getActionIdFromProperty(oldConfig.getProperties(), oldConfig.getName()); | ||
try { | ||
IdpMgtServiceComponentHolder.getInstance().getActionManagementService() | ||
.updateAction(Action.ActionTypes.AUTHENTICATION.getPathParam(), | ||
actionId, | ||
buildActionToUpdate(newConfig.getEndpointConfig().getEndpointConfig()), | ||
IdentityTenantUtil.getTenantDomain(tenantId)); | ||
newConfig.setProperties(oldConfig.getProperties()); | ||
} catch (ActionMgtException e) { | ||
throw new AuthenticatorEndpointConfigServerException(ERROR_CODE_ENDPOINT_CONFIG_MGT.getCode(), | ||
String.format("Error occurred while updating associated action with id %s for the authenticator %s", | ||
actionId, oldConfig.getName()), e); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve associated action of the user defined authenticator. | ||
* | ||
* @param config The Local application authenticator configuration. | ||
* @param tenantId The id of Tenant domain. | ||
* @return Local authenticator with endpoint configurations resolved. | ||
* @throws AuthenticatorEndpointConfigServerException If an error occurs retrieving updating associated action. | ||
*/ | ||
public UserDefinedLocalAuthenticatorConfig resolveEndpointConfigurations(UserDefinedLocalAuthenticatorConfig config, | ||
int tenantId) throws AuthenticatorEndpointConfigServerException { | ||
|
||
String actionId = getActionIdFromProperty(config.getProperties(), config.getName()); | ||
try { | ||
Action action = IdpMgtServiceComponentHolder.getInstance().getActionManagementService() | ||
.getActionByActionId(Action.ActionTypes.AUTHENTICATION.getPathParam(), | ||
actionId, | ||
IdentityTenantUtil.getTenantDomain(tenantId)); | ||
|
||
config.setEndpointConfig(buildUserDefinedAuthenticatorEndpointConfig(action.getEndpoint())); | ||
return config; | ||
} catch (ActionMgtException e) { | ||
throw new AuthenticatorEndpointConfigServerException(ERROR_CODE_ENDPOINT_CONFIG_MGT.getCode(), | ||
String.format("Error occurred retrieving associated action with id %s for the authenticator %s", | ||
actionId, config.getName()), e); | ||
} | ||
} | ||
|
||
private UserDefinedAuthenticatorEndpointConfig buildUserDefinedAuthenticatorEndpointConfig( | ||
EndpointConfig endpointConfig) { | ||
|
||
UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder endpointConfigBuilder = | ||
new UserDefinedAuthenticatorEndpointConfig.UserDefinedAuthenticatorEndpointConfigBuilder(); | ||
endpointConfigBuilder.uri(endpointConfig.getUri()); | ||
endpointConfigBuilder.authenticationType(endpointConfig.getAuthentication().getType().getName()); | ||
Map<String, String> propMap = new HashMap<>(); | ||
endpointConfig.getAuthentication().getProperties() | ||
.forEach(prop -> propMap.put(prop.getName(), prop.getValue())); | ||
endpointConfigBuilder.authenticationProperties(propMap); | ||
return endpointConfigBuilder.build(); | ||
} | ||
|
||
/** | ||
* Delete associated action of the user defined authenticator. | ||
* | ||
* @param config The Local application authenticator configuration. | ||
* @param tenantId The id of Tenant domain. | ||
* | ||
* @throws AuthenticatorEndpointConfigServerException If an error occurs while deleting associated action. | ||
*/ | ||
public void deleteEndpointConfigurations(UserDefinedLocalAuthenticatorConfig config, int tenantId) throws | ||
AuthenticatorEndpointConfigServerException { | ||
|
||
if (config.getDefinedByType() == AuthenticatorPropertyConstants.DefinedByType.SYSTEM) { | ||
return; | ||
} | ||
|
||
String actionId = getActionIdFromProperty(config.getProperties(), config.getName()); | ||
try { | ||
IdpMgtServiceComponentHolder.getInstance().getActionManagementService() | ||
.deleteAction(Action.ActionTypes.AUTHENTICATION.getPathParam(), | ||
actionId, | ||
IdentityTenantUtil.getTenantDomain(tenantId)); | ||
} catch (ActionMgtException e) { | ||
throw new AuthenticatorEndpointConfigServerException(ERROR_CODE_ENDPOINT_CONFIG_MGT.getCode(), | ||
String.format("Error occurred while deleting associated action with id %s for the authenticator %s", | ||
actionId, config.getName()), e); | ||
} | ||
} | ||
|
||
private Action buildActionToCreate(String authenticatorName, EndpointConfig endpointConfig) { | ||
|
||
Action.ActionRequestBuilder actionRequestBuilder = new Action.ActionRequestBuilder(); | ||
actionRequestBuilder.name(authenticatorName); | ||
actionRequestBuilder.description(String.format("This is the action associated to the user defined Local" + | ||
"authenticator %s.", authenticatorName)); | ||
actionRequestBuilder.endpoint(endpointConfig); | ||
|
||
return actionRequestBuilder.build(); | ||
} | ||
|
||
private Action buildActionToUpdate(EndpointConfig endpointConfig) { | ||
|
||
Action.ActionRequestBuilder actionRequestBuilder = new Action.ActionRequestBuilder(); | ||
actionRequestBuilder.endpoint(endpointConfig); | ||
|
||
return actionRequestBuilder.build(); | ||
} | ||
|
||
private String getActionIdFromProperty(Property[] properties, String authenticatorName) | ||
throws AuthenticatorEndpointConfigServerException { | ||
|
||
return Arrays.stream(properties) | ||
.filter(property -> ACTION_ID_PROPERTY.equals(property.getName())) | ||
.map(Property::getValue) | ||
.findFirst() | ||
.orElseThrow(() -> new AuthenticatorEndpointConfigServerException( | ||
"No action Id was found in the properties of the authenticator configurations for" + | ||
" the authenticator: " + authenticatorName)); | ||
} | ||
} |
Oops, something went wrong.