package cn.teammodel.security.filter;

import cn.teammodel.model.entity.TmdUserDetail;
import cn.teammodel.model.entity.User;
import cn.teammodel.security.utils.JwtTokenUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * x-auth-authToken filter
 * @author winter
 * @create 2023-11-09 10:43
 */
@Slf4j
public class AuthInnerTokenFilter extends OncePerRequestFilter {
    JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
    //白名单id
    //private List<String> whiteList = new ArrayList<>(Arrays.asList("1530782422","1531391504","1529649295","1531030192","1533091862","1565140494","1532621201","1532605781","1563262699","1530782422","1530670067","1578275404","6711941735","1528783259"));

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,  FilterChain filterChain) throws ServletException, IOException {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        // 进入此过滤器说明 OIDC 认证成功，则验证 authToken
        // 验证 authToken 合法
        TmdUserDetail tmdUserDetail = jwtTokenUtil.getValidUserDetail(request);
        if (tmdUserDetail == null) {
//            log.error("authToken authentication failed: {}", request.getHeader("x-auth-authToken"));
            SecurityContextHolder.clearContext(); // 验证失败不应该在此处抛出异常,应该维护好 context 的值,以便整个过滤器链正常运行
            filterChain.doFilter(request, response);
            return;
        }
        /*User user = tmdUserDetail.getUser();
        if (!whiteList.contains(user.getId()) && !"IES".equals(user.getWebsite()))
        {
            SecurityContextHolder.clearContext(); // 验证失败不应该在此处抛出异常,应该维护好 context 的值,以便整个过滤器链正常运行
            filterChain.doFilter(request, response);
            return;
        }*/
        /*if (!"GP".equals(user.getWebsite()) && !"IES".equals(user.getWebsite()))
        {
            SecurityContextHolder.clearContext(); // 验证失败不应该在此处抛出异常,应该维护好 context 的值,以便整个过滤器链正常运行
            filterChain.doFilter(request, response);
            return;
        }*/

        // 组装 authToken 的 jwt 进 authentication
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        UsernamePasswordAuthenticationToken finalAuthentication = new UsernamePasswordAuthenticationToken(tmdUserDetail, null, authorities);
        context.setAuthentication(finalAuthentication);
        filterChain.doFilter(request, response);
    }
}
