Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion openid/src/main/java/com/inrupt/client/openid/PKCE.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
*/
public final class PKCE {

private static final BigInteger PADDING = BigInteger.valueOf(2).pow(256);

/**
* Create a PKCE challenge value using the S256 algorithm.
*
Expand Down Expand Up @@ -73,7 +75,7 @@ static String createChallenge(final String verifier, final String alg) {
* @return the Base64URL-encoded verifier
*/
static String createVerifier() {
final byte[] rand = new BigInteger(32 * 8, new SecureRandom()).toByteArray();
final byte[] rand = PADDING.add(new BigInteger(32 * 8, new SecureRandom())).toByteArray();
return Base64.getUrlEncoder().withoutPadding().encodeToString(rand);
}

Expand Down
13 changes: 6 additions & 7 deletions openid/src/test/java/com/inrupt/client/openid/PKCETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
package com.inrupt.client.openid;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -30,16 +29,16 @@ class PKCETest {

@Test
void createChallengeTest() {
assertTrue(PKCE.createChallenge("🐶🐶🐶", "SHA-256").getBytes(UTF_8).length >= 43);
assertTrue(PKCE.createChallenge("🐶🐶🐶", "SHA-256").getBytes(UTF_8).length <= 128);
assertTrue(PKCE.createChallenge("", "SHA-256").getBytes(UTF_8).length >= 43);
assertTrue(PKCE.createChallenge("", "SHA-256").getBytes(UTF_8).length <= 128);
assertTrue(PKCE.createChallenge("🐶🐶🐶", "SHA-256").length() >= 43);
Comment on lines -36 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own understanding, was the .getBytes(UTF_8) useful in the first place?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the getBytes test was unnecessary. The spec requires checking the character length of the resulting string, not the size of the byte array. Admittedly, for ASCII strings, the two will be equivalent, but it is unnecessary

assertTrue(PKCE.createChallenge("🐶🐶🐶", "SHA-256").length() <= 128);
assertTrue(PKCE.createChallenge("", "SHA-256").length() >= 43);
assertTrue(PKCE.createChallenge("", "SHA-256").length() <= 128);
assertThrows(NullPointerException.class, () -> PKCE.createChallenge(null, "SHA-256"));
}

@Test
void createVerifierTest() {
assertTrue(PKCE.createVerifier().getBytes(UTF_8).length >= 43);
assertTrue(PKCE.createVerifier().getBytes(UTF_8).length <= 128);
assertTrue(PKCE.createVerifier().length() >= 43);
assertTrue(PKCE.createVerifier().length() <= 128);
}
}