package com.miaxis.framework.security.provider;


import com.miaxis.framework.security.token.OpenIdAuthenticationToken;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author : chenjian
 * @version v1.0
 */
@Log4j2
@Data
public class OpenIdAuthenticationProvider implements  AuthenticationProvider {

    private UserDetailsService userDetailsService;
    /**
     * 自定义验证逻辑
     * @param authentication
     * @return
     * @throws AuthenticationException
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        UserDetails userDetails = userDetailsService.loadUserByUsername((String) authentication.getPrincipal());
        OpenIdAuthenticationToken moblieCodeAuthenticationToken = new OpenIdAuthenticationToken(userDetails,userDetails.getAuthorities());
        moblieCodeAuthenticationToken.setDetails(moblieCodeAuthenticationToken);
        return moblieCodeAuthenticationToken;
    }

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