package ink.wgink.login.base.authentication;

import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.base.consts.IUserCenterConst;
import ink.wgink.login.base.exceptions.UserAuthenticationException;
import ink.wgink.login.base.manager.ConfigManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: BaseAuthenticationProcessingFilter
 * @Description: 认证基础
 * @Author: WangGeng
 * @Date: 2020/5/25 5:57 下午
 * @Version: 1.0
 **/
public abstract class BaseAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter {

    protected static final String KEY_USERNAME = "username";
    protected static final String KEY_PASSWORD = "password";
    protected static final String METHOD_POST = "POST";
    protected static final String DING_DING_LOGIN_TMP_CODE = "code";

    protected BaseAuthenticationProcessingFilter(String defaultFilterProcessesUrl) {
        super(defaultFilterProcessesUrl);
    }

    protected BaseAuthenticationProcessingFilter(RequestMatcher requiresAuthenticationRequestMatcher) {
        super(requiresAuthenticationRequestMatcher);
    }

    /**
     * 校验验证码
     *
     * @param request
     */
    protected void checkVerificationCode(HttpServletRequest request) {
        Map<String, String> config = ConfigManager.getInstance().getConfig();
        if (config.get(ISystemConstant.VERIFICATION_CODE) == null) {
            return;
        }
        if (!StringUtils.equals(ISystemConstant.IS_TRUE, config.get(ISystemConstant.VERIFICATION_CODE))) {
            return;
        }
        String verificationCode = request.getParameter(ISystemConstant.VERIFICATION_CODE);
        String verificationAttributeCode = (String) request.getSession().getAttribute(ISystemConstant.VERIFICATION_CODE);
        if (!StringUtils.equalsIgnoreCase(verificationCode, verificationAttributeCode)) {
            throw new UserAuthenticationException("验证码错误");
        }
    }

    /**
     * 校验POST方法
     *
     * @param request
     */
    protected void checkPostMethod(HttpServletRequest request) {
        if (!StringUtils.equals(request.getMethod(), METHOD_POST)) {
            throw new UserAuthenticationException("登录方法必须是POST");
        }
    }

    protected void checkReferToken(HttpServletRequest request) {
        Object referToken = request.getSession().getAttribute(IUserCenterConst.REFER_TOKEN);
        if(referToken == null) {
            throw new UserAuthenticationException("refer参数为空，请重新刷新页面");
        }
        String sessionReferToken = referToken.toString();
        if(StringUtils.isBlank(sessionReferToken)) {
            throw new UserAuthenticationException("refer参数为空，请重新刷新页面");
        }
        String pageReferToken = request.getParameter(IUserCenterConst.REFER_TOKEN);
        if(StringUtils.isBlank(pageReferToken)) {
            throw new UserAuthenticationException("请求头中缺少refer参数");
        }
        if(!StringUtils.equals(sessionReferToken, pageReferToken)) {
            throw new UserAuthenticationException("refer不匹配");
        }
        // 校验成功删除session中的refer
        request.getSession().removeAttribute(IUserCenterConst.REFER_TOKEN);
    }
}
