Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/com/bastiaanjansen/otp/helpers/URIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ public static URI createURI(String scheme, String host, String path, Map<String,

public static String encode(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
// Standard URLEncoder uses + for spaces (application/x-www-form-urlencoded)
// We replace it with %20 to comply with URI spec (RFC 3986)
return URLEncoder.encode(value, StandardCharsets.UTF_8.name())
.replace("+", "%20");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
throw new RuntimeException(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void getURIWithIssuerWithSpace_doesEscapeIssuer() throws URISyntaxException {

String url = generator.getURI(10, "issuer with space").toString();

assertThat(url, is("otpauth://hotp/issuer+with+space?digits=6&counter=10&secret=vv3kox7uqj4kyakohmzpph3us4cjimh6f3zknb5c2oobq6v2kiyhm27q&issuer=issuer+with+space&algorithm=SHA1"));
assertThat(url, is("otpauth://hotp/issuer%20with%20space?digits=6&counter=10&secret=vv3kox7uqj4kyakohmzpph3us4cjimh6f3zknb5c2oobq6v2kiyhm27q&issuer=issuer%20with%20space&algorithm=SHA1"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void createURIWithUrlUnsafeCharacters() throws URISyntaxException {
query.put("test", "value 1");
query.put("test2", "value?&2");
URI uri = URIHelper.createURI("scheme", "host", "path", query);
String expected = "scheme://host/path?test2=value%3F%262&test=value+1";
String expected = "scheme://host/path?test2=value%3F%262&test=value%201";

assertThat(uri.toString(), is(expected));
}
Expand Down
Loading