diff --git a/server/src/main/java/access/api/InvitationController.java b/server/src/main/java/access/api/InvitationController.java index 876a2c94..8168d06d 100644 --- a/server/src/main/java/access/api/InvitationController.java +++ b/server/src/main/java/access/api/InvitationController.java @@ -113,7 +113,7 @@ public ResponseEntity> newInvitation(@Validated @RequestBod .filter(emailFormatValidator::isValid) .map(invitee -> new Invitation( intendedAuthority, - HashGenerator.generateHash(), + HashGenerator.generateRandomHash(), invitee, invitationRequest.isEnforceEmailEquality(), invitationRequest.isEduIDOnly(), diff --git a/server/src/main/java/access/config/HashGenerator.java b/server/src/main/java/access/config/HashGenerator.java index 6404cae1..9a2daf2a 100644 --- a/server/src/main/java/access/config/HashGenerator.java +++ b/server/src/main/java/access/config/HashGenerator.java @@ -1,9 +1,8 @@ package access.config; + import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.RandomStringUtils; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.security.SecureRandom; import java.util.Base64; import java.util.Random; @@ -11,15 +10,16 @@ public class HashGenerator { private static final Random secureRandom = new SecureRandom(); + public static final DigestUtils digestUtils = new DigestUtils("SHA3-256"); private HashGenerator() { } - public static String generateHash() { + public static String generateRandomHash() { byte[] aesKey = new byte[128]; secureRandom.nextBytes(aesKey); - String base64 = Base64.getEncoder().encodeToString(aesKey); - return URLEncoder.encode(base64, StandardCharsets.UTF_8).replaceAll("%", ""); + //Avoid decoding / encoding as URL parameter problems + return Base64.getUrlEncoder().withoutPadding().encodeToString(aesKey); } public static String generateToken() { @@ -27,7 +27,7 @@ public static String generateToken() { } public static String hashToken(String token) { - return new DigestUtils("SHA3-256").digestAsHex(token); + return digestUtils.digestAsHex(token); } } diff --git a/server/src/test/java/access/config/HashGeneratorTest.java b/server/src/test/java/access/config/HashGeneratorTest.java index 97f64114..45f31a99 100644 --- a/server/src/test/java/access/config/HashGeneratorTest.java +++ b/server/src/test/java/access/config/HashGeneratorTest.java @@ -2,15 +2,21 @@ import org.junit.jupiter.api.Test; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.Charset; + import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; class HashGeneratorTest { @Test - void generateHash() { - String hash = HashGenerator.generateHash(); - assertTrue(hash.length() > 172); + void generateRandomHash() { + String hash = HashGenerator.generateRandomHash(); + assertEquals(171, hash.length()); + String encoded = URLEncoder.encode(hash, Charset.defaultCharset()); + String decoded = URLDecoder.decode(encoded, Charset.defaultCharset()); + assertEquals(encoded, decoded); } @Test