package com.ningdatech.pmapi.user.security.auth.common;

import com.ningdatech.basic.exception.BizException;
import com.ningdatech.pmapi.user.security.auth.model.WebRequestDetails;
import com.ningdatech.pmapi.user.security.auth.validate.CommonLoginException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author LiuXinXin
 * @Date 2020/8/3 8:46 下午
 * @Version 1.0
 **/
public class CommonAuthFilter extends AbstractAuthenticationProcessingFilter {

    private boolean postOnly = true;

    private static final String CREDENTIAL_PARAMETER = "credential";
    private static final String PLATFORM_PARAMETER = "platform";

    // ~ Constructors
    // ===================================================================================================

    public CommonAuthFilter(String processingUrl) {
        super(new AntPathRequestMatcher(processingUrl, HttpMethod.POST.name()));
    }

    // ~ Methods
    // ========================================================================================================

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
        if (postOnly && !request.getMethod().equals(HttpMethod.POST.name())) {
            throw new AuthenticationServiceException("请求方法错误");
        }
        String credential = request.getParameter(CREDENTIAL_PARAMETER);
        String platform = request.getParameter(PLATFORM_PARAMETER);
        if (StringUtils.isBlank(credential)) {
            throw new CommonLoginException("凭证 不能为空");
        }
        if (StringUtils.isBlank(platform)) {
            throw new CommonLoginException("平台 不能为空");
        }

        platform = trim(platform);
        credential = trim(credential);
        try {
            CommonAuthToken authRequest = new CommonAuthToken(platform, credential);
            authRequest.setDetails(new WebRequestDetails(request));
            return this.getAuthenticationManager().authenticate(authRequest);
        } catch (AuthenticationException e) {
            throw new BadCredentialsException("用户状态");
        } catch (BizException e) {
            throw new BadCredentialsException(e.getMessage());
        } catch (Exception e) {
            throw new InternalAuthenticationServiceException("授权失败：", e);
        }
    }

    private String trim(String trimStr) {
        if (StringUtils.isNotBlank(trimStr)) {
            return trimStr.trim();
        }
        return null;
    }
}
