Skip to content

Commit

Permalink
Use HTTP Basic authentication for SQ 9.9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
fourls committed Jun 28, 2024
1 parent e00e98c commit ef833a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

* Incompatibility with SonarQube 9.9, by using the HTTP Basic authentication scheme instead of the Bearer scheme.

## [1.1.1] - 2024-06-11

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -94,13 +97,21 @@ public String getText(String url, Map<String, String> params) throws SonarHostEx
return getText(url + HttpUtils.buildParamString(params));
}

private <T> T getResponse(String url, BodyHandler<T> handler) throws SonarHostException {
var reqBuilder = HttpRequest.newBuilder(URI.create(url));

if (!this.token.isEmpty()) {
reqBuilder.header("Authorization", "Bearer " + token);
private Optional<String> getAuthorizationHeader() {
if (token.isEmpty()) {
return Optional.empty();
}

// While SonarQube 10.0 and up support the Bearer authentication scheme, SonarQube 9.9 only
// supports Basic authentication. Tokens should be supplied via the user field.
var userPass = token + ":";
var credentials = Base64.getEncoder().encodeToString(userPass.getBytes(StandardCharsets.UTF_8));
return Optional.of("Basic " + credentials);
}

private <T> T getResponse(String url, BodyHandler<T> handler) throws SonarHostException {
var reqBuilder = HttpRequest.newBuilder(URI.create(url));
getAuthorizationHeader().ifPresent(value -> reqBuilder.header("Authorization", value));
HttpRequest request = reqBuilder.build();

try {
Expand Down

0 comments on commit ef833a7

Please sign in to comment.