]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7774 Create AuthenticationWsAction marker interface 1424/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 2 Dec 2016 16:12:57 +0000 (17:12 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 5 Dec 2016 09:07:12 +0000 (10:07 +0100)
server/sonar-server/src/main/java/org/sonar/server/authentication/UserSessionInitializer.java
server/sonar-server/src/main/java/org/sonar/server/authentication/ws/AuthenticationWs.java
server/sonar-server/src/main/java/org/sonar/server/authentication/ws/AuthenticationWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/authentication/ws/LoginAction.java
server/sonar-server/src/main/java/org/sonar/server/authentication/ws/LogoutAction.java
server/sonar-server/src/main/java/org/sonar/server/authentication/ws/ValidateAction.java
server/sonar-server/src/test/java/org/sonar/server/authentication/ws/AuthenticationWsTest.java

index 9a721554920d5e8184690c3bc23fc1632b880d68..df21619b05ba4e5fe5fe82e4b111f4b9aa6c0b52 100644 (file)
@@ -41,9 +41,9 @@ import static org.sonar.api.web.ServletFilter.UrlPattern.Builder.staticResourceP
 import static org.sonar.server.authentication.AuthenticationError.handleAuthenticationError;
 import static org.sonar.server.authentication.event.AuthenticationEvent.Method;
 import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
-import static org.sonar.server.authentication.ws.LoginAction.AUTH_LOGIN_URL;
-import static org.sonar.server.authentication.ws.LogoutAction.AUTH_LOGOUT_URL;
-import static org.sonar.server.authentication.ws.ValidateAction.AUTH_VALIDATE_URL;
+import static org.sonar.server.authentication.ws.LoginAction.LOGIN_URL;
+import static org.sonar.server.authentication.ws.LogoutAction.LOGOUT_URL;
+import static org.sonar.server.authentication.ws.ValidateAction.VALIDATE_URL;
 import static org.sonar.server.user.ServerUserSession.createForAnonymous;
 import static org.sonar.server.user.ServerUserSession.createForUser;
 
@@ -65,7 +65,7 @@ public class UserSessionInitializer {
     "/sessions/*",
     "/api/system/db_migration_status", "/api/system/status", "/api/system/migrate_db",
     "/api/server/index", "/api/server/setup", "/api/server/version",
-    AUTH_LOGIN_URL, AUTH_VALIDATE_URL, AUTH_LOGOUT_URL);
+    LOGIN_URL, LOGOUT_URL, VALIDATE_URL);
 
   private static final UrlPattern URL_PATTERN = UrlPattern.builder()
     .includes("/*")
index f17864b081ee5c125c6fa30a07a42dbaa3cdc3db..b8c186ec4d4c62d28cc50ce20556ae9d5945d4ff 100644 (file)
  */
 package org.sonar.server.authentication.ws;
 
-import com.google.common.io.Resources;
-import org.sonar.api.server.ws.RailsHandler;
+import java.util.List;
 import org.sonar.api.server.ws.WebService;
-import org.sonar.server.ws.ServletFilterHandler;
 
 public class AuthenticationWs implements WebService {
 
+  public static final String AUTHENTICATION_CONTROLLER = "api/authentication";
+  private final List<AuthenticationWsAction> actions;
+
+  public AuthenticationWs(List<AuthenticationWsAction> actions) {
+    this.actions = actions;
+  }
+
   @Override
   public void define(Context context) {
-    NewController controller = context.createController("api/authentication");
+    NewController controller = context.createController(AUTHENTICATION_CONTROLLER);
     controller.setDescription("Handle authentication.");
-
-    defineLoginAction(controller);
-    defineLogoutAction(controller);
-    defineValidateAction(controller);
-
+    actions.forEach(action -> action.define(controller));
     controller.done();
   }
-
-  private void defineValidateAction(NewController controller) {
-    NewAction action = controller.createAction("validate")
-      .setDescription("Check credentials.")
-      .setSince("3.3")
-      .setHandler(ServletFilterHandler.INSTANCE)
-      .setResponseExample(Resources.getResource(this.getClass(), "example-validate.json"));
-
-    RailsHandler.addFormatParam(action);
-  }
-
-  private static void defineLoginAction(NewController controller) {
-    NewAction action = controller.createAction("login")
-      .setDescription("Authenticate a user.")
-      .setSince("6.0")
-      .setPost(true)
-      .setHandler(ServletFilterHandler.INSTANCE);
-    action.createParam("login")
-      .setDescription("Login of the user")
-      .setRequired(true);
-    action.createParam("password")
-      .setDescription("Password of the user")
-      .setRequired(true);
-  }
-
-  private static void defineLogoutAction(NewController controller) {
-    controller.createAction("logout")
-      .setDescription("Logout a user.")
-      .setSince("6.3")
-      .setPost(true)
-      .setHandler(ServletFilterHandler.INSTANCE);
-  }
-
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/authentication/ws/AuthenticationWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/authentication/ws/AuthenticationWsAction.java
new file mode 100644 (file)
index 0000000..d285d77
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.ws;
+
+import org.sonar.api.server.ws.WebService;
+
+@FunctionalInterface
+public interface AuthenticationWsAction {
+  // marker interface
+
+  void define(WebService.NewController controller);
+}
index 6ca2cfead9420da97329954ee15dc4589bc0b9c4..7e3cd764801ef17befce8624968ad6adf93109cf 100644 (file)
@@ -29,6 +29,7 @@ import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import org.sonar.api.server.ws.WebService;
 import org.sonar.api.web.ServletFilter;
 import org.sonar.db.DbClient;
 import org.sonar.db.user.UserDto;
@@ -39,17 +40,20 @@ import org.sonar.server.authentication.event.AuthenticationException;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.user.ServerUserSession;
 import org.sonar.server.user.ThreadLocalUserSession;
+import org.sonar.server.ws.ServletFilterHandler;
 
 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
 import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
 import static org.apache.commons.lang.StringUtils.isEmpty;
 import static org.sonar.server.authentication.event.AuthenticationEvent.Method;
 import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
+import static org.sonar.server.authentication.ws.AuthenticationWs.AUTHENTICATION_CONTROLLER;
 import static org.sonarqube.ws.client.WsRequest.Method.POST;
 
-public class LoginAction extends ServletFilter {
+public class LoginAction extends ServletFilter implements AuthenticationWsAction {
 
-  public static final String AUTH_LOGIN_URL = "/api/authentication/login";
+  private static final String LOGIN_ACTION = "login";
+  public static final String LOGIN_URL = "/" + AUTHENTICATION_CONTROLLER + "/" + LOGIN_ACTION;
 
   private final DbClient dbClient;
   private final CredentialsAuthenticator credentialsAuthenticator;
@@ -66,9 +70,24 @@ public class LoginAction extends ServletFilter {
     this.authenticationEvent = authenticationEvent;
   }
 
+  @Override
+  public void define(WebService.NewController controller) {
+    WebService.NewAction action = controller.createAction(LOGIN_ACTION)
+      .setDescription("Authenticate a user.")
+      .setSince("6.0")
+      .setPost(true)
+      .setHandler(ServletFilterHandler.INSTANCE);
+    action.createParam("login")
+      .setDescription("Login of the user")
+      .setRequired(true);
+    action.createParam("password")
+      .setDescription("Password of the user")
+      .setRequired(true);
+  }
+
   @Override
   public UrlPattern doGetPattern() {
-    return UrlPattern.create(AUTH_LOGIN_URL);
+    return UrlPattern.create(LOGIN_URL);
   }
 
   @Override
index 2fe77c2f8bcffd382da1d0bef88119a3dd7b6e64..aa48ec86df3ad3427a66cc611b892162d8fb10a5 100644 (file)
@@ -29,18 +29,22 @@ import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import org.sonar.api.server.ws.WebService;
 import org.sonar.api.web.ServletFilter;
 import org.sonar.server.authentication.JwtHttpHandler;
 import org.sonar.server.authentication.event.AuthenticationEvent;
 import org.sonar.server.authentication.event.AuthenticationException;
+import org.sonar.server.ws.ServletFilterHandler;
 
 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
 import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
+import static org.sonar.server.authentication.ws.AuthenticationWs.AUTHENTICATION_CONTROLLER;
 import static org.sonarqube.ws.client.WsRequest.Method.POST;
 
-public class LogoutAction extends ServletFilter {
+public class LogoutAction extends ServletFilter implements AuthenticationWsAction {
 
-  public static final String AUTH_LOGOUT_URL = "/api/authentication/logout";
+  private static final String LOGOUT_ACTION = "logout";
+  public static final String LOGOUT_URL = "/" + AUTHENTICATION_CONTROLLER + "/" + LOGOUT_ACTION;
 
   private final JwtHttpHandler jwtHttpHandler;
   private final AuthenticationEvent authenticationEvent;
@@ -50,9 +54,18 @@ public class LogoutAction extends ServletFilter {
     this.authenticationEvent = authenticationEvent;
   }
 
+  @Override
+  public void define(WebService.NewController controller) {
+    controller.createAction(LOGOUT_ACTION)
+      .setDescription("Logout a user.")
+      .setSince("6.3")
+      .setPost(true)
+      .setHandler(ServletFilterHandler.INSTANCE);
+  }
+
   @Override
   public UrlPattern doGetPattern() {
-    return UrlPattern.create(AUTH_LOGOUT_URL);
+    return UrlPattern.create(LOGOUT_URL);
   }
 
   @Override
index 60791c2607a520136dd958a886deb1503b4d6bac..1aed8f0ed5b0f04e6eb0e0777461991893d0d63d 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.sonar.server.authentication.ws;
 
+import com.google.common.io.Resources;
 import java.io.IOException;
 import java.util.Optional;
 import javax.servlet.FilterChain;
@@ -30,19 +31,23 @@ import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import org.sonar.api.config.Settings;
+import org.sonar.api.server.ws.WebService;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.api.web.ServletFilter;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.authentication.BasicAuthenticator;
 import org.sonar.server.authentication.JwtHttpHandler;
 import org.sonar.server.authentication.event.AuthenticationException;
+import org.sonar.server.ws.ServletFilterHandler;
 import org.sonarqube.ws.MediaTypes;
 
 import static org.sonar.api.CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY;
+import static org.sonar.server.authentication.ws.AuthenticationWs.AUTHENTICATION_CONTROLLER;
 
-public class ValidateAction extends ServletFilter {
+public class ValidateAction extends ServletFilter implements AuthenticationWsAction {
 
-  public static final String AUTH_VALIDATE_URL = "/api/authentication/validate";
+  private static final String VALIDATE_ACTION = "validate";
+  public static final String VALIDATE_URL = "/" + AUTHENTICATION_CONTROLLER + "/" + VALIDATE_ACTION;
 
   private final Settings settings;
   private final JwtHttpHandler jwtHttpHandler;
@@ -54,9 +59,18 @@ public class ValidateAction extends ServletFilter {
     this.jwtHttpHandler = jwtHttpHandler;
   }
 
+  @Override
+  public void define(WebService.NewController controller) {
+    controller.createAction("validate")
+      .setDescription("Check credentials.")
+      .setSince("3.3")
+      .setHandler(ServletFilterHandler.INSTANCE)
+      .setResponseExample(Resources.getResource(this.getClass(), "example-validate.json"));
+  }
+
   @Override
   public UrlPattern doGetPattern() {
-    return UrlPattern.create(AUTH_VALIDATE_URL);
+    return UrlPattern.create(VALIDATE_URL);
   }
 
   @Override
index 980e4a2e433fa6060fed2e38cbb9bfb6f69038a5..86c916212a1fd7cb7f31c82c38e8c39dd281a1ac 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.authentication.ws;
 
+import java.util.Arrays;
 import org.junit.Test;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.server.ws.ServletFilterHandler;
@@ -28,7 +29,10 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 public class AuthenticationWsTest {
 
-  WsTester tester = new WsTester(new AuthenticationWs());
+  WsTester tester = new WsTester(new AuthenticationWs(Arrays.asList(
+    new LoginAction(null, null, null, null, null),
+    new LogoutAction(null, null),
+    new ValidateAction(null, null, null))));
 
   @Test
   public void define_ws() {
@@ -41,7 +45,7 @@ public class AuthenticationWsTest {
     assertThat(validate).isNotNull();
     assertThat(validate.handler()).isInstanceOf(ServletFilterHandler.class);
     assertThat(validate.responseExampleAsString()).isNotEmpty();
-    assertThat(validate.params()).hasSize(1);
+    assertThat(validate.params()).isEmpty();
 
     WebService.Action login = controller.action("login");
     assertThat(login).isNotNull();