Search in sources :

Example 1 with UserDetails

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

the class RealmAuthenticatorTest method fail_to_authenticate_when_external_authentication_fails.

@Test
public void fail_to_authenticate_when_external_authentication_fails() throws Exception {
    executeStartWithoutGroupSync();
    when(externalUsersProvider.doGetUserDetails(any(ExternalUsersProvider.Context.class))).thenReturn(new UserDetails());
    when(authenticator.doAuthenticate(any(Authenticator.Context.class))).thenReturn(false);
    expectedException.expect(authenticationException().from(Source.realm(BASIC, REALM_NAME)).withLogin(LOGIN).andNoPublicMessage());
    expectedException.expectMessage("Realm returned authenticate=false");
    try {
        underTest.authenticate(LOGIN, PASSWORD, request, BASIC);
    } finally {
        verifyZeroInteractions(authenticationEvent);
    }
}
Also used : UserDetails(org.sonar.api.security.UserDetails) Test(org.junit.Test)

Example 2 with UserDetails

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

the class RealmAuthenticatorTest method fail_to_authenticate_when_any_exception_is_thrown.

@Test
public void fail_to_authenticate_when_any_exception_is_thrown() throws Exception {
    executeStartWithoutGroupSync();
    String expectedMessage = "emulating exception in doAuthenticate";
    doThrow(new IllegalArgumentException(expectedMessage)).when(authenticator).doAuthenticate(any(Authenticator.Context.class));
    when(externalUsersProvider.doGetUserDetails(any(ExternalUsersProvider.Context.class))).thenReturn(new UserDetails());
    expectedException.expect(authenticationException().from(Source.realm(BASIC_TOKEN, REALM_NAME)).withLogin(LOGIN).andNoPublicMessage());
    expectedException.expectMessage(expectedMessage);
    try {
        underTest.authenticate(LOGIN, PASSWORD, request, BASIC_TOKEN);
    } finally {
        verifyZeroInteractions(authenticationEvent);
    }
}
Also used : UserDetails(org.sonar.api.security.UserDetails) Test(org.junit.Test)

Example 3 with UserDetails

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

the class RealmAuthenticatorTest method login_is_used_when_no_name_provided.

@Test
public void login_is_used_when_no_name_provided() throws Exception {
    executeStartWithoutGroupSync();
    when(authenticator.doAuthenticate(any(Authenticator.Context.class))).thenReturn(true);
    UserDetails userDetails = new UserDetails();
    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());
    assertThat(identityProviderArgumentCaptor.getValue().getName()).isEqualTo("sonarqube");
    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 4 with UserDetails

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

the class RealmAuthenticatorTest method authenticate_with_sonarqube_identity_provider.

@Test
public void authenticate_with_sonarqube_identity_provider() 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());
    assertThat(identityProviderArgumentCaptor.getValue().getKey()).isEqualTo("sonarqube");
    assertThat(identityProviderArgumentCaptor.getValue().getName()).isEqualTo("sonarqube");
    assertThat(identityProviderArgumentCaptor.getValue().getDisplay()).isNull();
    assertThat(identityProviderArgumentCaptor.getValue().isEnabled()).isTrue();
    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 5 with UserDetails

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

the class RealmAuthenticatorTest method use_login_if_user_details_contains_no_name.

@Test
public void use_login_if_user_details_contains_no_name() throws Exception {
    executeStartWithoutGroupSync();
    when(authenticator.doAuthenticate(any(Authenticator.Context.class))).thenReturn(true);
    UserDetails userDetails = new UserDetails();
    userDetails.setName(null);
    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());
    assertThat(userIdentityArgumentCaptor.getValue().getName()).isEqualTo(LOGIN);
    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

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