// Path: src/main/java/com/pugnascotia/reactdemo/utils/Cookies.java
// public final class Cookies {
// 
// 	public static final String XSRF_TOKEN_NAME = "XSRF-TOKEN";
// 
// 	/** Ensures that if a request does not supply a CSRF token in a cookie, or
// 	 * if the token is not up-to-date, we set it in our response so that subsequent
// 	 * requests can succeed. */
// 	public static void setSecurityTokens(HttpServletRequest request, HttpServletResponse response) {
// 		CsrfToken csrf = (CsrfToken) request.getAttribute("_csrf");
// 		if (csrf != null) {
// 			Cookie cookie = WebUtils.getCookie(request, XSRF_TOKEN_NAME);
// 			String token = csrf.getToken();
// 			if (cookie == null || token != null && !token.equals(cookie.getValue())) {
// 				cookie = new Cookie(XSRF_TOKEN_NAME, token);
// 				cookie.setPath("/");
// 				cookie.setHttpOnly(false);
// 				response.addCookie(cookie);
// 			}
// 		}
// 	}
// }
// 
// Path: src/main/java/com/pugnascotia/reactdemo/utils/State.java
// public final class State {
// 
// 	/** Populates standard parts of the shared client/server model into the Spring {@link Model}.
// 	 * Values prefixed with "__" will be made available to the JavaScript
// 	 * render function only. All other values will be passed in the client's state object.
// 	 */
// 	public static void populateModel(Model model, HttpServletRequest request) {
// 		model.addAttribute("__requestPath", getRequestPath(request));
// 		model.addAttribute("auth", getAuthState(request));
// 	}
// 
// 	/**
// 	 * Returns the request string, including the query fragment, so that it can be made available
// 	 * during server-side react-router rendering.
// 	 */
// 	private static String getRequestPath(HttpServletRequest request) {
// 		String queryString = request.getQueryString();
// 		return request.getRequestURI() + (queryString == null ? "" : "?" + queryString);
// 	}
// 
// 	/**
// 	 * Returns a representation of the user's authentication state, in the shape expected by the client.
// 	 */
// 	public static Map<String, Object> getAuthState(HttpServletRequest request) {
// 		Optional<List<String>> optionalRoles = getRoles(request);
// 
// 		return optionalRoles.map(roles -> {
// 			Map<String, Object> authState = new HashMap<>();
// 			authState.put("signedIn", !roles.contains("ROLE_ANONYMOUS"));
// 			authState.put("roles", roles);
// 
// 			return authState;
// 		})
// 		.orElseGet(() -> {
// 			Map<String, Object> authState = new HashMap<>();
// 			authState.put("signedIn", false);
// 			authState.put("roles", Collections.singletonList("ROLE_ANONYMOUS"));
// 
// 			return authState;
// 		});
// 	}
// 
// 	/**
// 	 * Return a list of the current user's roles. If they are not authenticated then they will
// 	 * have "ROLE_ANONYMOUS".
// 	 */
// 	private static Optional<List<String>> getRoles(HttpServletRequest request) {
// 		return getAuthentication(request)
// 			.map(a -> Functions.map(a.getAuthorities(), GrantedAuthority::getAuthority));
// 	}
// 
// 	/**
// 	 * Getting the current authentication object ought to be easy, by getting a context with
// 	 * {@link SecurityContextHolder#getContext()}, then calling {@link SecurityContext#getAuthentication()}.
// 	 * However there are circumstances where that doesn't work reliably, such as when handling
// 	 * exceptions. This method makes several attempts to get an {@link Authentication} instance.
// 	 */
// 	private static Optional<Authentication> getAuthentication(HttpServletRequest request) {
// 		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// 
// 		if (authentication == null) {
// 			RequestAttributes requestAttributes = new ServletRequestAttributes(request);
// 			SecurityContext securityContext = (SecurityContext) requestAttributes.getAttribute("SPRING_SECURITY_CONTEXT", RequestAttributes.SCOPE_SESSION);
// 			if (securityContext == null) {
// 				securityContext = (SecurityContext) requestAttributes.getAttribute("SPRING_SECURITY_CONTEXT", RequestAttributes.SCOPE_GLOBAL_SESSION);
// 			}
// 			if (securityContext != null) {
// 				authentication = securityContext.getAuthentication();
// 			}
// 		}
// 
// 		return Optional.ofNullable(authentication);
// 	}
// }


import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import com.pugnascotia.reactdemo.utils.Cookies;
import com.pugnascotia.reactdemo.utils.State;
package com.pugnascotia.reactdemo.config.ajax;



/**
 * A handler that returns HTTP 200 OK for successful AJAX authentications,
 * and includes the user's roles in the response body.
 */

@Component
public class AjaxAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

	private final ObjectMapper mapper;

	@Inject
	public AjaxAuthenticationSuccessHandler(ObjectMapper mapper) {
		this.mapper = mapper;
	}

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		response.setStatus(HttpServletResponse.SC_OK);
// Path: src/main/java/com/pugnascotia/reactdemo/utils/Cookies.java
// public final class Cookies {
// 
// 	public static final String XSRF_TOKEN_NAME = "XSRF-TOKEN";
// 
// 	/** Ensures that if a request does not supply a CSRF token in a cookie, or
// 	 * if the token is not up-to-date, we set it in our response so that subsequent
// 	 * requests can succeed. */
// 	public static void setSecurityTokens(HttpServletRequest request, HttpServletResponse response) {
// 		CsrfToken csrf = (CsrfToken) request.getAttribute("_csrf");
// 		if (csrf != null) {
// 			Cookie cookie = WebUtils.getCookie(request, XSRF_TOKEN_NAME);
// 			String token = csrf.getToken();
// 			if (cookie == null || token != null && !token.equals(cookie.getValue())) {
// 				cookie = new Cookie(XSRF_TOKEN_NAME, token);
// 				cookie.setPath("/");
// 				cookie.setHttpOnly(false);
// 				response.addCookie(cookie);
// 			}
// 		}
// 	}
// }
// 
// Path: src/main/java/com/pugnascotia/reactdemo/utils/State.java
// public final class State {
// 
// 	/** Populates standard parts of the shared client/server model into the Spring {@link Model}.
// 	 * Values prefixed with "__" will be made available to the JavaScript
// 	 * render function only. All other values will be passed in the client's state object.
// 	 */
// 	public static void populateModel(Model model, HttpServletRequest request) {
// 		model.addAttribute("__requestPath", getRequestPath(request));
// 		model.addAttribute("auth", getAuthState(request));
// 	}
// 
// 	/**
// 	 * Returns the request string, including the query fragment, so that it can be made available
// 	 * during server-side react-router rendering.
// 	 */
// 	private static String getRequestPath(HttpServletRequest request) {
// 		String queryString = request.getQueryString();
// 		return request.getRequestURI() + (queryString == null ? "" : "?" + queryString);
// 	}
// 
// 	/**
// 	 * Returns a representation of the user's authentication state, in the shape expected by the client.
// 	 */
// 	public static Map<String, Object> getAuthState(HttpServletRequest request) {
// 		Optional<List<String>> optionalRoles = getRoles(request);
// 
// 		return optionalRoles.map(roles -> {
// 			Map<String, Object> authState = new HashMap<>();
// 			authState.put("signedIn", !roles.contains("ROLE_ANONYMOUS"));
// 			authState.put("roles", roles);
// 
// 			return authState;
// 		})
// 		.orElseGet(() -> {
// 			Map<String, Object> authState = new HashMap<>();
// 			authState.put("signedIn", false);
// 			authState.put("roles", Collections.singletonList("ROLE_ANONYMOUS"));
// 
// 			return authState;
// 		});
// 	}
// 
// 	/**
// 	 * Return a list of the current user's roles. If they are not authenticated then they will
// 	 * have "ROLE_ANONYMOUS".
// 	 */
// 	private static Optional<List<String>> getRoles(HttpServletRequest request) {
// 		return getAuthentication(request)
// 			.map(a -> Functions.map(a.getAuthorities(), GrantedAuthority::getAuthority));
// 	}
// 
// 	/**
// 	 * Getting the current authentication object ought to be easy, by getting a context with
// 	 * {@link SecurityContextHolder#getContext()}, then calling {@link SecurityContext#getAuthentication()}.
// 	 * However there are circumstances where that doesn't work reliably, such as when handling
// 	 * exceptions. This method makes several attempts to get an {@link Authentication} instance.
// 	 */
// 	private static Optional<Authentication> getAuthentication(HttpServletRequest request) {
// 		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// 
// 		if (authentication == null) {
// 			RequestAttributes requestAttributes = new ServletRequestAttributes(request);
// 			SecurityContext securityContext = (SecurityContext) requestAttributes.getAttribute("SPRING_SECURITY_CONTEXT", RequestAttributes.SCOPE_SESSION);
// 			if (securityContext == null) {
// 				securityContext = (SecurityContext) requestAttributes.getAttribute("SPRING_SECURITY_CONTEXT", RequestAttributes.SCOPE_GLOBAL_SESSION);
// 			}
// 			if (securityContext != null) {
// 				authentication = securityContext.getAuthentication();
// 			}
// 		}
// 
// 		return Optional.ofNullable(authentication);
// 	}
// }


// Path: src/main/java/com/pugnascotia/reactdemo/config/ajax/AjaxAuthenticationSuccessHandler.java
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import com.pugnascotia.reactdemo.utils.Cookies;
import com.pugnascotia.reactdemo.utils.State;

package com.pugnascotia.reactdemo.config.ajax;



/**
 * A handler that returns HTTP 200 OK for successful AJAX authentications,
 * and includes the user's roles in the response body.
 */

@Component
public class AjaxAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

	private final ObjectMapper mapper;

	@Inject
	public AjaxAuthenticationSuccessHandler(ObjectMapper mapper) {
		this.mapper = mapper;
	}

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		response.setStatus(HttpServletResponse.SC_OK);
		Cookies.setSecurityTokens(request, response);