diff options
author | Alexander Alexeev | 2016-12-01 00:34:25 +0700 |
---|---|---|
committer | Alexander Alexeev | 2016-12-01 00:34:25 +0700 |
commit | 92d6700919e87c81db4a42a4b286257f62566cbf (patch) | |
tree | e1ff4baee90c20194593e6886c6769f442890d19 | |
parent | 92f638429647d328d8a6b6168755b148ec237d3a (diff) |
remember-me for juick-api by request parameter
-rw-r--r-- | juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java | 10 | ||||
-rw-r--r-- | juick-api/src/test/java/com/juick/api/tests/MessagesTests.java | 19 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java (renamed from juick-server/src/main/java/com/juick/service/security/SimpleRememberMeServices.java) | 27 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java | 71 | ||||
-rw-r--r-- | juick-server/src/main/resources/juick.conf.example | 3 |
5 files changed, 111 insertions, 19 deletions
diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java index 9f937c98..99731d2a 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java @@ -3,7 +3,8 @@ package com.juick.api.configuration; import com.juick.server.security.JuickAuthenticationEntryPoint; import com.juick.service.UserService; import com.juick.service.security.JuickUserDetailsService; -import com.juick.service.security.SimpleRememberMeServices; +import com.juick.service.security.deprecated.CookieSimpleHashRememberMeServices; +import com.juick.service.security.deprecated.RequestParamHashRememberMeServices; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @@ -21,6 +22,7 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import javax.inject.Inject; import java.util.Arrays; +import java.util.Collections; import java.util.concurrent.TimeUnit; /** @@ -77,7 +79,7 @@ public class ApiSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public RememberMeServices rememberMeServices() throws Exception { - return new SimpleRememberMeServices(env.getProperty("auth_remember_me_key"), userService, env); + return new RequestParamHashRememberMeServices(env.getProperty("auth_remember_me_key"), userService); } @Bean @@ -89,9 +91,9 @@ public class ApiSecurityConfig extends WebSecurityConfigurerAdapter { public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins(Arrays.asList("*")); + configuration.setAllowedOrigins(Collections.singletonList("*")); configuration.setAllowedMethods(Arrays.asList("POST", "GET", "PUT", "OPTIONS", "DELETE")); - configuration.setAllowedHeaders(Arrays.asList("*")); + configuration.setAllowedHeaders(Collections.singletonList("*")); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java index 33704fc5..05744a61 100644 --- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java +++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java @@ -19,7 +19,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Primary; import org.springframework.http.MediaType; -import org.springframework.security.crypto.codec.Base64; import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -29,7 +28,6 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import javax.inject.Inject; -import javax.servlet.http.Cookie; import java.util.*; import static org.hamcrest.Matchers.hasSize; @@ -158,6 +156,8 @@ public class MessagesTests { User user = getUser(1, ugnichName, uginchPassword); Message msg = getMessage(user, msgText); + when(userService.getUIDbyName(ugnichName)) + .thenReturn(1); when(userService.getUserByName(ugnichName)) .thenReturn(user); when(userService.getUserByUID(1)) @@ -171,12 +171,17 @@ public class MessagesTests { when(userService.getUIDbyHash(hash)) .thenReturn(1); - Cookie cookie = new Cookie("hash", new String(Base64.encode(hash.getBytes()))); - cookie.setDomain("juick.com"); - cookie.setMaxAge(100); - mockMvc.perform( - get("/home").cookie(cookie)) + get("/home") + .with(httpBasic(ugnichName, uginchPassword))) + .andExpect(status().isOk()) + .andReturn(); + + when(userService.getUserByHash(hash)) + .thenReturn(user); + + mockMvc.perform(get("/home") + .param("hash", hash)) .andExpect(status().isOk()); } diff --git a/juick-server/src/main/java/com/juick/service/security/SimpleRememberMeServices.java b/juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java index aef133d3..ec44baa0 100644 --- a/juick-server/src/main/java/com/juick/service/security/SimpleRememberMeServices.java +++ b/juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java @@ -1,8 +1,9 @@ -package com.juick.service.security; +package com.juick.service.security.deprecated; import com.juick.User; import com.juick.server.security.entities.JuickUser; import com.juick.service.UserService; +import com.juick.service.security.NullUserDetailsService; import com.juick.util.UserUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -23,13 +24,18 @@ import java.util.Optional; /** * Created by aalexeev on 11/28/16. + * + * @deprecated not recommended use for secure reasons */ -public class SimpleRememberMeServices extends AbstractRememberMeServices implements RememberMeServices { - private static final Logger logger = LoggerFactory.getLogger(SimpleRememberMeServices.class); +@Deprecated +public class CookieSimpleHashRememberMeServices extends AbstractRememberMeServices implements RememberMeServices { + private static final Logger logger = LoggerFactory.getLogger(CookieSimpleHashRememberMeServices.class); + + private static final String COOKIE_PARAM_NAME = "hash"; private final UserService userService; - public SimpleRememberMeServices( + public CookieSimpleHashRememberMeServices( final String key, final UserService userService, final Environment environment) { super(key, new NullUserDetailsService()); @@ -38,8 +44,9 @@ public class SimpleRememberMeServices extends AbstractRememberMeServices impleme this.userService = userService; - setCookieName(environment.getProperty("auth_cookie_name", "hash")); + setCookieName(COOKIE_PARAM_NAME); setCookieDomain(environment.getProperty("web_domain", "juick.com")); + setAlwaysRemember(true); } @Override @@ -89,4 +96,14 @@ public class SimpleRememberMeServices extends AbstractRememberMeServices impleme return new JuickUser(userOptional.get()); } + + @Override + protected String[] decodeCookie(String cookieValue) throws InvalidCookieException { + return new String[]{cookieValue}; + } + + @Override + protected String encodeCookie(String[] cookieTokens) { + return cookieTokens != null && cookieTokens.length > 0 ? cookieTokens[0] : ""; + } } diff --git a/juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java b/juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java new file mode 100644 index 00000000..0e635927 --- /dev/null +++ b/juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java @@ -0,0 +1,71 @@ +package com.juick.service.security.deprecated; + +import com.juick.User; +import com.juick.server.security.entities.JuickUser; +import com.juick.service.UserService; +import com.juick.service.security.NullUserDetailsService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.web.authentication.RememberMeServices; +import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices; +import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException; +import org.springframework.util.Assert; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Created by aalexeev on 11/30/16. + * + * @deprecated for security reasons + */ +@Deprecated +public class RequestParamHashRememberMeServices extends AbstractRememberMeServices implements RememberMeServices { + private static final String PARAM_NAME = "hash"; + + private final UserService userService; + + public RequestParamHashRememberMeServices(String key, UserService userService) { + super(key, new NullUserDetailsService()); + + Assert.notNull(userService); + this.userService = userService; + setAlwaysRemember(false); + } + + @Override + protected void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) { + // do nothing + } + + @Override + protected boolean rememberMeRequested(HttpServletRequest request, String parameter) { + return false; // always false + } + + @Override + protected void cancelCookie(HttpServletRequest request, HttpServletResponse response) { + // do nothing + } + + @Override + protected String extractRememberMeCookie(HttpServletRequest request) { + return PARAM_NAME; // return any not blank value + } + + @Override + protected UserDetails processAutoLoginCookie( + String[] cookieTokens, HttpServletRequest request, HttpServletResponse response) + throws RememberMeAuthenticationException, UsernameNotFoundException { + String hash = request.getParameter(PARAM_NAME); + + if (StringUtils.isNotBlank(hash)) { + User user = userService.getUserByHash(hash); + if (user.getUid() > 0) + return new JuickUser(user); + } + throw new UsernameNotFoundException("User not found by hash " + hash); + } +} diff --git a/juick-server/src/main/resources/juick.conf.example b/juick-server/src/main/resources/juick.conf.example index 6a931ab5..7c39ef16 100644 --- a/juick-server/src/main/resources/juick.conf.example +++ b/juick-server/src/main/resources/juick.conf.example @@ -29,9 +29,6 @@ sphinx_password= # The domain name for Web (default value - "juick.com") web_domain=juick.com -# Authority cookie name (default value - "hash") -auth_cookie_name=hash - # Authority remember-me key auth_remember_me_key=3vHcy3OUDQlkpRDm |