Skip to content

Commit 9a11e42

Browse files
Nanne Baarsostrya
Nanne Baars
authored andcommitted
use base-64 encoding without padding
The spec states (see https://datatracker.ietf.org/doc/html/rfc7515#appendix-C) that all trailing '=' characters should be omitted. Resolves #144 Signed-off-by: Nanne Baars <[email protected]>
1 parent b5ddf55 commit 9a11e42

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

mock/src/main/java/com/tngtech/keycloakmock/impl/handler/JwksRoute.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,30 @@ private static JsonObject toSigningKey(
4848
if (publicKey instanceof RSAPublicKey) {
4949
result.put("kty", "RSA");
5050
RSAPublicKey rsaKey = (RSAPublicKey) publicKey;
51-
result.put("n", Base64.getUrlEncoder().encodeToString(rsaKey.getModulus().toByteArray()));
5251
result.put(
53-
"e", Base64.getUrlEncoder().encodeToString(rsaKey.getPublicExponent().toByteArray()));
52+
"n",
53+
Base64.getUrlEncoder()
54+
.withoutPadding()
55+
.encodeToString(rsaKey.getModulus().toByteArray()));
56+
result.put(
57+
"e",
58+
Base64.getUrlEncoder()
59+
.withoutPadding()
60+
.encodeToString(rsaKey.getPublicExponent().toByteArray()));
5461
} else if (publicKey instanceof ECPublicKey) {
5562
result.put("kty", "EC");
5663
ECPublicKey ecKey = (ECPublicKey) publicKey;
5764
result.put("crv", "P-" + ecKey.getParams().getOrder().bitLength());
5865
result.put(
59-
"x", Base64.getUrlEncoder().encodeToString(ecKey.getW().getAffineX().toByteArray()));
66+
"x",
67+
Base64.getUrlEncoder()
68+
.withoutPadding()
69+
.encodeToString(ecKey.getW().getAffineX().toByteArray()));
6070
result.put(
61-
"y", Base64.getUrlEncoder().encodeToString(ecKey.getW().getAffineY().toByteArray()));
71+
"y",
72+
Base64.getUrlEncoder()
73+
.withoutPadding()
74+
.encodeToString(ecKey.getW().getAffineY().toByteArray()));
6275
} else {
6376
throw new IllegalStateException("Invalid public key type found");
6477
}

mock/src/test/java/com/tngtech/keycloakmock/impl/handler/JwksRouteTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void rsaKeyIsCorrectlyExported() throws Exception {
3333
.hasFieldOrPropertyWithValue("use", "sig")
3434
.hasFieldOrPropertyWithValue(
3535
"n",
36-
"AKzaf4nijuwtAn9ieZaz-iGXBp1pFm6dJMAxRO6ax2CV9cBFeThxrKJNFmDY7j7gKRnrgWxvgJKSd3hAm_CGmXHbTM8cPi_gsof-CsOohv7LH0UYbr0UpCIJncTiRrKQto7q_NOO4Jh1EBSLMPX7MzttEhh35Ue9txHLq3zkdkR6BR6nGS7QxEg7FzYzA4IooV59OPr-TvlDxbEpwc1wkRZDGavo-WjngAt7m_BEQtHnav3whitbrMmi_1tWY8cQbO9D4FuQTM7yvACLSv94G2TCvsjm_gGJmOJyRBkI1r-uEIfhz9-VIKlswqapKSul-Hoxv5NycucRa4xi4N39dfM=")
36+
"AKzaf4nijuwtAn9ieZaz-iGXBp1pFm6dJMAxRO6ax2CV9cBFeThxrKJNFmDY7j7gKRnrgWxvgJKSd3hAm_CGmXHbTM8cPi_gsof-CsOohv7LH0UYbr0UpCIJncTiRrKQto7q_NOO4Jh1EBSLMPX7MzttEhh35Ue9txHLq3zkdkR6BR6nGS7QxEg7FzYzA4IooV59OPr-TvlDxbEpwc1wkRZDGavo-WjngAt7m_BEQtHnav3whitbrMmi_1tWY8cQbO9D4FuQTM7yvACLSv94G2TCvsjm_gGJmOJyRBkI1r-uEIfhz9-VIKlswqapKSul-Hoxv5NycucRa4xi4N39dfM")
3737
.hasFieldOrPropertyWithValue("e", "AQAB");
3838
}
3939

0 commit comments

Comments
 (0)