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));
}
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);
}
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;
}
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");
}
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();
}
}
Aggregations