Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the contract of requests made to github/gitlab access token end… #416

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions src/main/java/io/github/jhipster/online/web/rest/GitResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -114,45 +116,44 @@ public RedirectView callback(@PathVariable String gitProvider, String code) {
try {
String url;
GitProvider gitProviderEnum;
GitAccessTokenRequest request = new GitAccessTokenRequest();
Map<String, String> params = new HashMap<>();
switch (gitProvider.toLowerCase()) {
case GITHUB:
url = applicationProperties.getGithub().getHost() + "/login/oauth/access_token";
gitProviderEnum = GitProvider.GITHUB;
request.setClientId(applicationProperties.getGithub().getClientId());
request.setClientSecret(applicationProperties.getGithub().getClientSecret());
request.setCode(code);
params.put("client_id", applicationProperties.getGithub().getClientId());
params.put("client_secret", applicationProperties.getGithub().getClientSecret());
params.put("code", code);
break;
case GITLAB:
url = applicationProperties.getGitlab().getHost() + "/oauth/token";
gitProviderEnum = GitProvider.GITLAB;
request.setClientId(applicationProperties.getGitlab().getClientId());
request.setClientSecret(applicationProperties.getGitlab().getClientSecret());
request.setGrantType("authorization_code");
request.setRedirectUri(applicationProperties.getGitlab().getRedirectUri());
request.setCode(code);
params.put("client_id", applicationProperties.getGitlab().getClientId());
params.put("client_secret", applicationProperties.getGitlab().getClientSecret());
params.put("code", code);
params.put("grant_type", "authorization_code");
params.put("redirect_uri", applicationProperties.getGitlab().getRedirectUri());
break;
default:
return new ResponseEntity<>(UNKNOWN_GIT_PROVIDER + gitProvider, HttpStatus.INTERNAL_SERVER_ERROR);
}

ObjectMapper objectMapper = new ObjectMapper();
String requestBody = objectMapper.writeValueAsString(request);
HttpClient client = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest
.newBuilder()
.uri(URI.create(url))
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.header("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.POST(HttpRequest.BodyPublishers.ofString(buildQueryString(params)))
.build();

CompletableFuture<HttpResponse<String>> response = client.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString());

String jsonResponse = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
GitAccessTokenResponse accessTokenResponse = objectMapper.readValue(jsonResponse, GitAccessTokenResponse.class);
this.userService.saveToken(accessTokenResponse.getAccessToken(), gitProviderEnum);
this.userService.saveToken(accessTokenResponse.getAccess_token(), gitProviderEnum);
} catch (InterruptedException e) {
log.warn("Interrupted!", e);
// Restore interrupted state...
Expand All @@ -164,6 +165,19 @@ public RedirectView callback(@PathVariable String gitProvider, String code) {
return new ResponseEntity<>(HttpStatus.CREATED);
}

private static String buildQueryString(Map<String, String> params) {
StringBuilder queryString = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
if (queryString.length() > 0) {
queryString.append("&");
}
queryString.append(entry.getKey());
queryString.append("=");
queryString.append(entry.getValue());
}
return queryString.toString();
}

public static class GitAccessTokenRequest {

private String clientId;
Expand Down Expand Up @@ -243,14 +257,14 @@ public String toString() {
@JsonIgnoreProperties(ignoreUnknown = true)
public static class GitAccessTokenResponse {

private String accessToken;
private String access_token;

public String getAccessToken() {
return accessToken;
public String getAccess_token() {
return access_token;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
}

Expand Down