Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit 0cc957f

Browse files
author
Jason
authored
DefaultTokenResponse.toJson did not include id_token in the JSON #1239 (#1240)
* DefaultTokenResponse.toJson did not include id_token in the JSON #1239
1 parent f699791 commit 0cc957f

File tree

3 files changed

+129
-8
lines changed

3 files changed

+129
-8
lines changed

extensions/oauth/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
<groupId>org.slf4j</groupId>
5959
<artifactId>jcl-over-slf4j</artifactId>
6060
</dependency>
61+
<dependency>
62+
<groupId>org.hamcrest</groupId>
63+
<artifactId>hamcrest-library</artifactId>
64+
<scope>test</scope>
65+
</dependency>
6166
</dependencies>
6267

6368
</project>

extensions/oauth/src/main/java/com/stormpath/sdk/impl/oauth/authz/DefaultTokenResponse.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.stormpath.sdk.impl.oauth.authz;
22

33
import com.stormpath.sdk.lang.Assert;
4+
import com.stormpath.sdk.lang.Strings;
45
import com.stormpath.sdk.oauth.TokenResponse;
56
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
6-
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
7-
import org.apache.oltu.oauth2.common.message.OAuthResponse;
87
import org.apache.oltu.oauth2.common.message.types.TokenType;
8+
import org.json.JSONObject;
99

1010
import javax.servlet.http.HttpServletResponse;
1111

@@ -26,7 +26,7 @@ public class DefaultTokenResponse implements TokenResponse {
2626

2727
private final String applicationHref;
2828

29-
private final OAuthResponse oAuthResponse;
29+
private final JSONObject oAuthResponse;
3030
private final String idToken;
3131

3232
private DefaultTokenResponse(Builder builder) {
@@ -42,10 +42,22 @@ private DefaultTokenResponse(Builder builder) {
4242
Assert.hasText(expiresIn);
4343
Assert.hasText(applicationHref);
4444

45-
try {
46-
oAuthResponse = builder.tokenResponseBuilder.buildJSONMessage();
47-
} catch (OAuthSystemException e) {
48-
throw new IllegalStateException("Unexpected error when building Json OAuth response.", e);
45+
oAuthResponse = new JSONObject();
46+
initOAuthResponse();
47+
}
48+
49+
private void initOAuthResponse() {
50+
oAuthResponse.put("token_type", tokenType);
51+
oAuthResponse.put("access_token", accessToken);
52+
oAuthResponse.put("expires_in", Long.parseLong(expiresIn));
53+
if (Strings.hasText(scope)) {
54+
oAuthResponse.put("scope", scope);
55+
}
56+
if (Strings.hasText(refreshToken)) {
57+
oAuthResponse.put("refresh_token", refreshToken);
58+
}
59+
if (Strings.hasText(idToken)) {
60+
oAuthResponse.put("id_token", idToken);
4961
}
5062
}
5163

@@ -81,7 +93,7 @@ public String getApplicationHref() {
8193

8294
@Override
8395
public String toJson() {
84-
return oAuthResponse.getBody();
96+
return oAuthResponse.toString();
8597
}
8698

8799
public static Builder tokenType(TokenType tokenType) {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.stormpath.sdk.impl.oauth.authz
2+
3+
import com.stormpath.sdk.oauth.TokenResponse
4+
import org.apache.oltu.oauth2.common.message.types.TokenType
5+
import org.hamcrest.Matchers
6+
import org.json.JSONObject
7+
import org.testng.annotations.Test
8+
9+
import static Matchers.is
10+
import static org.hamcrest.MatcherAssert.assertThat
11+
12+
class DefaultTokenResponseTest {
13+
14+
public static final String APP_HREF = "http://test.app.href.com"
15+
public static final String ACCESS_TOKEN = "testAccessToken"
16+
public static final String REFRESH_TOKEN = "testRefreshToken"
17+
public static final String ID_TOKEN = "testIdToken"
18+
public static final String SCOPE = "test scope"
19+
public static final String EXPIRES_IN = "3600"
20+
public static final String TOKEN_TYPE = "Bearer"
21+
22+
@Test
23+
void testBuildCompleteResponse() {
24+
TokenResponse tokenResponse = DefaultTokenResponse.tokenType(TokenType.BEARER)
25+
.accessToken(ACCESS_TOKEN)
26+
.refreshToken(REFRESH_TOKEN)
27+
.idToken(ID_TOKEN)
28+
.scope(SCOPE)
29+
.expiresIn(EXPIRES_IN)
30+
.applicationHref(APP_HREF)
31+
.build()
32+
assertThat(tokenResponse.tokenType, is(TOKEN_TYPE))
33+
assertThat(tokenResponse.accessToken, is(ACCESS_TOKEN))
34+
assertThat(tokenResponse.refreshToken, is(REFRESH_TOKEN))
35+
assertThat(tokenResponse.idToken, is(ID_TOKEN))
36+
assertThat(tokenResponse.scope, is(SCOPE))
37+
assertThat(tokenResponse.expiresIn, is(EXPIRES_IN))
38+
assertThat(tokenResponse.applicationHref, is(APP_HREF))
39+
}
40+
41+
@Test
42+
void testJsonWithOnlyAccessToken() {
43+
TokenResponse tokenResponse = DefaultTokenResponse.tokenType(TokenType.BEARER)
44+
.accessToken(ACCESS_TOKEN)
45+
.expiresIn(EXPIRES_IN)
46+
.scope(SCOPE)
47+
.applicationHref(APP_HREF)
48+
.build()
49+
50+
String json = tokenResponse.toJson()
51+
JSONObject actual = new JSONObject(json)
52+
assertField(actual, "token_type", TOKEN_TYPE)
53+
assertField(actual, "access_token", ACCESS_TOKEN)
54+
assertField(actual, "expires_in", EXPIRES_IN)
55+
assertField(actual, "scope", SCOPE)
56+
assertNoField(actual, "refresh_token")
57+
assertNoField(actual, "id_token")
58+
}
59+
60+
@Test
61+
void testJsonWithAccessAndRefreshTokens() {
62+
TokenResponse tokenResponse = DefaultTokenResponse.tokenType(TokenType.BEARER)
63+
.accessToken(ACCESS_TOKEN)
64+
.refreshToken(REFRESH_TOKEN)
65+
.expiresIn(EXPIRES_IN)
66+
.applicationHref(APP_HREF)
67+
.build()
68+
69+
String json = tokenResponse.toJson()
70+
JSONObject actual = new JSONObject(json)
71+
assertField(actual, "token_type", TOKEN_TYPE)
72+
assertField(actual, "access_token", ACCESS_TOKEN)
73+
assertField(actual, "refresh_token", REFRESH_TOKEN)
74+
assertField(actual, "expires_in", EXPIRES_IN)
75+
assertNoField(actual, "id_token")
76+
}
77+
78+
@Test
79+
void testJsonWithAccessAndRefreshAndIdTokens() {
80+
TokenResponse tokenResponse = DefaultTokenResponse.tokenType(TokenType.BEARER)
81+
.accessToken(ACCESS_TOKEN)
82+
.refreshToken(REFRESH_TOKEN)
83+
.idToken(ID_TOKEN)
84+
.expiresIn(EXPIRES_IN)
85+
.applicationHref(APP_HREF)
86+
.build()
87+
88+
String json = tokenResponse.toJson()
89+
JSONObject actual = new JSONObject(json)
90+
assertField(actual, "token_type", TOKEN_TYPE)
91+
assertField(actual, "access_token", ACCESS_TOKEN)
92+
assertField(actual, "refresh_token", REFRESH_TOKEN)
93+
assertField(actual, "id_token", ID_TOKEN)
94+
assertField(actual, "expires_in", EXPIRES_IN)
95+
}
96+
97+
private static void assertField(JSONObject actual, String field, String expected) {
98+
assertThat("${field} in ${actual.toString(2)}", actual.optString(field), is(expected))
99+
}
100+
101+
private static void assertNoField(JSONObject actual, String field) {
102+
assertThat("${field} present in ${actual.toString(2)}", actual.has(field), is(false))
103+
}
104+
}

0 commit comments

Comments
 (0)