package com.github.joostvdg.keepwatching;


import com.github.joostvdg.keepwatching.config.OAuthClientResources;
import org.jooq.SQLDialect;
import org.jooq.impl.DefaultConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter;
import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.Filter;

@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages = "com.github.joostvdg.keepwatching")
@EnableTransactionManagement
@EnableAutoConfiguration
//@EnableOAuth2Sso
@EnableOAuth2Client
public class Application extends WebSecurityConfigurerAdapter {

    private OAuth2ClientContext oauth2ClientContext;

    public Application(OAuth2ClientContext oauth2ClientContext){
        this.oauth2ClientContext = oauth2ClientContext;
    }

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/**")
                .authorizeRequests()
                .antMatchers("/","/login**", "/webjars/**"," /view**", "/authenticated").permitAll()
                .anyRequest().authenticated()
                .and().logout().logoutSuccessUrl("/").permitAll()
                .and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .and().csrf().ignoringAntMatchers("/logout")
                .and().addFilterBefore(ssoFilter(github(), "/view/github.html"), BasicAuthenticationFilter.class)
                .addFilterBefore(ssoFilter(facebook(), "/view/facebook.html"), BasicAuthenticationFilter.class);
    }

    private Filter ssoFilter(OAuthClientResources client, String path) {
        OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(path);
        OAuth2RestTemplate template = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
        filter.setRestTemplate(template);
        UserInfoTokenServices tokenServices = new UserInfoTokenServices(
                client.getResource().getUserInfoUri(),
                client.getClient().getClientId());
        tokenServices.setRestTemplate(template);
        filter.setTokenServices(tokenServices);
        return filter;
    }

    @Bean
    @ConfigurationProperties("github")
    public OAuthClientResources github() {
        return new OAuthClientResources();
    }

    @Bean
    @ConfigurationProperties("facebook")
    public OAuthClientResources facebook() {
        return new OAuthClientResources();
    }

    @Bean
    public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(-100);
        return registration;
    }

    private CsrfTokenRepository csrfTokenRepository() {
        HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
        repository.setHeaderName("X-XSRF-TOKEN");
        return repository;
    }

    @Bean
    public DefaultConfiguration configuration() {
        DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
        SQLDialect dialect = SQLDialect.POSTGRES_9_5;
        jooqConfiguration.set(dialect);
        return jooqConfiguration;
    }
}

@@ -1969,10 +1969,8 @@
 ion%0A
-//
 @Ena
@@ -1982,31 +1982,36 @@
 uth2
-Sso%0A@EnableOAuth2Client
+Client%0A//@EnableConfigServer
 %0Apub

Disable the config server for now, as we first need to make it build