package com.hz.pm.api.user.security.auth.code;

import com.hz.pm.api.user.manage.AuthCodeLoginManage;
import com.hz.pm.api.user.security.config.AuthProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

/**
 * <p>
 * AuthCodeSecurityConfig
 * </p>
 *
 * @author WendyYang
 * @since 16:02 2024/1/4
 */
@Component
public class AuthCodeLoginSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {


    protected final AuthenticationSuccessHandler defaultLoginSuccessHandler;
    protected final AuthenticationFailureHandler defaultLoginFailureHandler;
    private final UserDetailsService userDetailsService;
    private final AuthProperties authProps;
    private final AuthCodeLoginManage authCodeLoginManage;

    public AuthCodeLoginSecurityConfig(@Qualifier(value = "defaultLoginSuccessHandler") AuthenticationSuccessHandler loginSuccessHandler,
                                       @Qualifier(value = "defaultLoginFailureHandler") AuthenticationFailureHandler loginFailureHandler,
                                       @Qualifier(value = "authCodeLoginUserDetailService") UserDetailsService userDetailsService,
                                       AuthProperties authProps,
                                       AuthCodeLoginManage authCodeLoginManage) {
        this.defaultLoginSuccessHandler = loginSuccessHandler;
        this.defaultLoginFailureHandler = loginFailureHandler;
        this.userDetailsService = userDetailsService;
        this.authProps = authProps;
        this.authCodeLoginManage = authCodeLoginManage;
    }

    @Override
    public void configure(HttpSecurity http) {
        AuthCodeLoginFilter filter = new AuthCodeLoginFilter(authProps.getAuthCodeLoginUrl(), authCodeLoginManage);
        AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
        filter.setAuthenticationManager(authenticationManager);
        filter.setAuthenticationSuccessHandler(defaultLoginSuccessHandler);
        filter.setAuthenticationFailureHandler(defaultLoginFailureHandler);

        AuthCodeLoginProvider authenticationProvider = new AuthCodeLoginProvider();
        authenticationProvider.setUserDetailsService(userDetailsService);

        http.authenticationProvider(authenticationProvider)
                .addFilterAfter(filter, UsernamePasswordAuthenticationFilter.class);
    }

}
