Skip to content

Commit a428d1a

Browse files
committed
Refactor and cleanup
1 parent 6d73964 commit a428d1a

1 file changed

Lines changed: 48 additions & 48 deletions

File tree

plugins/user-authenticators/oauth2/src/main/java/org/apache/cloudstack/oauth2/github/GithubOAuth2Provider.java

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -63,54 +63,6 @@ public String verifyCodeAndFetchEmail(String secretCode) {
6363
return verifyCodeAndFetchEmail(secretCode, null);
6464
}
6565

66-
public String getUserEmailAddress() throws CloudRuntimeException {
67-
if (StringUtils.isEmpty(accessToken)) {
68-
throw new CloudRuntimeException("Access Token not found to fetch the email address");
69-
}
70-
71-
String apiUrl = "https://api.github.com/user/emails";
72-
String email = null;
73-
try {
74-
URL url = new URL(apiUrl);
75-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
76-
connection.setRequestMethod("GET");
77-
connection.setRequestProperty("Authorization", "token " + accessToken);
78-
79-
int responseCode = connection.getResponseCode();
80-
if (responseCode == HttpURLConnection.HTTP_OK) {
81-
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
82-
String inputLine;
83-
StringBuilder response = new StringBuilder();
84-
while ((inputLine = in.readLine()) != null) {
85-
response.append(inputLine);
86-
}
87-
88-
try {
89-
ObjectMapper objectMapper = new ObjectMapper();
90-
JsonNode jsonNode = objectMapper.readTree(response.toString());
91-
if (jsonNode != null && jsonNode.isArray()) {
92-
JsonNode firstObject = jsonNode.get(0);
93-
email = firstObject.get("email").asText();
94-
} else {
95-
throw new CloudRuntimeException("Invalid JSON format found while accessing email from github");
96-
}
97-
} catch (Exception e) {
98-
throw new CloudRuntimeException(String.format("Error occurred while accessing email from github: %s", e.getMessage()));
99-
} }
100-
} else {
101-
throw new CloudRuntimeException(String.format("HTTP Request Failed with error code: %s", responseCode));
102-
}
103-
} catch (IOException e) {
104-
throw new CloudRuntimeException(String.format("Error while trying to fetch email from github : %s", e.getMessage()));
105-
}
106-
107-
return email;
108-
}
109-
110-
private void clearAccessToken() {
111-
accessToken = null;
112-
}
113-
11466
@Override
11567
public boolean verifyUser(String email, String secretCode, Long domainId) {
11668
if (StringUtils.isAnyEmpty(email, secretCode)) {
@@ -190,4 +142,52 @@ protected String getAccessToken(String secretCode, Long domainId) throws CloudRu
190142
return accessToken;
191143
}
192144

145+
public String getUserEmailAddress() throws CloudRuntimeException {
146+
if (StringUtils.isEmpty(accessToken)) {
147+
throw new CloudRuntimeException("Access Token not found to fetch the email address");
148+
}
149+
150+
String apiUrl = "https://api.github.com/user/emails";
151+
String email = null;
152+
try {
153+
URL url = new URL(apiUrl);
154+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
155+
connection.setRequestMethod("GET");
156+
connection.setRequestProperty("Authorization", "token " + accessToken);
157+
158+
int responseCode = connection.getResponseCode();
159+
if (responseCode == HttpURLConnection.HTTP_OK) {
160+
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
161+
String inputLine;
162+
StringBuilder response = new StringBuilder();
163+
while ((inputLine = in.readLine()) != null) {
164+
response.append(inputLine);
165+
}
166+
167+
try {
168+
ObjectMapper objectMapper = new ObjectMapper();
169+
JsonNode jsonNode = objectMapper.readTree(response.toString());
170+
if (jsonNode != null && jsonNode.isArray()) {
171+
JsonNode firstObject = jsonNode.get(0);
172+
email = firstObject.get("email").asText();
173+
} else {
174+
throw new CloudRuntimeException("Invalid JSON format found while accessing email from github");
175+
}
176+
} catch (Exception e) {
177+
throw new CloudRuntimeException(String.format("Error occurred while accessing email from github: %s", e.getMessage()));
178+
} }
179+
} else {
180+
throw new CloudRuntimeException(String.format("HTTP Request Failed with error code: %s", responseCode));
181+
}
182+
} catch (IOException e) {
183+
throw new CloudRuntimeException(String.format("Error while trying to fetch email from github : %s", e.getMessage()));
184+
}
185+
186+
return email;
187+
}
188+
189+
private void clearAccessToken() {
190+
accessToken = null;
191+
}
192+
193193
}

0 commit comments

Comments
 (0)