/*
 * Copyright 2018-2020 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.rest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.StrictHttpFirewall;

import com.b2international.commons.platform.PlatformUtil;
import com.b2international.snowowl.core.rest.util.CORSFilter;

/**
 * @since 7.0
 */
@Configuration
@EnableWebSecurity
public class SnowOwlSecurityConfig extends WebSecurityConfigurerAdapter {

	@Bean
	public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
		StrictHttpFirewall firewall = new StrictHttpFirewall();
		firewall.setAllowUrlEncodedSlash(true);
		return firewall;
	}

	@Override
	protected void configure(final HttpSecurity http) throws Exception {
		http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

		// add dev time CORS filter
		if (PlatformUtil.isDevVersion()) {
			http.addFilterAfter(new CORSFilter(), BasicAuthenticationFilter.class);
		}

		// authentication is handled internally in AuthorizedRequest
		http.authorizeRequests().antMatchers("/**").permitAll();
	}

	@Override
	public void configure(WebSecurity web) throws Exception {
		web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
	}

}

@@ -16,18 +16,18 @@
  2018-20
-20
+19
  B2i Hea
@@ -1744,17 +1744,20 @@
 ll() %7B%0A%09
-%09
+    
 StrictHt
@@ -1805,17 +1805,20 @@
 all();%0A%09
-%09
+    
 firewall
@@ -1850,17 +1850,20 @@
 true);%0A%09
-%09
+    
 return f
@@ -1870,25 +1870,26 @@
 irewall;%0A%09%7D%0A
+%09
 %0A
-
 %09@Override%0A%09
@@ -1963,16 +1963,20 @@
 %7B%0A%09%09http
+%0A%09%09%09
 .session
@@ -1987,16 +1987,21 @@
 gement()
+%0A%09%09%09%09
 .session
@@ -2047,18 +2047,51 @@
 ATELESS)
-;
 %0A
+%09%09%09.and()%0A%09%09%09.csrf().disable();%0A%09%09
 %0A%09%09// ad
@@ -2229,17 +2229,19 @@
 s);%0A%09%09%7D%0A
+%09%09
 %0A
-
 %09%09// aut
@@ -2321,16 +2321,20 @@
 quests()
+%0A%09%09%09
 .antMatc
@@ -2344,16 +2344,20 @@
 s(%22/**%22)
+%0A%09%09%09
 .permitA
@@ -2365,17 +2365,18 @@
 l();%0A%09%7D%0A
+%09
 %0A
-
 %09@Overri
@@ -2496,11 +2496,12 @@
 ());%0A%09%7D%0A
+%09
 %0A%7D%0A

Revert "[qa] fix lgtm issue https://lgtm.com/rules/1511220334423/"