Skip to content

Commit d84aa9d

Browse files
authored
Merge pull request auth0#151 from auth0/fixed-json-parsing
Accept blanks, new line and carriage returns on JSON
2 parents 1d85d27 + 197091e commit d84aa9d

File tree

3 files changed

+6
-20
lines changed

3 files changed

+6
-20
lines changed

lib/src/main/java/com/auth0/jwt/impl/JWTParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static ObjectMapper getDefaultObjectMapper() {
5050
@SuppressWarnings("WeakerAccess")
5151
<T> T convertFromJSON(String json, Class<T> tClazz) throws JWTDecodeException {
5252
JWTDecodeException exception = new JWTDecodeException(String.format("The string '%s' doesn't have a valid JSON format.", json));
53-
if (json == null || !json.startsWith("{") || !json.endsWith("}")) {
53+
if (json == null) {
5454
throw exception;
5555
}
5656
try {

lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void shouldThrowIfMoreThan3Parts() throws Exception {
4747
@Test
4848
public void shouldThrowIfPayloadHasInvalidJSONFormat() throws Exception {
4949
String validJson = "{}";
50-
String invalidJson = "{}}{";
50+
String invalidJson = "}{";
5151
exception.expect(JWTDecodeException.class);
5252
exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", invalidJson));
5353
customJWT(validJson, invalidJson, "signature");
@@ -56,7 +56,7 @@ public void shouldThrowIfPayloadHasInvalidJSONFormat() throws Exception {
5656
@Test
5757
public void shouldThrowIfHeaderHasInvalidJSONFormat() throws Exception {
5858
String validJson = "{}";
59-
String invalidJson = "{}}{";
59+
String invalidJson = "}{";
6060
exception.expect(JWTDecodeException.class);
6161
exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", invalidJson));
6262
customJWT(invalidJson, validJson, "signature");

lib/src/test/java/com/auth0/jwt/impl/JWTParserTest.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
import org.junit.Test;
1212
import org.junit.rules.ExpectedException;
1313

14-
import java.io.IOException;
15-
1614
import static com.auth0.jwt.impl.JWTParser.getDefaultObjectMapper;
1715
import static org.hamcrest.MatcherAssert.assertThat;
1816
import static org.hamcrest.Matchers.*;
1917
import static org.mockito.ArgumentMatchers.any;
20-
import static org.mockito.Mockito.*;
18+
import static org.mockito.Mockito.mock;
19+
import static org.mockito.Mockito.verify;
2120

2221
public class JWTParserTest {
2322

@@ -45,19 +44,6 @@ public void shouldAddDeserializers() throws Exception {
4544
verify(mapper).registerModule(any(Module.class));
4645
}
4746

48-
@Test
49-
public void shouldThrowOnReadValueException() throws Exception {
50-
String jsonPayload = "{}";
51-
exception.expect(JWTDecodeException.class);
52-
exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", jsonPayload));
53-
54-
ObjectMapper mapper = mock(ObjectMapper.class);
55-
when(mapper.readValue(eq(jsonPayload), eq(Object.class))).thenThrow(IOException.class);
56-
JWTParser parser = new JWTParser(mapper);
57-
58-
parser.convertFromJSON(jsonPayload, Object.class);
59-
}
60-
6147
@Test
6248
public void shouldParsePayload() throws Exception {
6349
ObjectMapper mapper = mock(ObjectMapper.class);
@@ -96,7 +82,7 @@ public void shouldThrowOnInvalidHeader() throws Exception {
9682

9783
@Test
9884
public void shouldConvertFromValidJSON() throws Exception {
99-
String json = "{}";
85+
String json = "\r\n { \r\n } \r\n";
10086
Object object = parser.convertFromJSON(json, Object.class);
10187
assertThat(object, is(notNullValue()));
10288
}

0 commit comments

Comments
 (0)