|
1 | 1 | package org.keycloak.test.examples; |
2 | 2 |
|
3 | | -import com.nimbusds.oauth2.sdk.AuthorizationResponse; |
4 | | -import com.nimbusds.oauth2.sdk.TokenIntrospectionResponse; |
5 | | -import com.nimbusds.oauth2.sdk.TokenResponse; |
6 | | -import com.nimbusds.oauth2.sdk.token.AccessToken; |
7 | | -import jakarta.ws.rs.core.Response; |
8 | 3 | import org.junit.jupiter.api.Assertions; |
9 | 4 | import org.junit.jupiter.api.Test; |
10 | | -import org.keycloak.testframework.oauth.nimbus.annotations.InjectOAuthClient; |
| 5 | +import org.keycloak.testframework.annotations.InjectClient; |
| 6 | +import org.keycloak.testframework.annotations.InjectRealm; |
11 | 7 | import org.keycloak.testframework.annotations.InjectUser; |
12 | 8 | import org.keycloak.testframework.annotations.KeycloakIntegrationTest; |
13 | | -import org.keycloak.testframework.oauth.nimbus.OAuthClient; |
| 9 | +import org.keycloak.testframework.oauth.OAuthClient; |
| 10 | +import org.keycloak.testframework.oauth.annotations.InjectOAuthClient; |
| 11 | +import org.keycloak.testframework.realm.ClientConfig; |
| 12 | +import org.keycloak.testframework.realm.ClientConfigBuilder; |
| 13 | +import org.keycloak.testframework.realm.ManagedClient; |
| 14 | +import org.keycloak.testframework.realm.ManagedRealm; |
14 | 15 | import org.keycloak.testframework.realm.ManagedUser; |
15 | 16 | import org.keycloak.testframework.realm.UserConfig; |
16 | 17 | import org.keycloak.testframework.realm.UserConfigBuilder; |
17 | | -import org.keycloak.testframework.ui.annotations.InjectPage; |
18 | | -import org.keycloak.testframework.ui.annotations.InjectWebDriver; |
19 | | -import org.keycloak.testframework.ui.page.LoginPage; |
20 | | -import org.openqa.selenium.WebDriver; |
21 | | - |
22 | | -import java.net.URI; |
23 | | -import java.net.URL; |
| 18 | +import org.keycloak.testsuite.util.oauth.AccessTokenResponse; |
| 19 | +import org.keycloak.testsuite.util.oauth.TokenRevocationResponse; |
24 | 20 |
|
25 | 21 | @KeycloakIntegrationTest |
26 | 22 | public class OAuthClientTest { |
27 | 23 |
|
28 | | - @InjectUser(config = OAuthUserConfig.class) |
29 | | - ManagedUser user; |
30 | | - |
31 | 24 | @InjectOAuthClient |
32 | 25 | OAuthClient oAuthClient; |
33 | 26 |
|
34 | | - @InjectWebDriver |
35 | | - WebDriver webDriver; |
| 27 | + @InjectRealm |
| 28 | + ManagedRealm managedRealm; |
36 | 29 |
|
37 | | - @InjectPage |
38 | | - LoginPage loginPage; |
| 30 | + @InjectClient(config = OAuthClientConfig.class) |
| 31 | + ManagedClient client; |
39 | 32 |
|
40 | | - @Test |
41 | | - public void testClientCredentials() throws Exception { |
42 | | - TokenResponse tokenResponse = oAuthClient.clientCredentialGrant(); |
43 | | - Assertions.assertTrue(tokenResponse.indicatesSuccess()); |
44 | | - Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken()); |
45 | | - } |
| 33 | + @InjectUser(config = OAuthUserConfig.class) |
| 34 | + ManagedUser user; |
46 | 35 |
|
47 | 36 | @Test |
48 | | - public void testIntrospection() throws Exception { |
49 | | - AccessToken accessToken = oAuthClient.clientCredentialGrant().toSuccessResponse().getTokens().getAccessToken(); |
50 | | - TokenIntrospectionResponse introspectionResponse = oAuthClient.introspection(accessToken); |
51 | | - Assertions.assertTrue(introspectionResponse.indicatesSuccess()); |
52 | | - Assertions.assertNotNull(introspectionResponse.toSuccessResponse().getIssuer()); |
| 37 | + public void testConfig() { |
| 38 | + Assertions.assertEquals(managedRealm.getName(), oAuthClient.config().getRealm()); |
| 39 | + Assertions.assertEquals(managedRealm.getBaseUrl() + "/protocol/openid-connect/token", oAuthClient.getEndpoints().getToken()); |
53 | 40 | } |
54 | 41 |
|
55 | 42 | @Test |
56 | | - public void testAuthorizationCode() throws Exception { |
57 | | - URL authorizationRequestURL = oAuthClient.authorizationRequest(); |
58 | | - webDriver.navigate().to(authorizationRequestURL); |
59 | | - loginPage.fillLogin(user.getUsername(), user.getPassword()); |
60 | | - loginPage.submit(); |
| 43 | + public void testPasswordGrant() { |
| 44 | + AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword()); |
| 45 | + Assertions.assertTrue(accessTokenResponse.isSuccess()); |
61 | 46 |
|
62 | | - Assertions.assertEquals(1, oAuthClient.getCallbacks().size()); |
| 47 | + accessTokenResponse = oAuthClient.passwordGrantRequest(user.getUsername(), "invalid").send(); |
| 48 | + Assertions.assertFalse(accessTokenResponse.isSuccess()); |
| 49 | + Assertions.assertEquals("Invalid user credentials", accessTokenResponse.getErrorDescription()); |
| 50 | + } |
63 | 51 |
|
64 | | - URI callbackUri = oAuthClient.getCallbacks().remove(0); |
| 52 | + @Test |
| 53 | + public void testClientCredential() { |
| 54 | + AccessTokenResponse accessTokenResponse = oAuthClient.doClientCredentialsGrantAccessTokenRequest(); |
| 55 | + Assertions.assertTrue(accessTokenResponse.isSuccess()); |
| 56 | + } |
65 | 57 |
|
66 | | - AuthorizationResponse authorizationResponse = AuthorizationResponse.parse(callbackUri); |
67 | | - Assertions.assertTrue(authorizationResponse.indicatesSuccess()); |
68 | | - Assertions.assertNotNull(authorizationResponse.toSuccessResponse().getAuthorizationCode()); |
| 58 | + @Test |
| 59 | + public void testRefresh() { |
| 60 | + AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword()); |
69 | 61 |
|
70 | | - TokenResponse tokenResponse = oAuthClient.tokenRequest(authorizationResponse.toSuccessResponse().getAuthorizationCode()); |
71 | | - Assertions.assertTrue(tokenResponse.indicatesSuccess()); |
72 | | - Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken()); |
| 62 | + AccessTokenResponse refreshResponse = oAuthClient.doRefreshTokenRequest(accessTokenResponse.getRefreshToken()); |
| 63 | + Assertions.assertTrue(refreshResponse.isSuccess()); |
| 64 | + Assertions.assertNotEquals(accessTokenResponse.getAccessToken(), refreshResponse.getAccessToken()); |
73 | 65 | } |
74 | 66 |
|
75 | 67 | @Test |
76 | | - public void testAccessTokenRevocation() throws Exception { |
77 | | - TokenResponse tokenResponse = oAuthClient.clientCredentialGrant(); |
78 | | - Assertions.assertTrue(tokenResponse.indicatesSuccess()); |
79 | | - Assertions.assertNotNull(tokenResponse.toSuccessResponse().getTokens().getAccessToken()); |
| 68 | + public void testRevocation() { |
| 69 | + AccessTokenResponse accessTokenResponse = oAuthClient.doPasswordGrantRequest(user.getUsername(), user.getPassword()); |
80 | 70 |
|
81 | | - AccessToken accessToken = tokenResponse.toSuccessResponse().getTokens().getAccessToken(); |
82 | | - TokenIntrospectionResponse introspectionResponse = oAuthClient.introspection(accessToken); |
83 | | - Assertions.assertTrue(introspectionResponse.indicatesSuccess()); |
84 | | - Assertions.assertNotNull(introspectionResponse.toSuccessResponse().getScope()); |
| 71 | + TokenRevocationResponse tokenRevocationResponse = oAuthClient.doTokenRevoke(accessTokenResponse.getRefreshToken()); |
| 72 | + Assertions.assertTrue(tokenRevocationResponse.isSuccess()); |
85 | 73 |
|
86 | | - Assertions.assertEquals(Response.Status.OK.getStatusCode(), oAuthClient.revokeAccessToken(accessToken).getStatusCode()); |
| 74 | + AccessTokenResponse refreshResponse = oAuthClient.doRefreshTokenRequest(accessTokenResponse.getRefreshToken()); |
| 75 | + Assertions.assertFalse(refreshResponse.isSuccess()); |
| 76 | + } |
| 77 | + |
| 78 | + public static class OAuthClientConfig implements ClientConfig { |
87 | 79 |
|
88 | | - introspectionResponse = oAuthClient.introspection(accessToken); |
89 | | - Assertions.assertTrue(introspectionResponse.indicatesSuccess()); |
90 | | - Assertions.assertNull(introspectionResponse.toSuccessResponse().getScope()); |
| 80 | + @Override |
| 81 | + public ClientConfigBuilder configure(ClientConfigBuilder client) { |
| 82 | + return client.clientId("myclient").secret("mysecret").directAccessGrants().serviceAccount(); |
| 83 | + } |
91 | 84 | } |
92 | 85 |
|
93 | 86 | public static class OAuthUserConfig implements UserConfig { |
94 | 87 |
|
95 | 88 | @Override |
96 | 89 | public UserConfigBuilder configure(UserConfigBuilder user) { |
97 | | - return user.name("First", "Last") |
| 90 | + return user.username("myuser").name("First", "Last") |
98 | 91 | .email("test@local") |
99 | 92 | .password("password"); |
100 | 93 | } |
|
0 commit comments