summaryrefslogtreecommitdiff
path: root/plugins/github/src/org/jetbrains/plugins/github/api/GithubConnection.java
blob: 6358b79e38eed9cc16d65f7616ebb04c5bd82fbe (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package org.jetbrains.plugins.github.api;

import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.net.HttpConfigurable;
import com.intellij.util.net.ssl.CertificateManager;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.plugins.github.exceptions.*;
import org.jetbrains.plugins.github.util.GithubAuthData;
import org.jetbrains.plugins.github.util.GithubSettings;
import org.jetbrains.plugins.github.util.GithubUrlUtil;
import org.jetbrains.plugins.github.util.GithubUtil;
import sun.security.validator.ValidatorException;

import javax.net.ssl.SSLHandshakeException;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;
import java.util.List;

import static org.jetbrains.plugins.github.api.GithubApiUtil.createDataFromRaw;
import static org.jetbrains.plugins.github.api.GithubApiUtil.fromJson;

public class GithubConnection {
  private static final Logger LOG = GithubUtil.LOG;

  private static final HttpRequestInterceptor PREEMPTIVE_BASIC_AUTH = new PreemptiveBasicAuthInterceptor();

  @NotNull private final String myHost;
  @NotNull private final CloseableHttpClient myClient;
  private final boolean myReusable;

  private volatile HttpUriRequest myRequest;
  private volatile boolean myAborted;

  @TestOnly
  public GithubConnection(@NotNull GithubAuthData auth) {
    this(auth, false);
  }

  public GithubConnection(@NotNull GithubAuthData auth, boolean reusable) {
    myHost = auth.getHost();
    myClient = createClient(auth);
    myReusable = reusable;
  }

  private enum HttpVerb {
    GET, POST, DELETE, HEAD, PATCH
  }

  @Nullable
  public JsonElement getRequest(@NotNull String path,
                                @NotNull Header... headers) throws IOException {
    return request(path, null, Arrays.asList(headers), HttpVerb.GET).getJsonElement();
  }

  @Nullable
  public JsonElement postRequest(@NotNull String path,
                                 @Nullable String requestBody,
                                 @NotNull Header... headers) throws IOException {
    return request(path, requestBody, Arrays.asList(headers), HttpVerb.POST).getJsonElement();
  }

  @Nullable
  public JsonElement patchRequest(@NotNull String path,
                                  @Nullable String requestBody,
                                  @NotNull Header... headers) throws IOException {
    return request(path, requestBody, Arrays.asList(headers), HttpVerb.PATCH).getJsonElement();
  }

  @Nullable
  public JsonElement deleteRequest(@NotNull String path,
                                   @NotNull Header... headers) throws IOException {
    return request(path, null, Arrays.asList(headers), HttpVerb.DELETE).getJsonElement();
  }

  @NotNull
  public Header[] headRequest(@NotNull String path,
                              @NotNull Header... headers) throws IOException {
    return request(path, null, Arrays.asList(headers), HttpVerb.HEAD).getHeaders();
  }

  public void abort() {
    if (myAborted) return;
    myAborted = true;

    HttpUriRequest request = myRequest;
    if (request != null) request.abort();
  }

  public void close() throws IOException {
    myClient.close();
  }

  @NotNull
  private static CloseableHttpClient createClient(@NotNull GithubAuthData auth) {
    HttpClientBuilder builder = HttpClients.custom();

    return builder
      .setDefaultRequestConfig(createRequestConfig(auth))
      .setDefaultConnectionConfig(createConnectionConfig(auth))
      .setDefaultCredentialsProvider(createCredentialsProvider(auth))
      .setDefaultHeaders(createHeaders(auth))
      .addInterceptorFirst(PREEMPTIVE_BASIC_AUTH)
      .setSslcontext(CertificateManager.getInstance().getSslContext())
      .setHostnameVerifier((X509HostnameVerifier)CertificateManager.HOSTNAME_VERIFIER)
      .build();
  }

  @NotNull
  private static RequestConfig createRequestConfig(@NotNull GithubAuthData auth) {
    RequestConfig.Builder builder = RequestConfig.custom();

    int timeout = GithubSettings.getInstance().getConnectionTimeout();
    builder
      .setConnectTimeout(timeout)
      .setSocketTimeout(timeout);

    final HttpConfigurable proxySettings = HttpConfigurable.getInstance();
    if (auth.isUseProxy() && proxySettings.USE_HTTP_PROXY && !StringUtil.isEmptyOrSpaces(proxySettings.PROXY_HOST)) {
      builder
        .setProxy(new HttpHost(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT));
    }

    return builder.build();
  }

  @NotNull
  private static ConnectionConfig createConnectionConfig(@NotNull GithubAuthData auth) {
    return ConnectionConfig.custom()
      .setCharset(Consts.UTF_8)
      .build();
  }


  @NotNull
  private static CredentialsProvider createCredentialsProvider(@NotNull GithubAuthData auth) {
    CredentialsProvider provider = new BasicCredentialsProvider();
    // Basic authentication
    GithubAuthData.BasicAuth basicAuth = auth.getBasicAuth();
    if (basicAuth != null) {
      provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(basicAuth.getLogin(), basicAuth.getPassword()));
    }

    final HttpConfigurable proxySettings = HttpConfigurable.getInstance();
    //proxySettings.USE_HTTP_PROXY
    if (auth.isUseProxy() && proxySettings.USE_HTTP_PROXY && !StringUtil.isEmptyOrSpaces(proxySettings.PROXY_HOST)) {
      if (proxySettings.PROXY_AUTHENTICATION) {
        provider.setCredentials(new AuthScope(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT),
                                new UsernamePasswordCredentials(proxySettings.PROXY_LOGIN, proxySettings.getPlainProxyPassword()));
      }
    }
    return provider;
  }

  @NotNull
  private static Collection<? extends Header> createHeaders(@NotNull GithubAuthData auth) {
    List<Header> headers = new ArrayList<Header>();
    GithubAuthData.TokenAuth tokenAuth = auth.getTokenAuth();
    if (tokenAuth != null) {
      headers.add(new BasicHeader("Authorization", "token " + tokenAuth.getToken()));
    }
    GithubAuthData.BasicAuth basicAuth = auth.getBasicAuth();
    if (basicAuth != null && basicAuth.getCode() != null) {
      headers.add(new BasicHeader("X-GitHub-OTP", basicAuth.getCode()));
    }
    return headers;
  }

  @NotNull
  private ResponsePage request(@NotNull String path,
                               @Nullable String requestBody,
                               @NotNull Collection<Header> headers,
                               @NotNull HttpVerb verb) throws IOException {
    if (myAborted) throw new GithubOperationCanceledException();

    if (EventQueue.isDispatchThread() && !ApplicationManager.getApplication().isUnitTestMode()) {
      LOG.warn("Network operation in EDT"); // TODO: fix
    }

    CloseableHttpResponse response = null;
    try {
      String uri = GithubUrlUtil.getApiUrl(myHost) + path;
      response = doREST(uri, requestBody, headers, verb);

      if (myAborted) throw new GithubOperationCanceledException();

      checkStatusCode(response, requestBody);

      HttpEntity entity = response.getEntity();
      if (entity == null) {
        return createResponse(response);
      }

      JsonElement ret = parseResponse(entity.getContent());
      if (ret.isJsonNull()) {
        return createResponse(response);
      }

      String newPath = null;
      Header pageHeader = response.getFirstHeader("Link");
      if (pageHeader != null) {
        for (HeaderElement element : pageHeader.getElements()) {
          NameValuePair rel = element.getParameterByName("rel");
          if (rel != null && "next".equals(rel.getValue())) {
            String urlString = element.toString();
            int begin = urlString.indexOf('<');
            int end = urlString.lastIndexOf('>');
            if (begin == -1 || end == -1) {
              LOG.error("Invalid 'Link' header", "{" + pageHeader.toString() + "}");
              break;
            }

            String url = urlString.substring(begin + 1, end);
            String newUrl = GithubUrlUtil.removeProtocolPrefix(url);
            int index = newUrl.indexOf('/');
            newPath = newUrl.substring(index);
            break;
          }
        }
      }

      return createResponse(ret, newPath, response);
    }
    catch (SSLHandshakeException e) { // User canceled operation from CertificateManager
      if (e.getCause() instanceof ValidatorException) {
        LOG.info("Host SSL certificate is not trusted", e);
        throw new GithubOperationCanceledException("Host SSL certificate is not trusted", e);
      }
      throw e;
    }
    catch (IOException e) {
      if (myAborted) throw new GithubOperationCanceledException("Operation canceled", e);
      throw e;
    }
    finally {
      myRequest = null;
      if (response != null) {
        response.close();
      }
      if (!myReusable) {
        myClient.close();
      }
    }
  }

  @NotNull
  private CloseableHttpResponse doREST(@NotNull final String uri,
                                       @Nullable final String requestBody,
                                       @NotNull final Collection<Header> headers,
                                       @NotNull final HttpVerb verb) throws IOException {
    HttpRequestBase request;
    switch (verb) {
      case POST:
        request = new HttpPost(uri);
        if (requestBody != null) {
          ((HttpPost)request).setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
        }
        break;
      case PATCH:
        request = new HttpPatch(uri);
        if (requestBody != null) {
          ((HttpPatch)request).setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
        }
        break;
      case GET:
        request = new HttpGet(uri);
        break;
      case DELETE:
        request = new HttpDelete(uri);
        break;
      case HEAD:
        request = new HttpHead(uri);
        break;
      default:
        throw new IllegalStateException("Unknown HttpVerb: " + verb.toString());
    }

    for (Header header : headers) {
      request.addHeader(header);
    }

    myRequest = request;
    return myClient.execute(request);
  }

  private static void checkStatusCode(@NotNull CloseableHttpResponse response, @Nullable String body) throws IOException {
    int code = response.getStatusLine().getStatusCode();
    switch (code) {
      case HttpStatus.SC_OK:
      case HttpStatus.SC_CREATED:
      case HttpStatus.SC_ACCEPTED:
      case HttpStatus.SC_NO_CONTENT:
        return;
      case HttpStatus.SC_UNAUTHORIZED:
      case HttpStatus.SC_PAYMENT_REQUIRED:
      case HttpStatus.SC_FORBIDDEN:
        String message = getErrorMessage(response);

        Header headerOTP = response.getFirstHeader("X-GitHub-OTP");
        if (headerOTP != null) {
          for (HeaderElement element : headerOTP.getElements()) {
            if ("required".equals(element.getName())) {
              throw new GithubTwoFactorAuthenticationException(message);
            }
          }
        }

        if (message.contains("API rate limit exceeded")) {
          throw new GithubRateLimitExceededException(message);
        }

        throw new GithubAuthenticationException("Request response: " + message);
      case HttpStatus.SC_BAD_REQUEST:
      case HttpStatus.SC_UNPROCESSABLE_ENTITY:
        if (body != null) {
          LOG.info(body);
        }
        throw new GithubStatusCodeException(code + ": " + getErrorMessage(response), code);
      default:
        throw new GithubStatusCodeException(code + ": " + getErrorMessage(response), code);
    }
  }

  @NotNull
  private static String getErrorMessage(@NotNull CloseableHttpResponse response) {
    try {
      HttpEntity entity = response.getEntity();
      if (entity != null) {
        GithubErrorMessageRaw error = fromJson(parseResponse(entity.getContent()), GithubErrorMessageRaw.class);
        return response.getStatusLine().getReasonPhrase() + " - " + error.getMessage();
      }
    }
    catch (IOException e) {
      LOG.info(e);
    }
    return response.getStatusLine().getReasonPhrase();
  }

  @NotNull
  private static JsonElement parseResponse(@NotNull InputStream githubResponse) throws IOException {
    Reader reader = new InputStreamReader(githubResponse, "UTF-8");
    try {
      return new JsonParser().parse(reader);
    }
    catch (JsonParseException jse) {
      throw new GithubJsonException("Couldn't parse GitHub response", jse);
    }
    finally {
      reader.close();
    }
  }

  public static class PagedRequest<T> {
    @Nullable private String myNextPage;
    @NotNull private final Collection<Header> myHeaders;
    @NotNull private final Class<T> myResult;
    @NotNull private final Class<? extends DataConstructor[]> myRawArray;

    @SuppressWarnings("NullableProblems")
    public PagedRequest(@NotNull String path,
                        @NotNull Class<T> result,
                        @NotNull Class<? extends DataConstructor[]> rawArray,
                        @NotNull Header... headers) {
      myNextPage = path;
      myResult = result;
      myRawArray = rawArray;
      myHeaders = Arrays.asList(headers);
    }

    @NotNull
    public List<T> next(@NotNull GithubConnection connection) throws IOException {
      if (myNextPage == null) {
        throw new NoSuchElementException();
      }

      String page = myNextPage;
      myNextPage = null;

      ResponsePage response = connection.request(page, null, myHeaders, HttpVerb.GET);

      if (response.getJsonElement() == null) {
        throw new GithubConfusingException("Empty response");
      }

      if (!response.getJsonElement().isJsonArray()) {
        throw new GithubJsonException("Wrong json type: expected JsonArray", new Exception(response.getJsonElement().toString()));
      }

      myNextPage = response.getNextPage();

      List<T> result = new ArrayList<T>();
      for (DataConstructor raw : fromJson(response.getJsonElement().getAsJsonArray(), myRawArray)) {
        result.add(createDataFromRaw(raw, myResult));
      }
      return result;
    }

    public boolean hasNext() {
      return myNextPage != null;
    }

    @NotNull
    public List<T> getAll(@NotNull GithubConnection connection) throws IOException {
      List<T> result = new ArrayList<T>();
      while (hasNext()) {
        result.addAll(next(connection));
      }
      return result;
    }
  }

  private ResponsePage createResponse(@NotNull CloseableHttpResponse response) throws GithubOperationCanceledException {
    if (myAborted) throw new GithubOperationCanceledException();

    return new ResponsePage(null, null, response.getAllHeaders());
  }

  private ResponsePage createResponse(@NotNull JsonElement ret, @Nullable String path, @NotNull CloseableHttpResponse response)
    throws GithubOperationCanceledException {
    if (myAborted) throw new GithubOperationCanceledException();

    return new ResponsePage(ret, path, response.getAllHeaders());
  }

  private static class ResponsePage {
    @Nullable private final JsonElement myResponse;
    @Nullable private final String myNextPage;
    @NotNull private final Header[] myHeaders;

    public ResponsePage(@Nullable JsonElement response, @Nullable String next, @NotNull Header[] headers) {
      myResponse = response;
      myNextPage = next;
      myHeaders = headers;
    }

    @Nullable
    public JsonElement getJsonElement() {
      return myResponse;
    }

    @Nullable
    public String getNextPage() {
      return myNextPage;
    }

    @NotNull
    public Header[] getHeaders() {
      return myHeaders;
    }
  }

  private static class PreemptiveBasicAuthInterceptor implements HttpRequestInterceptor {
    @Override
    public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
      CredentialsProvider provider = (CredentialsProvider)context.getAttribute(HttpClientContext.CREDS_PROVIDER);
      Credentials credentials = provider.getCredentials(AuthScope.ANY);
      if (credentials != null) {
        request.addHeader(new BasicScheme(Consts.UTF_8).authenticate(credentials, request, context));
      }
    }
  }
}