package com.qs.mp.framework.security.mobile.auth;

import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

/**
 *
 * @auther duota
 * @create 2021 2021/9/3 11:57 上午
 * @describe
 *
 */

public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {
    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
    private final Object principal;
    private final Object credentials;
    private final String code;

    public SmsCodeAuthenticationToken(String mobile,String messageId,String code) {
        super(null);
        this.principal = mobile;
        this.credentials = messageId;
        this.code = code;
        setAuthenticated(false);
        logger.info("SmsCodeAuthenticationToken false... principal："+this.principal+" credentials："+this.credentials+" code:"+this.code);
    }


    public SmsCodeAuthenticationToken(Object principal,
                                      Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = null;
        this.code = "";
        super.setAuthenticated(true); // must use super, as we override
        logger.info("SmsCodeAuthenticationToken true... principal："+this.principal+" credentials："+this.credentials+" code:"+this.code);
    }


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

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

    public String getCode(){
        return this.code;
    }

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