package top.sunriseydy.syhthems.sso.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
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.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;
import top.sunriseydy.syhthems.common.properties.SyhthemsProperties;

import javax.sql.DataSource;
import java.security.KeyPair;

/**
 * OAuth2 认证服务器配置
 *
 * 参考:https://www.baeldung.com/spring-security-oauth-jwt
 *
 * @author SunriseYDY
 * @date 2019-03-18 16:20
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    SyhthemsProperties syhthemsProperties;

    @Autowired
    private ClientDetailsService clientDetailsService;

    @Autowired
    UserDetailsService userDetailsService;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.withClientDetails(clientDetailsService)
                    /*.withClient("syhthems-web")
                    .secret(passwordEncoder.encode("sunriseydy-syhthems-web-secret"))
                    .authorizedGrantTypes("password", "refresh_token")
                    .scopes("web")
                    .autoApprove(true)*/;
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .tokenStore(jwtTokenStore())
                .accessTokenConverter(jwtAccessTokenConverter())
                .authenticationManager(authenticationManager)
                // 一定要添加自定义的UserDetailsService,不然 refresh token 时会报错
                .userDetailsService(userDetailsService)
                .pathMapping("/oauth/token", "/sso/oauth/token")
                .pathMapping("/oauth/authorize", "/sso/oauth/authorize")
                .pathMapping("/oauth/confirm_access", "/sso/oauth/confirm_access")
                .pathMapping("/oauth/error", "/sso/oauth/error")
                .pathMapping("/oauth/check_token", "/sso/oauth/check_token")
                .pathMapping("/oauth/token_key", "/sso/oauth/token_key")
        ;
    }

    @Bean
    protected TokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public KeyPair keyPair() {
        KeyStoreKeyFactory keyStoreKeyFactory =
                new KeyStoreKeyFactory(new ClassPathResource("syhthems.jks"), "syhthems-sunriseydy".toCharArray());
        return keyStoreKeyFactory.getKeyPair("syhthems");
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setKeyPair(keyPair());
        return converter;
    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(jwtTokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        return defaultTokenServices;
    }

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