Skip to content

Commit

Permalink
add settings for IdentityEventException management
Browse files Browse the repository at this point in the history
  • Loading branch information
astik committed Sep 16, 2020
1 parent 239a6e1 commit 418ea07
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 12 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
import java.util.List;
import java.util.Map;

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

public class SCIMConfigProcessorTest {
private SCIMConfigProcessor scimConfigProcessor;
private AuthenticationSchema authenticationSchema;
private IdentityEventExceptionSettings identityEventExceptionSettings;

@DataProvider(name = "propertyProvider")
public static Object[][] propertyProvider() {
Expand Down Expand Up @@ -70,6 +73,11 @@ public void setUp() throws Exception {
authenticationSchema.setPrimary("true");
scimConfigProcessor.authenticationSchemas = new ArrayList<>();
scimConfigProcessor.authenticationSchemas.add(authenticationSchema);

identityEventExceptionSettings = scimConfigProcessor.getIdentityEventExceptionSettings();
identityEventExceptionSettings.setExposeErrorCodeInMessage(true);
identityEventExceptionSettings.getBadRequestErrorCodes().add("FOO");
identityEventExceptionSettings.getBadRequestErrorCodes().add("BAR");
}

@Test
Expand Down Expand Up @@ -102,6 +110,15 @@ public void testGetAuthenticationSchemas() throws Exception {
}
}

@Test
public void testGetIdentityEventExceptionSettings() throws Exception {
IdentityEventExceptionSettings ieeSettings = scimConfigProcessor.getIdentityEventExceptionSettings();
assertTrue(ieeSettings.isExposeErrorCodeInMessage());
assertEquals(ieeSettings.getBadRequestErrorCodes().size(), 2);
assertEquals(ieeSettings.getBadRequestErrorCodes().get(0), identityEventExceptionSettings.getBadRequestErrorCodes().get(0));
assertEquals(ieeSettings.getBadRequestErrorCodes().get(1), identityEventExceptionSettings.getBadRequestErrorCodes().get(1));
}

@Test(dataProvider = "filePathProvider", expectedExceptions = CharonException.class)
public void testBuildConfigFromFile(String filePath) throws Exception {
scimConfigProcessor.buildConfigFromFile(filePath);
Expand All @@ -113,11 +130,15 @@ public void testBuildConfigFromFileHappy() throws Exception {
"charon-config-test.xml").toString();
scimConfigProcessor.buildConfigFromFile(filePath);

IdentityEventExceptionSettings ieeSettings = scimConfigProcessor.getIdentityEventExceptionSettings();
assertFalse(ieeSettings.isExposeErrorCodeInMessage());
assertEquals(ieeSettings.getBadRequestErrorCodes().size(), 1);
assertEquals(ieeSettings.getBadRequestErrorCodes().get(0), "22001");
}

@Test
public void testGetInstance() throws Exception {
SCIMConfigProcessor scimConfigProcessor1 = scimConfigProcessor.getInstance();
SCIMConfigProcessor scimConfigProcessor1 = SCIMConfigProcessor.getInstance();
assertNotNull(scimConfigProcessor1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@
<Property name="primary">false</Property>
</schema>
</authenticationSchemes>
<identityEventExceptionSettings>
<Property name="exposeErrorCodeInMessage">false</Property>
<badRequestErrorCodes>
<badRequestErrorCode>
<!-- ERROR_CODE_PASSWORD_HISTORY_VIOLATION -->
22001
</badRequestErrorCode>
</badRequestErrorCodes>
</identityEventExceptionSettings>
</provisioning-config>

0 comments on commit 418ea07

Please sign in to comment.