package com.github.timtebeek.rst.config;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.stereotype.Component;
import org.springframework.test.web.servlet.request.RequestPostProcessor;

@Component
public class OAuthHelper {
	// For use with MockMvc
	public RequestPostProcessor bearerToken(final String clientid, final String user) {
		return mockRequest -> {
			OAuth2AccessToken token = createAccessToken(clientid, user);
			mockRequest.addHeader("Authorization", "Bearer " + token.getValue());
			return mockRequest;
		};
	}

	@Autowired
	ClientDetailsService				clientDetailsService;
	@Autowired
	UserDetailsService					userDetailsService;
	@Autowired
	AuthorizationServerTokenServices	tokenservice;

	OAuth2AccessToken createAccessToken(final String clientId, final String username) {
		OAuth2Authentication auth = oAuth2Authentication(clientId, username);
		return tokenservice.createAccessToken(auth);
	}

	public OAuth2Authentication oAuth2Authentication(final String clientId, final String username) {
		// Look up authorities, resourceIds and scopes based on clientId
		ClientDetails client = clientDetailsService.loadClientByClientId(clientId);
		Collection<GrantedAuthority> authorities = client.getAuthorities();
		Set<String> resourceIds = client.getResourceIds();
		Set<String> scopes = client.getScope();

		// Default values for other parameters
		Map<String, String> requestParameters = Collections.emptyMap();
		boolean approved = true;
		String redirectUrl = null;
		Set<String> responseTypes = Collections.emptySet();
		Map<String, Serializable> extensionProperties = Collections.emptyMap();

		// Create request
		OAuth2Request oAuth2Request = new OAuth2Request(requestParameters, clientId, authorities, approved, scopes, resourceIds, redirectUrl, responseTypes, extensionProperties);

		// Create OAuth2AccessToken
		UserDetails user = userDetailsService.loadUserByUsername(username);
		UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, null, authorities);
		OAuth2Authentication auth = new OAuth2Authentication(oAuth2Request, authenticationToken);
		return auth;
	}
}

@@ -179,62 +179,33 @@
 ort 
-org.springframework.beans.factory.annotation.Autowired
+lombok.AllArgsConstructor
 ;%0Aim
@@ -1066,16 +1066,36 @@
 mponent%0A
+@AllArgsConstructor%0A
 public c
@@ -1117,325 +1117,8 @@
 r %7B%0A
-%09// For use with MockMvc%0A%09public RequestPostProcessor bearerToken(final String clientid, final String user) %7B%0A%09%09return mockRequest -%3E %7B%0A%09%09%09OAuth2AccessToken token = createAccessToken(clientid, user);%0A%09%09%09mockRequest.addHeader(%22Authorization%22, %22Bearer %22 + token.getValue());%0A%09%09%09return mockRequest;%0A%09%09%7D;%0A%09%7D%0A%0A%09@Autowired%0A
 %09Cli
@@ -1160,28 +1160,16 @@
 ervice;%0A
-%09@Autowired%0A
 %09UserDet
@@ -1208,20 +1208,8 @@
 ce;%0A
-%09@Autowired%0A
 %09Aut
@@ -1258,38 +1258,67 @@
 ;%0A%0A%09
-OAuth2AccessToken createAccess
+// For use with MockMvc%0A%09public RequestPostProcessor bearer
 Toke
@@ -1330,33 +1330,33 @@
 al String client
-I
+i
 d, final String 
@@ -1367,16 +1367,43 @@
 name) %7B%0A
+%09%09return mockRequest -%3E %7B%0A%09
 %09%09OAuth2
@@ -1451,17 +1451,17 @@
 n(client
-I
+i
 d, usern
@@ -1468,22 +1468,42 @@
 ame);%0A%09%09
-return
+%09OAuth2AccessToken token =
  tokense
@@ -1534,18 +1534,162 @@
 auth);%0A%09
-%7D%0A
+%09%09mockRequest.addHeader(%22Authorization%22, %22Bearer %22 + token.getValue());%0A%09%09%09return mockRequest;%0A%09%09%7D;%0A%09%7D%0A%0A%09// For use with @WithOAuth2Authentication
 %0A%09public

Simplify class signature