package com.njzscloud.common.security.support;

import com.njzscloud.common.core.ex.ExceptionMsg;
import com.njzscloud.common.core.tuple.Tuple2;
import com.njzscloud.common.security.ex.UserLoginException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.AuthenticationException;

/**
 * 登录前置处理器
 */
public interface LoginPreparer {
    /**
     * 是否处理当前请求
     *
     * @param request 请求对象
     * @return true-->处理器可以处理当前请求
     */
    boolean support(HttpServletRequest request);

    /**
     * 创建登录表单
     *
     * @param request 请求对象
     * @return 登录表单
     */
    LoginForm createLoginForm(HttpServletRequest request);

    default void validate(LoginForm loginForm) throws AuthenticationException {
        Tuple2<Boolean, String> validate = loginForm.validate();
        if (!validate.get_0()) {
            throw new UserLoginException(ExceptionMsg.CLI_ERR_MSG, validate.get_1());
        }
    }
}
