Search in sources :

Example 6 with UserDetails

use of org.sonar.api.security.UserDetails 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)

Example 7 with UserDetails

use of org.sonar.api.security.UserDetails in project sonarqube by SonarSource.

the class RealmAuthenticatorTest method executeAuthenticate.

private void executeAuthenticate(String login) {
    when(authenticator.doAuthenticate(any(Authenticator.Context.class))).thenReturn(true);
    UserDetails userDetails = new UserDetails();
    userDetails.setName("name");
    when(externalUsersProvider.doGetUserDetails(any(ExternalUsersProvider.Context.class))).thenReturn(userDetails);
    underTest.authenticate(login, PASSWORD, request, BASIC);
}
Also used : UserDetails(org.sonar.api.security.UserDetails)

Example 8 with UserDetails

use of org.sonar.api.security.UserDetails in project sonarqube by SonarSource.

the class FakeAuthenticator method doGetUserDetails.

public UserDetails doGetUserDetails(String username) {
    // Never touch admin
    if (isAdmin(username)) {
        return null;
    }
    reloadData();
    checkExistence(username);
    UserDetails result = new UserDetails();
    result.setName(Strings.nullToEmpty(data.get(username + ".name")));
    result.setEmail(Strings.nullToEmpty(data.get(username + ".email")));
    LOG.info("details for user {} : {}", username, result);
    return result;
}
Also used : UserDetails(org.sonar.api.security.UserDetails)

Example 9 with UserDetails

use of org.sonar.api.security.UserDetails in project sonarqube by SonarSource.

the class FakeAuthenticatorTest method shouldGetUserDetails.

@Test
public void shouldGetUserDetails() {
    settings.setProperty(FakeAuthenticator.DATA_PROPERTY, "evgeny.password=foo\n" + "evgeny.name=Tester Testerovich\n" + "evgeny.email=evgeny@example.org");
    UserDetails details = authenticator.doGetUserDetails("evgeny");
    assertThat(details.getName()).isEqualTo("Tester Testerovich");
    assertThat(details.getEmail()).isEqualTo("evgeny@example.org");
}
Also used : UserDetails(org.sonar.api.security.UserDetails) Test(org.junit.Test)

Example 10 with UserDetails

use of org.sonar.api.security.UserDetails in project sonarqube by SonarSource.

the class RealmAuthenticator method doAuthenticate.

private UserDto doAuthenticate(String userLogin, String userPassword, HttpServletRequest request, AuthenticationEvent.Method method) {
    try {
        ExternalUsersProvider.Context externalUsersProviderContext = new ExternalUsersProvider.Context(userLogin, request);
        UserDetails details = externalUsersProvider.doGetUserDetails(externalUsersProviderContext);
        if (details == null) {
            throw AuthenticationException.newBuilder().setSource(realmEventSource(method)).setLogin(userLogin).setMessage("No user details").build();
        }
        Authenticator.Context authenticatorContext = new Authenticator.Context(userLogin, userPassword, request);
        boolean status = authenticator.doAuthenticate(authenticatorContext);
        if (!status) {
            throw AuthenticationException.newBuilder().setSource(realmEventSource(method)).setLogin(userLogin).setMessage("Realm returned authenticate=false").build();
        }
        UserDto userDto = synchronize(userLogin, details, request, method);
        authenticationEvent.loginSuccess(request, userLogin, realmEventSource(method));
        return userDto;
    } catch (AuthenticationException e) {
        throw e;
    } catch (Exception e) {
        // It seems that with Realm API it's expected to log the error and to not authenticate the user
        LOG.error("Error during authentication", e);
        throw AuthenticationException.newBuilder().setSource(realmEventSource(method)).setLogin(userLogin).setMessage(e.getMessage()).build();
    }
}
Also used : UserDetails(org.sonar.api.security.UserDetails) AuthenticationException(org.sonar.server.authentication.event.AuthenticationException) ExternalUsersProvider(org.sonar.api.security.ExternalUsersProvider) UserDto(org.sonar.db.user.UserDto) Authenticator(org.sonar.api.security.Authenticator) AuthenticationException(org.sonar.server.authentication.event.AuthenticationException)

Aggregations

UserDetails (org.sonar.api.security.UserDetails)10 Test (org.junit.Test)7 IdentityProvider (org.sonar.api.server.authentication.IdentityProvider)4 UserIdentity (org.sonar.api.server.authentication.UserIdentity)4 Source (org.sonar.server.authentication.event.AuthenticationEvent.Source)4 Authenticator (org.sonar.api.security.Authenticator)1 ExternalUsersProvider (org.sonar.api.security.ExternalUsersProvider)1 UserDto (org.sonar.db.user.UserDto)1 AuthenticationException (org.sonar.server.authentication.event.AuthenticationException)1