/*
 * Copyright 2017 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
 *
 *      http://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 org.codenergic.theskeleton.core.security;

import org.codenergic.theskeleton.client.OAuth2ClientService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
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.approval.ApprovalStore;
import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;

@Configuration
@EnableAuthorizationServer
@ConditionalOnWebApplication
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
	private final AccessTokenConverter accessTokenConverter;
	private final ApprovalStore approvalStore;
	private final AuthenticationManager authenticationManager;
	private final OAuth2ClientService clientService;
	private final TokenEnhancer tokenEnhancer;
	private final TokenStore tokenStore;

	public OAuth2AuthorizationServerConfig(AccessTokenConverter accessTokenConverter, ApprovalStore approvalStore, AuthenticationManager authenticationManager, OAuth2ClientService clientService, TokenEnhancer tokenEnhancer, TokenStore tokenStore) {
		this.accessTokenConverter = accessTokenConverter;
		this.approvalStore = approvalStore;
		this.authenticationManager = authenticationManager;
		this.clientService = clientService;
		this.tokenEnhancer = tokenEnhancer;
		this.tokenStore = tokenStore;
	}

	@Override
	public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
		clients.withClientDetails(clientService);
	}

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

@@ -938,24 +938,97 @@
 ionManager;%0A
+import org.springframework.security.core.userdetails.UserDetailsService;%0A
 import org.s
@@ -2216,16 +2216,70 @@
 enStore;
+%0A%09private final UserDetailsService userDetailsService;
 %0A%0A%09publi
@@ -2383,16 +2383,29 @@
 alStore,
+%0A%09%09%09%09%09%09%09%09%09%09  
  Authent
@@ -2476,16 +2476,29 @@
 Service,
+%0A%09%09%09%09%09%09%09%09%09%09  
  TokenEn
@@ -2540,16 +2540,55 @@
 kenStore
+, UserDetailsService userDetailsService
 ) %7B%0A%09%09th
@@ -2835,16 +2835,64 @@
 nStore;%0A
+%09%09this.userDetailsService = userDetailsService;%0A
 %09%7D%0A%0A%09@Ov
@@ -3146,17 +3146,16 @@
 ints%0A%09%09%09
-%09
 .authent
@@ -3195,17 +3195,16 @@
 ger)%0A%09%09%09
-%09
 .accessT
@@ -3235,25 +3235,24 @@
 nConverter)%0A
-%09
 %09%09%09.approval
@@ -3271,25 +3271,24 @@
 alStore)%0A%09%09%09
-%09
 .tokenEnhanc
@@ -3308,17 +3308,16 @@
 cer)%0A%09%09%09
-%09
 .tokenSt
@@ -3331,15 +3331,58 @@
 enStore)
+%0A%09%09%09.userDetailsService(userDetailsService)
 ;%0A%09%7D%0A%7D%0A

Enable oauth2 refresh token