Skip to content

Commit

Permalink
add get access token method (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiaso authored and Incarnation-p-lee committed Sep 11, 2018
1 parent dc73f95 commit d477e63
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
import org.apache.tools.ant.types.ZipFileSet;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StreamUtils;
Expand All @@ -37,6 +42,7 @@ public class MainController extends AbstractPlaygroundController {

private final TelemetryProxy telemetryProxy;
private final ProjectGenerator projectGenerator;
private final OAuth2AuthorizedClientService clientService;

private static final String TELEMETRY_EVENT_ACCESS = "SpringCloudPlaygroundAccess";
private static final String TELEMETRY_EVENT_GENERATE = "SpringCloudPlaygroundGenerate";
Expand All @@ -46,10 +52,11 @@ public class MainController extends AbstractPlaygroundController {
private static final String GREETING_HTML = "greeting";

public MainController(GeneratorMetadataProvider metadataProvider, ResourceUrlProvider resourceUrlProvider,
ProjectGenerator projectGenerator) {
ProjectGenerator projectGenerator, OAuth2AuthorizedClientService clientService) {
super(metadataProvider, resourceUrlProvider);
this.projectGenerator = projectGenerator;
this.telemetryProxy = new TelemetryProxy();
this.clientService = clientService;
}

@GetMapping("/greeting")
Expand Down Expand Up @@ -178,4 +185,14 @@ private ResponseEntity<byte[]> createResponseEntity(byte[] content, String conte
return ResponseEntity.ok().header("Content-Type", contentType)
.header("Content-Disposition", contentDispositionValue).body(content);
}

private OAuth2AccessToken getAccessToken() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
OAuth2AuthenticationToken oauthToken = (OAuth2AuthenticationToken) authentication;

OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(
oauthToken.getAuthorizedClientRegistrationId(),oauthToken.getName());

return client.getAccessToken();
}
}

0 comments on commit d477e63

Please sign in to comment.