Merge branch '2.2.x'

This commit is contained in:
spencergibb
2021-03-29 18:06:37 -04:00
4 changed files with 35 additions and 2 deletions

View File

@@ -16,8 +16,11 @@
package org.springframework.cloud.config.client;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -342,9 +345,14 @@ public class ConfigClientProperties {
// But the username can be overridden
result.username = explicitCredentials.username;
}
result.password = URLDecoder.decode(result.password,
StandardCharsets.UTF_8.toString());
result.username = URLDecoder.decode(result.username,
StandardCharsets.UTF_8.toString());
return result;
}
catch (MalformedURLException e) {
catch (MalformedURLException | UnsupportedEncodingException e) {
throw new IllegalStateException("Invalid URL: " + uri, e);
}
}

View File

@@ -58,6 +58,15 @@ public class ConfigClientPropertiesTests {
assertThat(credentials.getPassword()).isEqualTo("bar");
}
@Test
public void uriCredsWithAtInPassword() {
this.locator.setUri(new String[] { "http://foo:bar%40@localhost:9999" });
Credentials credentials = this.locator.getCredentials(0);
assertThat(credentials.getUri()).isEqualTo("http://localhost:9999");
assertThat(credentials.getUsername()).isEqualTo("foo");
assertThat(credentials.getPassword()).isEqualTo("bar@");
}
@Test
public void explicitPassword() {
this.locator.setUri(new String[] { "http://foo:bar@localhost:9999" });

View File

@@ -92,6 +92,14 @@ public class GitCredentialsProviderFactory {
this.logger.debug("Constructing UsernamePasswordCredentialsProvider for URI " + uri);
provider = new UsernamePasswordCredentialsProvider(username, password.toCharArray());
}
else if (hasText(username) && !hasText(passphrase)) {
// useful for token based login gh-1602
// see
// https://stackoverflow.com/questions/28073266/how-to-use-jgit-to-push-changes-to-remote-with-oauth-access-token
this.logger.debug(
"Constructing UsernamePasswordCredentialsProvider for URI " + uri);
provider = new UsernamePasswordCredentialsProvider(username, (String) null);
}
else if (hasText(passphrase)) {
this.logger.debug("Constructing PassphraseCredentialsProvider for URI " + uri);
provider = new PassphraseCredentialsProvider(passphrase);

View File

@@ -60,12 +60,20 @@ public class GitCredentialsProviderFactoryTests {
}
@Test
public void testCreateForFileWithUsername() {
public void testCreateForFileWithUsernameAndPassword() {
CredentialsProvider provider = this.factory.createFor(FILE_REPO, USER, PASSWORD, null, false);
assertThat(provider).isNotNull();
assertThat(provider instanceof UsernamePasswordCredentialsProvider).isTrue();
}
@Test
public void testCreateForServerWithUsernameAndNoPassword() {
CredentialsProvider provider = this.factory.createFor(HTTPS_GIT_REPO, USER, "",
null, false);
assertThat(provider).isNotNull();
assertThat(provider instanceof UsernamePasswordCredentialsProvider).isTrue();
}
@Test
public void testCreateForServerNoUsernameIsNull() {
CredentialsProvider provider = this.factory.createFor(HTTPS_GIT_REPO, null, null, null, false);