8000 Create client passwords calculating the entropy size for JWT with cli… by rmartinc · Pull Request #39637 · keycloak/keycloak · GitHub
[go: up one dir, main page]

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
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public String randomString(int length, char[] symbols) {

return new String(buf);
}

public byte[] randomBytes() {
return randomBytes(SECRET_LENGTH_256_BITS);
}
Expand All @@ -70,4 +71,28 @@ public byte[] randomBytes(int length) {
return buf;
}

/**
* Returns the equivalent length for a destination alphabet to have the same
* entropy bits than a byte array random generated.
*
* @param byteLengthEntropy The desired entropy in bytes
* @param dstAlphabetLeng The length of the destination alphabet
* @return The equivalent length in destination alphabet to have the same entropy bits
*/
public static int equivalentEntropySize(int byteLengthEntropy, int dstAlphabetLeng) {
return equivalentEntropySize(byteLengthEntropy, 256, dstAlphabetLeng);
}

/**
* Returns the equivalent length for a destination alphabet to have the same
* entropy bits than another source alphabet.
*
* @param length The length of the string encoded in source alphabet
* @param srcAlphabetLength The length of the source alphabet
* @param dstAlphabetLeng The length of the destination alphabet
* @return The equivalent length (same entropy) in destination alphabet for a string of length in source alphabet
*/
public static int equivalentEntropySize(int length, int srcAlphabetLength, int dstAlphabetLeng) {
return (int) Math.ceil(length * ((Math.log(srcAlphabetLength)) / (Math.log(dstAlphabetLeng))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,12 @@ public static int getSecretLengthByAuthenticationType(String clientAuthenticator
if (clientAuthenticatorType != null)
switch (clientAuthenticatorType) {
case AUTH_TYPE_CLIENT_SECRET_JWT: {
if (Algorithm.HS384.equals(signingAlg)) return SecretGenerator.SECRET_LENGTH_384_BITS;
if (Algorithm.HS512.equals(signingAlg)) return SecretGenerator.SECRET_LENGTH_512_BITS;
if (Algorithm.HS384.equals(signingAlg))
return SecretGenerator.equivalentEntropySize(SecretGenerator.SECRET_LENGTH_384_BITS, SecretGenerator.ALPHANUM.length);
else if (Algorithm.HS512.equals(signingAlg))
return SecretGenerator.equivalentEntropySize(SecretGenerator.SECRET_LENGTH_512_BITS, SecretGenerator.ALPHANUM.length);
else
return SecretGenerator.equivalentEntropySize(SecretGenerator.SECRET_LENGTH_256_BITS, SecretGenerator.ALPHANUM.length);
}
}
return SecretGenerator.SECRET_LENGTH_256_BITS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,16 @@ private void processAuthenticateWithAlgorithm(String algorithm, Integer secretLe
configureDefaultProfileAndPolicy();

String firstSecret = clientResource.generateNewSecret().getValue(); //clientResource.getSecret().getValue();
assertThat(firstSecret.length(),is(secretLength));
assertThat(firstSecret.length(), is(SecretGenerator.equivalentEntropySize(secretLength, SecretGenerator.ALPHANUM.length)));

//generate new secret, rotate the secret
String newSecret = clientResource.generateNewSecret().getValue();
assertThat(firstSecret, not(equalTo(newSecret)));
assertThat(newSecret.length(),is(secretLength));
assertThat(newSecret.length(), is(SecretGenerator.equivalentEntropySize(secretLength, SecretGenerator.ALPHANUM.length)));

oauth.clientId("jwt-client");
oauth.doLogin("test-user@localhost", "password");
EventRepresentation loginEvent = events.expectLogin().client("jwt-client").assertEvent();
events.expectLogin().client("jwt-client").assertEvent();
String code = oauth.parseLoginResponse().getCode();
AccessTokenResponse response = doAccessTokenRequest(code, getClientSignedJWT(firstSecret, 20, algorithm));
assertThat(response.getStatusCode(), is(HttpStatus.SC_OK));
Expand Down
0