Move doLogin to AbstractOAuthClient (#37638)

Closes #37637

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
Stian Thorgersen
2025-02-26 12:34:03 +01:00
committed by GitHub
parent acb7abc255
commit c22f76867f
226 changed files with 1197 additions and 3787 deletions

View File

@@ -72,10 +72,6 @@
<groupId>org.keycloak.testframework</groupId>
<artifactId>keycloak-test-framework-oauth</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak.testframework</groupId>
<artifactId>keycloak-test-framework-oauth-nimbus-poc</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak.testframework</groupId>
<artifactId>keycloak-test-framework-email-server</artifactId>

View File

@@ -1,6 +1,5 @@
package org.keycloak.test.examples;
import com.nimbusds.oauth2.sdk.GeneralException;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.junit.jupiter.api.Assertions;
@@ -11,8 +10,8 @@ import org.keycloak.testframework.annotations.InjectUser;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.mail.MailServer;
import org.keycloak.testframework.mail.annotations.InjectMailServer;
import org.keycloak.testframework.oauth.nimbus.OAuthClient;
import org.keycloak.testframework.oauth.nimbus.annotations.InjectOAuthClient;
import org.keycloak.testframework.oauth.OAuthClient;
import org.keycloak.testframework.oauth.annotations.InjectOAuthClient;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.ManagedUser;
import org.keycloak.testframework.realm.RealmConfig;
@@ -20,7 +19,6 @@ import org.keycloak.testframework.realm.RealmConfigBuilder;
import org.keycloak.testframework.realm.UserConfig;
import org.keycloak.testframework.realm.UserConfigBuilder;
import java.io.IOException;
import java.util.Map;
@KeycloakIntegrationTest
@@ -39,8 +37,8 @@ public class EmailTest {
OAuthClient oAuthClient;
@Test
public void testEmail() throws GeneralException, IOException, MessagingException {
oAuthClient.resourceOwnerCredentialGrant(user.getUsername(), "invalid");
public void testEmail() throws MessagingException {
oAuthClient.doPasswordGrantRequest(user.getUsername(), "invalid");
Map<String, String> smtpServer = realm.admin().toRepresentation().getSmtpServer();
Assertions.assertEquals("auto@keycloak.org", smtpServer.get("from"));

View File

@@ -1,6 +1,5 @@
package org.keycloak.test.examples;
import com.nimbusds.oauth2.sdk.GeneralException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.events.EventType;
@@ -9,14 +8,12 @@ import org.keycloak.testframework.annotations.InjectEvents;
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.events.Events;
import org.keycloak.testframework.oauth.nimbus.OAuthClient;
import org.keycloak.testframework.oauth.nimbus.annotations.InjectOAuthClient;
import org.keycloak.testframework.oauth.OAuthClient;
import org.keycloak.testframework.oauth.annotations.InjectOAuthClient;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.remote.timeoffset.InjectTimeOffSet;
import org.keycloak.testframework.remote.timeoffset.TimeOffSet;
import java.io.IOException;
@KeycloakIntegrationTest
public class EventsTest {
@@ -34,13 +31,13 @@ public class EventsTest {
@Test
public void testFailedLogin() {
oAuthClient.resourceOwnerCredentialGrant("invalid", "invalid");
oAuthClient.doPasswordGrantRequest("invalid", "invalid");
EventRepresentation event = events.poll();
Assertions.assertEquals(EventType.LOGIN_ERROR.name(), event.getType());
Assertions.assertEquals("invalid", event.getDetails().get("username"));
oAuthClient.resourceOwnerCredentialGrant("invalid2", "invalid");
oAuthClient.doPasswordGrantRequest("invalid2", "invalid");
event = events.poll();
Assertions.assertEquals(EventType.LOGIN_ERROR.name(), event.getType());
@@ -48,17 +45,17 @@ public class EventsTest {
}
@Test
public void testTimeOffset() throws GeneralException, IOException {
public void testTimeOffset() {
timeOffSet.set(60);
oAuthClient.clientCredentialGrant();
oAuthClient.doClientCredentialsGrantAccessTokenRequest();
Assertions.assertEquals(EventType.CLIENT_LOGIN.name(), events.poll().getType());
}
@Test
public void testClientLogin() throws GeneralException, IOException {
oAuthClient.clientCredentialGrant();
public void testClientLogin() {
oAuthClient.doClientCredentialsGrantAccessTokenRequest();
Assertions.assertEquals(EventType.CLIENT_LOGIN.name(), events.poll().getType());
}

View File

@@ -1,103 +0,0 @@
package org.keycloak.test.examples;
import com.nimbusds.oauth2.sdk.AuthorizationResponse;
import com.nimbusds.oauth2.sdk.TokenIntrospectionResponse;
import com.nimbusds.oauth2.sdk.TokenResponse;
import com.nimbusds.oauth2.sdk.token.AccessToken;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.testframework.oauth.nimbus.annotations.InjectOAuthClient;
import org.keycloak.testframework.annotations.InjectUser;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.oauth.nimbus.OAuthClient;
import org.keycloak.testframework.realm.ManagedUser;
import org.keycloak.testframework.realm.UserConfig;
import org.keycloak.testframework.realm.UserConfigBuilder;
import org.keycloak.testframework.ui.annotations.InjectPage;
import org.keycloak.testframework.ui.annotations.InjectWebDriver;
import org.keycloak.testframework.ui.page.LoginPage;
import org.openqa.selenium.WebDriver;
import java.net.URI;
import java.net.URL;
@KeycloakIntegrationTest
public class NimbusOAuthClientTest {
@InjectUser(config = OAuthUserConfig.class)
ManagedUser user;
@InjectOAuthClient
OAuthClient oAuthClient;
@InjectWebDriver
WebDriver webDriver;
@InjectPage
LoginPage loginPage;
@Test
public void testClientCredentials() throws Exception {
TokenResponse tokenResponse = oAuthClient.clientCredentialGrant();
Assertions.assertTrue(tokenResponse.indicatesSuccess());
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
}
@Test
public void testIntrospection() throws Exception {
AccessToken accessToken = oAuthClient.clientCredentialGrant().toSuccessResponse().getTokens().getAccessToken();
TokenIntrospectionResponse introspectionResponse = oAuthClient.introspection(accessToken);
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
Assertions.assertNotNull(introspectionResponse.toSuccessResponse().getIssuer());
}
@Test
public void testAuthorizationCode() throws Exception {
URL authorizationRequestURL = oAuthClient.authorizationRequest();
webDriver.navigate().to(authorizationRequestURL);
loginPage.fillLogin(user.getUsername(), user.getPassword());
loginPage.submit();
Assertions.assertEquals(1, oAuthClient.getCallbacks().size());
URI callbackUri = oAuthClient.getCallbacks().remove(0);
AuthorizationResponse authorizationResponse = AuthorizationResponse.parse(callbackUri);
Assertions.assertTrue(authorizationResponse.indicatesSuccess());
Assertions.assertNotNull(authorizationResponse.toSuccessResponse().getAuthorizationCode());
TokenResponse tokenResponse = oAuthClient.tokenRequest(authorizationResponse.toSuccessResponse().getAuthorizationCode());
Assertions.assertTrue(tokenResponse.indicatesSuccess());
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
}
@Test
public void testAccessTokenRevocation() throws Exception {
TokenResponse tokenResponse = oAuthClient.clientCredentialGrant();
Assertions.assertTrue(tokenResponse.indicatesSuccess());
Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken());
AccessToken accessToken = tokenResponse.toSuccessResponse().getTokens().getAccessToken();
TokenIntrospectionResponse introspectionResponse = oAuthClient.introspection(accessToken);
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
Assertions.assertNotNull(introspectionResponse.toSuccessResponse().getScope());
Assertions.assertEquals(Response.Status.OK.getStatusCode(), oAuthClient.revokeAccessToken(accessToken).getStatusCode());
introspectionResponse = oAuthClient.introspection(accessToken);
Assertions.assertTrue(introspectionResponse.indicatesSuccess());
Assertions.assertNull(introspectionResponse.toSuccessResponse().getScope());
}
public static class OAuthUserConfig implements UserConfig {
@Override
public UserConfigBuilder configure(UserConfigBuilder user) {
return user.name("First", "Last")
.email("test@local")
.password("password");
}
}
}

View File

@@ -2,20 +2,17 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.testframework.annotations.InjectClient;
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.InjectUser;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.oauth.OAuthClient;
import org.keycloak.testframework.oauth.annotations.InjectOAuthClient;
import org.keycloak.testframework.realm.ClientConfig;
import org.keycloak.testframework.realm.ClientConfigBuilder;
import org.keycloak.testframework.realm.ManagedClient;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.realm.ManagedUser;
import org.keycloak.testframework.realm.UserConfig;
import org.keycloak.testframework.realm.UserConfigBuilder;
import org.keycloak.testsuite.util.oauth.AccessTokenResponse;
import org.keycloak.testsuite.util.oauth.AuthorizationEndpointResponse;
import org.keycloak.testsuite.util.oauth.TokenRevocationResponse;
import org.keycloak.testsuite.util.oauth.UserInfoResponse;
@@ -23,76 +20,71 @@ import org.keycloak.testsuite.util.oauth.UserInfoResponse;
public class OAuthClientTest {
@InjectOAuthClient
OAuthClient oAuthClient;
OAuthClient oauth;
@InjectRealm
ManagedRealm managedRealm;
@InjectClient(config = OAuthClientConfig.class)
ManagedClient client;
@InjectUser(config = OAuthUserConfig.class)
ManagedUser user;
@Test
public void testConfig() {
Assertions.assertEquals(managedRealm.getName(), oAuthClient.config().getRealm());
Assertions.assertEquals(managedRealm.getBaseUrl() + "/protocol/openid-connect/token", oAuthClient.getEndpoints().getToken());
Assertions.assertEquals(managedRealm.getName(), oauth.config().getRealm());
Assertions.assertEquals(managedRealm.getBaseUrl() + "/protocol/openid-connect/token", oauth.getEndpoints().getToken());
}
@Test
public void testLogin() {
AuthorizationEndpointResponse response = oauth.doLogin(user.getUsername(), user.getPassword());
Assertions.assertTrue(response.isRedirected());
}
@Test
public void testPasswordGrant() {
AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword());
AccessTokenResponse accessTokenResponse = oauth.doPasswordGrantRequest(user.getUsername(), user.getPassword());
Assertions.assertTrue(accessTokenResponse.isSuccess());
accessTokenResponse = oAuthClient.passwordGrantRequest(user.getUsername(), "invalid").send();
accessTokenResponse = oauth.passwordGrantRequest(user.getUsername(), "invalid").send();
Assertions.assertFalse(accessTokenResponse.isSuccess());
Assertions.assertEquals("Invalid user credentials", accessTokenResponse.getErrorDescription());
}
@Test
public void testClientCredential() {
AccessTokenResponse accessTokenResponse = oAuthClient.doClientCredentialsGrantAccessTokenRequest();
AccessTokenResponse accessTokenResponse = oauth.doClientCredentialsGrantAccessTokenRequest();
Assertions.assertTrue(accessTokenResponse.isSuccess());
}
@Test
public void testUserInfo() {
AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword());
AccessTokenResponse accessTokenResponse = oauth.doPasswordGrantRequest(user.getUsername(), user.getPassword());
UserInfoResponse userInfoResponse = oAuthClient.doUserInfoRequest(accessTokenResponse.getAccessToken());
UserInfoResponse userInfoResponse = oauth.doUserInfoRequest(accessTokenResponse.getAccessToken());
Assertions.assertTrue(userInfoResponse.isSuccess());
Assertions.assertEquals(user.getUsername(), userInfoResponse.getUserInfo().getPreferredUsername());
}
@Test
public void testRefresh() {
AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword());
AccessTokenResponse accessTokenResponse = oauth.doPasswordGrantRequest(user.getUsername(), user.getPassword());
AccessTokenResponse refreshResponse = oAuthClient.doRefreshTokenRequest(accessTokenResponse.getRefreshToken());
AccessTokenResponse refreshResponse = oauth.doRefreshTokenRequest(accessTokenResponse.getRefreshToken());
Assertions.assertTrue(refreshResponse.isSuccess());
Assertions.assertNotEquals(accessTokenResponse.getAccessToken(), refreshResponse.getAccessToken());
}
@Test
public void testRevocation() {
AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword());
AccessTokenResponse accessTokenResponse = oauth.doPasswordGrantRequest(user.getUsername(), user.getPassword());
TokenRevocationResponse tokenRevocationResponse = oAuthClient.doTokenRevoke(accessTokenResponse.getRefreshToken());
TokenRevocationResponse tokenRevocationResponse = oauth.doTokenRevoke(accessTokenResponse.getRefreshToken());
Assertions.assertTrue(tokenRevocationResponse.isSuccess());
AccessTokenResponse refreshResponse = oAuthClient.doRefreshTokenRequest(accessTokenResponse.getRefreshToken());
AccessTokenResponse refreshResponse = oauth.doRefreshTokenRequest(accessTokenResponse.getRefreshToken());
Assertions.assertFalse(refreshResponse.isSuccess());
}
public static class OAuthClientConfig implements ClientConfig {
@Override
public ClientConfigBuilder configure(ClientConfigBuilder client) {
return client.clientId("myclient").secret("mysecret").directAccessGrants().serviceAccount();
}
}
public static class OAuthUserConfig implements UserConfig {
@Override

View File

@@ -1,14 +1,22 @@
package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.keycloak.testframework.ui.annotations.InjectPage;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.ui.annotations.InjectPage;
import org.keycloak.testframework.ui.annotations.InjectWebDriver;
import org.keycloak.testframework.ui.page.LoginPage;
import org.keycloak.testframework.ui.page.WelcomePage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
@KeycloakIntegrationTest
public class PagesTest {
@InjectWebDriver
WebDriver webDriver;
@InjectPage
WelcomePage welcomePage;
@@ -18,8 +26,25 @@ public class PagesTest {
@Test
public void testLoginFromWelcome() {
welcomePage.navigateTo();
loginPage.fillLogin("admin", "admin");
loginPage.submit();
if (welcomePage.isActivePage()) {
welcomePage.fillRegistration("admin", "admin");
welcomePage.submit();
welcomePage.clickOpenAdminConsole();
}
if (webDriver instanceof HtmlUnitDriver) {
String pageId = webDriver.findElement(By.xpath("//body")).getAttribute("data-page-id");
Assertions.assertEquals("admin", pageId);
Assertions.assertTrue(webDriver.getCurrentUrl().endsWith("/admin/master/console/"));
} else {
loginPage.waitForPage();
Assertions.assertTrue(loginPage.isActivePage());
loginPage.fillLogin("admin", "admin");
loginPage.submit();
}
}
}