package io.fundreqest.platform.tweb.config;

import org.keycloak.adapters.KeycloakConfigResolver;
import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver;
import org.keycloak.adapters.springsecurity.KeycloakSecurityComponents;
import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider;
import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;

@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
@Order(2)
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
    /**
     * Registers the KeycloakAuthenticationProvider with the authentication manager.
     */
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
        auth.authenticationProvider(keycloakAuthenticationProvider);
    }

    @Bean
    public KeycloakConfigResolver KeycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }

    /**
     * Defines the session authentication strategy.
     */
    @Bean
    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                .and()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/profile", "/profile/**").authenticated()
                .antMatchers("/user", "/user/**").authenticated()
                .antMatchers("/fund", "/fund/**", "/requests/*/fund").authenticated()
                .anyRequest().permitAll();
    }
}
@@ -700,24 +700,69 @@
 tion.Order;%0A
+import org.springframework.http.HttpHeaders;%0A
 import org.s
@@ -1447,16 +1447,190 @@
 trategy;
+%0Aimport org.springframework.web.cors.CorsConfiguration;%0Aimport org.springframework.web.cors.UrlBasedCorsConfigurationSource;%0Aimport org.springframework.web.filter.CorsFilter;
 %0A%0A@Confi
@@ -3266,10 +3266,731 @@
 );%0A    %7D
+%0A%0A    @Bean%0A    public CorsFilter corsFilter() %7B%0A        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();%0A        CorsConfiguration config = new CorsConfiguration();%0A        config.setAllowCredentials(true);%0A        config.addAllowedOrigin(%22http://localhost:2000%22);%0A        config.addAllowedOrigin(%22https://alpha-key.fundrequest.io%22);%0A        config.addAllowedOrigin(%22https://key.fundrequest.io%22);%0A        config.addAllowedHeader(%22*%22);%0A        config.addExposedHeader(HttpHeaders.AUTHORIZATION);%0A        config.addExposedHeader(HttpHeaders.LOCATION);%0A        config.addAllowedMethod(%22*%22);%0A        source.registerCorsConfiguration(%22/**%22, config);%0A        return new CorsFilter(source);%0A    %7D
 %0A%7D

Add cars configuration for keycloak