Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void configure(String encodedAuthParamString) {
URI filePath = URI.create(encodedAuthParamString);
this.tokenSupplier = () -> {
try {
return new String(Files.readAllBytes(Paths.get(filePath)), Charsets.UTF_8);
return new String(Files.readAllBytes(Paths.get(filePath)), Charsets.UTF_8).trim();
} catch (IOException e) {
throw new RuntimeException("Failed to read token from file", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ public void testAuthTokenConfigFromFile() throws Exception {
authToken.close();
}

/**
* File can have spaces and newlines before or after the token. We should be able to read
* the token correctly anyway.
*/
@Test
public void testAuthTokenConfigFromFileWithNewline() throws Exception {
File tokenFile = File.createTempFile("pular-test-token", ".key");
tokenFile.deleteOnExit();
FileUtils.write(tokenFile, " my-test-token-string \r\n", Charsets.UTF_8);

AuthenticationToken authToken = new AuthenticationToken();
authToken.configure("file://" + tokenFile);
assertEquals(authToken.getAuthMethodName(), "token");

AuthenticationDataProvider authData = authToken.getAuthData();
assertTrue(authData.hasDataFromCommand());
assertEquals(authData.getCommandData(), "my-test-token-string");

// Ensure if the file content changes, the token will get refreshed as well
FileUtils.write(tokenFile, "other-token", Charsets.UTF_8);

AuthenticationDataProvider authData2 = authToken.getAuthData();
assertTrue(authData2.hasDataFromCommand());
assertEquals(authData2.getCommandData(), "other-token");

authToken.close();
}

@Test
public void testAuthTokenConfigNoPrefix() throws Exception {
AuthenticationToken authToken = new AuthenticationToken();
Expand Down