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

Replace Bearer authentication with HTTP Basic for SQ 9.9 support #55

Merged
merged 1 commit into from
Jun 28, 2024
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
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
Loading