Skip to content

Commit

Permalink
Merge pull request #760 from Thisara-Welmilla/improve-error-code
Browse files Browse the repository at this point in the history
Improve error codes for user defined local authenticator mgt
  • Loading branch information
Thisara-Welmilla authored Jan 2, 2025
2 parents 8376330 + 3c7923b commit d7eb0bb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ public enum ErrorMessage {
"Filter needs to be in the format <attribute>+<operation>+<value>. Eg: tag+eq+2FA"),
ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE("60002", "Unsupported filter attribute.",
"The filter attribute '%s' is not supported."),
ERROR_CODE_INVALID_ENDPOINT_CONFIG("60003", "Invalid endpoint configuration provided.",
"Invalid endpoint configuration is provided for the authenticator %s."),
ERROR_CODE_ERROR_AUTHENTICATOR_NOT_FOUND("60004", "Authenticator not found.",
"Authenticator not found by the given name: %s."),

ERROR_CODE_ERROR_LISTING_AUTHENTICATORS("65001", "Unable to list the existing authenticators.",
"Server encountered an error while listing the authenticators."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.model.ExpressionNode;
Expand Down Expand Up @@ -265,8 +266,10 @@ public Authenticator updateUserDefinedLocalAuthenticator(
LocalAuthenticatorConfig existingAuthenticator = getApplicationAuthenticatorService()
.getLocalAuthenticatorByName(authenticatorName, tenantDomain);
if (existingAuthenticator == null) {
throw handleException(Response.Status.NOT_FOUND,
Constants.ErrorMessage.ERROR_CODE_ERROR_AUTHENTICATOR_NOT_FOUND, authenticatorName);
AuthenticatorMgtError error = AuthenticatorMgtError.ERROR_CODE_ERROR_AUTHENTICATOR_NOT_FOUND;
throw handleAuthenticatorException(new AuthenticatorMgtClientException(error.getCode(),
error.getMessage(), String.format(error.getMessage(), authenticatorName)),
Response.Status.NOT_FOUND);
}
UserDefinedLocalAuthenticatorConfig updatedConfig = getApplicationAuthenticatorService()
.updateUserDefinedLocalAuthenticator(
Expand Down Expand Up @@ -1008,13 +1011,16 @@ private APIError handleIdPException(IdentityProviderManagementException e,
* @param e IdentityProviderManagementException.
* @return APIError.
*/
private APIError handleAuthenticatorException(AuthenticatorMgtException e) {
private APIError handleAuthenticatorException(AuthenticatorMgtException e, Response.Status... responseStatus) {

ErrorResponse.Builder errorResponseBuilder = new ErrorResponse.Builder()
.withCode(e.getErrorCode())
.withMessage(e.getMessage())
.withDescription(e.getDescription());
Response.Status status;
Response.Status status = null;
if (ArrayUtils.isNotEmpty(responseStatus)) {
status = responseStatus[0];
}
ErrorResponse errorResponse;

if (e instanceof AuthenticatorMgtClientException) {
Expand All @@ -1027,7 +1033,9 @@ private APIError handleAuthenticatorException(AuthenticatorMgtException e) {
errorResponse.setCode(errorCode);
}
errorResponse.setDescription(e.getDescription());
status = Response.Status.BAD_REQUEST;
if (status == null) {
status = Response.Status.BAD_REQUEST;
}
} else if (e instanceof AuthenticatorMgtServerException) {
errorResponse = errorResponseBuilder.build(log, e, e.getMessage());
if (e.getErrorCode() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package org.wso2.carbon.identity.api.server.authenticators.v1.impl;

import org.wso2.carbon.identity.api.server.authenticators.common.Constants;
import org.wso2.carbon.identity.api.server.authenticators.v1.model.Authenticator;
import org.wso2.carbon.identity.api.server.authenticators.v1.model.Endpoint;
import org.wso2.carbon.identity.api.server.authenticators.v1.model.UserDefinedLocalAuthenticatorCreation;
Expand All @@ -28,6 +27,7 @@
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedAuthenticatorEndpointConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants;

import java.util.Arrays;
Expand All @@ -36,7 +36,6 @@
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.api.server.authenticators.common.Constants.CONFIGS_AUTHENTICATOR_PATH_COMPONENT;
import static org.wso2.carbon.identity.api.server.authenticators.common.Constants.ErrorMessage.ERROR_CODE_INVALID_ENDPOINT_CONFIG;
import static org.wso2.carbon.identity.api.server.common.Constants.V1_API_PATH_COMPONENT;
import static org.wso2.carbon.identity.api.server.common.Util.base64URLEncode;

Expand Down Expand Up @@ -128,7 +127,7 @@ private static UserDefinedAuthenticatorEndpointConfig buildEndpointConfig(Endpoi
Map.Entry::getKey, entry -> entry.getValue().toString())));
return endpointConfigBuilder.build();
} catch (NoSuchElementException | IllegalArgumentException e) {
Constants.ErrorMessage error = ERROR_CODE_INVALID_ENDPOINT_CONFIG;
AuthenticatorMgtError error = AuthenticatorMgtError.ERROR_CODE_INVALID_ENDPOINT_CONFIG;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), e.getMessage());
}
}
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@
<maven.buildnumber.plugin.version>1.4</maven.buildnumber.plugin.version>
<org.apache.felix.annotations.version>1.2.4</org.apache.felix.annotations.version>
<identity.governance.version>1.11.21</identity.governance.version>
<carbon.identity.framework.version>7.7.41</carbon.identity.framework.version>
<carbon.identity.framework.version>7.7.66</carbon.identity.framework.version>
<maven.findbugsplugin.version>3.0.5</maven.findbugsplugin.version>
<findsecbugs-plugin.version>1.12.0</findsecbugs-plugin.version>
<maven.checkstyleplugin.excludes>**/gen/**/*</maven.checkstyleplugin.excludes>
Expand Down

0 comments on commit d7eb0bb

Please sign in to comment.