Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
a0e59719
Unverified
Commit
a0e59719
authored
May 09, 2019
by
Greg Messner
Committed by
GitHub
May 09, 2019
Browse files
Add support for passwords with special characters to OAUTH2 login. (#345)
parent
be337ebb
Changes
4
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
a0e59719
...
@@ -12,7 +12,7 @@ To utilize GitLab4J™ API in your Java project, simply add the following de
...
@@ -12,7 +12,7 @@ To utilize GitLab4J™ API in your Java project, simply add the following de
```
java
```
java
dependencies
{
dependencies
{
...
...
compile
group:
'
org
.
gitlab4j
'
,
name:
'
gitlab4j
-
api
'
,
version:
'
4.10
.
1
1
'
compile
group:
'
org
.
gitlab4j
'
,
name:
'
gitlab4j
-
api
'
,
version:
'
4.10
.
1
2
'
}
}
```
```
...
@@ -23,7 +23,7 @@ dependencies {
...
@@ -23,7 +23,7 @@ dependencies {
<dependency>
<dependency>
<groupId>
org.gitlab4j
</groupId>
<groupId>
org.gitlab4j
</groupId>
<artifactId>
gitlab4j-api
</artifactId>
<artifactId>
gitlab4j-api
</artifactId>
<version>
4.10.1
1
</version>
<version>
4.10.1
2
</version>
</dependency>
</dependency>
```
```
...
...
src/main/java/org/gitlab4j/api/utils/Oauth2LoginStreamingOutput.java
View file @
a0e59719
package
org.gitlab4j.api.utils
;
package
org.gitlab4j.api.utils
;
import
java.io.BufferedWriter
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.OutputStream
;
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.WebApplicationException
;
import
javax.ws.rs.core.StreamingOutput
;
import
javax.ws.rs.core.StreamingOutput
;
...
@@ -30,7 +33,7 @@ public class Oauth2LoginStreamingOutput implements StreamingOutput, AutoCloseabl
...
@@ -30,7 +33,7 @@ public class Oauth2LoginStreamingOutput implements StreamingOutput, AutoCloseabl
@Override
@Override
public
void
write
(
OutputStream
output
)
throws
IOException
,
WebApplicationException
{
public
void
write
(
OutputStream
output
)
throws
IOException
,
WebApplicationException
{
Print
Writer
writer
=
new
PrintWriter
(
output
);
Writer
writer
=
new
BufferedWriter
(
new
OutputStreamWriter
(
output
,
StandardCharsets
.
UTF_8
)
);
writer
.
write
(
"{ "
);
writer
.
write
(
"{ "
);
writer
.
write
(
"\"grant_type\": \"password\", "
);
writer
.
write
(
"\"grant_type\": \"password\", "
);
writer
.
write
(
"\"username\": \""
+
username
+
"\", "
);
writer
.
write
(
"\"username\": \""
+
username
+
"\", "
);
...
@@ -39,8 +42,15 @@ public class Oauth2LoginStreamingOutput implements StreamingOutput, AutoCloseabl
...
@@ -39,8 +42,15 @@ public class Oauth2LoginStreamingOutput implements StreamingOutput, AutoCloseabl
// Output the quoted password
// Output the quoted password
writer
.
write
(
'"'
);
writer
.
write
(
'"'
);
for
(
int
i
=
0
,
length
=
password
.
length
();
i
<
length
;
i
++)
{
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
(
'"'
);
writer
.
write
(
" }"
);
writer
.
write
(
" }"
);
...
...
src/test/java/org/gitlab4j/api/JsonUtils.java
View file @
a0e59719
...
@@ -25,6 +25,10 @@ public class JsonUtils {
...
@@ -25,6 +25,10 @@ public class JsonUtils {
jacksonJson
.
getObjectMapper
().
configure
(
MapperFeature
.
SORT_PROPERTIES_ALPHABETICALLY
,
true
);
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
{
static
JsonNode
readTreeFromResource
(
String
filename
)
throws
JsonParseException
,
JsonMappingException
,
IOException
{
InputStreamReader
reader
=
new
InputStreamReader
(
TestGitLabApiBeans
.
class
.
getResourceAsStream
(
filename
));
InputStreamReader
reader
=
new
InputStreamReader
(
TestGitLabApiBeans
.
class
.
getResourceAsStream
(
filename
));
return
(
jacksonJson
.
readTree
(
reader
));
return
(
jacksonJson
.
readTree
(
reader
));
...
@@ -73,7 +77,6 @@ public class JsonUtils {
...
@@ -73,7 +77,6 @@ public class JsonUtils {
return
(
jacksonJson
.
unmarshalMap
(
returnType
,
json
));
return
(
jacksonJson
.
unmarshalMap
(
returnType
,
json
));
}
}
static
<
T
>
boolean
compareJson
(
T
apiObject
,
String
filename
)
throws
IOException
{
static
<
T
>
boolean
compareJson
(
T
apiObject
,
String
filename
)
throws
IOException
{
InputStreamReader
reader
=
new
InputStreamReader
(
TestGitLabApiBeans
.
class
.
getResourceAsStream
(
filename
));
InputStreamReader
reader
=
new
InputStreamReader
(
TestGitLabApiBeans
.
class
.
getResourceAsStream
(
filename
));
return
(
compareJson
(
apiObject
,
reader
));
return
(
compareJson
(
apiObject
,
reader
));
...
...
src/test/java/org/gitlab4j/api/TestOauth2LoginStreamingOutput.java
0 → 100644
View file @
a0e59719
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
());
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment