<commit_before>package io.github.orionhealth.xbdd.config;

import javax.ws.rs.HttpMethod;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class ServerSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(final HttpSecurity http) throws Exception {
		http
			.authorizeRequests(a -> a
				.antMatchers("/", "/login", "/error", "/rest/user/loggedin", "/rest/attachment/**").permitAll()
				.antMatchers(HttpMethod.PUT, "/rest/reports/**").permitAll()
				.antMatchers(HttpMethod.POST, "/rest/reports/**").permitAll()
				.anyRequest().authenticated()
			)
			.csrf(c -> c.disable()) // TODO - we probably want CSRF on but it's failing PUT/POST atm for some reason.
			.logout(l -> l
				// No logout URL as the frontend provides the logout redirect.
				.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK))
				.permitAll()
			);
		http.oauth2Login();
		http.formLogin();
	}

	@Override
	protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
		auth
			.ldapAuthentication()
				.userDnPatterns("uid={0},ou=people")
				.groupSearchBase("ou=groups")
				.contextSource()
					.url("ldap://localhost:8389/dc=springframework,dc=org")
					.and()<commit_msg>Added old csrf code back in but commented out, for reference when we can fix this.<commit_after>package io.github.orionhealth.xbdd.config;

import javax.ws.rs.HttpMethod;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class ServerSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(final HttpSecurity http) throws Exception {
		http
			.authorizeRequests(a -> a
				.antMatchers("/", "/login", "/error", "/rest/user/loggedin", "/rest/attachment/**").permitAll()
				.antMatchers(HttpMethod.PUT, "/rest/reports/**").permitAll()
				.antMatchers(HttpMethod.POST, "/rest/reports/**").permitAll()
				.anyRequest().authenticated()
			)
			.csrf(c -> c
//				.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
//				.ignoringAntMatchers("/login", "/logout")
				.disable()) // TODO - we probably want CSRF on, but it's failing PUT/POST requests atm for some reason.
			.logout(l -> l
				// No logout URL as the frontend provides the logout redirect.
				.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK))
				.permitAll()
			);
		http.oauth2Login();
		http.formLogin();
	}

	@Override
	protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
		auth
			.ldapAuthentication()
				.userDnPatterns("uid={0},ou=people")
				.groupSearchBase("ou=groups")
				.contextSource()
					.url("ldap://localhost:8389/dc=springframework,dc=org")
					.and()
{'fraction_non_alphanumeric': 0.11575342465753424, 'fraction_numerical': 0.0027397260273972603, 'mean_word_length': 5.509658246656761, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3319617', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1204, 'n_words': 223}