package nl.fhict.happynews.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
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.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.cors.CorsUtils;

/**
 * Configuration for the OAuth2 server.
 */
@Configuration
@EnableAuthorizationServer
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    private final TokenStore tokenStore;
    private final AuthenticationManager authenticationManager;

    @Value("${oauth2.client.client-id}")
    private String clientId;

    @Value("${oauth2.client.secret}")
    private String clientSecret;

    @Autowired
    public AuthorizationServerConfig(@Qualifier("mongoTokenStore") final TokenStore tokenStore,
                                     final AuthenticationManager authenticationManager) {
        this.tokenStore = tokenStore;
        this.authenticationManager = authenticationManager;
    }

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

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
            .inMemory()
            .withClient(clientId)
            .authorities(WebSecurityConfig.Roles.EDITOR, WebSecurityConfig.Roles.ADMIN)
            .scopes("read", "write")
            .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
            .secret(clientSecret);
    }

}

@@ -317,40 +317,201 @@
 ork.
-context.annotation.Configuration
+boot.web.servlet.FilterRegistrationBean;%0Aimport org.springframework.context.annotation.Bean;%0Aimport org.springframework.context.annotation.Configuration;%0Aimport org.springframework.core.Ordered
 ;%0Aim
@@ -636,89 +636,8 @@
 er;%0A
-import org.springframework.security.config.annotation.web.builders.HttpSecurity;%0A
 impo
@@ -1191,13 +1191,140 @@
 Cors
-Utils
+Configuration;%0Aimport org.springframework.web.cors.UrlBasedCorsConfigurationSource;%0Aimport org.springframework.web.filter.CorsFilter
 ;%0A%0A/
@@ -2726,16 +2726,16 @@
 licit%22)%0A
-
         
@@ -2768,10 +2768,595 @@
 %0A    %7D%0A%0A
+    @Bean%0A    FilterRegistrationBean corsFilter() %7B%0A        CorsConfiguration config = new CorsConfiguration();%0A        config.setAllowCredentials(true);%0A        config.addAllowedOrigin(%22*%22);%0A        config.addAllowedHeader(%22*%22);%0A        config.addAllowedMethod(%22*%22);%0A%0A        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();%0A        source.registerCorsConfiguration(%22/**%22, config);%0A%0A        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));%0A        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);%0A%0A        return bean;%0A    %7D%0A%0A
 %7D%0A

Use cors filter for oauth