aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-auth-gitlab
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2019-09-24 11:44:20 +0200
committerSonarTech <sonartech@sonarsource.com>2019-10-07 20:21:06 +0200
commit42f0cff638b6b7055acc6cf75bbb2215867d0474 (patch)
tree9c4bbdb045a1cabe844c14fdda2413c1b94a7fcb /server/sonar-auth-gitlab
parentdaf5a60dd259039b97fd3598f894169d7ecc74e5 (diff)
downloadsonarqube-42f0cff638b6b7055acc6cf75bbb2215867d0474.tar.gz
sonarqube-42f0cff638b6b7055acc6cf75bbb2215867d0474.zip
SONAR-12471 Embed GitHub authentication
Diffstat (limited to 'server/sonar-auth-gitlab')
-rw-r--r--server/sonar-auth-gitlab/build.gradle1
-rw-r--r--server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabIdentityProvider.java4
-rw-r--r--server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java71
3 files changed, 7 insertions, 69 deletions
diff --git a/server/sonar-auth-gitlab/build.gradle b/server/sonar-auth-gitlab/build.gradle
index f30f08323b0..54f97687849 100644
--- a/server/sonar-auth-gitlab/build.gradle
+++ b/server/sonar-auth-gitlab/build.gradle
@@ -10,6 +10,7 @@ dependencies {
compile 'com.github.scribejava:scribejava-apis'
compile 'com.github.scribejava:scribejava-core'
compile 'com.google.code.gson:gson'
+ compile project(':server:sonar-auth-common')
compileOnly 'com.google.code.findbugs:jsr305'
compileOnly 'com.squareup.okhttp3:okhttp'
diff --git a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabIdentityProvider.java b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabIdentityProvider.java
index 5921bac826b..ab0de2d184f 100644
--- a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabIdentityProvider.java
+++ b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabIdentityProvider.java
@@ -62,7 +62,9 @@ public class GitLabIdentityProvider implements OAuth2IdentityProvider {
@Override
public Display getDisplay() {
return Display.builder()
- .setIconPath("/images/gitlab-icon-rgb.svg").setBackgroundColor("#6a4fbb").build();
+ .setIconPath("/images/gitlab-icon-rgb.svg")
+ .setBackgroundColor("#6a4fbb")
+ .build();
}
@Override
diff --git a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java
index 9abfcbe35ac..06a922ae6c7 100644
--- a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java
+++ b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java
@@ -20,26 +20,14 @@
package org.sonar.auth.gitlab;
import com.github.scribejava.core.model.OAuth2AccessToken;
-import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
-import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth20Service;
import java.io.IOException;
-import java.util.ArrayList;
import java.util.List;
-import java.util.Optional;
-import java.util.concurrent.ExecutionException;
-import java.util.function.Function;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import static java.lang.String.format;
+import org.sonar.auth.OAuthRestClient;
public class GitLabRestClient {
- private static final int DEFAULT_PAGE_SIZE = 100;
- private static final Pattern NEXT_LINK_PATTERN = Pattern.compile(".*<(.*)>; rel=\"next\"");
-
private static final String API_SUFFIX = "/api/v4";
private final GitLabSettings settings;
@@ -49,7 +37,7 @@ public class GitLabRestClient {
}
GsonUser getUser(OAuth20Service scribe, OAuth2AccessToken accessToken) {
- try (Response response = executeRequest(settings.url() + API_SUFFIX + "/user", scribe, accessToken)) {
+ try (Response response = OAuthRestClient.executeRequest(settings.url() + API_SUFFIX + "/user", scribe, accessToken)) {
String responseBody = response.getBody();
return GsonUser.parse(responseBody);
} catch (IOException e) {
@@ -58,59 +46,6 @@ public class GitLabRestClient {
}
List<GsonGroup> getGroups(OAuth20Service scribe, OAuth2AccessToken accessToken) {
- return executePaginatedQuery(settings.url() + API_SUFFIX + "/groups", scribe, accessToken, GsonGroup::parse);
- }
-
- private static Response executeRequest(String requestUrl, OAuth20Service scribe, OAuth2AccessToken accessToken) throws IOException {
- OAuthRequest request = new OAuthRequest(Verb.GET, requestUrl);
- scribe.signRequest(accessToken, request);
- try {
- Response response = scribe.execute(request);
- if (!response.isSuccessful()) {
- throw unexpectedResponseCode(requestUrl, response);
- }
- return response;
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- throw new IllegalStateException(e);
- } catch (ExecutionException e) {
- throw new IllegalStateException(e);
- }
- }
-
- private static <E> List<E> executePaginatedQuery(String query, OAuth20Service scribe, OAuth2AccessToken accessToken, Function<String, List<E>> function) {
- List<E> result = new ArrayList<>();
- readNextPage(result, scribe, accessToken, query + "?per_page=" + DEFAULT_PAGE_SIZE, function);
- return result;
- }
-
- private static <E> void readNextPage(List<E> result, OAuth20Service scribe, OAuth2AccessToken accessToken, String nextEndPoint, Function<String, List<E>> function) {
- try (Response nextResponse = executeRequest(nextEndPoint, scribe, accessToken)) {
- String content = nextResponse.getBody();
- if (content == null) {
- return;
- }
- result.addAll(function.apply(content));
- readNextEndPoint(nextResponse).ifPresent(newNextEndPoint -> readNextPage(result, scribe, accessToken, newNextEndPoint, function));
- } catch (IOException e) {
- throw new IllegalStateException(format("Failed to get %s", nextEndPoint), e);
- }
+ return OAuthRestClient.executePaginatedRequest(settings.url() + API_SUFFIX + "/groups", scribe, accessToken, GsonGroup::parse);
}
-
- private static Optional<String> readNextEndPoint(Response response) {
- String link = response.getHeader("Link");
- if (link == null || link.isEmpty() || !link.contains("rel=\"next\"")) {
- return Optional.empty();
- }
- Matcher nextLinkMatcher = NEXT_LINK_PATTERN.matcher(link);
- if (!nextLinkMatcher.find()) {
- return Optional.empty();
- }
- return Optional.of(nextLinkMatcher.group(1));
- }
-
- private static IllegalStateException unexpectedResponseCode(String requestUrl, Response response) throws IOException {
- return new IllegalStateException(format("Fail to execute request '%s'. HTTP code: %s, response: %s", requestUrl, response.getCode(), response.getBody()));
- }
-
}