package org.onetwo.boot.module.oauth2.util;

import java.io.Serializable;
import java.util.Optional;
import java.util.function.Supplier;

import javax.servlet.http.HttpServletRequest;

import org.onetwo.boot.core.web.utils.BootWebUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.authentication.TokenExtractor;
import org.springframework.util.Assert;

/**
 * @author wayshall
 * <br/>
 */
public abstract class OAuth2Utils {
	private static final String CLIENT_DETAILS_ATTR_KEY = "__CLIENT_DETAILS__";

	public static <T extends Serializable> Optional<T> getCurrentClientDetails() {
		HttpServletRequest req = BootWebUtils.request();
		if(req==null){
			return Optional.empty();
		}
		return getClientDetails(req);
	}
	public static <T extends Serializable> Optional<T> getClientDetails(HttpServletRequest request) {
		return getOrSetClientDetails(request, null);
	}
	
	@SuppressWarnings("unchecked")
	public static <T extends Serializable> Optional<T> getOrSetClientDetails(HttpServletRequest request, Supplier<T> supplier) {
		Object data = request.getAttribute(CLIENT_DETAILS_ATTR_KEY);
		if(data!=null){
			return Optional.of((T)data);
		}
		
		if(supplier==null){
			return Optional.empty();
		}
		
		T details = supplier.get();
		setCurrentClientDetails(request, details);
		return Optional.ofNullable(details);
	}
	
	public static void setCurrentClientDetails(HttpServletRequest request, Object clientDetail) {
		Assert.notNull(clientDetail);
		request.setAttribute(CLIENT_DETAILS_ATTR_KEY, clientDetail);
	}
	
	
	public static Optional<String> getAccessTokenValue(TokenExtractor tokenExtractor,  HttpServletRequest request){
		String accessToken = (String)request.getAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE);
		if(accessToken==null){
			Authentication authentication = tokenExtractor.extract(request);
			accessToken = authentication==null?null:(String)authentication.getPrincipal();
		}
		return Optional.ofNullable(accessToken);
	}
	
	private OAuth2Utils(){
	}
	
}

@@ -200,17 +200,14 @@
 two.
-boot.core
+common
 .web
@@ -213,28 +213,25 @@
 b.utils.
-BootWebUtils
+WebHolder
 ;%0Aimport
@@ -734,16 +734,25 @@
 s() %7B%0A%09%09
+Optional%3C
 HttpServ
@@ -765,29 +765,30 @@
 uest
+%3E
  req = 
-BootWebUtils.r
+WebHolder.getR
 eque
@@ -802,17 +802,24 @@
 %09if(
+!
 req
-==null
+.isPresent()
 )%7B%0A%09
@@ -878,16 +878,22 @@
 ails(req
+.get()
 );%0A%09%7D%0A%09p

fix oauthUtils