package com.faforever.api.config.security.oauth2;

import com.faforever.api.config.FafApiProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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;

@Configuration
public class OAuthJwtConfig {

  private final FafApiProperties fafApiProperties;

  public OAuthJwtConfig(FafApiProperties fafApiProperties) {
    this.fafApiProperties = fafApiProperties;
  }

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

  @Bean
  public TokenStore tokenStore() {
    return new JwtTokenStore(jwtAccessTokenConverter());
  }

  @Bean
  protected JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
    jwtAccessTokenConverter.setSigningKey(fafApiProperties.getJwtSecret());
    return jwtAccessTokenConverter;
  }

}

@@ -302,120 +302,461 @@
 ity.
-oauth2.provider.token.DefaultTokenServices;%0Aimport org.springframework.security.oauth2.provider.token.TokenStore
+core.userdetails.UserDetailsService;%0Aimport org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;%0Aimport org.springframework.security.oauth2.provider.token.DefaultTokenServices;%0Aimport org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;%0Aimport org.springframework.security.oauth2.provider.token.TokenStore;%0Aimport org.springframework.security.oauth2.provider.token.UserAuthenticationConverter
 ;%0Aim
@@ -1195,16 +1195,37 @@
 ervices(
+TokenStore tokenStore
 ) %7B%0A    
@@ -1345,18 +1345,16 @@
 kenStore
-()
 );%0A    d
@@ -1480,16 +1480,63 @@
 enStore(
+JwtAccessTokenConverter jwtAccessTokenConverter
 ) %7B%0A    
@@ -1583,18 +1583,16 @@
 onverter
-()
 );%0A  %7D%0A%0A
@@ -1659,16 +1659,71 @@
 nverter(
+UserAuthenticationConverter userAuthenticationConverter
 ) %7B%0A    
@@ -1887,43 +1887,546 @@
     
-return jwtAccessTokenConverter;%0A  %7D%0A
+((DefaultAccessTokenConverter) jwtAccessTokenConverter.getAccessTokenConverter()).setUserTokenConverter(userAuthenticationConverter);%0A    return jwtAccessTokenConverter;%0A  %7D%0A%0A  @Bean%0A  public UserAuthenticationConverter userAuthenticationConverter(UserDetailsService userDetailsService) %7B%0A    DefaultUserAuthenticationConverter defaultUserAuthenticationConverter = new DefaultUserAuthenticationConverter();%0A    defaultUserAuthenticationConverter.setUserDetailsService(userDetailsService);%0A    return defaultUserAuthenticationConverter;%0A  %7D
 %0A%7D%0A

Set correct authentication also when using JWT