Skip to content

Commit b1f7887

Browse files
committed
fix: issue#62 strip auth key
Fixing the issue raised in #62 Problem: auth key with leading / trailing whitespaces is throwing exceptions. For example, assigning the secret via `echo` leaves a trailing `\n`. Solution: strip input auth key as an input sanitization procedure Notice: Java 8 does not support `String.strip()`, while `String.trim()` does not support Unicode whitespaces. We anticipate ASCII only input and utilizing `trim()` for simplicity.
1 parent 87e78b1 commit b1f7887

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

deepl-java/src/main/java/com/deepl/api/Translator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,16 @@ public class Translator {
4141
*/
4242
@Deprecated
4343
public Translator(String authKey, TranslatorOptions options) throws IllegalArgumentException {
44-
if (authKey == null || authKey.length() == 0) {
45-
throw new IllegalArgumentException("authKey must be a non-empty string");
44+
if (authKey == null) {
45+
throw new IllegalArgumentException("authKey cannot be null");
4646
}
47+
48+
authKey = authKey.trim();
49+
50+
if (authKey.isEmpty()) {
51+
throw new IllegalArgumentException("authKey cannot be empty");
52+
}
53+
4754
String serverUrl =
4855
(options.getServerUrl() != null)
4956
? options.getServerUrl()

deepl-java/src/test/java/com/deepl/api/GeneralTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ void testEmptyAuthKey() {
2828
});
2929
}
3030

31+
@Test
32+
void testNullAuthKey() {
33+
IllegalArgumentException thrown =
34+
Assertions.assertThrows(
35+
IllegalArgumentException.class,
36+
() -> {
37+
Translator translator = new Translator(null);
38+
});
39+
}
40+
3141
@Test
3242
void testInvalidAuthKey() {
3343
String authKey = "invalid";

0 commit comments

Comments
 (0)