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

import com.hz.pm.api.external.MhAuthClient;
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>
 * MhAuthSecurityConfig
 * </p>
 *
 * @author WendyYang
 * @since 14:24 2023/12/16
 */
@Component
public class MhAuthSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {


    protected final AuthenticationSuccessHandler defaultLoginSuccessHandler;
    protected final AuthenticationFailureHandler defaultLoginFailureHandler;
    private final UserDetailsService mhLoginUserDetailService;
    private final MhAuthClient authClient;
    private final AuthProperties authProperties;

    public MhAuthSecurityConfig(@Qualifier(value = "defaultLoginSuccessHandler") AuthenticationSuccessHandler loginSuccessHandler,
                                @Qualifier(value = "defaultLoginFailureHandler") AuthenticationFailureHandler loginFailureHandler,
                                @Qualifier(value = "mhLoginUserDetailService") UserDetailsService userDetailsService, MhAuthClient authClient,
                                AuthProperties authProperties) {
        this.defaultLoginSuccessHandler = loginSuccessHandler;
        this.defaultLoginFailureHandler = loginFailureHandler;
        this.mhLoginUserDetailService = userDetailsService;
        this.authClient = authClient;
        this.authProperties = authProperties;
    }

    @Override
    public void configure(HttpSecurity http) {
        MhAuthFilter agentAuthFilter = new MhAuthFilter(authProperties.getMhLoginUrl(), authClient);
        AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
        agentAuthFilter.setAuthenticationManager(authenticationManager);
        agentAuthFilter.setAuthenticationSuccessHandler(defaultLoginSuccessHandler);
        agentAuthFilter.setAuthenticationFailureHandler(defaultLoginFailureHandler);

        MhAuthProvider authenticationProvider = new MhAuthProvider();
        authenticationProvider.setUserDetailsService(mhLoginUserDetailService);

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

}
