content: package br.com.alevhvm.adotai.auth.security;
  
  import java.util.List;
  
  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.config.annotation.authentication.configuration.AuthenticationConfiguration;
  import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
  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.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.security.web.SecurityFilterChain;
  import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  import org.springframework.web.cors.CorsConfiguration;
  import org.springframework.web.cors.CorsConfigurationSource;
  import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  
  import lombok.RequiredArgsConstructor;
  
  @Configuration
  @EnableWebSecurity
  @RequiredArgsConstructor
  @EnableMethodSecurity(prePostEnabled = true)
  public class SecurityConfig {
  
      private final SecurityFilter securityFilter;
      private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
      private final CustomAccessDeniedHandler customAccessDeniedHandler;
  
      @Bean
      public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
          return httpSecurity
                  .csrf(csrf -> csrf.disable()) // possibilita redirecionamento de dados em cyberataques de um site logado
                                                // para outro
  
                  .cors(cors -> {})
  
                  // gerencia as rotas e os acessos com token e sem
                  .authorizeHttpRequests(authorize -> authorize
  
                          .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
  
                          .requestMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html", "/webjars/**")
                          .permitAll()
  
                          // Rotas específicas com restrição
  
                          // Rotas de auth
                          .requestMatchers(HttpMethod.POST,"/auth/admin/register").hasRole("ADMINMASTER")
  
                          // Rotas de administrador
                          .requestMatchers("/api/v1/administradores/**").hasRole("ADMINMASTER")
  
                          // Rotas de usuário
                          .requestMatchers(HttpMethod.GET, "/api/v1/usuarios").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.GET, "/api/v1/usuarios/nomeUsuario/{nomeUsuario}").hasAnyRole("ONG", "USER")
                          .requestMatchers(HttpMethod.GET, "/api/v1/usuarios/{id}").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.GET, "/api/v1/usuarios/{nomeUsuario}/adocoes").hasRole("USER")
                          .requestMatchers(HttpMethod.GET, "/api/v1/usuarios/{nomeUsuario}/favoritos").hasRole("USER")
                          .requestMatchers(HttpMethod.POST, "/api/v1/usuarios").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.POST, "/api/v1/usuarios/favoritar/{nomeUsuario}/{animalId}").hasRole("USER")
                          .requestMatchers(HttpMethod.PUT, "/api/v1/usuarios/{nomeUsuario}").hasRole("USER")
                          .requestMatchers(HttpMethod.PATCH, "/api/v1/usuarios/{nomeUsuario}").hasRole("USER")
                          .requestMatchers(HttpMethod.DELETE, "/api/v1/usuarios/{nomeUsuario}").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.PATCH, "/api/v1/usuarios/mudarStatus/{nomeUsuario}").hasAnyRole("USER", "ADMIN")
                          .requestMatchers(HttpMethod.PATCH, "/api/v1/usuarios/bloquearUsuario/{nomeUsuario}").hasAnyRole("ADMIN")
  
                          // Rotas de ong
                          .requestMatchers(HttpMethod.GET, "/api/v1/ongs/{id}").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.GET, "/api/v1/ongs/{id}/adocoes").hasRole("ONG")
                          .requestMatchers(HttpMethod.POST, "/api/v1/ongs").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.PUT, "/api/v1/ongs/{nomeUsuario}").hasRole("ONG")
                          .requestMatchers(HttpMethod.PATCH, "/api/v1/ongs/{nomeUsuario}").hasRole("ONG")
                          .requestMatchers(HttpMethod.DELETE, "/api/v1/ongs/{nomeUsuario}").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.PATCH, "/api/v1/ongs/mudarStatus/{nomeUsuario}").hasAnyRole("ONG", "ADMIN")
                          .requestMatchers(HttpMethod.PATCH, "/api/v1/ongs/bloquearOng/{nomeUsuario}").hasAnyRole("ADMIN")
  
                          // Rotas de animal
                          .requestMatchers(HttpMethod.GET, "/api/v1/animais/{id}").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.POST, "/api/v1/animais").hasRole("ONG")
                          .requestMatchers(HttpMethod.PUT, "/api/v1/animais/{nome}").hasRole("ONG")
                          .requestMatchers(HttpMethod.PATCH, "/api/v1/animais/{nome}").hasRole("ONG")
                          .requestMatchers(HttpMethod.DELETE, "/api/v1/animais/{nome}").hasRole("ONG")
  
                          // Rotas de adoção
                          .requestMatchers(HttpMethod.GET, "/api/v1/adocoes").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.GET, "/api/v1/adocoes/{id}").hasAnyRole("ONG", "USER")
                          .requestMatchers(HttpMethod.POST, "/api/v1/adocoes").hasRole("ONG")
                          .requestMatchers(HttpMethod.PUT, "/api/v1/adocoes/{id}").hasRole("ONG")
                          .requestMatchers(HttpMethod.PATCH, "/api/v1/adocoes/{id}").hasRole("ONG")
                          .requestMatchers(HttpMethod.DELETE, "/api/v1/adocoes/{id}").hasRole("ONG")
  
                          // Rotas de avaliação
                          .requestMatchers(HttpMethod.GET, "/api/v1/avaliacoes").hasRole("ADMIN")
                          .requestMatchers(HttpMethod.GET, "/api/v1/avaliacoes/{id}").hasAnyRole("ONG", "ADMIN")
                          .requestMatchers(HttpMethod.POST, "/api/v1/avaliacoes").hasAnyRole("USER", "ADMIN")
  
                          // Qualquer outra rota com permissão não especificada fica liberada
                          .anyRequest().permitAll())
  
                  // antes de verificar as roles, vai validar o token do usuário
                  .exceptionHandling(eh -> eh
                          .authenticationEntryPoint(jwtAuthenticationEntryPoint) // trata as exceções lançadas dentro do
                                                                                 // filtro
                          .accessDeniedHandler(customAccessDeniedHandler))
                  .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // Stateful
                                                                                                                // ->
                                                                                                                // guarda
                                                                                                                // no site
                                                                                                                // as
                                                                                                                // infos
                                                                                                                // de
                                                                                                                // user/senha,
                  // Stateless -> tokenização n armazena nada no servidor antes de verificar as
                  // roles,
                  // vai validar o token do usuário
                  .addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class) // ordem dos filtros,
                                                                                               // primeiro parâmetro e dps
                                                                                               // o segundo, q é do spring
                  .build();
      }
  
      @Bean
      public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
              throws Exception {
          return authenticationConfiguration.getAuthenticationManager();
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      public CorsConfigurationSource corsConfigurationSource() {
          CorsConfiguration corsConfig = new CorsConfiguration();
  
          // Exemplo com allowedOriginPatterns para aceitar curingas (ex: localhost:3000, outros subdomínios, etc)
          corsConfig.setAllowedOriginPatterns(List.of("http://localhost:3000")); // aqui pode usar curingas
  
          // Se preferir, pode listar explicitamente as URLs e NÃO usar curingas, mas nesse caso allowedOrigins:
          // corsConfig.setAllowedOrigins(List.of("http://localhost:3000"));
  
          corsConfig.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
          corsConfig.setAllowedHeaders(List.of("Content-Type", "Authorization", "Accept", "X-Requested-With"));
          corsConfig.setAllowCredentials(true);
  
          UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
          source.registerCorsConfiguration("/**", corsConfig);
          return source;
      }
  
  }
  