<commit_before>package manon.app.security;

import lombok.RequiredArgsConstructor;
import manon.user.model.UserAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
import java.util.Collections;

import static manon.app.config.Globals.API.API_SYS;
import static manon.app.config.Globals.API.API_USER;
import static manon.app.config.Globals.API.API_USER_ADMIN;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    private static final String ADMIN = UserAuthority.ROLE_ADMIN.name();
    private static final String PLAYER = UserAuthority.ROLE_PLAYER.name();
    private static final String ACTUATOR = UserAuthority.ROLE_ACTUATOR.name();
    private static final String DEV = UserAuthority.ROLE_DEV.name();
    
    private final PasswordEncoderService passwordEncoderService;
    private final UserDetailsServiceImpl UserDetailsService;
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()
            .sessionManagement().sessionCreationPolicy(STATELESS)
            .and().authorizeRequests()
            
            .antMatchers(API_SYS + "/info/up").permitAll()
            .antMatchers(API_SYS + "/**").hasAuthority(ADMIN)
            .antMatchers(API_USER_ADMIN + "/**").hasAuthority(ADMIN)
            
            .antMatchers(POST, API_USER).permitAll() // user registration
            .antMatchers(API_USER + "/**").hasAuthority(PLAYER)
            
            .antMatchers("/actuator").hasAuthority(ACTUATOR)
            .antMatchers("/actuator/**").hasAuthority(ACTUATOR)
            
            .antMatchers("/swagger-resources",
                "/swagger-resources/configuration/ui",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html",
                "/webjars/**",
                "/v2/api-docs").hasAuthority(DEV)
            
            .and().httpBasic()
            .and().csrf().disable();
    }
    
    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Collections.singletonList("*"));
        configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
        configuration.setAllowCredentials(true);
        configuration.setAllowedHeaders(Arrays.asList(
            "Accept", "Origin", "Content-Type", "Depth", "User-Agent", "If-Modified-Since,",
            "Cache-Control", "Authorization", "X-Req", "X-File-Size", "X-Requested-With", "X-File-Name"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(UserDetailsService).passwordEncoder(passwordEncoderService.getEncoder());
    }
}<commit_msg>(refac) fix a variable name, code cleanup<commit_after>package manon.app.security;

import lombok.RequiredArgsConstructor;
import manon.user.model.UserAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
import java.util.Collections;

import static manon.app.config.Globals.API.API_SYS;
import static manon.app.config.Globals.API.API_USER;
import static manon.app.config.Globals.API.API_USER_ADMIN;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    private static final String ADMIN = UserAuthority.ROLE_ADMIN.name();
    private static final String PLAYER = UserAuthority.ROLE_PLAYER.name();
    private static final String ACTUATOR = UserAuthority.ROLE_ACTUATOR.name();
    private static final String DEV = UserAuthority.ROLE_DEV.name();
    
    private final PasswordEncoderService passwordEncoderService;
    private final UserDetailsService userDetailsService;
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()
            .sessionManagement().sessionCreationPolicy(STATELESS)
            .and().authorizeRequests()
            
            .antMatchers(API_SYS + "/info/up").permitAll()
            .antMatchers(API_SYS + "/**").hasAuthority(ADMIN)
            .antMatchers(API_USER_ADMIN + "/**").hasAuthority(ADMIN)
            
            .antMatchers(POST, API_USER).permitAll() // user registration
            .antMatchers(API_USER + "/**").hasAuthority(PLAYER)
            
            .antMatchers("/actuator").hasAuthority(ACTUATOR)
            .antMatchers("/actuator/**").hasAuthority(ACTUATOR)
            
            .antMatchers("/swagger-resources",
                "/swagger-resources/configuration/ui",
                "/swagger-resources/configuration/security",
                "/swagger-ui.html",
                "/webjars/**",
                "/v2/api-docs").hasAuthority(DEV)
            
            .and().httpBasic()
            .and().csrf().disable();
    }
    
    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Collections.singletonList("*"));
        configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
        configuration.setAllowCredentials(true);
        configuration.setAllowedHeaders(Arrays.asList(
            "Accept", "Origin", "Content-Type", "Depth", "User-Agent", "If-Modified-Since,",
            "Cache-Control", "Authorization", "X-Req", "X-File-Size", "X-Requested-With", "X-File-Name"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
    
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoderService.getEncoder());
    }
}
{'fraction_non_alphanumeric': 0.11032656663724624, 'fraction_numerical': 0.00025217500945656286, 'mean_word_length': 3.699052132701422, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3516751', 'n_tokens_mistral': 2082, 'n_tokens_neox': 2082, 'n_words': 332}