package me.academeg.blog.security.config;

import me.academeg.blog.security.CorsFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.web.access.channel.ChannelProcessingFilter;

import javax.sql.DataSource;

/**
 * OAuth2Config Configuration
 *
 * @author Yuriy A. Samsonov <yuriy.samsonov96@gmail.com>
 * @version 1.0
 */
@Configuration
public class OAuth2Config {

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

        private CorsFilter corsFilter;

        @Autowired
        public ResourceServerConfiguration(CorsFilter corsFilter) {
            this.corsFilter = corsFilter;
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .exceptionHandling()
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/").permitAll()
                .antMatchers(HttpMethod.GET, "/api/accounts/**").permitAll()
                .antMatchers(HttpMethod.POST, "/api/accounts").permitAll()
                .antMatchers(HttpMethod.GET, "/api/articles/*").permitAll()
                .antMatchers(HttpMethod.GET, "/api/articles/list").permitAll()
                .antMatchers(HttpMethod.GET, "/api/comments/**").permitAll()
                .antMatchers(HttpMethod.GET, "/api/tags/**").permitAll()
                .antMatchers(HttpMethod.GET, "/api/avatars/**").permitAll()
                .antMatchers(HttpMethod.GET, "/api/images/**").permitAll()
                .anyRequest().authenticated()
                .and().httpBasic()
                .and().addFilterBefore(corsFilter, ChannelProcessingFilter.class); //@TODO remove on production
        }
    }

    @Configuration
    @EnableAuthorizationServer
    protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

        private AuthenticationManager authenticationManager;
        private DataSource dataSource;
        private TokenEnhancer tokenEnhancer;

        @Autowired
        public AuthorizationServerConfiguration(
            @Qualifier("authenticationManagerBean") AuthenticationManager authenticationManager,
            DataSource dataSource,
            TokenEnhancer tokenEnhancer
        ) {
            this.authenticationManager = authenticationManager;
            this.dataSource = dataSource;
            this.tokenEnhancer = tokenEnhancer;
        }

        @Override
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
            oauthServer
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients
                .inMemory()
                .withClient("web_app")
                .secret("secret_key")
                .authorizedGrantTypes("password", "refresh_token")
                .scopes("read", "write")
                .autoApprove(true);
        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints
                .tokenStore(tokenStoreBean())
                .tokenEnhancer(tokenEnhancer)
                .authenticationManager(authenticationManager);
        }

        @Bean
        public TokenStore tokenStoreBean() {
            return new JdbcTokenStore(dataSource);
        }
    }
}

@@ -519,16 +519,89 @@
 curity;%0A
+import org.springframework.security.core.userdetails.UserDetailsService;%0A
 import o
@@ -3564,16 +3564,71 @@
 nhancer;
+%0A        private UserDetailsService userDetailsService;
 %0A%0A      
@@ -3860,16 +3860,67 @@
 Enhancer
+,%0A            UserDetailsService userDetailsService
 %0A       
@@ -4022,32 +4022,32 @@
 e = dataSource;%0A
-
             this
@@ -4070,32 +4070,90 @@
  tokenEnhancer;%0A
+            this.userDetailsService = userDetailsService;%0A
         %7D%0A%0A     
@@ -5082,16 +5082,72 @@
 Manager)
+%0A                .userDetailsService(userDetailsService)
 ;%0A      

add user details service to oauth configuration