Skip to content

Commit 26f1d4e

Browse files
authored
fixes #29 ingore extra properties from the token response parsing (#30)
1 parent d265c03 commit 26f1d4e

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

src/main/java/com/networknt/client/oauth/OauthHelper.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -579,31 +579,22 @@ public static String getEncodedString(TokenRequest request) throws UnsupportedEn
579579
private static Result<TokenResponse> handleResponse(ContentType contentType, String responseBody) {
580580
TokenResponse tokenResponse;
581581
Result<TokenResponse> result;
582-
if(logger.isTraceEnabled()) logger.trace("contentType = " + contentType + " responseBody = " + responseBody);
583-
try {
584-
//only accept json format response so that can map to a TokenResponse, otherwise escapes server's response and return to the client.
585-
if(!contentType.equals(ContentType.APPLICATION_JSON)) {
586-
return Failure.of(new Status(GET_TOKEN_ERROR, escapeBasedOnType(contentType, responseBody)));
587-
}
588-
if (responseBody != null && responseBody.length() > 0) {
589-
tokenResponse = Config.getInstance().getMapper().readValue(responseBody, TokenResponse.class);
590-
// sometimes, the token response contains an error status instead of the access token.
591-
if(tokenResponse != null && tokenResponse.getAccessToken() != null) {
592-
result = Success.of(tokenResponse);
593-
} else {
594-
result = Failure.of(new Status(tokenResponse.getStatusCode(), tokenResponse.getCode(), tokenResponse.getMessage(), tokenResponse.getDescription(), tokenResponse.getSeverity()));
595-
}
582+
if(logger.isTraceEnabled()) logger.trace("contentType = {} responseBody = {}", contentType, responseBody);
583+
//only accept json format response so that can map to a TokenResponse, otherwise escapes server's response and return to the client.
584+
if(!contentType.equals(ContentType.APPLICATION_JSON)) {
585+
return Failure.of(new Status(GET_TOKEN_ERROR, escapeBasedOnType(contentType, responseBody)));
586+
}
587+
if (responseBody != null && !responseBody.isEmpty()) {
588+
tokenResponse = JsonMapper.fromJson(responseBody, TokenResponse.class);
589+
// sometimes, the token response contains an error status instead of the access token.
590+
if(tokenResponse != null && tokenResponse.getAccessToken() != null) {
591+
result = Success.of(tokenResponse);
596592
} else {
597-
result = Failure.of(new Status(GET_TOKEN_ERROR, "no auth server response"));
598-
logger.error("Error in token retrieval, response = " + responseBody);
593+
result = Failure.of(new Status(tokenResponse.getStatusCode(), tokenResponse.getCode(), tokenResponse.getMessage(), tokenResponse.getDescription(), tokenResponse.getSeverity()));
599594
}
600-
} catch (UnrecognizedPropertyException e) {
601-
//in this case, cannot parse success token, which means the server doesn't response a successful token but some messages, we need to pass this message out.
602-
result = Failure.of(new Status(GET_TOKEN_ERROR, escapeBasedOnType(contentType, responseBody)));
603-
logger.error("Error in token parsing", e);
604-
} catch (IOException | RuntimeException e) {
605-
result = Failure.of(new Status(GET_TOKEN_ERROR, e.getMessage()));
606-
logger.error("Error in token retrieval", e);
595+
} else {
596+
result = Failure.of(new Status(GET_TOKEN_ERROR, "no auth server response"));
597+
logger.error("Error in token retrieval, response = {}", responseBody);
607598
}
608599
return result;
609600
}

src/main/java/com/networknt/client/oauth/TokenResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.networknt.client.oauth;
1818

19+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1920
import com.fasterxml.jackson.annotation.JsonProperty;
2021
import com.networknt.status.Status;
2122

@@ -26,6 +27,7 @@
2627
* @author Steve Hu
2728
*
2829
*/
30+
@JsonIgnoreProperties(ignoreUnknown = true)
2931
public class TokenResponse extends Status {
3032
@JsonProperty(value="access_token")
3133
private String accessToken;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.networknt.client.oauth;
2+
3+
import ch.qos.logback.core.subst.Token;
4+
import com.networknt.config.Config;
5+
import com.networknt.http.client.JsonMapper;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class TokenResponseTest {
9+
@Test
10+
public void testTokenResponseWithExtraProperties() {
11+
String json = "{\"access_token\":\"access_token\",\"token_type\":\"token_type\",\"scope\":\"scope\",\"signature\":\"signature\",\"id\":\"id\", \"issued_at\":1000}";
12+
try {
13+
TokenResponse tokenResponse = Config.getInstance().getMapper().readValue(json, TokenResponse.class);
14+
assert(tokenResponse.getAccessToken().equals("access_token"));
15+
} catch (Exception e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
20+
@Test
21+
public void testTokenResponseWithExtraPropertiesJsonMapper() {
22+
String json = "{\"access_token\":\"access_token\",\"token_type\":\"token_type\",\"scope\":\"scope\",\"signature\":\"signature\",\"id\":\"id\", \"issued_at\":1000}";
23+
TokenResponse tokenResponse = JsonMapper.fromJson(json, TokenResponse.class);
24+
assert(tokenResponse.getAccessToken().equals("access_token"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)