Skip to content

Commit a0e5971

Browse files
authored
Add support for passwords with special characters to OAUTH2 login. (#345)
1 parent be337eb commit a0e5971

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ To utilize GitLab4J™ API in your Java project, simply add the following de
1212
```java
1313
dependencies {
1414
...
15-
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.10.11'
15+
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.10.12'
1616
}
1717
```
1818

@@ -23,7 +23,7 @@ dependencies {
2323
<dependency>
2424
<groupId>org.gitlab4j</groupId>
2525
<artifactId>gitlab4j-api</artifactId>
26-
<version>4.10.11</version>
26+
<version>4.10.12</version>
2727
</dependency>
2828
```
2929

src/main/java/org/gitlab4j/api/utils/Oauth2LoginStreamingOutput.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.gitlab4j.api.utils;
22

3+
import java.io.BufferedWriter;
34
import java.io.IOException;
45
import java.io.OutputStream;
5-
import java.io.PrintWriter;
6+
import java.io.OutputStreamWriter;
7+
import java.io.Writer;
8+
import java.nio.charset.StandardCharsets;
69

710
import javax.ws.rs.WebApplicationException;
811
import javax.ws.rs.core.StreamingOutput;
@@ -30,7 +33,7 @@ public Oauth2LoginStreamingOutput(String username, char[] password) {
3033
@Override
3134
public void write(OutputStream output) throws IOException, WebApplicationException {
3235

33-
PrintWriter writer = new PrintWriter(output);
36+
Writer writer = new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8));
3437
writer.write("{ ");
3538
writer.write("\"grant_type\": \"password\", ");
3639
writer.write("\"username\": \"" + username + "\", ");
@@ -39,8 +42,15 @@ public void write(OutputStream output) throws IOException, WebApplicationExcepti
3942
// Output the quoted password
4043
writer.write('"');
4144
for (int i = 0, length = password.length(); i < length; i++) {
42-
writer.write(password.charAt(i));
43-
}
45+
46+
char c = password.charAt(i);
47+
if (c == '"' || c == '\\') {
48+
writer.write('\\');
49+
}
50+
51+
writer.write(c);
52+
}
53+
4454
writer.write('"');
4555

4656
writer.write(" }");

src/test/java/org/gitlab4j/api/JsonUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public class JsonUtils {
2525
jacksonJson.getObjectMapper().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
2626
}
2727

28+
static JsonNode readTreeFromString(String jsonString) throws JsonParseException, JsonMappingException, IOException {
29+
return (jacksonJson.readTree(jsonString));
30+
}
31+
2832
static JsonNode readTreeFromResource(String filename) throws JsonParseException, JsonMappingException, IOException {
2933
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
3034
return (jacksonJson.readTree(reader));
@@ -73,7 +77,6 @@ static <T> Map<String, T> unmarshalMap(Class<T> returnType, String json) throws
7377
return (jacksonJson.unmarshalMap(returnType, json));
7478
}
7579

76-
7780
static <T> boolean compareJson(T apiObject, String filename) throws IOException {
7881
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
7982
return (compareJson(apiObject, reader));
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.gitlab4j.api;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.nio.charset.StandardCharsets;
7+
8+
import org.gitlab4j.api.utils.Oauth2LoginStreamingOutput;
9+
import org.junit.Test;
10+
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
13+
public class TestOauth2LoginStreamingOutput {
14+
15+
private static final String USERNAME = "test-user";
16+
17+
@Test
18+
public void testPasswordsWithBackslashes() throws Exception {
19+
20+
final String password = "Password with \\backslashes\\";
21+
try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) {
22+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
23+
oauth2Stream.write(stream);
24+
25+
String json = stream.toString(StandardCharsets.UTF_8.name());
26+
System.out.println(json);
27+
JsonNode tree = JsonUtils.readTreeFromString(json);
28+
assertEquals(password, tree.path("password").asText());
29+
}
30+
}
31+
32+
@Test
33+
public void testPasswordsWithDoubleQuotes() throws Exception {
34+
35+
final String password = "Password with \"double quotes\"";
36+
try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) {
37+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
38+
oauth2Stream.write(stream);
39+
40+
String json = stream.toString(StandardCharsets.UTF_8.name());
41+
System.out.println(json);
42+
JsonNode tree = JsonUtils.readTreeFromString(json);
43+
assertEquals(password, tree.path("password").asText());
44+
}
45+
}
46+
47+
@Test
48+
public void testPasswordsWithSpecialLetters() throws Exception {
49+
50+
final String password = "Password with special letters 'Ää - Öö - Üü - ẞ'";
51+
try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) {
52+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
53+
oauth2Stream.write(stream);
54+
55+
String json = stream.toString(StandardCharsets.UTF_8.name());
56+
System.out.println(json);
57+
JsonNode tree = JsonUtils.readTreeFromString(json);
58+
assertEquals(password, tree.path("password").asText());
59+
}
60+
}
61+
62+
@Test
63+
public void testPasswordsWithManySpecialChars() throws Exception {
64+
65+
final String password = "Password with many special chars '\\ - \" - [] - () - ~ - ! - ^ - ` - Ää - Öö - Üü - ẞ'";
66+
try (Oauth2LoginStreamingOutput oauth2Stream = new Oauth2LoginStreamingOutput(USERNAME, password)) {
67+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
68+
oauth2Stream.write(stream);
69+
70+
String json = stream.toString(StandardCharsets.UTF_8.name());
71+
System.out.println(json);
72+
JsonNode tree = JsonUtils.readTreeFromString(json);
73+
assertEquals(password, tree.path("password").asText());
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)