Search in sources :

Example 6 with UserIdentity

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

the class GitHubIdentityProvider method onCallback.

private void onCallback(CallbackContext context) throws InterruptedException, ExecutionException, IOException {
    context.verifyCsrfState();
    HttpServletRequest request = context.getRequest();
    OAuth20Service scribe = newScribeBuilder(context).build(scribeApi);
    String code = request.getParameter("code");
    OAuth2AccessToken accessToken = scribe.getAccessToken(code);
    GsonUser user = gitHubRestClient.getUser(scribe, accessToken);
    check(scribe, accessToken, user);
    final String email;
    if (user.getEmail() == null) {
        // if the user has not specified a public email address in their profile
        email = gitHubRestClient.getEmail(scribe, accessToken);
    } else {
        email = user.getEmail();
    }
    UserIdentity userIdentity = userIdentityFactory.create(user, email, settings.syncGroups() ? gitHubRestClient.getTeams(scribe, accessToken) : null);
    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 7 with UserIdentity

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

the class UserRegistrarImplTest method authenticate_existing_user_succeeds_when_email_has_not_changed.

@Test
public void authenticate_existing_user_succeeds_when_email_has_not_changed() {
    UserDto currentUser = db.users().insertUser(u -> u.setEmail("john@email.com").setExternalIdentityProvider(GH_IDENTITY_PROVIDER.getKey()));
    UserIdentity userIdentity = UserIdentity.builder().setProviderId(currentUser.getExternalId()).setProviderLogin(currentUser.getExternalLogin()).setName("John").setEmail("john@email.com").build();
    underTest.register(newUserRegistration(userIdentity));
    UserDto currentUserReloaded = db.users().selectUserByLogin(currentUser.getLogin()).get();
    assertThat(currentUserReloaded.getEmail()).isEqualTo("john@email.com");
}
Also used : UserDto(org.sonar.db.user.UserDto) UserTesting.newUserDto(org.sonar.db.user.UserTesting.newUserDto) UserIdentity(org.sonar.api.server.authentication.UserIdentity) Test(org.junit.Test)

Example 8 with UserIdentity

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

the class UserRegistrarImplTest method authenticating_existing_user_throws_AuthenticationException_when_email_already_exists.

@Test
public void authenticating_existing_user_throws_AuthenticationException_when_email_already_exists() {
    UserDto existingUser = db.users().insertUser(u -> u.setEmail("john@email.com"));
    UserDto currentUser = db.users().insertUser(u -> u.setEmail(null));
    UserIdentity userIdentity = UserIdentity.builder().setProviderLogin("johndoo").setName("John").setEmail("john@email.com").build();
    Source source = Source.realm(AuthenticationEvent.Method.FORM, GH_IDENTITY_PROVIDER.getName());
    assertThatThrownBy(() -> underTest.register(newUserRegistration(userIdentity, source))).isInstanceOf(AuthenticationException.class).hasMessage("Email 'john@email.com' is already used").hasFieldOrPropertyWithValue("source", source).hasFieldOrPropertyWithValue("login", USER_IDENTITY.getProviderLogin()).hasFieldOrPropertyWithValue("publicMessage", "This account is already associated with another authentication method." + " Sign in using the current authentication method," + " or contact your administrator to transfer your account to a different authentication method.");
}
Also used : UserDto(org.sonar.db.user.UserDto) UserTesting.newUserDto(org.sonar.db.user.UserTesting.newUserDto) UserIdentity(org.sonar.api.server.authentication.UserIdentity) Source(org.sonar.server.authentication.event.AuthenticationEvent.Source) Test(org.junit.Test)

Example 9 with UserIdentity

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

the class RealmAuthenticatorTest method use_downcase_login.

@Test
public void use_downcase_login() throws Exception {
    settings.setProperty("sonar.authenticator.downcase", true);
    when(userIdentityAuthenticator.authenticate(any(UserIdentity.class), any(IdentityProvider.class), any(Source.class))).thenReturn(USER);
    executeStartWithoutGroupSync();
    executeAuthenticate("LOGIN");
    verify(userIdentityAuthenticator).authenticate(userIdentityArgumentCaptor.capture(), identityProviderArgumentCaptor.capture(), sourceCaptor.capture());
    UserIdentity userIdentity = userIdentityArgumentCaptor.getValue();
    assertThat(userIdentity.getLogin()).isEqualTo("login");
    assertThat(userIdentity.getProviderLogin()).isEqualTo("login");
    verify(authenticationEvent).loginSuccess(request, "login", Source.realm(BASIC, REALM_NAME));
}
Also used : UserIdentity(org.sonar.api.server.authentication.UserIdentity) IdentityProvider(org.sonar.api.server.authentication.IdentityProvider) Source(org.sonar.server.authentication.event.AuthenticationEvent.Source) Test(org.junit.Test)

Example 10 with UserIdentity

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

the class RealmAuthenticatorTest method authenticate.

@Test
public void authenticate() throws Exception {
    executeStartWithoutGroupSync();
    when(authenticator.doAuthenticate(any(Authenticator.Context.class))).thenReturn(true);
    UserDetails userDetails = new UserDetails();
    userDetails.setName("name");
    userDetails.setEmail("email");
    when(externalUsersProvider.doGetUserDetails(any(ExternalUsersProvider.Context.class))).thenReturn(userDetails);
    when(userIdentityAuthenticator.authenticate(any(UserIdentity.class), any(IdentityProvider.class), any(Source.class))).thenReturn(USER);
    underTest.authenticate(LOGIN, PASSWORD, request, BASIC);
    verify(userIdentityAuthenticator).authenticate(userIdentityArgumentCaptor.capture(), identityProviderArgumentCaptor.capture(), sourceCaptor.capture());
    UserIdentity userIdentity = userIdentityArgumentCaptor.getValue();
    assertThat(userIdentity.getLogin()).isEqualTo(LOGIN);
    assertThat(userIdentity.getProviderLogin()).isEqualTo(LOGIN);
    assertThat(userIdentity.getName()).isEqualTo("name");
    assertThat(userIdentity.getEmail()).isEqualTo("email");
    assertThat(userIdentity.shouldSyncGroups()).isFalse();
    verify(authenticationEvent).loginSuccess(request, LOGIN, Source.realm(BASIC, REALM_NAME));
}
Also used : UserDetails(org.sonar.api.security.UserDetails) UserIdentity(org.sonar.api.server.authentication.UserIdentity) IdentityProvider(org.sonar.api.server.authentication.IdentityProvider) Source(org.sonar.server.authentication.event.AuthenticationEvent.Source) 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