Skip to content

Commit b48baec

Browse files
Merge pull request #64 from timazhum/strip-auth-key
fix: issue#62 strip auth key
2 parents dc87c5e + 644ff12 commit b48baec

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2025 DeepL SE (https://www.deepl.com)
2+
// Use of this source code is governed by an MIT
3+
// license that can be found in the LICENSE file.
14
package com.deepl.api;
25

36
public enum DeepLApiVersion {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,24 @@ public class Translator {
4242
*/
4343
@Deprecated
4444
public Translator(String authKey, TranslatorOptions options) throws IllegalArgumentException {
45-
if (authKey == null || authKey.length() == 0) {
46-
throw new IllegalArgumentException("authKey must be a non-empty string");
45+
if (authKey == null || authKey.isEmpty()) {
46+
throw new IllegalArgumentException("authKey cannot be null or empty");
4747
}
48+
49+
String sanitizedAuthKey = authKey.trim();
4850
this.apiVersion = options.apiVersion;
4951
String serverUrl =
5052
(options.getServerUrl() != null)
5153
? options.getServerUrl()
52-
: (isFreeAccountAuthKey(authKey) ? DEEPL_SERVER_URL_FREE : DEEPL_SERVER_URL_PRO);
54+
: (isFreeAccountAuthKey(sanitizedAuthKey)
55+
? DEEPL_SERVER_URL_FREE
56+
: DEEPL_SERVER_URL_PRO);
5357

5458
Map<String, String> headers = new HashMap<>();
5559
if (options.getHeaders() != null) {
5660
headers.putAll(options.getHeaders());
5761
}
58-
headers.putIfAbsent("Authorization", "DeepL-Auth-Key " + authKey);
62+
headers.putIfAbsent("Authorization", "DeepL-Auth-Key " + sanitizedAuthKey);
5963
headers.putIfAbsent(
6064
"User-Agent",
6165
constructUserAgentString(options.getSendPlatformInfo(), options.getAppInfo()));

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

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

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

0 commit comments

Comments
 (0)