KEYCLOAK-13983 Include algorithm parameters

Lazy initialization of additional parameters
This commit is contained in:
Elisabeth Schulz 2020-08-07 11:27:10 +02:00 committed by Marek Posolda
parent 4c4750f1d9
commit 73564c5815
2 changed files with 18 additions and 11 deletions

View file

@ -11,7 +11,8 @@ import java.util.Map;
public class PasswordCredentialData { public class PasswordCredentialData {
private final int hashIterations; private final int hashIterations;
private final String algorithm; private final String algorithm;
private final MultivaluedHashMap<String, String> additionalParameters;
private MultivaluedHashMap<String, String> additionalParameters;
/** /**
* Creator for standard algorithms (no algorithm tuning beyond hash iterations) * Creator for standard algorithms (no algorithm tuning beyond hash iterations)
@ -19,7 +20,7 @@ public class PasswordCredentialData {
* @param algorithm algorithm id * @param algorithm algorithm id
*/ */
public PasswordCredentialData(int hashIterations, String algorithm) { public PasswordCredentialData(int hashIterations, String algorithm) {
this(hashIterations, algorithm, Collections.emptyMap()); this(hashIterations, algorithm, null);
} }
/** /**
@ -32,7 +33,7 @@ public class PasswordCredentialData {
public PasswordCredentialData(@JsonProperty("hashIterations") int hashIterations, @JsonProperty("algorithm") String algorithm, @JsonProperty("algorithmData") Map<String, List<String>> additionalParameters) { public PasswordCredentialData(@JsonProperty("hashIterations") int hashIterations, @JsonProperty("algorithm") String algorithm, @JsonProperty("algorithmData") Map<String, List<String>> additionalParameters) {
this.hashIterations = hashIterations; this.hashIterations = hashIterations;
this.algorithm = algorithm; this.algorithm = algorithm;
this.additionalParameters = new MultivaluedHashMap<>(additionalParameters == null ? Collections.emptyMap() : additionalParameters); this.additionalParameters = additionalParameters != null ? new MultivaluedHashMap<>(additionalParameters) : null;
} }
@ -51,6 +52,9 @@ public class PasswordCredentialData {
* @return algorithm data * @return algorithm data
*/ */
public MultivaluedHashMap<String, String> getAdditionalParameters() { public MultivaluedHashMap<String, String> getAdditionalParameters() {
if (additionalParameters == null) {
additionalParameters = new MultivaluedHashMap<>();
}
return additionalParameters; return additionalParameters;
} }
} }

View file

@ -1,23 +1,23 @@
package org.keycloak.models.credential.dto; package org.keycloak.models.credential.dto;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.common.util.Base64; import org.keycloak.common.util.Base64;
import org.keycloak.common.util.MultivaluedHashMap; import org.keycloak.common.util.MultivaluedHashMap;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class PasswordSecretData { public class PasswordSecretData {
public static final Logger logger = Logger.getLogger(PasswordSecretData.class); public static final Logger logger = Logger.getLogger(PasswordSecretData.class);
private final String value; private final String value;
private final byte[] salt; private final byte[] salt;
private final MultivaluedHashMap<String, String> additionalParameters;
private MultivaluedHashMap<String, String> additionalParameters;
/** /**
* Creator with the option to provide customized secret data (multiple salt values, chiefly) * Creator with the option to provide customized secret data (multiple salt values, chiefly)
@ -28,7 +28,7 @@ public class PasswordSecretData {
*/ */
@JsonCreator @JsonCreator
public PasswordSecretData(@JsonProperty("value") String value, @JsonProperty("salt") String salt, @JsonProperty("algorithmData") Map<String, List<String>> additionalParameters) throws IOException { public PasswordSecretData(@JsonProperty("value") String value, @JsonProperty("salt") String salt, @JsonProperty("algorithmData") Map<String, List<String>> additionalParameters) throws IOException {
this.additionalParameters = new MultivaluedHashMap<>(additionalParameters == null ? Collections.emptyMap() : additionalParameters); this.additionalParameters = additionalParameters != null ? new MultivaluedHashMap<>( additionalParameters) : null;
if (salt == null || "__SALT__".equals(salt)) { if (salt == null || "__SALT__".equals(salt)) {
this.value = value; this.value = value;
@ -48,7 +48,7 @@ public class PasswordSecretData {
public PasswordSecretData(String value, byte[] salt) { public PasswordSecretData(String value, byte[] salt) {
this.value = value; this.value = value;
this.salt = salt; this.salt = salt;
this.additionalParameters = new MultivaluedHashMap<>(); this.additionalParameters = null;
} }
public String getValue() { public String getValue() {
@ -60,6 +60,9 @@ public class PasswordSecretData {
} }
public MultivaluedHashMap<String, String> getAdditionalParameters() { public MultivaluedHashMap<String, String> getAdditionalParameters() {
if (additionalParameters == null) {
additionalParameters = new MultivaluedHashMap<>();
}
return additionalParameters; return additionalParameters;
} }
} }