forked from sritejakv/splitwise-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthUtil.java
More file actions
181 lines (166 loc) · 6.27 KB
/
OAuthUtil.java
File metadata and controls
181 lines (166 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package utils;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.builder.api.DefaultApi10a;
import com.github.scribejava.core.model.*;
import com.github.scribejava.core.oauth.OAuth10aService;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
/**
* Utility class that performs OAuth authentication.
*/
public class OAuthUtil {
private String apiKey;
private String apiSecret;
private OAuth10aService service;
private OAuth1RequestToken requestToken;
private OAuth1AccessToken accessToken;
private List<Integer> httpErrorCodes = new ArrayList<Integer>() {{
add(400);
add(401);
add(403);
add(404);
add(500);
add(503);
}};
/**
* Set the consumer key of the API.
* @param apiKey consumer key
* @return OAuthUtil instance
*/
public OAuthUtil setApiKey(@NotNull String apiKey) {
this.apiKey = apiKey;
return this;
}
/**
* Set the consumer secret of the API.
* @param apiSecret consumer secret
* @return OAuthUtil instance
*/
public OAuthUtil setApiSecret(@NotNull String apiSecret) {
this.apiSecret = apiSecret;
return this;
}
/**
* Returns the authorization url from apiKey and apiSecret.
* @return authorization url
* @throws InterruptedException
* @throws ExecutionException
* @throws IOException
*/
public String getAuthorizationUrl() throws InterruptedException, ExecutionException, IOException {
this.requestToken = service.getRequestToken();
String authUrl = service.getAuthorizationUrl(requestToken);
return authUrl;
}
/**
* Sets the access token by using the token verifier received from authorization url.
* @param verifier token verifier
* @throws InterruptedException
* @throws ExecutionException
* @throws IOException
*/
public void setAccessToken(@NotNull String verifier) throws InterruptedException, ExecutionException, IOException {
this.accessToken = this.service.getAccessToken(requestToken, verifier);
}
/**
* Set the access token details.
* @param token access token
* @param tokenSecret access token secret
* @param rawResponse raw response of the access token
*/
public void setAccessToken(@NotNull String token, @NotNull String tokenSecret, String rawResponse){
this.accessToken = new OAuth1AccessToken(token, tokenSecret, rawResponse);
}
/**
* Returns the access token details received.
* @return access token details received
*/
public Token getAccessToken(){
return this.accessToken;
}
/**
* Generates the API request, communicates to the API and returns the response.
* @param url URL to communicate
* @param httpMethod GET or POST
* @param data data to be sent if the httpMethod is POST
* @return response from the API
* @throws Exception
*/
public Response makeRequest(String url, Verb httpMethod, Object... data) throws Exception {
OAuthRequest request = new OAuthRequest(httpMethod, url);
if (data.length > 0 && data[0] instanceof Map){
Map<String, String> details = (Map<String, String>) data[0];
for (Map.Entry<String, String> entry: details.entrySet()){
request.addBodyParameter(entry.getKey(), entry.getValue());
}
}
this.service.signRequest(this.accessToken, request);
Response response = null;
try {
response = this.service.execute(request);
} catch (InterruptedException e) {
throw new Exception(String.format(
"Invalid response received - %s. " +
"Please check your client id and secret.", e.getMessage()));
} catch (ExecutionException e) {
throw new Exception(String.format(
"Invalid response received - %s. " +
"Please check your client id and secret.", e.getMessage()));
} catch (IOException e) {
throw new Exception(String.format(
"Invalid response received - %s. " +
"Please check your client id and secret.", e.getMessage()));
} catch (Exception e) {
throw new Exception(String.format(
"Invalid response received - %s. " +
"Please check your client id and secret.", e.getMessage()));
}
return parseResponse(response);
}
/**
* Parse the response from the API
* @param response response from the API
* @return response if the response code is 200
* @throws Exception throws exception for other response codes
*/
private Response parseResponse(@NotNull Response response) throws Exception {
if (httpErrorCodes.contains(response.getCode())){
throw new Exception(String.format(
"Invalid response received with body - %s, message - %s. " +
"Please check your client id and secret. Response code - %s", response.getBody(),
response.getMessage(),
response.getCode()));
}
return response;
}
/**
* Utility builder for OAuth authentication
* @param instance API instance containing OAuth details
* @return OAuthUtil instance
*/
public OAuthUtil build(DefaultApi10a instance){
this.service = new ServiceBuilder(this.apiKey)
.apiSecret(this.apiSecret)
.build(instance);
return this;
}
/**
* Utility builder for OAuth authentication
* @param apiKey consumerKey
* @param apiSecret consuerSecret
* @param instance API instance containing OAuth details
* @return OAuthUtil instance
*/
public OAuthUtil build(@NotNull String apiKey, @NotNull String apiSecret, DefaultApi10a instance){
setApiKey(apiKey);
setApiSecret(apiSecret);
this.service = new ServiceBuilder(this.apiKey)
.apiSecret(this.apiSecret)
.build(instance);
return this;
}
}