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.

GitLabIdentityProviderTest.java 11KB

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