Skip to content

Commit 5e992a5

Browse files
committed
BAEL-4438 Add Postman Collections and Live test for Keycloack Endpoints
1 parent 07b7c19 commit 5e992a5

File tree

3 files changed

+978
-54
lines changed

3 files changed

+978
-54
lines changed
Lines changed: 156 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,169 @@
11
package com.baeldung.jwt;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
3+
import io.restassured.RestAssured;
4+
import io.restassured.response.Response;
5+
import java.util.Base64;
56
import java.util.HashMap;
67
import java.util.Map;
7-
88
import org.junit.Test;
99
import org.springframework.http.HttpHeaders;
1010
import org.springframework.http.HttpStatus;
1111

12-
import io.restassured.RestAssured;
13-
import io.restassured.response.Response;
12+
import static org.assertj.core.api.Assertions.assertThat;
1413

1514
public class AuthorizationServerLiveTest {
1615

17-
@Test
18-
public void givenAuthorizationCodeGrant_whenObtainAccessToken_thenSuccess() {
19-
String accessToken = obtainAccessToken();
20-
21-
assertThat(accessToken).isNotBlank();
22-
}
23-
24-
@Test
25-
public void whenServiceStartsAndLoadsRealmConfigurations_thenOidcDiscoveryEndpointIsAvailable() {
26-
final String oidcDiscoveryUrl = "http://localhost:8083/auth/realms/baeldung/.well-known/openid-configuration";
27-
28-
Response response = RestAssured.given().redirects().follow(false).get(oidcDiscoveryUrl);
29-
30-
assertThat(HttpStatus.OK.value()).isEqualTo(response.getStatusCode());
31-
System.out.println(response.asString());
32-
assertThat(response.jsonPath().getMap("$.")).containsKeys("issuer", "authorization_endpoint", "token_endpoint",
33-
"userinfo_endpoint");
34-
}
35-
36-
private String obtainAccessToken() {
37-
final String redirectUrl = "http://localhost:8082/jwt-client/login/oauth2/code/custom";
38-
final String authorizeUrl = "http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/auth?response_type=code&client_id=jwtClient&scope=read&redirect_uri="
39-
+ redirectUrl;
40-
final String tokenUrl = "http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/token";
41-
// obtain authentication url with custom codes
42-
Response response = RestAssured.given().redirects().follow(false).get(authorizeUrl);
43-
String authSessionId = response.getCookie("AUTH_SESSION_ID");
44-
String kcPostAuthenticationUrl = response.asString().split("action=\"")[1].split("\"")[0].replace("&", "&");
45-
46-
// obtain authentication code and state
47-
response = RestAssured.given().redirects().follow(false).cookie("AUTH_SESSION_ID", authSessionId)
48-
.formParams("username", "john@test.com", "password", "123", "credentialId", "")
49-
.post(kcPostAuthenticationUrl);
50-
assertThat(HttpStatus.FOUND.value()).isEqualTo(response.getStatusCode());
51-
52-
// extract authorization code
53-
String location = response.getHeader(HttpHeaders.LOCATION);
54-
String code = location.split("code=")[1].split("&")[0];
55-
56-
// get access token
57-
Map<String, String> params = new HashMap<String, String>();
58-
params.put("grant_type", "authorization_code");
59-
params.put("code", code);
60-
params.put("client_id", "jwtClient");
61-
params.put("redirect_uri", redirectUrl);
62-
params.put("client_secret", "jwtClientSecret");
63-
response = RestAssured.given().formParams(params).post(tokenUrl);
64-
return response.jsonPath().getString("access_token");
65-
}
16+
public static final String AUTHORIZE_CODE_URL = "http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/auth?response_type=code&client_id=jwtClient&scope=read&redirect_uri=";
17+
public static final String TOKEN_URL = "http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/token";
18+
public static final String INTROSPECT_URL = "http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/token/introspect";
19+
public static final String USER_INFO_URL = "http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/userinfo";
20+
21+
public static final String JWT_CLIENT_SECRET = "jwtClientSecret";
22+
public static final String JWT_CLIENT = "jwtClient";
23+
public static final String REDIRECT_URL = "http://localhost:8084/";
24+
25+
public static final String USERNAME = "john@test.com";
26+
public static final String PASSWORD = "123";
27+
public static final String OIDC_DISCOVERY_URL = "http://localhost:8083/auth/realms/baeldung/.well-known/openid-configuration";
28+
29+
@Test
30+
public void givenAuthorizationCodeGrant_whenObtainAccessToken_thenSuccess() {
31+
String accessToken = obtainTokens().accessToken;
32+
33+
assertThat(accessToken).isNotBlank();
34+
}
35+
36+
@Test
37+
public void givenRefreshTokenGrantAndValidRefreshToken_whenObtainAccess_thenSuccess() {
38+
39+
final String tokenUrl = "http://localhost:8083/auth/realms/baeldung/protocol/openid-connect/token";
40+
String refreshToken = obtainTokens().refreshToken;
41+
assertThat(refreshToken).isNotBlank();
42+
43+
Map<String, String> params = new HashMap<>();
44+
params.put("client_id", "jwtClient");
45+
params.put("client_secret", "jwtClientSecret");
46+
params.put("grant_type", "refresh_token");
47+
params.put("refresh_token", refreshToken);
48+
Response response = RestAssured
49+
.given()
50+
.formParams(params)
51+
.post(tokenUrl);
52+
assertThat(response
53+
.jsonPath()
54+
.getString("access_token")).isNotBlank();
55+
}
56+
57+
@Test
58+
public void givenPasswordGrant_whenObtainAccessToken_thenSuccess() {
59+
Map<String, String> params = new HashMap<>();
60+
params.put("client_id", JWT_CLIENT);
61+
params.put("client_secret", JWT_CLIENT_SECRET);
62+
params.put("grant_type", "password");
63+
params.put("username", USERNAME);
64+
params.put("password", PASSWORD);
65+
Response response = RestAssured
66+
.given()
67+
.formParams(params)
68+
.post(TOKEN_URL);
69+
assertThat(response
70+
.jsonPath()
71+
.getString("access_token")).isNotBlank();
72+
}
73+
74+
@Test
75+
public void givenAccessTokenWithProfileScope_whenGetUserProfile_thenUsernameIsMatched(){
76+
String accessToken = obtainTokens().accessToken;
77+
Response response = RestAssured
78+
.given()
79+
.header("Authorization", String.format("Bearer %s", accessToken))
80+
.get(USER_INFO_URL);
81+
assertThat(response
82+
.jsonPath()
83+
.getString("preferred_username")).isEqualTo(USERNAME);
84+
}
85+
86+
@Test
87+
public void givenAccessToken_whenIntrospect_thenTokenIsActive(){
88+
String accessToken = obtainTokens().accessToken;
89+
Response response = RestAssured
90+
.given()
91+
.header("Authorization", String.format("Basic %s", new String(Base64.getEncoder().encode((JWT_CLIENT + ":" + JWT_CLIENT_SECRET).getBytes()))))
92+
.formParam("token", accessToken)
93+
.post(INTROSPECT_URL);
94+
assertThat(response
95+
.jsonPath()
96+
.getBoolean("active")).isTrue();
97+
}
98+
99+
@Test
100+
public void whenServiceStartsAndLoadsRealmConfigurations_thenOidcDiscoveryEndpointIsAvailable() {
101+
Response response = RestAssured
102+
.given()
103+
.redirects()
104+
.follow(false)
105+
.get(OIDC_DISCOVERY_URL);
106+
107+
assertThat(HttpStatus.OK.value()).isEqualTo(response.getStatusCode());
108+
System.out.println(response.asString());
109+
assertThat(response
110+
.jsonPath()
111+
.getMap("$.")).containsKeys("issuer", "authorization_endpoint", "token_endpoint", "userinfo_endpoint");
112+
}
113+
114+
private Tokens obtainTokens() {
115+
final String authorizeUrl = AUTHORIZE_CODE_URL + REDIRECT_URL;
116+
// obtain authentication url with custom codes
117+
Response response = RestAssured
118+
.given()
119+
.redirects()
120+
.follow(false)
121+
.get(authorizeUrl);
122+
String authSessionId = response.getCookie("AUTH_SESSION_ID");
123+
String kcPostAuthenticationUrl = response
124+
.asString()
125+
.split("action=\"")[1].split("\"")[0].replace("&amp;", "&");
126+
127+
// obtain authentication code and state
128+
response = RestAssured
129+
.given()
130+
.redirects()
131+
.follow(false)
132+
.cookie("AUTH_SESSION_ID", authSessionId)
133+
.formParams("username", USERNAME, "password", PASSWORD, "credentialId", "")
134+
.post(kcPostAuthenticationUrl);
135+
assertThat(HttpStatus.FOUND.value()).isEqualTo(response.getStatusCode());
136+
137+
// extract authorization code
138+
String location = response.getHeader(HttpHeaders.LOCATION);
139+
String code = location.split("code=")[1].split("&")[0];
140+
141+
// get access token
142+
Map<String, String> params = new HashMap<>();
143+
params.put("grant_type", "authorization_code");
144+
params.put("code", code);
145+
params.put("client_id", "jwtClient");
146+
params.put("redirect_uri", REDIRECT_URL);
147+
params.put("client_secret", "jwtClientSecret");
148+
response = RestAssured
149+
.given()
150+
.formParams(params)
151+
.post(TOKEN_URL);
152+
return new Tokens(response
153+
.jsonPath()
154+
.getString("access_token"), response
155+
.jsonPath()
156+
.getString("refresh_token"));
157+
}
158+
159+
private static class Tokens {
160+
private final String accessToken;
161+
private final String refreshToken;
162+
163+
public Tokens(String accessToken, String refreshToken) {
164+
this.accessToken = accessToken;
165+
this.refreshToken = refreshToken;
166+
}
167+
}
66168

67169
}

0 commit comments

Comments
 (0)