Search in sources :

Example 1 with GitHubKey

use of org.eclipse.che.plugin.github.shared.GitHubKey in project che by eclipse.

the class GitHubKeyUploader method uploadKey.

@Override
public void uploadKey(String publicKey) throws IOException, UnauthorizedException {
    final OAuthToken token = tokenProvider.getToken("github", EnvironmentContext.getCurrent().getSubject().getUserId());
    if (token == null || token.getToken() == null) {
        LOG.debug("Token not found, user need to authorize to upload key.");
        throw new UnauthorizedException("To upload SSH key you need to authorize.");
    }
    StringBuilder answer = new StringBuilder();
    final String url = String.format("https://api.github.com/user/keys?access_token=%s", token.getToken());
    final List<GitHubKey> gitHubUserPublicKeys = getUserPublicKeys(url, answer);
    for (GitHubKey gitHubUserPublicKey : gitHubUserPublicKeys) {
        if (publicKey.startsWith(gitHubUserPublicKey.getKey())) {
            return;
        }
    }
    final Map<String, String> postParams = new HashMap<>(2);
    postParams.put("title", "IDE SSH Key (" + new SimpleDateFormat().format(new Date()) + ")");
    postParams.put("key", new String(publicKey.getBytes()));
    final String postBody = JsonHelper.toJson(postParams);
    LOG.debug("Upload public key: {}", postBody);
    int responseCode;
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setInstanceFollowRedirects(false);
        conn.setRequestMethod(HttpMethod.POST);
        conn.setRequestProperty(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
        conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
        conn.setRequestProperty(HttpHeaders.CONTENT_LENGTH, String.valueOf(postBody.length()));
        conn.setDoOutput(true);
        try (OutputStream out = conn.getOutputStream()) {
            out.write(postBody.getBytes());
        }
        responseCode = conn.getResponseCode();
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
    LOG.debug("Upload key response code: {}", responseCode);
    if (responseCode != HttpURLConnection.HTTP_CREATED) {
        throw new IOException(String.format("%d: Failed to upload public key to https://github.com/", responseCode));
    }
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Date(java.util.Date) URL(java.net.URL) OAuthToken(org.eclipse.che.api.auth.shared.dto.OAuthToken) HttpURLConnection(java.net.HttpURLConnection) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) GitHubKey(org.eclipse.che.plugin.github.shared.GitHubKey) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 OAuthToken (org.eclipse.che.api.auth.shared.dto.OAuthToken)1 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)1 GitHubKey (org.eclipse.che.plugin.github.shared.GitHubKey)1