]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7732 Do not remove cookies when user is not authenticated 1075/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 1 Jul 2016 12:26:32 +0000 (14:26 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 1 Jul 2016 12:26:32 +0000 (14:26 +0200)
server/sonar-server/src/main/java/org/sonar/server/authentication/JwtCsrfVerifier.java
server/sonar-server/src/main/java/org/sonar/server/authentication/JwtHttpHandler.java
server/sonar-server/src/main/java/org/sonar/server/authentication/UserSessionInitializer.java
server/sonar-server/src/test/java/org/sonar/server/authentication/JwtCsrfVerifierTest.java
server/sonar-server/src/test/java/org/sonar/server/authentication/JwtHttpHandlerTest.java
server/sonar-server/src/test/java/org/sonar/server/authentication/UserSessionInitializerTest.java

index 01c2adc46b088e8e24f8d463c38e19609126f5ee..f875b418d71471b971b41b83f48995733f5781b7 100644 (file)
@@ -81,10 +81,6 @@ public class JwtCsrfVerifier {
     response.addCookie(createCookie(csrfState, timeoutInSeconds));
   }
 
-  public void removeState(HttpServletResponse response){
-    response.addCookie(createCookie(null, 0));
-  }
-
   private static boolean shouldRequestBeChecked(HttpServletRequest request) {
     if (UPDATE_METHODS.contains(request.getMethod())) {
       String path = request.getRequestURI().replaceFirst(request.getContextPath(), "");
index 2e8e88e258f896263cbd9b93040512e3a5e82c1a..61bf975d51f0952a9036fb1f3b53685ec8cfcf3d 100644 (file)
@@ -94,7 +94,6 @@ public class JwtHttpHandler {
     if (userDto.isPresent()) {
       return userDto;
     }
-    removeToken(response);
     return Optional.empty();
   }
 
@@ -155,11 +154,6 @@ public class JwtHttpHandler {
     jwtCsrfVerifier.refreshState(response, (String) token.get(CSRF_JWT_PARAM), sessionTimeoutInSeconds);
   }
 
-  void removeToken(HttpServletResponse response) {
-    response.addCookie(createCookie(JWT_COOKIE, null, 0));
-    jwtCsrfVerifier.removeState(response);
-  }
-
   private Cookie createCookie(String name, @Nullable String value, int expirationInSeconds) {
     Cookie cookie = new Cookie(name, value);
     cookie.setPath("/");
index d25063ba7fde1d87927474f228f43d2fa035c595..3b2053eee661fe44de012daaf3ba5293357cff13 100644 (file)
@@ -85,7 +85,6 @@ public class UserSessionInitializer {
       setUserSession(request, response);
       return true;
     } catch (UnauthorizedException e) {
-      jwtHttpHandler.removeToken(response);
       response.setStatus(HTTP_UNAUTHORIZED);
       if (isWsUrl(path)) {
         return false;
index ab15b2d4027dfe1f2c11422885d521ae74ba051c..7e8e770a0efa73dd66bfed66420e116faf65a28a 100644 (file)
@@ -183,18 +183,6 @@ public class JwtCsrfVerifierTest {
     verifyCookie(cookieArgumentCaptor.getValue(), true);
   }
 
-  @Test
-  public void remove_state() throws Exception {
-    when(server.isSecured()).thenReturn(true);
-
-    underTest.removeState(response);
-
-    verify(response).addCookie(cookieArgumentCaptor.capture());
-    Cookie cookie = cookieArgumentCaptor.getValue();
-    assertThat(cookie.getValue()).isNull();
-    assertThat(cookie.getMaxAge()).isEqualTo(0);
-  }
-
   private void verifyCookie(Cookie cookie, boolean isSecured) {
     assertThat(cookie.getName()).isEqualTo("XSRF-TOKEN");
     assertThat(cookie.getValue()).isNotEmpty();
@@ -204,16 +192,16 @@ public class JwtCsrfVerifierTest {
     assertThat(cookie.getSecure()).isEqualTo(isSecured);
   }
 
-  private void mockPostJavaWsRequest(){
+  private void mockPostJavaWsRequest() {
     when(request.getRequestURI()).thenReturn(JAVA_WS_URL);
     when(request.getMethod()).thenReturn("POST");
   }
 
-  private void mockRequestCsrf(String csrfState){
+  private void mockRequestCsrf(String csrfState) {
     when(request.getHeader("X-XSRF-TOKEN")).thenReturn(csrfState);
   }
 
-  private void executeVerifyStateDoesNotFailOnRequest(String uri, String method){
+  private void executeVerifyStateDoesNotFailOnRequest(String uri, String method) {
     when(request.getRequestURI()).thenReturn(uri);
     when(request.getMethod()).thenReturn(method);
 
index e276fde60075b3d89a3698dc729b5d9dca5febb5..93ca53231f82147143a2c9422272016ae8ebcd5f 100644 (file)
@@ -196,7 +196,7 @@ public class JwtHttpHandlerTest {
   }
 
   @Test
-  public void validate_token_removes_session_when_disconnected_timeout_is_reached() throws Exception {
+  public void validate_token_does_not_refresh_session_when_disconnected_timeout_is_reached() throws Exception {
     addJwtCookie();
 
     // Token was created 4 months ago, refreshed 4 minutes ago, and it expired in 5 minutes
@@ -206,12 +206,10 @@ public class JwtHttpHandlerTest {
     when(jwtSerializer.decode(JWT_TOKEN)).thenReturn(Optional.of(claims));
 
     assertThat(underTest.validateToken(request, response).isPresent()).isFalse();
-
-    verifyCookie(findCookie("JWT-SESSION").get(), null, 0);
   }
 
   @Test
-  public void validate_token_removes_session_when_user_is_disabled() throws Exception {
+  public void validate_token_does_not_refresh_session_when_user_is_disabled() throws Exception {
     addJwtCookie();
     UserDto user = addUser(false);
 
@@ -219,19 +217,15 @@ public class JwtHttpHandlerTest {
     when(jwtSerializer.decode(JWT_TOKEN)).thenReturn(Optional.of(claims));
 
     assertThat(underTest.validateToken(request, response).isPresent()).isFalse();
-
-    verifyCookie(findCookie("JWT-SESSION").get(), null, 0);
   }
 
   @Test
-  public void validate_token_removes_session_when_token_is_no_more_valid() throws Exception {
+  public void validate_token_does_not_refresh_session_when_token_is_no_more_valid() throws Exception {
     addJwtCookie();
 
     when(jwtSerializer.decode(JWT_TOKEN)).thenReturn(Optional.empty());
 
     assertThat(underTest.validateToken(request, response).isPresent()).isFalse();
-
-    verifyCookie(findCookie("JWT-SESSION").get(), null, 0);
   }
 
   @Test
@@ -279,26 +273,6 @@ public class JwtHttpHandlerTest {
     verify(jwtCsrfVerifier).refreshState(response, "CSRF_STATE", 3 * 24 * 60 * 60);
   }
 
-  @Test
-  public void validate_token_remove_state_when_removing_token() throws Exception {
-    addJwtCookie();
-    // Token is invalid => it will be removed
-    when(jwtSerializer.decode(JWT_TOKEN)).thenReturn(Optional.empty());
-
-    underTest.validateToken(request, response);
-
-    verifyCookie(findCookie("JWT-SESSION").get(), null, 0);
-    verify(jwtCsrfVerifier).removeState(response);
-  }
-
-  @Test
-  public void remove_token() throws Exception {
-    underTest.removeToken(response);
-
-    verifyCookie(findCookie("JWT-SESSION").get(), null, 0);
-    verify(jwtCsrfVerifier).removeState(response);
-  }
-
   private void verifyToken(JwtSerializer.JwtSession token, int expectedExpirationTime, long expectedRefreshTime) {
     assertThat(token.getExpirationTimeInSeconds()).isEqualTo(expectedExpirationTime);
     assertThat(token.getUserLogin()).isEqualTo(USER_LOGIN);
index 6ea3fcd20071f3e6f59bf264027232d9686f395f..dba52e8d4dce2ca49e8ce2551a7a8a284ab2edba 100644 (file)
@@ -137,6 +137,7 @@ public class UserSessionInitializerTest {
     assertThat(underTest.initUserSession(request, response)).isTrue();
 
     verify(response).setStatus(401);
+    verifyZeroInteractions(userSession);
   }
 
   @Test
@@ -149,6 +150,7 @@ public class UserSessionInitializerTest {
     assertThat(underTest.initUserSession(request, response)).isTrue();
 
     verify(response).setStatus(401);
+    verifyZeroInteractions(userSession);
   }
 
   @Test
@@ -159,6 +161,7 @@ public class UserSessionInitializerTest {
     assertThat(underTest.initUserSession(request, response)).isFalse();
 
     verify(response).setStatus(401);
+    verifyZeroInteractions(userSession);
   }
 
   @Test
@@ -169,6 +172,7 @@ public class UserSessionInitializerTest {
     assertThat(underTest.initUserSession(request, response)).isFalse();
 
     verify(response).setStatus(401);
+    verifyZeroInteractions(userSession);
   }
 
   @Test