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

import cn.reghao.oss.console.account.service.AccountAuthService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

/**
 * 认证用户名/密码登录的 provider
 *
 * @author reghao
 * @date 2019-04-09 09:07:40
 */
@Component
public class AccountAuthProvider implements AuthenticationProvider {
    private final AccountAuthService accountAuthService;

    public AccountAuthProvider(AccountAuthService accountAuthService) {
        this.accountAuthService = accountAuthService;
    }

    /**
     * 对 Authentication 对象进行认证
     * 参照 DaoAuthenticationProvider
     *
     * @param
     * @return
     * @date 2020-05-06 上午11:16
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        AccountAuthToken preAuthToken = (AccountAuthToken) authentication;
        AccountAuthToken authToken = accountAuthService.authByPassword(preAuthToken);
        return authToken;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return (AccountAuthToken.class.isAssignableFrom(authentication));
    }
}
