From 197091e52a45ca744940ddde417a10aa13addb57 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Mon, 13 Mar 2017 16:46:15 -0300 Subject: [PATCH] accept blanks, new line and carriage returns on json --- .../java/com/auth0/jwt/impl/JWTParser.java | 2 +- .../java/com/auth0/jwt/JWTDecoderTest.java | 4 ++-- .../com/auth0/jwt/impl/JWTParserTest.java | 20 +++---------------- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/lib/src/main/java/com/auth0/jwt/impl/JWTParser.java b/lib/src/main/java/com/auth0/jwt/impl/JWTParser.java index b633e1b2..1efb825e 100644 --- a/lib/src/main/java/com/auth0/jwt/impl/JWTParser.java +++ b/lib/src/main/java/com/auth0/jwt/impl/JWTParser.java @@ -50,7 +50,7 @@ static ObjectMapper getDefaultObjectMapper() { @SuppressWarnings("WeakerAccess") T convertFromJSON(String json, Class tClazz) throws JWTDecodeException { JWTDecodeException exception = new JWTDecodeException(String.format("The string '%s' doesn't have a valid JSON format.", json)); - if (json == null || !json.startsWith("{") || !json.endsWith("}")) { + if (json == null) { throw exception; } try { diff --git a/lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java b/lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java index a4ab4376..792a3735 100644 --- a/lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java +++ b/lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java @@ -47,7 +47,7 @@ public void shouldThrowIfMoreThan3Parts() throws Exception { @Test public void shouldThrowIfPayloadHasInvalidJSONFormat() throws Exception { String validJson = "{}"; - String invalidJson = "{}}{"; + String invalidJson = "}{"; exception.expect(JWTDecodeException.class); exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", invalidJson)); customJWT(validJson, invalidJson, "signature"); @@ -56,7 +56,7 @@ public void shouldThrowIfPayloadHasInvalidJSONFormat() throws Exception { @Test public void shouldThrowIfHeaderHasInvalidJSONFormat() throws Exception { String validJson = "{}"; - String invalidJson = "{}}{"; + String invalidJson = "}{"; exception.expect(JWTDecodeException.class); exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", invalidJson)); customJWT(invalidJson, validJson, "signature"); diff --git a/lib/src/test/java/com/auth0/jwt/impl/JWTParserTest.java b/lib/src/test/java/com/auth0/jwt/impl/JWTParserTest.java index 09ab42fc..1c990fcb 100644 --- a/lib/src/test/java/com/auth0/jwt/impl/JWTParserTest.java +++ b/lib/src/test/java/com/auth0/jwt/impl/JWTParserTest.java @@ -11,13 +11,12 @@ import org.junit.Test; import org.junit.rules.ExpectedException; -import java.io.IOException; - import static com.auth0.jwt.impl.JWTParser.getDefaultObjectMapper; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class JWTParserTest { @@ -45,19 +44,6 @@ public void shouldAddDeserializers() throws Exception { verify(mapper).registerModule(any(Module.class)); } - @Test - public void shouldThrowOnReadValueException() throws Exception { - String jsonPayload = "{}"; - exception.expect(JWTDecodeException.class); - exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", jsonPayload)); - - ObjectMapper mapper = mock(ObjectMapper.class); - when(mapper.readValue(eq(jsonPayload), eq(Object.class))).thenThrow(IOException.class); - JWTParser parser = new JWTParser(mapper); - - parser.convertFromJSON(jsonPayload, Object.class); - } - @Test public void shouldParsePayload() throws Exception { ObjectMapper mapper = mock(ObjectMapper.class); @@ -96,7 +82,7 @@ public void shouldThrowOnInvalidHeader() throws Exception { @Test public void shouldConvertFromValidJSON() throws Exception { - String json = "{}"; + String json = "\r\n { \r\n } \r\n"; Object object = parser.convertFromJSON(json, Object.class); assertThat(object, is(notNullValue())); }