This repository has been archived by the owner on Dec 6, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CrestClient.java
271 lines (224 loc) · 8.31 KB
/
CrestClient.java
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package org.devfleet.crest.retrofit;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.exceptions.OAuthException;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.devfleet.crest.CrestAccess;
import org.devfleet.crest.CrestService;
import org.devfleet.crest.model.CrestToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public final class CrestClient {
private static final CrestStore STORE = new CrestStore() {
private Map<String, CrestToken> map = new HashMap<>();
@Override
public void save(CrestToken token) {
this.map.put(token.getRefreshToken(), token);
}
@Override
public void delete(String refresh) {
this.map.remove(refresh);
}
@Override
public CrestToken get(String refresh) {
return this.map.get(refresh);
}
};
private static final Logger LOG = LoggerFactory.getLogger(CrestClient.class);
private static final class CrestOAuth extends DefaultApi20 {
private final String loginHost;
public CrestOAuth(String loginHost) {
this.loginHost = loginHost;
}
@Override
public String getAccessTokenEndpoint() {
return "https://" + loginHost + "/oauth/token";
}
@Override
protected String getAuthorizationBaseUrl() {
return "https://" + loginHost + "/oauth/authorize";
}
}
private static final String TQ_LOGIN = "login.eveonline.com";
private static final String TQ_CREST = "crest-tq.eveonline.com";
private static final String SISI_LOGIN = "sisilogin.testeveonline.com";
private static final String SISI_CREST = "api-sisi.testeveonline.com";
private static final String AGENT = "eve-crest-java (https://github.com/evanova/eve-crest-java)";
public static final class Builder {
private final List<String> scopes;
private CrestStore store = STORE;
private String loginHost = TQ_LOGIN;
private String crestHost = SISI_LOGIN;
private String clientID;
private String clientKey;
private String clientRedirect = "http://localhost/redirect";
private String userAgent = AGENT;
private long timeout = 30L * 1000L;
public Builder() {
this.scopes = new ArrayList<>();
this.scopes.addAll(Arrays.asList(CrestAccess.PUBLIC_SCOPES));
}
public CrestClient.Builder store(final CrestStore store) {
this.store = store;
return this;
}
public CrestClient.Builder login(final String host) {
this.loginHost = host;
return this;
}
public CrestClient.Builder api(final String host) {
this.crestHost = host;
return this;
}
public CrestClient.Builder id(final String clientID) {
this.clientID = clientID;
return this;
}
public CrestClient.Builder key(final String clientKey) {
this.clientKey = clientKey;
return this;
}
public CrestClient.Builder redirect(final String to) {
this.clientRedirect = to;
return this;
}
public CrestClient.Builder timeout(final long timeoutInMillis) {
this.timeout = timeoutInMillis;
return this;
}
public CrestClient.Builder scopes(final String... scopes) {
for (String s: scopes) {
if (!this.scopes.contains(s)) {
this.scopes.add(s);
}
}
return this;
}
public CrestClient.Builder agent(final String userAgent) {
this.userAgent = userAgent;
return this;
}
public final CrestClient build() {
return new CrestClient(
this.loginHost,
this.crestHost,
this.clientID,
this.clientKey,
this.clientRedirect,
this.userAgent,
this.store,
this.timeout,
this.scopes.toArray(new String[this.scopes.size()]));
}
}
private final OAuth20Service oAuth;
private final String crestHost;
private final String loginHost;
private final String userAgent;
private final CrestStore store;
private final long timeout;
private CrestClient(
final String loginHost,
final String crestHost,
final String clientID,
final String clientKey,
final String callback,
final String userAgent,
final CrestStore store,
final long timeout,
final String... scopes) {
this.crestHost = crestHost;
this.loginHost = loginHost;
this.userAgent = userAgent;
this.timeout = timeout;
this.store = store;
StringBuilder scope = new StringBuilder();
if (!ArrayUtils.isEmpty(scopes)) {
for (String s : scopes) {
scope.append(s);
scope.append(" ");
}
}
this.oAuth = new ServiceBuilder()
.apiKey(clientID)
.apiSecret(clientKey)
.scope(StringUtils.removeEnd(scope.toString(), " "))
.callback(callback)
.build(new CrestOAuth(loginHost));
}
public static CrestClient.Builder SISI() {
return new CrestClient.Builder()
.login(SISI_LOGIN)
.api(SISI_CREST);
}
public static CrestClient.Builder SISI(String... scopes) {
return new CrestClient.Builder()
.login(SISI_LOGIN)
.api(SISI_CREST)
.scopes(scopes);
}
public static CrestClient.Builder TQ(String... scopes) {
return new CrestClient.Builder()
.login(TQ_LOGIN)
.api(TQ_CREST)
.scopes(scopes);
}
public static CrestClient.Builder TQ() {
return new CrestClient.Builder()
.login(TQ_LOGIN)
.api(TQ_CREST);
}
public String getAuthorizationUrl() {
return this.oAuth.getAuthorizationUrl();
}
public CrestToken fromAuthCode(final String authCode) {
try {
final OAuth2AccessToken token = this.oAuth.getAccessToken(authCode);
return save(token);
}
catch (OAuthException | IOException e) {
LOG.error(e.getMessage(), e);
return null;
}
}
public CrestToken fromRefresh(final String refresh) {
try {
CrestToken existing = this.store.get(refresh);
if ((null == existing) || (existing.getExpiresOn() < (System.currentTimeMillis() - 5 * 1000))) {
final OAuth2AccessToken token = this.oAuth.refreshAccessToken(refresh);
return save(token);
}
return existing;
}
catch (OAuthException | IOException e) {
LOG.error(e.getMessage(), e);
return null;
}
}
public CrestService newCrestService() {
return new CrestRetrofit(this.crestHost, this.loginHost, this.oAuth, this.store, this.userAgent, this.timeout, null);
}
public CrestService newCrestService(final String refresh) {
return new CrestRetrofit(this.crestHost, this.loginHost, this.oAuth, this.store, this.userAgent, this.timeout, refresh);
}
private CrestToken save(final OAuth2AccessToken token) {
CrestToken returned =
new CrestToken()
.setAccessToken(token.getAccessToken())
.setRefreshToken(token.getRefreshToken())
.setTokenType(token.getTokenType())
.setScope(token.getScope())
.setExpiresIn(token.getExpiresIn());
this.store.save(returned);
return returned;
}
}