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