Mercurial > hg4j
annotate src/org/tmatesoft/hg/internal/remote/HttpAuthMethod.java @ 708:4ffc17c0b534
Merge: tests for resolver and complex scenario. Enable commit for merged revisions. Reuse file revisions if nothing changed
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 20 Aug 2013 18:41:34 +0200 |
parents | a483b2b68a2e |
children |
rev | line source |
---|---|
699
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2013 TMate Software Ltd |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 * |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
7 * |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 * GNU General Public License for more details. |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 * |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
13 * For information on how to redistribute this software under |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
17 package org.tmatesoft.hg.internal.remote; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Info; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.io.InputStream; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
23 import java.net.HttpURLConnection; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 import java.net.URL; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 import java.security.cert.CertificateException; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 import java.security.cert.X509Certificate; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27 import java.util.prefs.BackingStoreException; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 import java.util.prefs.Preferences; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 import javax.net.ssl.HttpsURLConnection; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 import javax.net.ssl.SSLContext; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 import javax.net.ssl.TrustManager; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 import javax.net.ssl.X509TrustManager; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
35 import org.tmatesoft.hg.auth.HgAuthFailedException; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36 import org.tmatesoft.hg.auth.HgAuthMethod; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 import org.tmatesoft.hg.core.HgRemoteConnectionException; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 import org.tmatesoft.hg.core.SessionContext; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 import org.tmatesoft.hg.repo.HgInvalidStateException; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 /** |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 * |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
43 * @author Artem Tikhomirov |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44 * @author TMate Software Ltd. |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
45 */ |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
46 public class HttpAuthMethod implements HgAuthMethod { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 private final SessionContext ctx; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 private final URL url; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 private String authInfo; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 private SSLContext sslContext; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 /** |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 * @param sessionContext |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 * @param url location fully ready to attempt connection to perform authentication check, e.g. hello command (anything with *small* output will do) |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 * @throws HgRemoteConnectionException |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 */ |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 HttpAuthMethod(SessionContext sessionContext, URL url) throws HgRemoteConnectionException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 ctx = sessionContext; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 if (!"http".equals(url.getProtocol()) && !"https".equals(url.getProtocol())) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 throw new HgInvalidStateException(String.format("http protocol expected: %s", url.toString())); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 this.url = url; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 if ("https".equals(url.getProtocol())) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 try { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 sslContext = SSLContext.getInstance("SSL"); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 class TrustEveryone implements X509TrustManager { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
71 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 public X509Certificate[] getAcceptedIssuers() { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 return new X509Certificate[0]; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 }; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 sslContext.init(null, new TrustManager[] { new TrustEveryone() }, null); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 } catch (Exception ex) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 throw new HgRemoteConnectionException("Can't initialize secure connection", ex); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 } else { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 sslContext = null; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 public void tryWithUserInfo(String uriUserInfo) throws HgAuthFailedException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 int colon = uriUserInfo.indexOf(':'); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 if (colon == -1) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
88 withPassword(uriUserInfo, null); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 } else { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 withPassword(uriUserInfo.substring(0, colon), uriUserInfo.substring(colon+1)); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
92 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
93 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 public void noCredentials() throws HgAuthFailedException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 // TODO Auto-generated method stub |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 checkConnection(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
98 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
99 public boolean supportsPassword() { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 return true; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
101 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
102 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 public void withPassword(String username, String password) throws HgAuthFailedException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 authInfo = buildAuthValue(username, password == null ? "" : password); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 checkConnection(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
106 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
107 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
108 public boolean supportsPublicKey() { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
109 return false; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
110 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
111 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 public void withPublicKey(String username, InputStream privateKey, String passphrase) throws HgAuthFailedException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 public boolean supportsCertificate() { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
116 return "https".equals(url.getProtocol()); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
117 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
118 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
119 public void withCertificate(X509Certificate[] clientCert) throws HgAuthFailedException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
120 // TODO Auto-generated method stub |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
121 checkConnection(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
122 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
123 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
124 private void checkConnection() throws HgAuthFailedException { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
125 // we've checked the protocol to be http(s) |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
126 HttpURLConnection c = null; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
127 try { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
128 c = (HttpURLConnection) url.openConnection(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
129 c = setupConnection(c); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
130 c.connect(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
131 InputStream is = c.getInputStream(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
132 while (is.read() != -1) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
134 is.close(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
135 final int HTTP_UNAUTHORIZED = 401; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
136 if (c.getResponseCode() == HTTP_UNAUTHORIZED) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
137 throw new HgAuthFailedException(c.getResponseMessage(), null); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
138 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
139 } catch (IOException ex) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
140 throw new HgAuthFailedException("Communication failure while authenticating", ex); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
141 } finally { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
142 if (c != null) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
143 c.disconnect(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
144 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
145 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
146 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
147 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
148 HttpURLConnection setupConnection(HttpURLConnection urlConnection) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
149 if (authInfo != null) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
150 urlConnection.addRequestProperty("Authorization", "Basic " + authInfo); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
151 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
152 if (sslContext != null) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
153 ((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslContext.getSocketFactory()); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
154 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
155 return urlConnection; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
156 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
157 |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
158 private String buildAuthValue(String username, String password) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
159 String ai = null; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
160 try { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
161 // Hack to get Base64-encoded credentials |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
162 Preferences tempNode = Preferences.userRoot().node("xxx"); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
163 tempNode.putByteArray("xxx", String.format("%s:%s", username, password).getBytes()); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
164 ai = tempNode.get("xxx", null); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
165 tempNode.removeNode(); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
166 } catch (BackingStoreException ex) { |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
167 ctx.getLog().dump(getClass(), Info, ex, null); |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
168 // IGNORE |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
169 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
170 return ai; |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
171 } |
a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
172 } |