8000 Fix second factor got skipped when login started from forgot password page. by gaoyikeshuer · Pull Request #55588 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
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 @@ -31,11 +31,14 @@
import org.keycloak.common.util.Time;
import org.keycloak.models.AuthenticatedClientSessionModel;
import org.keycloak.models.AuthenticationFlowModel;
import org.keycloak.models.ClientModel;
import org.keycloak.models.Constants;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.utils.AuthenticationFlowResolver;
import org.keycloak.protocol.oidc.utils.AcrUtils;
import org.keycloak.protocol.saml.SamlProtocol;
import org.keycloak.sessions.AuthenticationSessionModel;
import org.keycloak.util.JsonSerialization;

Expand Down Expand Up @@ -69,12 +72,21 @@ public boolean isLevelOfAuthenticationForced() {

public int getRequestedLevelOfAuthentication(AuthenticationFlowModel executionModel) {
String requiredLoa = authSession.getClientNote(Constants.REQUESTED_LEVEL_OF_AUTHENTICATION);
int requestedLoaByClient = requiredLoa == null ? NO_LOA : Integer.parseInt(requiredLoa);
int requestedLoaByClient = requiredLoa == null ? getRequestedLevelOfAuthenticationByClientConfig() : Integer.parseInt(requiredLoa);
int requestedLoaByKcAction = getRequestedLevelOfAuthenticationByKcAction(executionModel);
logger.tracef("Level requested by client: %d, level requested by kc_action parameter: %d", requestedLoaByClient, requestedLoaByKcAction);
return Math.max(requestedLoaByClient, requestedLoaByKcAction);
}

private int getRequestedLevelOfAuthenticationByClientConfig() {
ClientModel client = authSession.getClient();
if (client == null || SamlProtocol.LOGIN_PROTOCOL.equals(client.getProtocol())) {
return NO_LOA;
}
List<String> acrValues = AcrUtils.getAcrValues(null, null, client);
return AcrUtils.getLowestLoaForAcrValues(acrValues, client).orElse(NO_LOA);
}

//
private int getRequestedLevelOfAuthenticationByKcAction(AuthenticationFlowModel topLevelFlow) {
RealmModel realm = authSession.getRealm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import org.keycloak.events.Errors;
import org.keycloak.events.EventType;
import org.keycloak.models.AuthenticationExecutionModel.Requirement;
import org.keycloak.models.Constants;
import org.keycloak.models.utils.TimeBasedOTP;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.representations.AccessToken;
import org.keycloak.representations.ClaimsRepresentation;
import org.keycloak.representations.IDToken;
import org.keycloak.testframework.annotations.InjectEvents;
Expand All @@ -31,15 +34,20 @@
import org.keycloak.testframework.mail.annotations.InjectMailServer;
import org.keycloak.testframework.oauth.OAuthClient;
import org.keycloak.testframework.oauth.annotations.InjectOAuthClient;
import org.keycloak.testframework.realm.ClientBuilder;
import org.keycloak.testframework.realm.ClientConfig;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.ManagedUser;
import org.keycloak.testframework.realm.UserBuilder;
import org.keycloak.testframework.realm.UserConfig;
import org.keycloak.testframework.remote.runonserver.InjectRunOnServer;
import org.keycloak.testframework.remote.runonserver.RunOnServerClient;
import org.keycloak.testframework.ui.annotations.InjectPage;
import org.keycloak.testframework.ui.annotations.InjectWebDriver;
import org.keycloak.testframework.ui.page.ErrorPage;
import org.keycloak.testframework.ui.page.LoginPage;
import org.keycloak.testframework.ui.page.LoginPasswordResetPage;
import org.keycloak.testframework.ui.page.LoginTotpPage;
import org.keycloak.testframework.ui.page.LoginUsernamePage;
import org.keycloak.testframework.ui.page.PasswordPage;
import org.keycloak.testframework.ui.page.RegisterPage;
Expand Down Expand Up @@ -72,12 +80,18 @@ public class LevelOfAssuranceEssentialAcrTest {
@InjectUser(config = BasicUserConfig.class)
ManagedUser user;

@InjectUser(ref = "otp-user", config = OtpUserConfig.class)
ManagedUser otpUser;

@InjectWebDriver(lifecycle = LifeCycle.METHOD)
ManagedWebDriver driver;

@InjectOAuthClient
OAuthClient oauth;

@InjectOAuthClient(ref = "minimum-acr", config = MinimumAcrClientConfig.class)
OAuthClient minimumAcrOauth;

@InjectRunOnServer
RunOnServerClient runOnServer;

Expand Down Expand Up @@ -105,6 +119,9 @@ public class LevelOfAssuranceEssentialAcrTest {
@InjectPage
LoginPasswordResetPage resetPasswordPage;

@InjectPage
LoginTotpPage loginTotpPage;

@BeforeEach
public void setUpFlow() {
runOnServer.run(session -> FlowUtil.inCurrentRealm(session).copyBrowserFlow(FLOW_ALIAS));
Expand Down Expand Up @@ -451,6 +468,30 @@ public void essentialAcrLevel2WithoutConditionsResetCredentialsFails() throws IO
.details(Details.AUTHENTICATION_ERROR_DETAIL, acrNotFulfilledDetail(2, 1));
}

@Test
public void minimumAcrLevel2DirectResetCredentialsLinkRequiresOtp() {
realm.updateWithCleanup(r -> r.resetPasswordAllowed(true));

// this link skips the authorization endpoint so the minimum ACR of the client is never recorded in the login session
driver.open(realm.getBaseUrl() + "/login-actions/reset-credentials?client_id=" + minimumAcrOauth.getClientId()
+ "&redirect_uri=" + minimumAcrOauth.getRedirectUri());
resetPasswordPage.assertCurrent();
resetPasswordPage.backToLogin();
loginUsernamePage.assertCurrent();
loginUsernamePage.fillLoginWithUsernameOnly(otpUser.getUsername());
loginUsernamePage.submit();
passwordPage.assertCurrent();
passwordPage.fillPassword(otpUser.getPassword());
passwordPage.submit();

loginTotpPage.assertCurrent();
loginTotpPage.login(new TimeBasedOTP().generateTOTP("totpsecret"));
driver.waiting().waitForOAuthCallback();
EventAssertion.expectLoginSuccess(events.poll());
AccessTokenResponse tokenResponse = minimumAcrOauth.doAccessTokenRequest(minimumAcrOauth.parseLoginResponse().getCode());
assertEquals("2", minimumAcrOauth.parseToken(tokenResponse.getAccessToken(), AccessToken.class).getAcr());
}

private void loginWithUsernamePassword() {
oauth.openLoginForm();
fillUsernameAndPassword();
Expand Down Expand Up @@ -622,4 +663,26 @@ private LoginUrlBuilder loginFormWithAcrClaim(boolean essential, String acr) {

return oauth.loginForm().claims(claims);
}

private static class OtpUserConfig implements UserConfig {
@Override
public UserBuilder configure(UserBuilder user) {
return user.username("otp-user")
.name("Otp", "User")
.email("otp-user@localhost")
.emailVerified(true)
.password("password")
.totpSecret("totpsecret");
}
}

private static class MinimumAcrClientConfig implements ClientConfig {
@Override
public ClientBuilder configure(ClientBuilder client) {
return client.clientId("minimum-acr-app")
.publicClient()
.attribute(Constants.ACR_LOA_MAP, "{\"2\":2}")
.attribute(Constants.MINIMUM_ACR_VALUE, "2");
}
}
}
Loading
0