arctic-sea
arctic-sea copied to clipboard
Add random setting
Add a new setting type that generates a random string (or number?) at every time the service starts. Can be used for security tokens etc.
See https://github.com/52North/iceland/issues/32
How about using UUID
?
This is more like an extension of a string setting but without a configurable default value. Instead the default value is generated every time the definition is created. You could test something like this:
public class RandomSettingDefinition
extends StringSettingDefinition implements Constructable {
private static final String DEFAULT_ALLOWED_CHARACTERS =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFHIJKLMNOPQRSTUVWXYZ"
private boolean secure = true;
private int length = 20;
private int[] allowedCharacters = DEFAULT_ALLOWED_CHARACTERS.codePoints().toArray();
public void setSecure(boolean secure) {
this.secure = secure;
}
public boolean isSecure() {
return this.secure;
}
public void setAllowedCharacters(String allowedCharacters) {
Preconditions.checkArgument(allowedCharacters.length() > 0);
this.allowedCharacters = allowedCharacters.codePoints().distinct().toArray();
}
public String getAllowedCharacters() {
return Arrays.stream(this.allowedCharacters)
.collect(StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append)
.toString();
}
public void setLength(int length) {
Preconditions.checkArgument(length > 0);
this.length = length;
}
public int getLength() {
return this.length;
}
@Override
public void init() {
String value = (this.secure ? new SecureRandom() : new Random())
.ints(this.length, 0, this.allowedCharacters.length)
.map(idx -> this.allowedCharacters[idx])
.collect(StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append)
.toString();
setDefaultValue(value);
}
}
Is this issue still open?
Yes, the issue is still open.