package cn.reghao.bnt.web.admin.security.form;

import cn.reghao.bnt.web.admin.model.po.User;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.util.List;

/**
 * 参考 org.springframework.security.authentication.UsernamePasswordAuthenticationToken 实现
 *
 * @author reghao
 * @date 2022-07-06 15:59:02
 */
public class AccountAuthToken extends AbstractAuthenticationToken {
    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    // ~ Instance fields
    // ================================================================================================
    private final int plat;
    private String loginId;
    private final int loginType;
    private final Integer userId;
    private final boolean rememberMe;
    private final Object principal;
    private Object credentials;

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

    /**
     * 等待认证时创建的 Authentication 对象
     *
     * This constructor can be safely used by any code that wishes to create a
     * <code>PasswordAuthToken</code>, as the {@link #isAuthenticated()}
     * will return <code>false</code>.
     *
     */
    public AccountAuthToken(String loginId, Boolean rememberMe, Object principal, Object credentials) {
        super(null);
        super.setAuthenticated(false);
        this.plat = 1;
        this.loginId = loginId;
        this.loginType = 1;
        this.rememberMe = rememberMe;
        this.userId = null;
        this.principal = principal;
        this.credentials = credentials;
    }

    /**
     * 认证通过时创建的 Authentication 对象
     *
     * This constructor should only be used by <code>AuthenticationManager</code> or
     * <code>AuthenticationProvider</code> implementations that are satisfied with
     * producing a trusted (i.e. {@link #isAuthenticated()} = <code>true</code>)
     * authentication token.
     *
     * @param authToken
     * @param user
     */
    public AccountAuthToken(AccountAuthToken authToken, User user) {
        super(user.getAuthorities());
        super.setAuthenticated(true); // must use super, as we override
        super.setDetails(user);
        this.plat = authToken.getPlat();
        this.loginId = authToken.getLoginId();
        this.loginType = authToken.getLoginType();
        this.rememberMe = authToken.isRememberMe();
        this.userId = user.getId();
        this.principal = user.getId();
        this.credentials = user.getEncodedPassword();
    }

    // ~ Methods
    // ========================================================================================================
    public void setLoginId(String loginId) {
        this.loginId = loginId;
    }

    public Integer getPlat() {
        return this.plat;
    }

    public String getLoginId() {
        return loginId;
    }

    public Integer getLoginType() {
        return this.loginType;
    }

    public boolean isRememberMe() {
        return rememberMe;
    }

    public Integer getUserId() {
        return this.userId;
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    /*@Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }*/

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        credentials = null;
    }
}
