You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitLabIdentityProvider.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.auth.gitlab;
  21. import com.github.scribejava.core.builder.ServiceBuilder;
  22. import com.github.scribejava.core.model.OAuth2AccessToken;
  23. import com.github.scribejava.core.model.OAuthConstants;
  24. import com.github.scribejava.core.oauth.OAuth20Service;
  25. import java.io.IOException;
  26. import java.util.Collection;
  27. import java.util.List;
  28. import java.util.Set;
  29. import java.util.concurrent.ExecutionException;
  30. import java.util.stream.Stream;
  31. import javax.servlet.http.HttpServletRequest;
  32. import org.sonar.api.server.authentication.Display;
  33. import org.sonar.api.server.authentication.OAuth2IdentityProvider;
  34. import org.sonar.api.server.authentication.UserIdentity;
  35. import static com.google.common.base.Preconditions.checkState;
  36. import static java.util.stream.Collectors.toSet;
  37. public class GitLabIdentityProvider implements OAuth2IdentityProvider {
  38. private final GitLabSettings gitLabSettings;
  39. private final ScribeGitLabOauth2Api scribeApi;
  40. private final GitLabRestClient gitLabRestClient;
  41. public GitLabIdentityProvider(GitLabSettings gitLabSettings, GitLabRestClient gitLabRestClient, ScribeGitLabOauth2Api scribeApi) {
  42. this.gitLabSettings = gitLabSettings;
  43. this.scribeApi = scribeApi;
  44. this.gitLabRestClient = gitLabRestClient;
  45. }
  46. @Override
  47. public String getKey() {
  48. return "gitlab";
  49. }
  50. @Override
  51. public String getName() {
  52. return "GitLab";
  53. }
  54. @Override
  55. public Display getDisplay() {
  56. return Display.builder()
  57. .setIconPath("/images/gitlab-icon-rgb.svg")
  58. .setBackgroundColor("#6a4fbb")
  59. .build();
  60. }
  61. @Override
  62. public boolean isEnabled() {
  63. return gitLabSettings.isEnabled();
  64. }
  65. @Override
  66. public boolean allowsUsersToSignUp() {
  67. return gitLabSettings.allowUsersToSignUp();
  68. }
  69. @Override
  70. public void init(InitContext context) {
  71. String state = context.generateCsrfState();
  72. OAuth20Service scribe = newScribeBuilder(context).build(scribeApi);
  73. String url = scribe.getAuthorizationUrl(state);
  74. context.redirectTo(url);
  75. }
  76. private ServiceBuilder newScribeBuilder(OAuth2Context context) {
  77. checkState(isEnabled(), "GitLab authentication is disabled");
  78. return new ServiceBuilder(gitLabSettings.applicationId())
  79. .apiSecret(gitLabSettings.secret())
  80. .callback(context.getCallbackUrl());
  81. }
  82. @Override
  83. public void callback(CallbackContext context) {
  84. try {
  85. onCallback(context);
  86. } catch (IOException | ExecutionException e) {
  87. throw new IllegalStateException(e);
  88. } catch (InterruptedException e) {
  89. Thread.currentThread().interrupt();
  90. throw new IllegalStateException(e);
  91. }
  92. }
  93. private void onCallback(CallbackContext context) throws InterruptedException, ExecutionException, IOException {
  94. HttpServletRequest request = context.getRequest();
  95. OAuth20Service scribe = newScribeBuilder(context).build(scribeApi);
  96. String code = request.getParameter(OAuthConstants.CODE);
  97. OAuth2AccessToken accessToken = scribe.getAccessToken(code);
  98. GsonUser user = gitLabRestClient.getUser(scribe, accessToken);
  99. UserIdentity.Builder builder = UserIdentity.builder()
  100. .setProviderId(Long.toString(user.getId()))
  101. .setProviderLogin(user.getUsername())
  102. .setName(user.getName())
  103. .setEmail(user.getEmail());
  104. if (gitLabSettings.syncUserGroups()) {
  105. builder.setGroups(getGroups(scribe, accessToken));
  106. }
  107. context.authenticate(builder.build());
  108. context.redirectToRequestedPage();
  109. }
  110. private Set<String> getGroups(OAuth20Service scribe, OAuth2AccessToken accessToken) {
  111. List<GsonGroup> groups = gitLabRestClient.getGroups(scribe, accessToken);
  112. return Stream.of(groups)
  113. .flatMap(Collection::stream)
  114. .map(GsonGroup::getFullPath)
  115. .collect(toSet());
  116. }
  117. }