mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-05-21 09:57:28 +08:00
Merge pull request #218 from nagisa77/codex/fix-github-api-404-error-handling
Fix GitHub authentication errors
This commit is contained in:
@@ -9,8 +9,11 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -30,12 +33,17 @@ public class GithubAuthService {
|
|||||||
String tokenUrl = "https://github.com/login/oauth/access_token";
|
String tokenUrl = "https://github.com/login/oauth/access_token";
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||||
HttpEntity<String> request = new HttpEntity<>(
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
"client_id=" + clientId +
|
|
||||||
"&client_secret=" + clientSecret +
|
Map<String, String> body = new HashMap<>();
|
||||||
"&code=" + code +
|
body.put("client_id", clientId);
|
||||||
(redirectUri != null ? "&redirect_uri=" + redirectUri : ""),
|
body.put("client_secret", clientSecret);
|
||||||
headers);
|
body.put("code", code);
|
||||||
|
if (redirectUri != null) {
|
||||||
|
body.put("redirect_uri", redirectUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpEntity<Map<String, String>> request = new HttpEntity<>(body, headers);
|
||||||
ResponseEntity<JsonNode> tokenRes = restTemplate.postForEntity(tokenUrl, request, JsonNode.class);
|
ResponseEntity<JsonNode> tokenRes = restTemplate.postForEntity(tokenUrl, request, JsonNode.class);
|
||||||
if (!tokenRes.getStatusCode().is2xxSuccessful() || tokenRes.getBody() == null || !tokenRes.getBody().has("access_token")) {
|
if (!tokenRes.getStatusCode().is2xxSuccessful() || tokenRes.getBody() == null || !tokenRes.getBody().has("access_token")) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
@@ -43,6 +51,7 @@ public class GithubAuthService {
|
|||||||
String accessToken = tokenRes.getBody().get("access_token").asText();
|
String accessToken = tokenRes.getBody().get("access_token").asText();
|
||||||
HttpHeaders authHeaders = new HttpHeaders();
|
HttpHeaders authHeaders = new HttpHeaders();
|
||||||
authHeaders.setBearerAuth(accessToken);
|
authHeaders.setBearerAuth(accessToken);
|
||||||
|
authHeaders.set(HttpHeaders.USER_AGENT, "OpenIsle");
|
||||||
HttpEntity<Void> entity = new HttpEntity<>(authHeaders);
|
HttpEntity<Void> entity = new HttpEntity<>(authHeaders);
|
||||||
ResponseEntity<JsonNode> userRes = restTemplate.exchange(
|
ResponseEntity<JsonNode> userRes = restTemplate.exchange(
|
||||||
"https://api.github.com/user", HttpMethod.GET, entity, JsonNode.class);
|
"https://api.github.com/user", HttpMethod.GET, entity, JsonNode.class);
|
||||||
@@ -56,15 +65,19 @@ public class GithubAuthService {
|
|||||||
email = userNode.get("email").asText();
|
email = userNode.get("email").asText();
|
||||||
}
|
}
|
||||||
if (email == null || email.isEmpty()) {
|
if (email == null || email.isEmpty()) {
|
||||||
ResponseEntity<JsonNode> emailsRes = restTemplate.exchange(
|
try {
|
||||||
"https://api.github.com/user/emails", HttpMethod.GET, entity, JsonNode.class);
|
ResponseEntity<JsonNode> emailsRes = restTemplate.exchange(
|
||||||
if (emailsRes.getStatusCode().is2xxSuccessful() && emailsRes.getBody() != null && emailsRes.getBody().isArray()) {
|
"https://api.github.com/user/emails", HttpMethod.GET, entity, JsonNode.class);
|
||||||
for (JsonNode n : emailsRes.getBody()) {
|
if (emailsRes.getStatusCode().is2xxSuccessful() && emailsRes.getBody() != null && emailsRes.getBody().isArray()) {
|
||||||
if (n.has("primary") && n.get("primary").asBoolean()) {
|
for (JsonNode n : emailsRes.getBody()) {
|
||||||
email = n.get("email").asText();
|
if (n.has("primary") && n.get("primary").asBoolean()) {
|
||||||
break;
|
email = n.get("email").asText();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (HttpClientErrorException ignored) {
|
||||||
|
// ignore when the email API is not accessible
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (email == null) {
|
if (email == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user