Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue#62 strip auth key #64

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
13 changes: 9 additions & 4 deletions deepl-java/src/main/java/com/deepl/api/Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,24 @@ public class Translator {
*/
@Deprecated
public Translator(String authKey, TranslatorOptions options) throws IllegalArgumentException {
if (authKey == null || authKey.length() == 0) {
throw new IllegalArgumentException("authKey must be a non-empty string");
if (authKey == null || authKey.isEmpty()) {
throw new IllegalArgumentException("authKey cannot be null or empty");
}

String sanitizedAuthKey = authKey.trim();

String serverUrl =
(options.getServerUrl() != null)
? options.getServerUrl()
: (isFreeAccountAuthKey(authKey) ? DEEPL_SERVER_URL_FREE : DEEPL_SERVER_URL_PRO);
: (isFreeAccountAuthKey(sanitizedAuthKey)
? DEEPL_SERVER_URL_FREE
: DEEPL_SERVER_URL_PRO);

Map<String, String> headers = new HashMap<>();
if (options.getHeaders() != null) {
headers.putAll(options.getHeaders());
}
headers.putIfAbsent("Authorization", "DeepL-Auth-Key " + authKey);
headers.putIfAbsent("Authorization", "DeepL-Auth-Key " + sanitizedAuthKey);
headers.putIfAbsent(
"User-Agent",
constructUserAgentString(options.getSendPlatformInfo(), options.getAppInfo()));
Expand Down
10 changes: 10 additions & 0 deletions deepl-java/src/test/java/com/deepl/api/GeneralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ void testEmptyAuthKey() {
});
}

@Test
void testNullAuthKey() {
IllegalArgumentException thrown =
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
Translator translator = new Translator(null);
});
}

@Test
void testInvalidAuthKey() {
String authKey = "invalid";
Expand Down