if (tokenUtils.validate(token)) {
// determine the user based on the (already validated) token
UserDetails user = tokenUtils.getUserFromToken(token);
// build an Authentication object with the user's info
PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(user, null);
authentication.setDetails(ads.buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authManager.authenticate(authentication));
// Add token to HTTP header
if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
// Add token to HTTP header
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.addHeader("X-Authentication-Token", token);
}
}
} else {
// System.out.println("DONT HAVE TOKEN");
// If there is not any token, check if there is a Google Accounts user
User googleUser = UserServiceFactory.getUserService().getCurrentUser();
// System.out.println(googleUser);
if (googleUser != null) {
UserDetails user = new org.springframework.security.core.userdetails.User(googleUser.getEmail(), "", new ArrayList<GrantedAuthority>());
// build an Authentication object with the user's info
PreAuthenticatedAuthenticationToken authentication = new PreAuthenticatedAuthenticationToken(user, null);
authentication.setDetails(ads.buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authManager.authenticate(authentication));
if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
// Add token to HTTP header
token = tokenUtils.getToken(user);