]> source.dussan.org Git - sonarqube.git/commitdiff
NO-JIRA cleanup - removed unused CustomAuthentication
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Mon, 30 Jan 2023 12:20:28 +0000 (13:20 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 30 Jan 2023 20:03:01 +0000 (20:03 +0000)
server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/CustomAuthentication.java [deleted file]
server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/RequestAuthenticatorImpl.java
server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/RequestAuthenticatorImplTest.java

diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/CustomAuthentication.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/CustomAuthentication.java
deleted file mode 100644 (file)
index 9ff5b95..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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.Optional;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import org.sonar.api.server.ServerSide;
-import org.sonar.server.user.UserSession;
-
-/**
- * Authentication that can create {@link org.sonar.server.user.UserSession}
- * that are not associated to a user.
- * That is convenient for authenticating bots that need special permissions.
- *
- * This is not an extension point, plugins can not provide their own
- * implementations.
- */
-@ServerSide
-public interface CustomAuthentication {
-
-  Optional<UserSession> authenticate(HttpServletRequest request, HttpServletResponse response);
-
-}
index 22db181d9714b48e74b9b6ed654300c72834227c..b74f1631c39b090078a646358f9e45901c7f515e 100644 (file)
@@ -19,9 +19,6 @@
  */
 package org.sonar.server.authentication;
 
-import java.util.Arrays;
-import java.util.List;
-import java.util.Optional;
 import java.util.function.Function;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -46,37 +43,28 @@ public class RequestAuthenticatorImpl implements RequestAuthenticator {
   private final HttpHeadersAuthentication httpHeadersAuthentication;
   private final GithubWebhookAuthentication githubWebhookAuthentication;
   private final UserSessionFactory userSessionFactory;
-  private final List<CustomAuthentication> customAuthentications;
 
   @Autowired(required = false)
   public RequestAuthenticatorImpl(JwtHttpHandler jwtHttpHandler, BasicAuthentication basicAuthentication, UserTokenAuthentication userTokenAuthentication,
     HttpHeadersAuthentication httpHeadersAuthentication,
-    GithubWebhookAuthentication githubWebhookAuthentication, UserSessionFactory userSessionFactory, CustomAuthentication[] customAuthentications) {
+    GithubWebhookAuthentication githubWebhookAuthentication, UserSessionFactory userSessionFactory) {
     this.jwtHttpHandler = jwtHttpHandler;
     this.basicAuthentication = basicAuthentication;
     this.userTokenAuthentication = userTokenAuthentication;
     this.httpHeadersAuthentication = httpHeadersAuthentication;
     this.githubWebhookAuthentication = githubWebhookAuthentication;
     this.userSessionFactory = userSessionFactory;
-    this.customAuthentications = Arrays.asList(customAuthentications);
   }
 
   @Autowired(required = false)
   public RequestAuthenticatorImpl(JwtHttpHandler jwtHttpHandler, BasicAuthentication basicAuthentication, UserTokenAuthentication userTokenAuthentication,
     HttpHeadersAuthentication httpHeadersAuthentication,
     UserSessionFactory userSessionFactory, GithubWebhookAuthentication githubWebhookAuthentication) {
-    this(jwtHttpHandler, basicAuthentication, userTokenAuthentication, httpHeadersAuthentication, githubWebhookAuthentication, userSessionFactory, new CustomAuthentication[0]);
+    this(jwtHttpHandler, basicAuthentication, userTokenAuthentication, httpHeadersAuthentication, githubWebhookAuthentication, userSessionFactory);
   }
 
   @Override
   public UserSession authenticate(HttpServletRequest request, HttpServletResponse response) {
-    for (CustomAuthentication customAuthentication : customAuthentications) {
-      Optional<UserSession> session = customAuthentication.authenticate(request, response);
-      if (session.isPresent()) {
-        return session.get();
-      }
-    }
-
     UserAuthResult userAuthResult = loadUser(request, response);
     if (nonNull(userAuthResult.getUserDto())) {
       if (TOKEN.equals(userAuthResult.getAuthType())) {
@@ -94,10 +82,10 @@ public class RequestAuthenticatorImpl implements RequestAuthenticator {
     // SSO authentication should come first in order to update JWT if user from header is not the same is user from JWT
     return httpHeadersAuthentication.authenticate(request, response).map(createUserAuthResult.apply(SSO))
       .orElseGet(() -> jwtHttpHandler.validateToken(request, response).map(createUserAuthResult.apply(JWT))
-      .orElseGet(() -> userTokenAuthentication.authenticate(request)
-        .or(() -> githubWebhookAuthentication.authenticate(request))
-        .or(() -> basicAuthentication.authenticate(request).map(createUserAuthResult.apply(BASIC)))
-        .orElseGet(UserAuthResult::new)));
+        .orElseGet(() -> userTokenAuthentication.authenticate(request)
+          .or(() -> githubWebhookAuthentication.authenticate(request))
+          .or(() -> basicAuthentication.authenticate(request).map(createUserAuthResult.apply(BASIC)))
+          .orElseGet(UserAuthResult::new)));
   }
 
 }
index 932e8fb2fa7cce257185bc3787a36e384a013d4a..1e5e79dc30111c4e95def642f9472bef42abf701 100644 (file)
@@ -26,8 +26,6 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.db.user.UserDto;
 import org.sonar.db.user.UserTokenDto;
-import org.sonar.server.authentication.event.AuthenticationEvent;
-import org.sonar.server.authentication.event.AuthenticationException;
 import org.sonar.server.tester.AnonymousMockUserSession;
 import org.sonar.server.tester.MockUserSession;
 import org.sonar.server.user.GithubWebhookUserSession;
@@ -57,11 +55,8 @@ public class RequestAuthenticatorImplTest {
   private final GithubWebhookAuthentication githubWebhookAuthentication = mock(GithubWebhookAuthentication.class);
   private final HttpHeadersAuthentication httpHeadersAuthentication = mock(HttpHeadersAuthentication.class);
   private final UserSessionFactory sessionFactory = mock(UserSessionFactory.class);
-  private final CustomAuthentication customAuthentication1 = mock(CustomAuthentication.class);
-  private final CustomAuthentication customAuthentication2 = mock(CustomAuthentication.class);
   private final RequestAuthenticator underTest = new RequestAuthenticatorImpl(jwtHttpHandler, basicAuthentication, userTokenAuthentication, httpHeadersAuthentication,
-    githubWebhookAuthentication, sessionFactory,
-    new CustomAuthentication[]{customAuthentication1, customAuthentication2});
+    githubWebhookAuthentication, sessionFactory);
 
   private final GithubWebhookUserSession githubWebhookMockUserSession = mock(GithubWebhookUserSession.class);
 
@@ -144,31 +139,6 @@ public class RequestAuthenticatorImplTest {
     verify(response, never()).setStatus(anyInt());
   }
 
-  @Test
-  public void delegate_to_CustomAuthentication() {
-    when(customAuthentication1.authenticate(request, response)).thenReturn(Optional.of(new MockUserSession("foo")));
-
-    UserSession session = underTest.authenticate(request, response);
-
-    assertThat(session.getLogin()).isEqualTo("foo");
-  }
-
-  @Test
-  public void CustomAuthentication_has_priority_over_core_authentications() {
-    // use-case: both custom and core authentications check the HTTP header "Authorization".
-    // The custom authentication should be able to test the header because that the core authentication
-    // throws an exception.
-    when(customAuthentication1.authenticate(request, response)).thenReturn(Optional.of(new MockUserSession("foo")));
-    when(basicAuthentication.authenticate(request)).thenThrow(AuthenticationException.newBuilder()
-      .setSource(AuthenticationEvent.Source.sso())
-      .setMessage("message")
-      .build());
-
-    UserSession session = underTest.authenticate(request, response);
-
-    assertThat(session.getLogin()).isEqualTo("foo");
-  }
-
   private static UserTokenDto mockUserTokenDto(UserDto userDto) {
     UserTokenDto userTokenDto = new UserTokenDto();
     userTokenDto.setType(USER_TOKEN.name());