Skip to content

Commit ef45725

Browse files
committed
SCANJLIB-224 Use Bearer scheme when a token is provided
1 parent 8ff68ee commit ef45725

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

lib/src/main/java/org/sonarsource/scanner/lib/internal/http/ServerConnection.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,21 @@ public class ServerConnection {
4949
private String restApiBaseUrl;
5050
private String userAgent;
5151
@Nullable
52-
private String credentials;
52+
private String token;
53+
@Nullable
54+
private String login;
55+
@Nullable
56+
private String password;
5357
private OkHttpClient httpClient;
5458

5559
public void init(Map<String, String> bootstrapProperties, Path sonarUserHome) {
5660
webApiBaseUrl = removeTrailingSlash(bootstrapProperties.get(ScannerProperties.HOST_URL));
5761
restApiBaseUrl = removeTrailingSlash(bootstrapProperties.get(ScannerProperties.API_BASE_URL));
5862
userAgent = format("%s/%s", bootstrapProperties.get(InternalProperties.SCANNER_APP),
5963
bootstrapProperties.get(InternalProperties.SCANNER_APP_VERSION));
60-
String token = bootstrapProperties.get(ScannerProperties.SONAR_TOKEN);
61-
String login = bootstrapProperties.getOrDefault(ScannerProperties.SONAR_LOGIN, token);
62-
if (login != null) {
63-
credentials = Credentials.basic(login, bootstrapProperties.getOrDefault(ScannerProperties.SONAR_PASSWORD, ""));
64-
}
64+
this.token = bootstrapProperties.get(ScannerProperties.SONAR_TOKEN);
65+
this.login = bootstrapProperties.get(ScannerProperties.SONAR_LOGIN);
66+
this.password = bootstrapProperties.get(ScannerProperties.SONAR_PASSWORD);
6567
httpClient = OkHttpClientFactory.create(bootstrapProperties, sonarUserHome);
6668
}
6769

@@ -158,8 +160,12 @@ private ResponseBody callUrl(String url, boolean authentication, @Nullable Strin
158160
.get()
159161
.url(url)
160162
.addHeader("User-Agent", userAgent);
161-
if (authentication && credentials != null) {
162-
requestBuilder.header("Authorization", credentials);
163+
if (authentication) {
164+
if (token != null) {
165+
requestBuilder.header("Authorization", "Bearer " + token);
166+
} else if (login != null) {
167+
requestBuilder.header("Authorization", Credentials.basic(login, password != null ? password : ""));
168+
}
163169
}
164170
if (acceptHeader != null) {
165171
requestBuilder.header("Accept", acceptHeader);

lib/src/test/java/org/sonarsource/scanner/lib/internal/http/ServerConnectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void should_authenticate_with_token() throws Exception {
136136
assertThat(content).isEqualTo(HELLO_WORLD);
137137

138138
sonarqube.verify(getRequestedFor(anyUrl())
139-
.withHeader("Authorization", equalTo("Basic " + Base64.getEncoder().encodeToString("some_token:".getBytes(StandardCharsets.UTF_8)))));
139+
.withHeader("Authorization", equalTo("Bearer some_token")));
140140
}
141141

142142
@Test

0 commit comments

Comments
 (0)