/*
 * Copyright 2023 the original author or authors.
 *
 * 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
 *
 *      https://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.hclsoftware.sso.kerberos.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.kerberos.authentication.KerberosAuthenticationProvider;
import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider;
import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter;
import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig{


	@Autowired
	private KerberosAuthenticationProvider kerberosAuthenticationProvider;
	@Autowired
	private SpnegoEntryPoint spnegoEntryPoint;
	@Autowired
	private KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider;
	@Autowired
	private ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider;
	@Autowired
	private SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter;

	private static final String[] SWAGGER_WHITELIST = {
			"/swagger-ui/**",
			"/v3/api-docs/**",
			"/swagger-resources/**",
			"/swagger-resources",
			"/swagger-ui.html"
	};

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

		http
				.csrf(csrf -> csrf.disable())
				.authorizeHttpRequests((authz) -> authz
						.requestMatchers(HttpMethod.GET, "/api/**").permitAll()
						.requestMatchers("/api/auth/**").permitAll()
						.requestMatchers(SWAGGER_WHITELIST).permitAll()
						.anyRequest().authenticated()
				)

				.exceptionHandling(
						(exceptionHandling) ->
								exceptionHandling.authenticationEntryPoint(spnegoEntryPoint)
				)
				.logout(Customizer.withDefaults())
				.authenticationProvider(kerberosAuthenticationProvider)
				.authenticationProvider(kerberosServiceAuthenticationProvider)
				.addFilterBefore(spnegoAuthenticationProcessingFilter,
						BasicAuthenticationFilter.class);

		return http.build();
	}
}
