Search in sources :

Example 16 with UserIdentity

use of org.sonar.api.server.authentication.UserIdentity in project sonarqube by SonarSource.

the class IntegrationTest method synchronize_groups.

@Test
public void synchronize_groups() throws InterruptedException {
    mapSettings.setProperty(GITLAB_AUTH_SYNC_USER_GROUPS, "true");
    OAuth2IdentityProvider.CallbackContext callbackContext = Mockito.mock(OAuth2IdentityProvider.CallbackContext.class);
    when(callbackContext.getCallbackUrl()).thenReturn("http://server/callback");
    HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
    when(httpServletRequest.getParameter("code")).thenReturn(ANY_CODE_VALUE);
    when(callbackContext.getRequest()).thenReturn(httpServletRequest);
    gitlab.enqueue(new MockResponse().setBody("{\n" + " \"access_token\": \"de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54\",\n" + " \"token_type\": \"bearer\",\n" + " \"expires_in\": 7200,\n" + " \"refresh_token\": \"8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1\"\n" + "}"));
    // response of /user
    gitlab.enqueue(new MockResponse().setBody("{\"id\": 123, \"username\": \"username\", \"name\": \"name\"}"));
    // response of /groups
    gitlab.enqueue(new MockResponse().setBody("[{\"full_path\": \"group1\"}, {\"full_path\": \"group2\"}]"));
    gitLabIdentityProvider.callback(callbackContext);
    ArgumentCaptor<UserIdentity> captor = ArgumentCaptor.forClass(UserIdentity.class);
    verify(callbackContext).authenticate(captor.capture());
    UserIdentity value = captor.getValue();
    assertThat(value.getGroups()).contains("group1", "group2");
    assertThat(gitlab.takeRequest().getPath()).isEqualTo("/oauth/token");
    assertThat(gitlab.takeRequest().getPath()).isEqualTo("/api/v4/user");
    assertThat(gitlab.takeRequest().getPath()).isEqualTo("/api/v4/groups?min_access_level=10&per_page=100");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OAuth2IdentityProvider(org.sonar.api.server.authentication.OAuth2IdentityProvider) UserIdentity(org.sonar.api.server.authentication.UserIdentity) Test(org.junit.Test)

Example 17 with UserIdentity

use of org.sonar.api.server.authentication.UserIdentity in project sonarqube by SonarSource.

the class BitbucketIdentityProvider method onCallback.

private void onCallback(CallbackContext context) throws InterruptedException, ExecutionException, IOException {
    HttpServletRequest request = context.getRequest();
    OAuth20Service scribe = newScribeBuilder(context).build(scribeApi);
    String code = request.getParameter(OAuthConstants.CODE);
    OAuth2AccessToken accessToken = scribe.getAccessToken(code);
    GsonUser gsonUser = requestUser(scribe, accessToken);
    GsonEmails gsonEmails = requestEmails(scribe, accessToken);
    checkTeamRestriction(scribe, accessToken, gsonUser);
    UserIdentity userIdentity = userIdentityFactory.create(gsonUser, gsonEmails);
    context.authenticate(userIdentity);
    context.redirectToRequestedPage();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) UserIdentity(org.sonar.api.server.authentication.UserIdentity) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 18 with UserIdentity

use of org.sonar.api.server.authentication.UserIdentity in project sonarqube by SonarSource.

the class UserRegistrarImplTest method authenticate_new_user_sets_external_id_to_provider_login_when_id_is_null.

@Test
public void authenticate_new_user_sets_external_id_to_provider_login_when_id_is_null() {
    UserIdentity newUser = UserIdentity.builder().setProviderId(null).setProviderLogin("johndoo").setName("JOhn").build();
    UserDto user = underTest.register(newUserRegistration(newUser));
    assertThat(db.users().selectUserByLogin(user.getLogin()).get()).extracting(UserDto::getLogin, UserDto::getExternalId, UserDto::getExternalLogin).contains(user.getLogin(), "johndoo", "johndoo");
}
Also used : UserIdentity(org.sonar.api.server.authentication.UserIdentity) UserDto(org.sonar.db.user.UserDto) UserTesting.newUserDto(org.sonar.db.user.UserTesting.newUserDto) Test(org.junit.Test)

Aggregations

UserIdentity (org.sonar.api.server.authentication.UserIdentity)18 Test (org.junit.Test)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Source (org.sonar.server.authentication.event.AuthenticationEvent.Source)5 IdentityProvider (org.sonar.api.server.authentication.IdentityProvider)4 MockResponse (okhttp3.mockwebserver.MockResponse)3 OAuth2IdentityProvider (org.sonar.api.server.authentication.OAuth2IdentityProvider)3 UserDto (org.sonar.db.user.UserDto)3 UserTesting.newUserDto (org.sonar.db.user.UserTesting.newUserDto)3 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)2 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)2 UserDetails (org.sonar.api.security.UserDetails)1