package it.smartcommunitylab.aac.config;

import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import com.google.common.collect.Maps;

import it.smartcommunitylab.aac.Config;
import it.smartcommunitylab.aac.authority.AuthorityHandler;
import it.smartcommunitylab.aac.authority.AuthorityHandlerContainer;
import it.smartcommunitylab.aac.authority.DefaultAuthorityHandler;
import it.smartcommunitylab.aac.authority.FBAuthorityHandler;
import it.smartcommunitylab.aac.authority.FBNativeAuthorityHandler;
import it.smartcommunitylab.aac.authority.GoogleNativeAuthorityHandler;
import it.smartcommunitylab.aac.authority.InternalAuthorityHandler;
import it.smartcommunitylab.aac.authority.NativeAuthorityHandler;
import it.smartcommunitylab.aac.authority.NativeAuthorityHandlerContainer;
import it.smartcommunitylab.aac.oauth.CachedResourceStorage;
import it.smartcommunitylab.aac.repository.ClientDetailsRepository;

@Configuration 
public class AACConfig extends WebMvcConfigurerAdapter {

	@Autowired
	private ClientDetailsRepository clientDetailsRepository;

	@Bean
	public CachedResourceStorage getResourceStorage() {
		return new CachedResourceStorage();
	}
	
	@Bean
	public AuthorityHandlerContainer getAuthorityHandlerContainer() {
		Map<String, AuthorityHandler> map = Maps.newTreeMap();
		map.put(Config.IDP_INTERNAL, getInternalHandler());
		FBAuthorityHandler fh = new FBAuthorityHandler();
		map.put("facebook", fh);
		AuthorityHandlerContainer bean = new AuthorityHandlerContainer(map);
		return bean;
	}

	@Bean
	public NativeAuthorityHandlerContainer getNativeAuthorityHandlerContainer() {
		Map<String, NativeAuthorityHandler> map = Maps.newTreeMap();
		
		GoogleNativeAuthorityHandler gh = new GoogleNativeAuthorityHandler(clientDetailsRepository);
		map.put("google", gh);
		FBNativeAuthorityHandler fh = new FBNativeAuthorityHandler();
		map.put("facebook", fh);
		
		NativeAuthorityHandlerContainer bean = new NativeAuthorityHandlerContainer(map);
		
		return bean;
	}

	@Bean
	public DefaultAuthorityHandler getDefaultHandler() {
		return new DefaultAuthorityHandler();
	}
	@Bean
	public InternalAuthorityHandler getInternalHandler() {
		return new InternalAuthorityHandler();
	}

	@Bean
	public CookieLocaleResolver getLocaleResolver() {
		CookieLocaleResolver bean = new CookieLocaleResolver();
		bean.setDefaultLocale(Locale.ITALY);
		return bean;
	}	

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedMethods("PUT", "DELETE", "GET", "POST").allowedOrigins("*");
	}	
	
}

@@ -330,16 +330,94 @@
 gistry;%0A
+import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;%0A
 import o
@@ -3002,10 +3002,534 @@
 ;%0A%09%7D%09%0A%09%0A
+    @Override%0A    public void configurePathMatch(final PathMatchConfigurer configurer) %7B%0A        //configure a sane path mapping, avoid huge security holes with: %0A        // * spring security considering /about /about/ /about.any correctly as different%0A        // * spring MVC considering all those the same%0A        // result is only /about is protected by antMatcher, all the other variants are open to the world%0A        configurer.setUseSuffixPatternMatch(false);%0A        configurer.setUseTrailingSlashMatch(false);%0A    %7D%0A
 %7D%0A

fix spring MVC path mapping to avoid non-matching security config and close holes