Skip to content

Commit b62d384

Browse files
committed
O365: merge #442 Refactor to use RestRequest, clean up code
git-svn-id: https://svn.code.sf.net/p/davmail/code/trunk@3966 3d1905a2-6b24-0410-a738-b14d5a86fcbd
1 parent c3cfa17 commit b62d384

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/java/davmail/exchange/auth/O365DeviceCodeAuthenticator.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,24 @@
2121

2222
import davmail.Settings;
2323
import davmail.http.HttpClientAdapter;
24-
import davmail.http.request.PostRequest;
24+
import davmail.http.request.RestRequest;
25+
import org.apache.http.Consts;
26+
import org.apache.http.NameValuePair;
27+
import org.apache.http.client.entity.UrlEncodedFormEntity;
28+
import org.apache.http.client.methods.CloseableHttpResponse;
29+
import org.apache.http.message.BasicNameValuePair;
2530
import org.apache.log4j.Logger;
2631
import org.codehaus.jettison.json.JSONException;
2732
import org.codehaus.jettison.json.JSONObject;
2833

2934
import java.io.IOException;
3035
import java.net.URI;
36+
import java.util.ArrayList;
3137

3238
public class O365DeviceCodeAuthenticator implements ExchangeAuthenticator {
33-
protected static final Logger LOGGER = Logger.getLogger(O365Token.class);
39+
protected static final Logger LOGGER = Logger.getLogger(O365DeviceCodeAuthenticator.class);
3440

35-
protected class DeviceCode {
41+
protected static class DeviceCode {
3642
final private String deviceCode;
3743
final private String message;
3844
DeviceCode(String deviceCode, String message) {
@@ -77,7 +83,7 @@ public void authenticate() throws IOException {
7783
// company tenantId or common
7884
String tenantId = Settings.getProperty("davmail.oauth.tenantId", "common");
7985

80-
// first try to load stored token, redirectUri is empty with devicecode
86+
// first try to load a stored token, redirectUri is empty with devicecode
8187
token = O365Token.load(tenantId, clientId, "", username, password);
8288
if (token != null) {
8389
return;
@@ -87,27 +93,23 @@ public void authenticate() throws IOException {
8793
String url = Settings.getO365LoginUrl() + "/" + tenantId + "/oauth2/devicecode?api-version=1.0";
8894

8995
DeviceCode deviceCode;
96+
ArrayList<NameValuePair> parameters = new ArrayList<>();
97+
parameters.add(new BasicNameValuePair("client_id", clientId));
98+
parameters.add(new BasicNameValuePair("resource", resource));
99+
RestRequest logonMethod = new RestRequest(url, new UrlEncodedFormEntity(parameters, Consts.UTF_8));
100+
// Executes device code request; parses response into DeviceCode object
90101
try (
91102
HttpClientAdapter httpClientAdapter = new HttpClientAdapter(url);
103+
CloseableHttpResponse response = httpClientAdapter.execute(logonMethod)
92104
) {
93105

94-
PostRequest logonMethod = new PostRequest(url);
95-
logonMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
96-
97-
logonMethod.setParameter("client_id", clientId);
98-
logonMethod.setParameter("resource", resource);
99-
100-
String responseBodyAsString = httpClientAdapter.executePostRequest(logonMethod);
101-
102-
JSONObject responseBodyAsJSON;
103-
try {
104-
responseBodyAsJSON = new JSONObject(responseBodyAsString);
105-
deviceCode = new DeviceCode(responseBodyAsJSON.getString("device_code"), responseBodyAsJSON.getString("message"));
106-
} catch (JSONException e) {
107-
throw new IOException(e);
108-
}
106+
JSONObject deviceCodeResponse = logonMethod.handleResponse(response);
107+
deviceCode = new DeviceCode(deviceCodeResponse.getString("device_code"), deviceCodeResponse.getString("message"));
108+
} catch (JSONException e) {
109+
throw new IOException("Exception parsing device code", e);
109110
}
110111

112+
// Polls for authorization completion; builds token on success
111113
try {
112114
while (token == null) {
113115
System.out.println(deviceCode.getMessage());

0 commit comments

Comments
 (0)