Added character set filter to random code generator, e.g. for client_id.
Change-Id: Ifc1f4be39752806baaaa0be3bc69f55558360f56
diff --git a/core/Changes b/core/Changes
index c1b2d80..b599c9a 100644
--- a/core/Changes
+++ b/core/Changes
@@ -1,7 +1,7 @@
# version 0.63.2
2021-06-11
- Updated OAuth2 token length & secure random algorithm config.
-
+ - Added character set filter to random code generator, e.g. for client_id.
# version 0.63.1
2021-03-25
- Updated Koral version for InfoController.
diff --git a/core/src/main/java/de/ids_mannheim/korap/encryption/RandomCodeGenerator.java b/core/src/main/java/de/ids_mannheim/korap/encryption/RandomCodeGenerator.java
index fe23f22..ea67bd6 100644
--- a/core/src/main/java/de/ids_mannheim/korap/encryption/RandomCodeGenerator.java
+++ b/core/src/main/java/de/ids_mannheim/korap/encryption/RandomCodeGenerator.java
@@ -3,7 +3,11 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
+import java.util.Arrays;
+import java.util.List;
import java.util.UUID;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
@@ -26,6 +30,15 @@
@Component
public class RandomCodeGenerator {
+ public static String alphanumeric = "34679bdfhmnprtFGHJLMNPRT";
+ public static List<Character> charList = alphanumeric.chars()
+ .mapToObj(c -> (char) c).collect(Collectors.toList());
+
+ public final static List<String> LIMITED_CHARACTERS =
+ Arrays.asList(new String[] { "3", "4", "6", "7", "9", "b", "d", "f",
+ "h", "m", "n", "p", "r", "t", "F", "G", "H", "J", "L", "M",
+ "N", "P", "R", "T" });
+
@Autowired
public KustvaktConfiguration config;
@@ -33,15 +46,15 @@
@PostConstruct
public void init () throws NoSuchAlgorithmException {
- String algorithm = config.getSecureRandomAlgorithm();
+ String algorithm = config.getSecureRandomAlgorithm();
if (!algorithm.isEmpty()) {
- secureRandom =
- SecureRandom.getInstance(algorithm);
+ secureRandom = SecureRandom.getInstance(algorithm);
}
else {
secureRandom = new SecureRandom();
}
- System.out.println("Secure random algorithm: "+secureRandom.getAlgorithm());
+ System.out.println(
+ "Secure random algorithm: " + secureRandom.getAlgorithm());
}
public String createRandomCode (KustvaktConfiguration c)
@@ -75,4 +88,16 @@
}
}
+ public String filterRandomCode (String code) {
+ StringBuffer s = new StringBuffer();
+ for (char c : code.toCharArray()) {
+ if (!charList.contains(c)) {
+ int n = ThreadLocalRandom.current().nextInt(0,
+ charList.size());
+ c = charList.get(n);
+ }
+ s.append(c);
+ }
+ return s.toString();
+ }
}