+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.authentication;
-
-import java.util.Set;
-import javax.annotation.Nullable;
-import org.sonar.api.server.authentication.OAuth2IdentityProvider;
-import org.sonar.api.server.authentication.UserIdentity;
-
-public interface OAuth2CallbackContext extends OAuth2IdentityProvider.CallbackContext {
-
- void authenticate(UserIdentity userIdentity, @Nullable Set<String> organizationAlmIds);
-
-}
return server.getPublicRootUrl() + CALLBACK_PATH + identityProviderKey;
}
- public class OAuthContextImpl implements OAuth2IdentityProvider.InitContext, OAuth2CallbackContext {
+ public class OAuthContextImpl implements OAuth2IdentityProvider.InitContext, OAuth2IdentityProvider.CallbackContext {
private final HttpServletRequest request;
private final HttpServletResponse response;
@Override
public void authenticate(UserIdentity userIdentity) {
- authenticate(userIdentity, null);
- }
-
- @Override
- public void authenticate(UserIdentity userIdentity, @Nullable Set<String> organizationAlmIds) {
UserDto userDto = userRegistrar.register(
UserRegistration.builder()
.setUserIdentity(userIdentity)
.setProvider(identityProvider)
.setSource(AuthenticationEvent.Source.oauth2(identityProvider))
- .setOrganizationAlmIds(organizationAlmIds)
.build());
jwtHttpHandler.generateToken(userDto, request, response);
threadLocalUserSession.set(userSessionFactory.create(userDto));
private final UserIdentity userIdentity;
private final IdentityProvider provider;
private final AuthenticationEvent.Source source;
- private final Set<String> organizationAlmIds;
UserRegistration(Builder builder) {
this.userIdentity = builder.userIdentity;
this.provider = builder.provider;
this.source = builder.source;
- this.organizationAlmIds = builder.organizationAlmIds;
}
public UserIdentity getUserIdentity() {
return source;
}
- @CheckForNull
- public Set<String> getOrganizationAlmIds() {
- return organizationAlmIds;
- }
-
public static UserRegistration.Builder builder() {
return new Builder();
}
private UserIdentity userIdentity;
private IdentityProvider provider;
private AuthenticationEvent.Source source;
- private Set<String> organizationAlmIds;
public Builder setUserIdentity(UserIdentity userIdentity) {
this.userIdentity = userIdentity;
return this;
}
- /**
- * List of ALM organization the user is member of.
- * When set to null, it means that no organization membership synchronization should be done.
- */
- public Builder setOrganizationAlmIds(@Nullable Set<String> organizationAlmIds) {
- this.organizationAlmIds = organizationAlmIds;
- return this;
- }
-
public UserRegistration build() {
requireNonNull(userIdentity, "userIdentity must be set");
requireNonNull(provider, "identityProvider must be set");
assertThat(userArgumentCaptor.getValue().getExternalIdentityProvider()).isEqualTo(PROVIDER_KEY);
}
- @Test
- public void authenticate_with_organization_alm_ids() {
- OAuthContextImpl callback = (OAuthContextImpl) newCallbackContext();
- Set<String> organizationAlmIds = ImmutableSet.of("ABCD", "EFGH");
-
- callback.authenticate(USER_IDENTITY, organizationAlmIds);
-
- assertThat(userIdentityAuthenticator.getAuthenticatorParameters().getOrganizationAlmIds()).containsAll(organizationAlmIds);
- }
-
@Test
public void redirect_to_home() throws Exception {
when(server.getContextPath()).thenReturn("");