From 4b0951790dfcd3d9304c54e7cc7f14f2a47e1511 Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Thu, 9 May 2019 10:32:50 -0700 Subject: [PATCH 1/2] Bumped version. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3bcc15ff7..d21dda57d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ To utilize GitLab4J™ API in your Java project, simply add the following de ```java dependencies { ... - compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.10.11' + compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.10.12' } ``` @@ -23,7 +23,7 @@ dependencies { org.gitlab4j gitlab4j-api - 4.10.11 + 4.10.12 ``` From 89d7e407bbd8491edf38d0abef8c6d3f2e8b253a Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Thu, 9 May 2019 10:46:57 -0700 Subject: [PATCH 2/2] Added support to OAUTH2 login for passwords with special characters (#343). --- .../api/utils/Oauth2LoginStreamingOutput.java | 18 ++++- src/test/java/org/gitlab4j/api/JsonUtils.java | 5 +- .../api/TestOauth2LoginStreamingOutput.java | 76 +++++++++++++++++++ 3 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/gitlab4j/api/TestOauth2LoginStreamingOutput.java diff --git a/src/main/java/org/gitlab4j/api/utils/Oauth2LoginStreamingOutput.java b/src/main/java/org/gitlab4j/api/utils/Oauth2LoginStreamingOutput.java index b829315b0..7472983ce 100644 --- a/src/main/java/org/gitlab4j/api/utils/Oauth2LoginStreamingOutput.java +++ b/src/main/java/org/gitlab4j/api/utils/Oauth2LoginStreamingOutput.java @@ -1,8 +1,11 @@ package org.gitlab4j.api.utils; +import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; -import java.io.PrintWriter; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.StreamingOutput; @@ -30,7 +33,7 @@ public Oauth2LoginStreamingOutput(String username, char[] password) { @Override public void write(OutputStream output) throws IOException, WebApplicationException { - PrintWriter writer = new PrintWriter(output); + Writer writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)); writer.write("{ "); writer.write("\"grant_type\": \"password\", "); writer.write("\"username\": \"" + username + "\", "); @@ -39,8 +42,15 @@ public void write(OutputStream output) throws IOException, WebApplicationExcepti // Output the quoted password writer.write('"'); for (int i = 0, length = password.length(); i < length; i++) { - writer.write(password.charAt(i)); - } + + char c = password.charAt(i); + if (c == '"' || c == '\\') { + writer.write('\\'); + } + + writer.write(c); + } + writer.write('"'); writer.write(" }"); diff --git a/src/test/java/org/gitlab4j/api/JsonUtils.java b/src/test/java/org/gitlab4j/api/JsonUtils.java index 9bcbf149b..d7b8fcffa 100644 --- a/src/test/java/org/gitlab4j/api/JsonUtils.java +++ b/src/test/java/org/gitlab4j/api/JsonUtils.java @@ -25,6 +25,10 @@ public class JsonUtils { jacksonJson.getObjectMapper().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); } + static JsonNode readTreeFromString(String jsonString) throws JsonParseException, JsonMappingException, IOException { + return (jacksonJson.readTree(jsonString)); + } + static JsonNode readTreeFromResource(String filename) throws JsonParseException, JsonMappingException, IOException { InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename)); return (jacksonJson.readTree(reader)); @@ -73,7 +77,6 @@ static Map unmarshalMap(Class returnType, String json) throws return (jacksonJson.unmarshalMap(returnType, json)); } - static boolean compareJson(T apiObject, String filename) throws IOException { InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename)); return (compareJson(apiObject, reader)); diff --git a/src/test/java/org/gitlab4j/api/TestOauth2LoginStreamingOutput.java b/src/test/java/org/gitlab4j/api/TestOauth2LoginStreamingOutput.java new file mode 100644 index 000000000..98d0136af --- /dev/null +++ b/src/test/java/org/gitlab4j/api/TestOauth2LoginStreamingOutput.java @@ -0,0 +1,76 @@ +package org.gitlab4j.api; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; + +import org.gitlab4j.api.utils.Oauth2LoginStreamingOutput; +import org.junit.Test; + +import com.fasterxml.jackson.databind.JsonNode; + +public class TestOauth2LoginStreamingOutput { + + private static final String USERNAME = "test-user"; + + @Test + public void testPasswordsWithBackslashes() throws Exception { + + final String password = "Password with \\backslashes\\"; + try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + oauth2Stream.write(stream); + + String json = stream.toString(StandardCharsets.UTF_8.name()); + System.out.println(json); + JsonNode tree = JsonUtils.readTreeFromString(json); + assertEquals(password, tree.path("password").asText()); + } + } + + @Test + public void testPasswordsWithDoubleQuotes() throws Exception { + + final String password = "Password with \"double quotes\""; + try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + oauth2Stream.write(stream); + + String json = stream.toString(StandardCharsets.UTF_8.name()); + System.out.println(json); + JsonNode tree = JsonUtils.readTreeFromString(json); + assertEquals(password, tree.path("password").asText()); + } + } + + @Test + public void testPasswordsWithSpecialLetters() throws Exception { + + final String password = "Password with special letters 'Ää - Öö - Üü - ẞ'"; + try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + oauth2Stream.write(stream); + + String json = stream.toString(StandardCharsets.UTF_8.name()); + System.out.println(json); + JsonNode tree = JsonUtils.readTreeFromString(json); + assertEquals(password, tree.path("password").asText()); + } + } + + @Test + public void testPasswordsWithManySpecialChars() throws Exception { + + final String password = "Password with many special chars '\\ - \" - [] - () - ~ - ! - ^ - ` - Ää - Öö - Üü - ẞ'"; + try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + oauth2Stream.write(stream); + + String json = stream.toString(StandardCharsets.UTF_8.name()); + System.out.println(json); + JsonNode tree = JsonUtils.readTreeFromString(json); + assertEquals(password, tree.path("password").asText()); + } + } +}