Skip to content

Commit

Permalink
use base-64 encoding without padding
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Nanne Baars authored and ostrya committed Jul 4, 2023
1 parent b5ddf55 commit 9a11e42
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,30 @@ private static JsonObject toSigningKey(
if (publicKey instanceof RSAPublicKey) {
result.put("kty", "RSA");
RSAPublicKey rsaKey = (RSAPublicKey) publicKey;
result.put("n", Base64.getUrlEncoder().encodeToString(rsaKey.getModulus().toByteArray()));
result.put(
"e", Base64.getUrlEncoder().encodeToString(rsaKey.getPublicExponent().toByteArray()));
"n",
Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(rsaKey.getModulus().toByteArray()));
result.put(
"e",
Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(rsaKey.getPublicExponent().toByteArray()));
} else if (publicKey instanceof ECPublicKey) {
result.put("kty", "EC");
ECPublicKey ecKey = (ECPublicKey) publicKey;
result.put("crv", "P-" + ecKey.getParams().getOrder().bitLength());
result.put(
"x", Base64.getUrlEncoder().encodeToString(ecKey.getW().getAffineX().toByteArray()));
"x",
Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(ecKey.getW().getAffineX().toByteArray()));
result.put(
"y", Base64.getUrlEncoder().encodeToString(ecKey.getW().getAffineY().toByteArray()));
"y",
Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(ecKey.getW().getAffineY().toByteArray()));
} else {
throw new IllegalStateException("Invalid public key type found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void rsaKeyIsCorrectlyExported() throws Exception {
.hasFieldOrPropertyWithValue("use", "sig")
.hasFieldOrPropertyWithValue(
"n",
"AKzaf4nijuwtAn9ieZaz-iGXBp1pFm6dJMAxRO6ax2CV9cBFeThxrKJNFmDY7j7gKRnrgWxvgJKSd3hAm_CGmXHbTM8cPi_gsof-CsOohv7LH0UYbr0UpCIJncTiRrKQto7q_NOO4Jh1EBSLMPX7MzttEhh35Ue9txHLq3zkdkR6BR6nGS7QxEg7FzYzA4IooV59OPr-TvlDxbEpwc1wkRZDGavo-WjngAt7m_BEQtHnav3whitbrMmi_1tWY8cQbO9D4FuQTM7yvACLSv94G2TCvsjm_gGJmOJyRBkI1r-uEIfhz9-VIKlswqapKSul-Hoxv5NycucRa4xi4N39dfM=")
"AKzaf4nijuwtAn9ieZaz-iGXBp1pFm6dJMAxRO6ax2CV9cBFeThxrKJNFmDY7j7gKRnrgWxvgJKSd3hAm_CGmXHbTM8cPi_gsof-CsOohv7LH0UYbr0UpCIJncTiRrKQto7q_NOO4Jh1EBSLMPX7MzttEhh35Ue9txHLq3zkdkR6BR6nGS7QxEg7FzYzA4IooV59OPr-TvlDxbEpwc1wkRZDGavo-WjngAt7m_BEQtHnav3whitbrMmi_1tWY8cQbO9D4FuQTM7yvACLSv94G2TCvsjm_gGJmOJyRBkI1r-uEIfhz9-VIKlswqapKSul-Hoxv5NycucRa4xi4N39dfM")
.hasFieldOrPropertyWithValue("e", "AQAB");
}

Expand Down

0 comments on commit 9a11e42

Please sign in to comment.