Add initialize HashProviderFactory and validate the parameter behaviours.
@@ -3,12 +3,16 @@ package org.wso2.carbon.identity.user.store.configuration.listener;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
+import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.user.store.configuration.dto.PropertyDTO;
import org.wso2.carbon.identity.user.store.configuration.dto.UserStoreDTO;
import org.wso2.carbon.identity.user.store.configuration.internal.UserStoreConfigListenersHolder;
import org.wso2.carbon.user.api.UserStoreException;
+import org.wso2.carbon.user.core.exceptions.HashProviderException;
import org.wso2.carbon.user.core.hash.HashProviderFactory;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Set;
/**
@@ -18,6 +22,7 @@ public class UserStoreHashProviderConfigListenerImpl extends AbstractUserStoreCo
public static final String DIGEST_FUNCTION = "PasswordDigest";
public static final String HASH_PROVIDER_PARAMS_JSON = "Hash.Algorithm.Properties";
+ private JsonObject hashPropertyJSON;
@Override
public void onUserStorePreUpdate(int tenantId, UserStoreDTO userStoreDTO, boolean isStateChange) throws
@@ -51,11 +56,20 @@ public class UserStoreHashProviderConfigListenerImpl extends AbstractUserStoreCo
hashProviderParamsJSON = propertyDTO.getValue();
}
}
+ if (StringUtils.isNotBlank(hashProviderParamsJSON)) {
HashProviderFactory hashProviderFactory = UserStoreConfigListenersHolder.getInstance().
getHashProviderFactory(digestFunction);
if (hashProviderFactory != null) {
Set<String> hashProviderMetaProperties = hashProviderFactory.getHashProviderConfigProperties();
validateParams(hashProviderParamsJSON, hashProviderMetaProperties);
+ Map<String, Object> hashProviderPropertiesMap =
+ getHashProviderInitConfigs(hashProviderParamsJSON);
+ try {
+ hashProviderFactory.getHashProvider(hashProviderPropertiesMap);
+ } catch (HashProviderException e) {
+ throw new UserStoreException("Error occurred while initializing the hashProvider.", e);
+ }
+ }
}
}
@@ -71,8 +85,7 @@ public class UserStoreHashProviderConfigListenerImpl extends AbstractUserStoreCo
try {
Gson gson = new Gson();
- JsonObject hashPropertyJSON = gson.fromJson(hashProviderParamsJSON, JsonObject.class);
- if (hashPropertyJSON != null) {
+ hashPropertyJSON = gson.fromJson(hashProviderParamsJSON, JsonObject.class);
Set<String> hashPropertyJSONKey = hashPropertyJSON.keySet();
for (String hashProperty : hashPropertyJSONKey) {
if (!hashProviderConfigProperties.contains(hashProperty)) {
@@ -80,9 +93,26 @@ public class UserStoreHashProviderConfigListenerImpl extends AbstractUserStoreCo
" is not a configuration property which needs to be configured.");
}
}
- }
} catch (JsonSyntaxException e) {
throw new UserStoreException("User store hashing configuration should be a proper JSON format", e);
}
}
+
+ /**
+ * Get map of user store properties related to the HashProviderFactory.
+ *
+ * @param hashingAlgorithmProperties The hashingAlgorithmProperties.
+ * @return The map of user store properties related to the HashProviderFactory.
+ */
+ private Map<String, Object> getHashProviderInitConfigs(String hashingAlgorithmProperties) {
+
+ Map<String, Object> hashProviderInitConfigsMap = new HashMap<>();
+ if (StringUtils.isNotBlank(hashingAlgorithmProperties)) {
+ Set<String> hashPropertyJSONKey = hashPropertyJSON.keySet();
+ for (String hashProperty : hashPropertyJSONKey) {
+ hashProviderInitConfigsMap.put(hashProperty, hashPropertyJSON.get(hashProperty).getAsString());
+ }
+ }
+ return hashProviderInitConfigsMap;
+ }
}
