Skip to content
Snippets Groups Projects
Commit 0bc34efb authored by Julian Hochstetter's avatar Julian Hochstetter
Browse files

Task #3834: Reuse existing guest username

* Make guest login working with new UserService
* Reuse existing guest username
* Adapted tests
parent 18d426cf
Branches
Tags
No related merge requests found
...@@ -47,6 +47,8 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -47,6 +47,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;
import de.thm.arsnova.entities.User; import de.thm.arsnova.entities.User;
import de.thm.arsnova.services.IUserService; import de.thm.arsnova.services.IUserService;
...@@ -69,35 +71,40 @@ public class LoginController { ...@@ -69,35 +71,40 @@ public class LoginController {
@Autowired @Autowired
IUserService userService; IUserService userService;
public static final Logger logger = LoggerFactory public static final Logger logger = LoggerFactory.getLogger(LoginController.class);
.getLogger(LoginController.class);
@RequestMapping(method = RequestMethod.GET, value = "/doLogin") @RequestMapping(method = RequestMethod.GET, value = "/doLogin")
public ModelAndView doLogin(@RequestParam("type") String type, HttpServletRequest request, HttpServletResponse response) public View doLogin(@RequestParam("type") String type, @RequestParam(value="user", required=false) String guestName, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException { throws IOException, ServletException {
request.getSession().setAttribute("ars-referer", request.getHeader("referer")); String referer = request.getHeader("referer");
request.getSession().setAttribute("ars-referer", referer);
if("cas".equals(type)) { if("cas".equals(type)) {
casEntryPoint.commence(request, response, null); casEntryPoint.commence(request, response, null);
} else if("twitter".equals(type)) { } else if("twitter".equals(type)) {
String authUrl = twitterProvider.getAuthorizationUrl(new HttpUserSession(request)); String authUrl = twitterProvider.getAuthorizationUrl(new HttpUserSession(request));
return new ModelAndView("redirect:" + authUrl); return new RedirectView(authUrl);
} else if("facebook".equals(type)) { } else if("facebook".equals(type)) {
String authUrl = facebookProvider.getAuthorizationUrl(new HttpUserSession(request)); String authUrl = facebookProvider.getAuthorizationUrl(new HttpUserSession(request));
return new ModelAndView("redirect:" + authUrl); return new RedirectView(authUrl);
} else if("google".equals(type)) { } else if("google".equals(type)) {
String authUrl = googleProvider.getAuthorizationUrl(new HttpUserSession(request)); String authUrl = googleProvider.getAuthorizationUrl(new HttpUserSession(request));
return new ModelAndView("redirect:" + authUrl); return new RedirectView(authUrl);
} else if("guest".equals(type)) { } else if("guest".equals(type)) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("ROLE_GUEST")); authorities.add(new SimpleGrantedAuthority("ROLE_GUEST"));
String username = "Guest" + Sha512DigestUtils.shaHex(request.getSession().getId()).substring(0, 10); String username = "";
if(guestName != null && guestName.startsWith("Guest") && guestName.length() == 15) {
username = guestName;
} else {
username = "Guest" + Sha512DigestUtils.shaHex(request.getSession().getId()).substring(0, 10);
}
org.springframework.security.core.userdetails.User user = org.springframework.security.core.userdetails.User user =
new org.springframework.security.core.userdetails.User(username, "", true, true, true, true, authorities); new org.springframework.security.core.userdetails.User(username, "", true, true, true, true, authorities);
Authentication token = new UsernamePasswordAuthenticationToken(user, null, authorities); Authentication token = new UsernamePasswordAuthenticationToken(user, null, authorities);
SecurityContextHolder.getContext().setAuthentication(token); SecurityContextHolder.getContext().setAuthentication(token);
request.getSession(true).setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext()); request.getSession(true).setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());
return new ModelAndView("redirect:/#auth/checkLogin"); return new RedirectView(referer != null ? referer : "/" + "#auth/checkLogin");
} }
return null; return null;
} }
......
...@@ -5,6 +5,7 @@ import org.scribe.up.profile.facebook.FacebookProfile; ...@@ -5,6 +5,7 @@ import org.scribe.up.profile.facebook.FacebookProfile;
import org.scribe.up.profile.google.Google2Profile; import org.scribe.up.profile.google.Google2Profile;
import org.scribe.up.profile.twitter.TwitterProfile; import org.scribe.up.profile.twitter.TwitterProfile;
import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
public class User { public class User {
...@@ -30,6 +31,10 @@ public class User { ...@@ -30,6 +31,10 @@ public class User {
setUsername("anonymous"); setUsername("anonymous");
} }
public User(UsernamePasswordAuthenticationToken token) {
setUsername(token.getName());
}
public String getUsername() { public String getUsername() {
return username; return username;
} }
......
...@@ -4,6 +4,7 @@ import org.scribe.up.profile.facebook.FacebookProfile; ...@@ -4,6 +4,7 @@ import org.scribe.up.profile.facebook.FacebookProfile;
import org.scribe.up.profile.google.Google2Profile; import org.scribe.up.profile.google.Google2Profile;
import org.scribe.up.profile.twitter.TwitterProfile; import org.scribe.up.profile.twitter.TwitterProfile;
import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.cas.authentication.CasAuthenticationToken; import org.springframework.security.cas.authentication.CasAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -39,7 +40,10 @@ public class UserService implements IUserService { ...@@ -39,7 +40,10 @@ public class UserService implements IUserService {
} else if(authentication instanceof AnonymousAuthenticationToken){ } else if(authentication instanceof AnonymousAuthenticationToken){
AnonymousAuthenticationToken token = (AnonymousAuthenticationToken) authentication; AnonymousAuthenticationToken token = (AnonymousAuthenticationToken) authentication;
return new User(token); return new User(token);
} } else if(authentication instanceof UsernamePasswordAuthenticationToken) {
UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
return new User(token);
}
return null; return null;
} }
......
...@@ -20,11 +20,9 @@ package de.thm.arsnova.controller; ...@@ -20,11 +20,9 @@ package de.thm.arsnova.controller;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import javax.servlet.Filter; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
...@@ -33,8 +31,10 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio ...@@ -33,8 +31,10 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import de.thm.arsnova.AbstractSpringContextTestBase; import de.thm.arsnova.AbstractSpringContextTestBase;
import de.thm.arsnova.entities.User;
public class LoginControllerTest extends AbstractSpringContextTestBase { public class LoginControllerTest extends AbstractSpringContextTestBase {
...@@ -53,10 +53,43 @@ public class LoginControllerTest extends AbstractSpringContextTestBase { ...@@ -53,10 +53,43 @@ public class LoginControllerTest extends AbstractSpringContextTestBase {
final ModelAndView mav = handle(request, response); final ModelAndView mav = handle(request, response);
assertNotNull(mav); assertNotNull(mav);
assertTrue(mav.getViewName().startsWith("redirect:/")); assertNotNull(mav.getView());
Authentication auth = SecurityContextHolder.getContext() RedirectView view = (RedirectView) mav.getView();
.getAuthentication(); assertEquals("/#auth/checkLogin", view.getUrl());
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class); assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class);
} }
@Test
public void testReuseGuestLogin() throws Exception {
request.setMethod("GET");
request.setRequestURI("/doLogin");
request.addParameter("type", "guest");
request.addParameter("user", "Guest1234567890");
final ModelAndView mav = handle(request, response);
assertNotNull(mav);
assertNotNull(mav.getView());
RedirectView view = (RedirectView) mav.getView();
assertEquals("/#auth/checkLogin", view.getUrl());
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class);
assertEquals("Guest1234567890", auth.getName());
}
@Test
public void testUser() throws Exception {
request.setMethod("GET");
request.setRequestURI("/whoami");
final ModelAndView mav = handle(request, response);
assertNotNull(mav);
assertTrue(mav.getModel().containsKey("user"));
assertEquals(mav.getModel().get("user").getClass(), User.class);
assertEquals("Guest1234567890", ((User)mav.getModel().get("user")).getUsername());
}
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment