Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception handling for IdentityEventException #288

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
import org.wso2.carbon.identity.scim2.common.group.SCIMGroupHandler;
import org.wso2.carbon.identity.scim2.common.internal.SCIMCommonComponentHolder;
import org.wso2.carbon.identity.scim2.common.utils.AttributeMapper;
import org.wso2.carbon.identity.scim2.common.utils.IdentityEventExceptionSettings;
import org.wso2.carbon.identity.scim2.common.utils.SCIMCommonConstants;
import org.wso2.carbon.identity.scim2.common.utils.SCIMCommonUtils;
import org.wso2.carbon.identity.scim2.common.utils.SCIMConfigProcessor;
import org.wso2.carbon.user.api.ClaimMapping;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.core.PaginatedUserStoreManager;
Expand Down Expand Up @@ -368,9 +370,16 @@ private void handleErrorsOnUserNameAndPasswordPolicy(Throwable e) throws BadRequ
if (e instanceof PolicyViolationException) {
throw new BadRequestException(e.getMessage(), ResponseCodeConstants.INVALID_VALUE);
}
if ((e instanceof IdentityEventException) && StringUtils
.equals(ERROR_CODE_PASSWORD_HISTORY_VIOLATION, ((IdentityEventException) e).getErrorCode())) {
throw new BadRequestException(e.getMessage(), ResponseCodeConstants.INVALID_VALUE);
if (e instanceof IdentityEventException) {
IdentityEventException iee = ((IdentityEventException) e);
IdentityEventExceptionSettings ieeSettings = SCIMConfigProcessor.getInstance().getIdentityEventExceptionSettings();
if (ieeSettings.getBadRequestErrorCodes().contains(iee.getErrorCode())) {
String errorMessage = e.getMessage();
if (ieeSettings.isExposeErrorCodeInMessage()) {
errorMessage = "[" + iee.getErrorCode() + "] " + errorMessage;
}
throw new BadRequestException(errorMessage, ResponseCodeConstants.INVALID_VALUE);
}
}
e = e.getCause();
i++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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.scim2.common.utils;

import java.util.ArrayList;
import java.util.List;

/**
* this class is the blue print of IdentityEventException settings used in SCIMUserManager.
*/
public class IdentityEventExceptionSettings {
private boolean exposeErrorCodeInMessage;
private List<String> badRequestErrorCodes = new ArrayList<>();

public boolean isExposeErrorCodeInMessage() {
return exposeErrorCodeInMessage;
}

public void setExposeErrorCodeInMessage(boolean exposeErrorCodeInMessage) {
this.exposeErrorCodeInMessage = exposeErrorCodeInMessage;
}

public List<String> getBadRequestErrorCodes() {
return badRequestErrorCodes;
}

public void setBadRequestErrorCodes(List<String> badRequestErrorCodes) {
this.badRequestErrorCodes = badRequestErrorCodes;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public class SCIMCommonConstants {

//config constants
public static final String CHARON_CONFIG_NAME = "charon-config.xml";
public static final String ELEMENT_NAME_AUTHENTICATION_SCHEMES = "authenticationSchemes";;
public static final String ELEMENT_NAME_AUTHENTICATION_SCHEMES = "authenticationSchemes";
public static final String ELEMENT_NAME_IEE_SETTINGS = "identityEventExceptionSettings";
public static final String ELEMENT_NAME_IEE_SETTINGS_EXPOSE_ERROR_CODE_IN_MESSAGE = "exposeErrorCodeInMessage";
public static final String ELEMENT_NAME_IEE_SETTINGS_BAD_REQUEST_ERROR_CODES = "badRequestErrorCodes";
public static final String ELEMENT_NAME_IEE_SETTINGS_BAD_REQUEST_ERROR_CODE = "badRequestErrorCode";
public static final String ELEMENT_NAME_PROPERTY = "Property";
public static final String ELEMENT_NAME_SCHEMA = "schema";
public static final String ATTRIBUTE_NAME_NAME = "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.user.core.NotImplementedException;
import org.wso2.charon3.core.exceptions.CharonException;

import javax.xml.namespace.QName;
Expand All @@ -46,9 +47,10 @@ public class SCIMConfigProcessor {
private static SCIMConfigProcessor scimConfigProcessor = new SCIMConfigProcessor();

//map to keep the properties values
Map<String, String> properties = new HashMap<String, String>();
Map<String, String> properties = new HashMap<>();
//list to keep the authentication schemas
List<AuthenticationSchema> authenticationSchemas = null;
List<AuthenticationSchema> authenticationSchemas = new ArrayList<>();
IdentityEventExceptionSettings identityEventExceptionSettings = new IdentityEventExceptionSettings();

private static final Log logger = LogFactory.getLog(SCIMConfigProcessor.class);

Expand All @@ -67,6 +69,10 @@ public List<AuthenticationSchema> getAuthenticationSchemas() {
return authenticationSchemas;
}

public IdentityEventExceptionSettings getIdentityEventExceptionSettings() {
return identityEventExceptionSettings;
}

public void buildConfigFromFile(String filePath) throws CharonException {
try {
InputStream inputStream = null;
Expand All @@ -83,19 +89,15 @@ public void buildConfigFromFile(String filePath) throws CharonException {
throw new FileNotFoundException();
}
} catch (FileNotFoundException e) {
throw new CharonException(SCIMCommonConstants.CHARON_CONFIG_NAME + "not found.");
throw new CharonException(filePath + "not found.");
} catch (XMLStreamException e) {
throw new CharonException("Error in building the configuration file: " +
SCIMCommonConstants.CHARON_CONFIG_NAME);
throw new CharonException("Error in building the configuration file: " + filePath);
} catch (IOException e) {
throw new CharonException("Error in building the configuration file: " +
SCIMCommonConstants.CHARON_CONFIG_NAME);
throw new CharonException("Error in building the configuration file: " + filePath);
}
}

private void buildConfigFromRootElement(OMElement rootElement) {


//read any properties defined.
Iterator<OMElement> propertiesIterator = rootElement.getChildrenWithName(
new QName(SCIMCommonConstants.ELEMENT_NAME_PROPERTY));
Expand All @@ -119,8 +121,13 @@ private void buildConfigFromRootElement(OMElement rootElement) {
if (authenticationSchemasIterator != null) {
authenticationSchemas = buildAuthenticationSchemasMap(authenticationSchemasIterator);
}
}

OMElement identityEventExceptionSettingsElement = rootElement.getFirstChildWithName(
new QName(SCIMCommonConstants.ELEMENT_NAME_IEE_SETTINGS));
if (identityEventExceptionSettingsElement != null) {
identityEventExceptionSettings = buildIdentityEventExceptionSettings(identityEventExceptionSettingsElement);
}
}

private List<AuthenticationSchema> buildAuthenticationSchemasMap
(Iterator<OMElement> schemasIterator) {
Expand Down Expand Up @@ -149,6 +156,43 @@ private void buildConfigFromRootElement(OMElement rootElement) {
return schemasList;
}

private IdentityEventExceptionSettings buildIdentityEventExceptionSettings(OMElement ieeSettingsElement) {
IdentityEventExceptionSettings result = new IdentityEventExceptionSettings();

// extract exposeErrorCodeInMessage
Iterator<OMElement> ieesPropertyIterator = ieeSettingsElement.getChildrenWithName(
new QName(SCIMCommonConstants.ELEMENT_NAME_PROPERTY));
if (ieesPropertyIterator != null) {
while (ieesPropertyIterator.hasNext()) {
OMElement propertyElement = ieesPropertyIterator.next();
String propertyName = propertyElement.getAttributeValue(
new QName(SCIMCommonConstants.ATTRIBUTE_NAME_NAME));
if (SCIMCommonConstants.ELEMENT_NAME_IEE_SETTINGS_EXPOSE_ERROR_CODE_IN_MESSAGE.equals(propertyName)) {
boolean exposeErrorCodeInMessage = "true".equals(propertyElement.getText());
result.setExposeErrorCodeInMessage(exposeErrorCodeInMessage);
}
}
}

// extract badRequestErrorCodes
Iterator<OMElement> ieesBadRequestErrorCodesIterator = ieeSettingsElement.getChildrenWithName(
new QName(SCIMCommonConstants.ELEMENT_NAME_IEE_SETTINGS_BAD_REQUEST_ERROR_CODES));
if (ieesBadRequestErrorCodesIterator != null && ieesBadRequestErrorCodesIterator.hasNext()) {
OMElement ieesBadRequestErrorCodesElement = ieesBadRequestErrorCodesIterator.next();
Iterator<OMElement> ieesBadRequestErrorCodeIterator = ieesBadRequestErrorCodesElement.getChildrenWithName(
new QName(SCIMCommonConstants.ELEMENT_NAME_IEE_SETTINGS_BAD_REQUEST_ERROR_CODE));
if (ieesBadRequestErrorCodeIterator != null && ieesBadRequestErrorCodeIterator.hasNext()) {
while (ieesBadRequestErrorCodeIterator.hasNext()) {
OMElement ieesBadRequestErrorCodeElement =ieesBadRequestErrorCodeIterator.next();
String errorCode = ieesBadRequestErrorCodeElement.getText();
result.getBadRequestErrorCodes().add(errorCode.trim());
}
}
}

return result;
}

public static SCIMConfigProcessor getInstance() {
return scimConfigProcessor;
}
Expand Down
Loading