package com.parrit.configurations;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.MessageDigestPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.firewall.StrictHttpFirewall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/api/login/project")
                .failureUrl("/error")
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/api/logout")
                .logoutSuccessUrl("/")
                .and()
            .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/", "/login", "/error").permitAll()
                .antMatchers(HttpMethod.POST, "/api/login", "/api/login/project", "/api/project").permitAll()
                .anyRequest().authenticated()
                .and()
            .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    }

    @Override
    public void configure(WebSecurity web) {
        web.ignoring().mvcMatchers("/favicon.ico", "/built/**", "/img/**");
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationProvider authenticationProvider) {
        List<AuthenticationProvider> providers = new ArrayList<>();
        providers.add(authenticationProvider);
        return new ProviderManager(providers);
    }

    @Bean
    public AuthenticationProvider authenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
        daoAuthenticationProvider.setHideUserNotFoundExceptions(false);
        return daoAuthenticationProvider;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        Map<String, PasswordEncoder> encoders = new HashMap<>();
        encoders.put("bcrypt", new BCryptPasswordEncoder());
        encoders.put("sha256", new MessageDigestPasswordEncoder("SHA-256"));
        return new DelegatingPasswordEncoder("bcrypt", encoders);
    }

    @Bean
    public StrictHttpFirewall httpFirewall() {
        StrictHttpFirewall firewall = new StrictHttpFirewall();
        firewall.setAllowedHeaderNames((header) -> true);
        firewall.setAllowedHeaderValues((header) -> true);
        firewall.setAllowedParameterNames((parameter) -> true);
        return firewall;
    }
}

@@ -1062,197 +1062,40 @@
 pto.
-bcrypt.BCryptPasswordEncoder;%0Aimport org.springframework.security.crypto.password.DelegatingPasswordEncoder;%0Aimport org.springframework.security.crypto.password.MessageDigestPasswordEncoder
+factory.PasswordEncoderFactories
 ;%0Aim
@@ -1307,108 +1307,8 @@
 l;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 @Con
@@ -2570,159 +2570,57 @@
     
-List%3CAuthenticationProvider%3E providers = new ArrayList%3C%3E();%0A        providers.add(authenticationProvider);%0A        return new ProviderManager(providers
+return new ProviderManager(authenticationProvider
 );%0A 
@@ -2733,41 +2733,8 @@
 vice
-, PasswordEncoder passwordEncoder
 ) %7B%0A
@@ -2909,79 +2909,8 @@
 e);%0A
-        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);%0A
     
@@ -3095,222 +3095,46 @@
     
-Map%3CString, PasswordEncoder%3E encoders = new HashMap%3C%3E();%0A        encoders.put(%22bcrypt%22, new BCryptPasswordEncoder());%0A        encoders.put(%22sha256%22, new MessageDigestPasswordEncoder(%22SHA-256%22));%0A        return new 
+return PasswordEncoderFactories.create
 Dele
@@ -3159,26 +3159,8 @@
 der(
-%22bcrypt%22, encoders
 );%0A 

use default password encoder