8000 Improve metadata for Recovery Codes by rmartinc · Pull Request #39311 · keycloak/keycloak · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve metadata for Recovery Codes #39311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
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 @@ -11,25 +11,27 @@ import {
} from "@patternfly/react-table";

type CredentialDataDialogProps = {
title: string;
credentialData: [string, string][];
onClose: () => void;
};

export const CredentialDataDialog = ({
title,
credentialData,
onClose,
}: CredentialDataDialogProps) => {
const { t } = useTranslation();
return (
<Modal
variant={ModalVariant.medium}
title={t("passwordDataTitle")}
title={title}
data-testid="passwordDataDialog"
isOpen
>
>
<Table
aria-label={t("passwordDataTitle")}
aria-label={title}
data-testid="password-data-dialog"
variant={TableVariant.compact}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const CredentialRow = ({
<>
{showData && Object.keys(credential).length !== 0 && (
<CredentialDataDialog
title={credential.userLabel || t("passwordDataTitle")}
credentialData={rows}
onClose={() => {
toggleShow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static RecoveryAuthnCodesCredentialModel createFromValues(List<String> or
Base64.encodeBytes(RecoveryAuthnCodesUtils.hashRawCode(originalGeneratedCodes.get(i)))))
.collect(Collectors.toList());
secretData = new RecoveryAuthnCodesSecretData(recoveryCodes);
credentialData = new RecoveryAuthnCodesCredentialData(RecoveryAuthnCodesUtils.NUM_HASH_ITERATIONS,
credentialData = new RecoveryAuthnCodesCredentialData(null,
RecoveryAuthnCodesUtils.NOM_ALGORITHM_TO_HASH, recoveryCodes.size(), recoveryCodes.size());
model = new RecoveryAuthnCodesCredentialModel(credentialData, secretData);
model.setCredentialData(JsonSerialization.writeValueAsString(credentialData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

public class RecoveryAuthnCodesCredentialData {

private final int hashIterations;
private final Integer hashIterations;
private final String algorithm;

private int totalCodes;
private int remainingCodes;

@JsonCreator
public RecoveryAuthnCodesCredentialData(@JsonProperty("hashIterations") int hashIterations,
public RecoveryAuthnCodesCredentialData(@JsonProperty("hashIterations") Integer hashIterations,
@JsonProperty("algorithm") String algorithm, @JsonProperty("remaining") int remainingCodes,
@JsonProperty("total") int totalCodes) {
this.hashIterations = hashIterations;
Expand All @@ -21,7 +21,7 @@ public RecoveryAuthnCodesCredentialData(@JsonProperty("hashIterations") int hash
this.totalCodes = totalCodes;
}

public int getHashIterations() {
public Integer getHashIterations() {
return hashIterations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ public class RecoveryAuthnCodesUtils {
private static final int CODE_LENGTH = 12;
public static final char[] UPPERNUM = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789".toCharArray();
private static final SecretGenerator SECRET_GENERATOR = SecretGenerator.getInstance();
public static final String NOM_ALGORITHM_TO_HASH = Algorithm.RS512;
public static final int NUM_HASH_ITERATIONS = 1;
public static final String NOM_ALGORITHM_TO_HASH = JavaAlgorithm.SHA512;
public static final String RECOVERY_AUTHN_CODES_INPUT_DEFAULT_ERROR_MESSAGE = "recovery-codes-error-invalid";
public static final String FIELD_RECOVERY_CODE_IN_BROWSER_FLOW = "recoveryCodeInput";

public static byte[] hashRawCode(String rawGeneratedCode) {
Objects.requireNonNull(rawGeneratedCode, "rawGeneratedCode cannot be null");

< EDBE span class="x x-first x-last">return HashUtils.hash(JavaAlgorithm.getJavaAlgorithmForHash(NOM_ALGORITHM_TO_HASH),
rawGeneratedCode.getBytes(StandardCharsets.UTF_8));
// If we allow the algorithm to be truly configurable, we should make sure that it works
// with both `SHA-512` as well as `RS512` (which was used in the versions prior to 26.2)
return HashUtils.hash(NOM_ALGORITHM_TO_HASH, rawGeneratedCode.getBytes(StandardCharsets.UTF_8));
}

public static boolean verifyRecoveryCodeInput(String rawInputRecoveryCode, String hashedSavedRecoveryCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.keycloak.authentication.authenticators.browser.UsernameFormFactory;
import org.keycloak.common.util.Time;
import org.keycloak.credential.CredentialModel;
import org.keycloak.crypto.JavaAlgorithm;
import org.keycloak.events.Details;
import org.keycloak.events.Errors;
import org.keycloak.events.EventType;
Expand All @@ -28,6 +29,7 @@
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.credential.RecoveryAuthnCodesCredentialModel;
import org.keycloak.models.credential.dto.RecoveryAuthnCodesCredentialData;
import org.keycloak.models.utils.RecoveryAuthnCodesUtils;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.representations.account.CredentialMetadataRepresentation;
Expand Down Expand Up @@ -55,6 +57,7 @@
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;
import org.keycloak.testsuite.util.oauth.OAuthClient;
import org.keycloak.testsuite.util.SecondBrowser;
import org.keycloak.util.JsonSerialization;
import org.openqa.selenium.WebDriver;

import java.util.Arrays;
Expand Down Expand Up @@ -505,6 +508,13 @@ public void test09recoveryAuthnCodesWithThresholdConfigured() throws Exception {
CredentialMetadataRepresentation recoveryCodesMetadata = getRecoveryCodeCredentialFromAccountRestApi(httpClient, response.getAccessToken());
Assert.assertNull("Expected not warning", recoveryCodesMetadata.getWarningMessageTitle());
Assert.assertEquals("0/12", recoveryCodesMetadata.getInfoMessage().getParameters()[0]);
Assert.assertNotNull(recoveryCodesMetadata.getCredential().getCredentialData());
RecoveryAuthnCodesCredentialData data = JsonSerialization.readValue(
recoveryCodesMetadata.getCredential().getCredentialData(), RecoveryAuthnCodesCredentialData.class);
Assert.assertEquals(12, data.getTotalCodes());
Assert.assertEquals(12, data.getRemainingCodes());
Assert.assertEquals(JavaAlgorithm.SHA512, data.getAlgorithm());
Assert.assertNull(data.getHashIterations());

// Re-authenticate with recovery codes
oauth.loginForm().prompt(OIDCLoginProtocol.PROMPT_VALUE_LOGIN).open();
Expand Down
Loading
0