Skip to content

Commit

Permalink
Improve error codes for user defined local authenticator mgt.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Dec 17, 2024
1 parent aaa300d commit 5781e57
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 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 @@ -259,8 +260,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 @@ -993,13 +996,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 errorResponse = new ErrorResponse.Builder()
.withCode(e.getErrorCode())
.withMessage(e.getMessage())
.withDescription(e.getDescription()).build();
Response.Status status;
Response.Status status = null;
if (responseStatus != null && responseStatus[0] != null) {
status = responseStatus[0];
}

if (e instanceof AuthenticatorMgtClientException) {
if (e.getErrorCode() != null) {
Expand All @@ -1010,7 +1016,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) {
if (e.getErrorCode() != null) {
String errorCode = e.getErrorCode();
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

0 comments on commit 5781e57

Please sign in to comment.