package cn.reghao.oss.console.account.security.form;

import cn.reghao.oss.console.account.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 String loginId;
    private final boolean rememberMe;
    private final Integer userId;
    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.loginId = loginId;
        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.loginId = authToken.getLoginId();
        this.rememberMe = authToken.isRememberMe();
        this.userId = user.getId();
        this.principal = authToken.getPrincipal();
        this.credentials = authToken.getCredentials();
    }

    /**
     * 从 AccessToken 中恢复的已认证 Authentication 对象
     *
     * @param
     * @return
     * @date 2023-02-17 17:42:27
     */
    public AccountAuthToken(int plat, String loginId, int loginType, Object principal, List<GrantedAuthority> authorities) {
        super(authorities);
        super.setAuthenticated(true);
        this.loginId = loginId;
        this.rememberMe = false;
        this.userId = Integer.parseInt((String) principal);
        this.principal = principal;
        this.credentials = null;
    }

    // ~ Methods
    // ========================================================================================================
    public String getLoginId() {
        return loginId;
    }

    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;
    }
}
