Skip to content

Commit b545339

Browse files
authored
Support using a builder to add custom parameters to login (#37659)
Closes #37658 Signed-off-by: stianst <[email protected]>
1 parent 873e4ff commit b545339

File tree

50 files changed

+218
-304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+218
-304
lines changed

tests/utils-shared/src/main/java/org/keycloak/testsuite/util/oauth/AbstractOAuthClient.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public T client(String clientId, String clientSecret) {
4747
return (T) this;
4848
}
4949

50-
public String getLoginFormUrl() {
51-
return new LoginUrlBuilder(this).toString();
50+
public LoginUrlBuilder loginForm() {
51+
return new LoginUrlBuilder(this);
5252
}
5353

5454
public void openLoginForm() {
55-
driver.navigate().to(getLoginFormUrl());
55+
loginForm().open();
5656
}
5757

5858
public AuthorizationEndpointResponse doLogin(String username, String password) {
@@ -63,6 +63,14 @@ public AuthorizationEndpointResponse doLogin(String username, String password) {
6363

6464
public abstract void fillLoginForm(String username, String password);
6565

66+
public void openRegistrationForm() {
67+
driver.navigate().to(registrationForm().build());
68+
}
69+
70+
public RegistrationUrlBuilder registrationForm() {
71+
return new RegistrationUrlBuilder(this);
72+
}
73+
6674
public AuthorizationEndpointResponse parseLoginResponse() {
6775
return new AuthorizationEndpointResponse(this);
6876
}

tests/utils-shared/src/main/java/org/keycloak/testsuite/util/oauth/AbstractUrlBuilder.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public AbstractUrlBuilder(AbstractOAuthClient<?> client) {
1616
protected abstract void initRequest();
1717

1818
public void open() {
19-
client.driver.navigate().to(toString());
19+
client.driver.navigate().to(build());
2020
}
2121

2222
protected void parameter(String name, String value) {
@@ -25,9 +25,16 @@ protected void parameter(String name, String value) {
2525
}
2626
}
2727

28-
public String toString() {
28+
protected void replaceParameter(String name, String value) {
29+
if (value != null) {
30+
uriBuilder.replaceQueryParam(name, value);
31+
}
32+
}
33+
34+
public String build() {
2935
uriBuilder = UriBuilder.fromUri(getEndpoint());
3036
initRequest();
37+
3138
return uriBuilder.build().toString();
3239
}
3340

tests/utils-shared/src/main/java/org/keycloak/testsuite/util/oauth/LoginUrlBuilder.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
import org.keycloak.models.Constants;
55
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
66

7+
import java.util.HashMap;
8+
import java.util.Map;
9+
710
public class LoginUrlBuilder extends AbstractUrlBuilder {
811

12+
private Map<String, String> customParameters;
13+
914
public LoginUrlBuilder(AbstractOAuthClient<?> client) {
1015
super(client);
1116
}
@@ -15,6 +20,24 @@ public String getEndpoint() {
1520
return client.getEndpoints().getAuthorization();
1621
}
1722

23+
public LoginUrlBuilder param(String name, String value) {
24+
if (customParameters == null) {
25+
customParameters = new HashMap<>();
26+
}
27+
customParameters.put(name, value);
28+
return this;
29+
}
30+
31+
public LoginUrlBuilder prompt(String value) {
32+
param(OIDCLoginProtocol.PROMPT_PARAM, value);
33+
return this;
34+
}
35+
36+
public LoginUrlBuilder loginHint(String value) {
37+
param(OIDCLoginProtocol.LOGIN_HINT_PARAM, value);
38+
return this;
39+
}
40+
1841
@Override
1942
protected void initRequest() {
2043
parameter(OAuth2Constants.RESPONSE_TYPE, client.config().getResponseType());
@@ -43,6 +66,10 @@ protected void initRequest() {
4366
if (client.getCustomParameters() != null) {
4467
client.getCustomParameters().forEach(this::parameter);
4568
}
69+
70+
if (customParameters != null) {
71+
customParameters.forEach(this::replaceParameter);
72+
}
4673
}
4774

4875
}

testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/oauth/OAuthClient.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,6 @@ public void setDriver(WebDriver driver) {
162162
this.driver = driver;
163163
}
164164

165-
public void openRegistrationForm() {
166-
driver.navigate().to(getRegistrationFormUrl());
167-
}
168-
169-
public String getRegistrationFormUrl() {
170-
return new RegistrationUrlBuilder(this).toString();
171-
}
172-
173165
public AuthorizationEndpointResponse doSilentLogin() {
174166
openLoginForm();
175167
WaitUtils.waitForPageToLoad();
@@ -187,25 +179,9 @@ public AuthorizationEndpointResponse doLoginSocial(String brokerId, String usern
187179
return parseLoginResponse();
188180
}
189181

190-
public AuthorizationEndpointResponse doRememberMeLogin(String username, String password) {
191-
openLoginForm();
192-
fillLoginForm(username, password, true);
193-
194-
return parseLoginResponse();
195-
}
196-
197182
public void fillLoginForm(String username, String password) {
198-
this.fillLoginForm(username, password, false);
199-
}
200-
201-
public void fillLoginForm(String username, String password, boolean rememberMe) {
202183
LoginPage loginPage = new LoginPage();
203184
PageFactory.initElements(driver, loginPage);
204-
205-
if (rememberMe) {
206-
loginPage.setRememberMe(true);
207-
}
208-
209185
loginPage.login(username, password);
210186
}
211187

testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/custom/CustomAuthFlowCookieTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class CustomAuthFlowCookieTest extends AbstractCustomAccountManagementTes
3232
public void cookieAlternative() {
3333
//test default setting of cookie provider
3434
//login
35-
driver.navigate().to(oauth.getLoginFormUrl());
35+
oauth.openLoginForm();
3636
testRealmLoginPage.form().login(testUser);
3737

3838
//check SSO is working
@@ -47,7 +47,7 @@ public void disabledCookie() {
4747
updateRequirement("browser", "auth-cookie", Requirement.DISABLED);
4848

4949
//login
50-
driver.navigate().to(oauth.getLoginFormUrl());
50+
oauth.openLoginForm();
5151
testRealmLoginPage.form().login(testUser);
5252

5353
//SSO shouldn't work

testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/account/custom/CustomAuthFlowOTPTest.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void configureRequiredActions() {
105105

106106
private void configureOTP() {
107107
//configure OTP for test user
108-
driver.navigate().to(oauth.getLoginFormUrl());
108+
oauth.openLoginForm();
109109
testRealmLoginPage.form().login(testUser);
110110
String totpSecret = testRealmLoginPage.form().totpForm().getTotpSecret();
111111
testRealmLoginPage.form().totpForm().setTotp(totp.generateTOTP(totpSecret));
@@ -126,12 +126,12 @@ public void requireOTPTest() {
126126
testRealmResource().update(realm);
127127

128128
updateRequirement("browser", Requirement.REQUIRED, (authExec) -> authExec.getDisplayName().equals("Browser - Conditional OTP"));
129-
driver.navigate().to(oauth.getLoginFormUrl());
129+
oauth.openLoginForm();
130130
testRealmLoginPage.form().login(testUser);
131131
assertTrue(loginConfigTotpPage.isCurrent());
132132

133133
configureOTP();
134-
driver.navigate().to(oauth.getLoginFormUrl());
134+
oauth.openLoginForm();
135135
testRealmLoginPage.form().login(testUser);
136136

137137
//verify that the page is login page, not totp setup
@@ -160,12 +160,12 @@ private void reuseExistingOtp(boolean allowReusingExistingOtp) {
160160
testRealmResource().update(realm);
161161

162162
updateRequirement("browser", Requirement.REQUIRED, (authExec) -> authExec.getDisplayName().equals("Browser - Conditional OTP"));
163-
driver.navigate().to(oauth.getLoginFormUrl());
163+
oauth.openLoginForm();
164164
testRealmLoginPage.form().login(testUser);
165165
assertTrue(loginConfigTotpPage.isCurrent());
166166

167167
//configure OTP for test user
168-
driver.navigate().to(oauth.getLoginFormUrl());
168+
oauth.openLoginForm();
169169
testRealmLoginPage.form().login(testUser);
170170

171171
final String totpSecret = testRealmLoginPage.form().totpForm().getTotpSecret();
@@ -178,7 +178,7 @@ private void reuseExistingOtp(boolean allowReusingExistingOtp) {
178178
testRealmLoginPage.form().totpForm().submit();
179179
AccountHelper.logout(testRealmResource(), testUser.getUsername());
180180

181-
driver.navigate().to(oauth.getLoginFormUrl());
181+
oauth.openLoginForm();
182182
testRealmLoginPage.form().login(testUser);
183183

184184
loginTotpPage.assertCurrent();
@@ -200,7 +200,7 @@ public void conditionalOTPNoDefault() {
200200
setConditionalOTPForm(config);
201201

202202
//test OTP is required
203-
driver.navigate().to(oauth.getLoginFormUrl());
203+
oauth.openLoginForm();
204204
testRealmLoginPage.form().login(testUser);
205205

206206
//verify that the page is login page, not totp setup
@@ -216,7 +216,7 @@ public void conditionalOTPDefaultSkip() {
216216
setConditionalOTPForm(config);
217217

218218
//test OTP is skipped
219-
driver.navigate().to(oauth.getLoginFormUrl());
219+
oauth.openLoginForm();
220220
testRealmLoginPage.form().login(testUser);
221221
assertCurrentUrlStartsWith(oauth.APP_AUTH_ROOT);
222222
}
@@ -231,12 +231,12 @@ public void conditionalOTPDefaultForce() {
231231
setConditionalOTPForm(config);
232232

233233
//test OTP is forced
234-
driver.navigate().to(oauth.getLoginFormUrl());
234+
oauth.openLoginForm();
235235
testRealmLoginPage.form().login(testUser);
236236
assertTrue(loginConfigTotpPage.isCurrent());
237237

238238
configureOTP();
239-
driver.navigate().to(oauth.getLoginFormUrl());
239+
oauth.openLoginForm();
240240
testRealmLoginPage.form().login(testUser);
241241

242242
//verify that the page is login page, not totp setup
@@ -258,7 +258,7 @@ public void conditionalOTPNoDefaultWithChecks() {
258258
setConditionalOTPForm(config);
259259

260260
//test OTP is required
261-
driver.navigate().to(oauth.getLoginFormUrl());
261+
oauth.openLoginForm();
262262
testRealmLoginPage.form().login(testUser);
263263

264264
//verify that the page is login page, not totp setup
@@ -279,7 +279,7 @@ public void conditionalOTPDefaultSkipWithChecks() {
279279
setConditionalOTPForm(config);
280280

281281
//test OTP is skipped
282-
driver.navigate().to(oauth.getLoginFormUrl());
282+
oauth.openLoginForm();
283283
testRealmLoginPage.form().login(testUser);
284284
assertCurrentUrlStartsWith(oauth.APP_AUTH_ROOT);
285285
}
@@ -298,12 +298,12 @@ public void conditionalOTPDefaultForceWithChecks() {
298298
setConditionalOTPForm(config);
299299

300300
//test OTP is forced
301-
driver.navigate().to(oauth.getLoginFormUrl());
301+
oauth.openLoginForm();
302302
testRealmLoginPage.form().login(testUser);
303303
assertTrue(loginConfigTotpPage.isCurrent());
304304

305305
configureOTP();
306-
driver.navigate().to(oauth.getLoginFormUrl());
306+
oauth.openLoginForm();
307307
testRealmLoginPage.form().login(testUser);
308308

309309
//verify that the page is login page, not totp setup
@@ -324,7 +324,7 @@ public void conditionalOTPUserAttributeSkip() {
324324
testRealmResource().users().get(testUser.getId()).update(testUser);
325325

326326
//test OTP is skipped
327-
driver.navigate().to(oauth.getLoginFormUrl());
327+
oauth.openLoginForm();
328328
testRealmLoginPage.form().login(testUser);
329329

330330
assertCurrentUrlStartsWith(oauth.APP_AUTH_ROOT);
@@ -344,12 +344,12 @@ public void conditionalOTPUserAttributeForce() {
344344
testRealmResource().users().get(testUser.getId()).update(testUser);
345345

346346
//test OTP is required
347-
driver.navigate().to(oauth.getLoginFormUrl());
347+
oauth.openLoginForm();
348348
testRealmLoginPage.form().login(testUser);
349349
assertTrue(loginConfigTotpPage.isCurrent());
350350

351351
configureOTP();
352-
driver.navigate().to(oauth.getLoginFormUrl());
352+
oauth.openLoginForm();
353353
testRealmLoginPage.form().login(testUser);
354354

355355
//verify that the page is login page, not totp setup
@@ -374,7 +374,7 @@ public void conditionalOTPRoleSkip() {
374374
testRealmResource().users().get(testUser.getId()).roles().realmLevel().add(realmRoles);
375375

376376
//test OTP is skipped
377-
driver.navigate().to(oauth.getLoginFormUrl());
377+
oauth.openLoginForm();
378378
testRealmLoginPage.form().login(testUser);
379379
assertCurrentUrlStartsWith(oauth.APP_AUTH_ROOT);
380380
}
@@ -397,13 +397,13 @@ public void conditionalOTPRoleForce() {
397397
testRealmResource().users().get(testUser.getId()).roles().realmLevel().add(realmRoles);
398398

399399
//test OTP is required
400-
driver.navigate().to(oauth.getLoginFormUrl());
400+
oauth.openLoginForm();
401401
testRealmLoginPage.form().login(testUser);
402402

403403
assertTrue(loginConfigTotpPage.isCurrent());
404404

405405
configureOTP();
406-
driver.navigate().to(oauth.getLoginFormUrl());
406+
oauth.openLoginForm();
407407
testRealmLoginPage.form().login(testUser);
408408

409409
//verify that the page is login page, not totp setup
@@ -426,13 +426,13 @@ public void conditionalOTPRoleForceViaGroup() {
426426
testRealmResource().users().get(testUser.getId()).joinGroup(group.getId());
427427

428428
//test OTP is required
429-
driver.navigate().to(oauth.getLoginFormUrl());
429+
oauth.openLoginForm();
430430
testRealmLoginPage.form().login(testUser);
431431

432432
assertTrue(loginConfigTotpPage.isCurrent());
433433

434434
configureOTP();
435-
driver.navigate().to(oauth.getLoginFormUrl());
435+
oauth.openLoginForm();
436436
testRealmLoginPage.form().login(testUser);
437437

438438
//verify that the page is login page, not totp setup
@@ -445,13 +445,13 @@ public void conditionalOTPEmptyConfiguration() {
445445
setConditionalOTPForm(null);
446446

447447
// test OTP is required
448-
driver.navigate().to(oauth.getLoginFormUrl());
448+
oauth.openLoginForm();
449449
testRealmLoginPage.form().login(testUser);
450450

451451
assertTrue(loginConfigTotpPage.isCurrent());
452452

453453
configureOTP();
454-
driver.navigate().to(oauth.getLoginFormUrl());
454+
oauth.openLoginForm();
455455
testRealmLoginPage.form().login(testUser);
456456

457457
// verify that the page is login page, not totp setup
@@ -492,7 +492,7 @@ public void conditionalOTPRequestHeaderSkip() {
492492
setConditionalOTPForm(config);
493493

494494
//test OTP is skipped
495-
driver.navigate().to(oauth.getLoginFormUrl());
495+
oauth.openLoginForm();
496496
testRealmLoginPage.form().login(testUser);
497497
assertCurrentUrlStartsWith(oauth.APP_AUTH_ROOT);
498498
}
@@ -508,12 +508,12 @@ public void conditionalOTPRequestHeaderForce() {
508508
setConditionalOTPForm(config);
509509

510510
//test OTP is required
511-
driver.navigate().to(oauth.getLoginFormUrl());
511+
oauth.openLoginForm();
512512
testRealmLoginPage.form().login(testUser);
513513
assertEquals(PageUtils.getPageTitle(driver), "Mobile Authenticator Setup");
514514

515515
configureOTP();
516-
driver.navigate().to(oauth.getLoginFormUrl());
516+
oauth.openLoginForm();
517517
testRealmLoginPage.form().login(testUser);
518518

519519
//verify that the page is login page, not totp setup

0 commit comments

Comments
 (0)