您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GitLabIdentityProviderTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.model.OAuth2AccessToken;
  22. import com.github.scribejava.core.model.OAuthConstants;
  23. import com.github.scribejava.core.oauth.OAuth20Service;
  24. import com.tngtech.java.junit.dataprovider.DataProvider;
  25. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  26. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  27. import java.io.IOException;
  28. import java.util.List;
  29. import java.util.Set;
  30. import java.util.concurrent.ExecutionException;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.mockito.Answers;
  35. import org.mockito.ArgumentCaptor;
  36. import org.mockito.Mock;
  37. import org.sonar.api.server.authentication.Display;
  38. import org.sonar.api.server.authentication.OAuth2IdentityProvider;
  39. import org.sonar.api.server.authentication.UnauthorizedException;
  40. import org.sonar.api.server.authentication.UserIdentity;
  41. import static java.util.stream.Collectors.toSet;
  42. import static org.assertj.core.api.Assertions.assertThat;
  43. import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
  44. import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
  45. import static org.assertj.core.api.Assertions.assertThatObject;
  46. import static org.assertj.core.api.InstanceOfAssertFactories.LONG;
  47. import static org.mockito.Mockito.any;
  48. import static org.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.never;
  50. import static org.mockito.Mockito.verify;
  51. import static org.mockito.Mockito.when;
  52. import static org.mockito.MockitoAnnotations.openMocks;
  53. @RunWith(DataProviderRunner.class)
  54. public class GitLabIdentityProviderTest {
  55. private static final String OAUTH_CODE = "code fdsojfsjodfg";
  56. private static final String AUTHORIZATION_URL = "AUTHORIZATION_URL";
  57. private static final String CALLBACK_URL = "CALLBACK_URL";
  58. private static final String STATE = "State request";
  59. @Mock
  60. private GitLabRestClient gitLabRestClient;
  61. @Mock
  62. private GitLabSettings gitLabSettings;
  63. @Mock(answer = Answers.RETURNS_DEEP_STUBS)
  64. private GitLabIdentityProvider.ScribeFactory scribeFactory;
  65. @Mock
  66. private OAuth2IdentityProvider.InitContext initContext;
  67. @Mock(answer = Answers.RETURNS_DEEP_STUBS)
  68. private OAuth2IdentityProvider.CallbackContext callbackContext;
  69. @Mock
  70. private OAuth20Service scribe;
  71. @Mock
  72. private ScribeGitLabOauth2Api scribeApi;
  73. @Mock
  74. private OAuth2AccessToken accessToken;
  75. private GitLabIdentityProvider gitLabIdentityProvider;
  76. @Before
  77. public void setup() throws IOException, ExecutionException, InterruptedException {
  78. openMocks(this);
  79. gitLabIdentityProvider = new GitLabIdentityProvider(gitLabSettings, gitLabRestClient, scribeApi, scribeFactory);
  80. when(initContext.generateCsrfState()).thenReturn(STATE);
  81. when(initContext.getCallbackUrl()).thenReturn(CALLBACK_URL);
  82. when(callbackContext.getCallbackUrl()).thenReturn(CALLBACK_URL);
  83. when(callbackContext.getRequest().getParameter(OAuthConstants.CODE)).thenReturn(OAUTH_CODE);
  84. when(scribeFactory.newScribe(gitLabSettings, CALLBACK_URL, scribeApi)).thenReturn(scribe);
  85. when(scribe.getAccessToken(OAUTH_CODE)).thenReturn(accessToken);
  86. when(scribe.getAuthorizationUrl(STATE)).thenReturn(AUTHORIZATION_URL);
  87. }
  88. @Test
  89. public void test_identity_provider() {
  90. when(gitLabSettings.isEnabled()).thenReturn(true);
  91. when(gitLabSettings.allowUsersToSignUp()).thenReturn(true);
  92. assertThat(gitLabIdentityProvider.getKey()).isEqualTo("gitlab");
  93. assertThat(gitLabIdentityProvider.getName()).isEqualTo("GitLab");
  94. Display display = gitLabIdentityProvider.getDisplay();
  95. assertThat(display.getIconPath()).isEqualTo("/images/alm/gitlab.svg");
  96. assertThat(display.getBackgroundColor()).isEqualTo("#6a4fbb");
  97. assertThat(gitLabIdentityProvider.isEnabled()).isTrue();
  98. assertThat(gitLabIdentityProvider.allowsUsersToSignUp()).isTrue();
  99. }
  100. @Test
  101. public void init_whenSuccessful_redirectsToUrl() {
  102. gitLabIdentityProvider.init(initContext);
  103. verify(initContext).generateCsrfState();
  104. verify(initContext).redirectTo(AUTHORIZATION_URL);
  105. }
  106. @Test
  107. public void init_whenErrorWhileBuildingScribe_shouldReThrow() {
  108. IllegalStateException exception = new IllegalStateException("GitLab authentication is disabled");
  109. when(scribeFactory.newScribe(any(), any(), any())).thenThrow(exception);
  110. OAuth2IdentityProvider.InitContext initContext = mock(OAuth2IdentityProvider.InitContext.class);
  111. when(initContext.getCallbackUrl()).thenReturn("http://server/callback");
  112. assertThatIllegalStateException()
  113. .isThrownBy(() -> gitLabIdentityProvider.init(initContext))
  114. .isEqualTo(exception);
  115. }
  116. @Test
  117. public void onCallback_withGroupSyncDisabledAndNoAllowedGroups_redirectsToRequestedPage() {
  118. GsonUser gsonUser = mockGsonUser();
  119. gitLabIdentityProvider.callback(callbackContext);
  120. verifyAuthenticateIsCalledWithExpectedIdentity(callbackContext, gsonUser, Set.of());
  121. verify(callbackContext).redirectToRequestedPage();
  122. verify(gitLabRestClient, never()).getGroups(any(), any());
  123. }
  124. @Test
  125. public void onCallback_withGroupSyncDisabledAndAllowedGroups_redirectsToRequestedPage() {
  126. when(gitLabSettings.syncUserGroups()).thenReturn(false);
  127. GsonUser gsonUser = mockGsonUser();
  128. gitLabIdentityProvider.callback(callbackContext);
  129. verifyAuthenticateIsCalledWithExpectedIdentity(callbackContext, gsonUser, Set.of());
  130. verify(callbackContext).redirectToRequestedPage();
  131. verify(gitLabRestClient, never()).getGroups(any(), any());
  132. }
  133. @Test
  134. @UseDataProvider("allowedGroups")
  135. public void onCallback_withGroupSyncAndAllowedGroupsMatching_redirectsToRequestedPage(Set<String> allowedGroups) {
  136. when(gitLabSettings.syncUserGroups()).thenReturn(true);
  137. when(gitLabSettings.allowedGroups()).thenReturn(allowedGroups);
  138. GsonUser gsonUser = mockGsonUser();
  139. Set<GsonGroup> gsonGroups = mockGitlabGroups();
  140. gitLabIdentityProvider.callback(callbackContext);
  141. verifyAuthenticateIsCalledWithExpectedIdentity(callbackContext, gsonUser, gsonGroups);
  142. verify(callbackContext).redirectToRequestedPage();
  143. }
  144. @DataProvider
  145. public static Object[][] allowedGroups() {
  146. return new Object[][]{
  147. {Set.of()},
  148. {Set.of("path")}
  149. };
  150. }
  151. @Test
  152. public void onCallback_withGroupSyncAndAllowedGroupsNotMatching_shouldThrow() {
  153. when(gitLabSettings.syncUserGroups()).thenReturn(true);
  154. when(gitLabSettings.allowedGroups()).thenReturn(Set.of("path2"));
  155. mockGsonUser();
  156. mockGitlabGroups();
  157. assertThatExceptionOfType(UnauthorizedException.class)
  158. .isThrownBy(() -> gitLabIdentityProvider.callback(callbackContext))
  159. .withMessage("You are not allowed to authenticate");
  160. }
  161. @Test
  162. public void onCallback_ifScribeFactoryFails_shouldThrow() {
  163. IllegalStateException exception = new IllegalStateException("message");
  164. when(scribeFactory.newScribe(any(), any(), any())).thenThrow(exception);
  165. assertThatIllegalStateException()
  166. .isThrownBy(() -> gitLabIdentityProvider.callback(callbackContext))
  167. .isEqualTo(exception);
  168. }
  169. private Set<GsonGroup> mockGitlabGroups() {
  170. GsonGroup gsonGroup = mock(GsonGroup.class);
  171. when(gsonGroup.getFullPath()).thenReturn("path/to/group");
  172. GsonGroup gsonGroup2 = mock(GsonGroup.class);
  173. when(gsonGroup2.getFullPath()).thenReturn("path/to/group2");
  174. when(gitLabRestClient.getGroups(scribe, accessToken)).thenReturn(List.of(gsonGroup, gsonGroup2));
  175. return Set.of(gsonGroup, gsonGroup2);
  176. }
  177. private static void verifyAuthenticateIsCalledWithExpectedIdentity(OAuth2IdentityProvider.CallbackContext callbackContext,
  178. GsonUser gsonUser, Set<GsonGroup> gsonGroups) {
  179. ArgumentCaptor<UserIdentity> userIdentityCaptor = ArgumentCaptor.forClass(UserIdentity.class);
  180. verify(callbackContext).authenticate(userIdentityCaptor.capture());
  181. UserIdentity actualIdentity = userIdentityCaptor.getValue();
  182. assertThatObject(actualIdentity.getProviderId()).extracting(Long::parseLong, LONG).isEqualTo(gsonUser.getId());
  183. assertThat(actualIdentity.getProviderLogin()).isEqualTo(gsonUser.getUsername());
  184. assertThat(actualIdentity.getName()).isEqualTo(gsonUser.getName());
  185. assertThat(actualIdentity.getEmail()).isEqualTo(gsonUser.getEmail());
  186. assertThat(actualIdentity.getGroups()).isEqualTo(gsonGroups.stream().map(GsonGroup::getFullPath).collect(toSet()));
  187. }
  188. private GsonUser mockGsonUser() {
  189. GsonUser gsonUser = mock();
  190. when(gsonUser.getId()).thenReturn(432423L);
  191. when(gsonUser.getUsername()).thenReturn("userName");
  192. when(gsonUser.getName()).thenReturn("name");
  193. when(gsonUser.getEmail()).thenReturn("toto@gitlab.com");
  194. when(gitLabRestClient.getUser(scribe, accessToken)).thenReturn(gsonUser);
  195. return gsonUser;
  196. }
  197. @Test
  198. public void newScribe_whenGitLabAuthIsDisabled_throws() {
  199. when(gitLabSettings.isEnabled()).thenReturn(false);
  200. assertThatIllegalStateException()
  201. .isThrownBy(() -> new GitLabIdentityProvider.ScribeFactory().newScribe(gitLabSettings, CALLBACK_URL, new ScribeGitLabOauth2Api(gitLabSettings)))
  202. .withMessage("GitLab authentication is disabled");
  203. }
  204. @Test
  205. @UseDataProvider("groupsSyncToScope")
  206. public void newScribe_whenGitLabSettingsValid_shouldUseCorrectScopeDependingOnGroupSync(boolean groupSyncEnabled, String expectedScope) {
  207. setupGitlabSettingsWithGroupSync(groupSyncEnabled);
  208. OAuth20Service realScribe = new GitLabIdentityProvider.ScribeFactory().newScribe(gitLabSettings, CALLBACK_URL,
  209. new ScribeGitLabOauth2Api(gitLabSettings));
  210. assertThat(realScribe).isNotNull();
  211. assertThat(realScribe.getCallback()).isEqualTo(CALLBACK_URL);
  212. assertThat(realScribe.getApiSecret()).isEqualTo(gitLabSettings.secret());
  213. assertThat(realScribe.getDefaultScope()).isEqualTo(expectedScope);
  214. }
  215. @DataProvider
  216. public static Object[][] groupsSyncToScope() {
  217. return new Object[][]{
  218. {false, "read_user"},
  219. {true, "api"}
  220. };
  221. }
  222. private void setupGitlabSettingsWithGroupSync(boolean enableGroupSync) {
  223. when(gitLabSettings.isEnabled()).thenReturn(true);
  224. when(gitLabSettings.applicationId()).thenReturn("123");
  225. when(gitLabSettings.secret()).thenReturn("456");
  226. when(gitLabSettings.syncUserGroups()).thenReturn(enableGroupSync);
  227. }
  228. }