8000 Make the OTP device picker in the keycloak.v2 login theme keyboard accessible by Rishavbhattarai · Pull Request #54079 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
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 @@ -108,10 +108,10 @@ private By getXPathForLookupAllCards() {
}

private By getCssSelectorForLookupActiveCard() {
return By.cssSelector(".pf-m-selected");
return By.cssSelector("input[name='selectedCredentialId']:checked + label");
}

private By getXPathForLookupCardWithName(String credentialName) {
return By.xpath("//div[contains(@class, 'pf-v5-c-tile')][normalize-space() = '"+ credentialName +"']");
return By.xpath("//label[contains(@class, 'pf-v5-c-tile')][normalize-space() = '"+ credentialName +"']");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
Expand Down Expand Up @@ -100,11 +101,11 @@ private By getXPathForLookupAllCards() {
}

private By getCssSelectorForLookupActiveCard() {
return By.cssSelector(".pf-m-selected");
return By.cssSelector("input[name='selectedCredentialId']:checked + label");
}

private By getXPathForLookupCardWithName(String credentialName) {
return By.xpath("//div[contains(@class, 'pf-v5-c-tile')][normalize-space() = '"+ credentialName +"']");
return By.xpath("//label[contains(@class, 'pf-v5-c-tile')][normalize-space() = '"+ credentialName +"']");
}


Expand All @@ -114,4 +115,10 @@ public void selectOtpCredential(String credentialName) {
UIUtils.click(webElement);
}

// Moves from the one-time code field back to the credential selector with Shift+Tab and returns the focused element
public WebElement focusOtpCredentialSelectorWithKeyboard() {
otpInput.sendKeys(Keys.chord(Keys.SHIFT, Keys.TAB));
return driver.switchTo().activeElement();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public String getExpectedPageId() {
}

public void selectOtp(int index) {
driver.findElement(By.id("kc-otp-credential-" + index)).click();
driver.findElement(By.cssSelector("label[for='kc-otp-credential-" + index + "']")).click();
}

public void submitOtpReset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.keycloak.testsuite.ActionURIUtils;
import org.keycloak.testsuite.AssertEvents;
import org.keycloak.testsuite.admin.AdminApiUtil;
import org.keycloak.testsuite.arquillian.annotation.IgnoreBrowserDriver;
import org.keycloak.testsuite.auth.page.login.OneTimeCode;
import org.keycloak.testsuite.authentication.SetUserAttributeAuthenticatorFactory;
import org.keycloak.testsuite.broker.SocialLoginTest;
Expand All @@ -58,8 +59,10 @@
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import static org.keycloak.testsuite.broker.BrokerTestTools.waitForPage;
import static org.keycloak.testsuite.broker.SocialLoginTest.Provider.GITHUB;
Expand Down Expand Up @@ -184,6 +187,9 @@ public void testUserWithTwoAdditionalFactors() {
Assertions.assertTrue(oneTimeCodePage.isOtpLabelPresent());
loginTotpPage.assertCurrent();
loginTotpPage.assertOtpCredentialSelectorAvailability(true);

// The default credential is pre-selected before any interaction
Assertions.assertEquals("first", loginTotpPage.getSelectedOtpCredential());
loginTotpPage.selectOtpCredential("first");

// Check that selected credential is "first"
Expand All @@ -205,6 +211,26 @@ public void testUserWithTwoAdditionalFactors() {
Assertions.assertFalse(oneTimeCodePage.isOtpLabelPresent());
}

@Test
@IgnoreBrowserDriver(HtmlUnitDriver.class) // HtmlUnit doesn't move focus or selection with Tab and arrow keys
public void testUserWithTwoAdditionalFactorsSelectedWithKeyboard() {
provideUsernamePassword("user-with-two-configured-otp");
loginTotpPage.assertCurrent();
loginTotpPage.assertOtpCredentialSelectorAvailability(true);
Assertions.assertEquals("first", loginTotpPage.getSelectedOtpCredential());

// Shift+Tab from the one-time code field reaches the checked credential
WebElement focused = loginTotpPage.focusOtpCredentialSelectorWithKeyboard();
Assertions.assertEquals("radio", focused.getAttribute("type"));
Assertions.assertTrue(focused.isSelected());

// Arrow key selects the "second" factor, then connect with its OTP code
focused.sendKeys(Keys.ARROW_DOWN);
Assertions.assertEquals(OTPFormAuthenticator.UNNAMED, loginTotpPage.getSelectedOtpCredential());
loginTotpPage.login(getOtpCode(USER_WITH_TWO_OTPS_OTP2_SECRET));
Assertions.assertFalse(oneTimeCodePage.isOtpLabelPresent());
}

private void testCredentialsOrder(String username, List<String> orderedCredentials) {
// Provide username and password
provideUsernamePassword(username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ loginTotpUnableToScan=Unable to scan?
loginTotpScanBarcode=Scan barcode?
loginCredential=Credential
loginOtpOneTime=One-time code
loginOtpCredentialSelection=Authenticator device
loginTotpType=Type
loginTotpAlgorithm=Algorithm
loginTotpDigits=Digits
Expand Down
24 changes: 7 additions & 17 deletions themes/src/main/resources/theme/keycloak.v2/login/login-otp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,30 @@
${msg("doLogIn")}
<#elseif section="form">
<form id="kc-otp-login-form" class="${properties.kcFormClass!}" = true; return true;" action="${url.loginAction}" method="post">
<input id="selectedCredentialId" type="hidden" name="selectedCredentialId" value="${otpLogin.selectedCredentialId!''}">
<#if otpLogin.userOtpCredentials?size gt 1>
<div class="${properties.kcFormGroupClass!}">
<div class="${properties.kcInputWrapperClass!}">
<div class="${properties.kcInputWrapperClass!}" role="radiogroup" aria-label="${msg("loginOtpCredentialSelection")}">
<#list otpLogin.userOtpCredentials as otpCredential>
<div id="kc-otp-credential-${otpCredential?index}" class="${properties.kcLoginOTPListClass!}"
'${otpCredential.id}')">
<input id="kc-otp-credential-${otpCredential?index}" class="${properties.kcLoginOTPListInputClass!}" type="radio"
name="selectedCredentialId" value="${otpCredential.id}" <#if otpCredential.id == (otpLogin.selectedCredentialId!'')>checked</#if>>
<label for="kc-otp-credential-${otpCredential?index}" class="${properties.kcLoginOTPListClass!}">
<span class="${properties.kcLoginOTPListItemHeaderClass!}">
<span class="${properties.kcLoginOTPListItemIconBodyClass!}">
<i class="${properties.kcLoginOTPListItemIconClass!}" aria-hidden="true"></i>
</span>
<span class="${properties.kcLoginOTPListItemTitleClass!}">${otpCredential.userLabel}</span>
</span>
</div>
</label>
</#list>
</div>
</div>
<#else>
<input id="selectedCredentialId" type="hidden" name="selectedCredentialId" value="${otpLogin.selectedCredentialId!''}">
</#if>

<@field.input name="otp" label=msg("loginOtpOneTime") autocomplete="one-time-code" fieldName="totp" autofocus=true />

<@buttons.loginButton />
</form>
<script>
<#outputformat "JavaScript">
function toggleOTP(index, value) {
// select the clicked OTP credential
document.getElementById("selectedCredentialId").value = value;
// remove selected class from all OTP credentials
Array.from(document.getElementsByClassName(${properties.kcLoginOTPListSelectedClass!?c})).map(i => i.classList.remove(${properties.kcLoginOTPListSelectedClass!?c}));
// add selected class to the clicked OTP credential
document.getElementById("kc-otp-credential-" + index).classList.add(${properties.kcLoginOTPListSelectedClass!?c});
}
</#outputformat>
</script>
</#if>
</@layout.registrationLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,20 @@ hr {

#kc-form-webauthn {
gap: 0.875rem;
}
}
/* OTP device pickers (login and reset): the radio is visually hidden, so the tile shows its checked state and keyboard focus */
.kc-login-otp-radio:checked + label {
--pf-v5-c-tile__title--Color: var(--pf-v5-c-tile--m-selected__title--Color);
--pf-v5-c-tile__icon--Color: var(--pf-v5-c-tile--m-selected__icon--Color);
--pf-v5-c-tile--TranslateY: var(--pf-v5-c-tile--m-selected--TranslateY);
--pf-v5-c-tile--Transition: var(--pf-v5-c-tile--m-selected--Transition);
--pf-v5-c-tile--after--Height: var(--pf-v5-c-tile--m-selected--after--Height);
--pf-v5-c-tile--after--BackgroundColor: var(--pf-v5-c-tile--m-selected--after--BackgroundColor);
--pf-v5-c-tile--after--Transition: var(--pf-v5-c-tile--m-selected--after--Transition);
--pf-v5-c-tile--after--ScaleY: var(--pf-v5-c-tile--m-selected--after--ScaleY);
}

.kc-login-otp-radio:focus-visible + label {
outline: var(--pf-v5-global--BorderWidth--md) solid var(--pf-v5-global--primary-color--100);
outline-offset: var(--pf-v5-global--spacer--xs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ kcWebAuthnDefaultIcon=pf-v5-c-icon pf-m-xl
kcMarginTopClass=pf-v5-u-mt-md-on-md

kcLoginOTPListClass=pf-v5-c-tile
kcLoginOTPListInputClass=pf-v5-screen-reader kc-login-otp-radio
kcLoginOTPListItemHeaderClass=pf-v5-c-tile__header pf-m-stacked
kcLoginOTPListItemIconBodyClass=pf-v5-c-tile__icon
kcLoginOTPListItemIconClass=fa fa-mobile
kcLoginOTPListItemTitleClass=pf-v5-c-tile__title
kcLoginOTPListSelectedClass=pf-m-selected

kcCopyIconClass=fas fa-copy
kcCheckIconClass=fas fa-check
Expand Down
Loading
0