Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/remote/SshAuthMethod.java @ 699:a483b2b68a2e
Provisional APIs and respective implementation for http, https and ssh remote repositories
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Thu, 08 Aug 2013 19:18:50 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 698:822f3a83ff57 | 699:a483b2b68a2e |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2013 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.internal.remote; | |
| 18 | |
| 19 import java.io.CharArrayWriter; | |
| 20 import java.io.IOException; | |
| 21 import java.io.InputStream; | |
| 22 import java.security.cert.X509Certificate; | |
| 23 import java.util.Arrays; | |
| 24 | |
| 25 import org.tmatesoft.hg.auth.HgAuthFailedException; | |
| 26 import org.tmatesoft.hg.auth.HgAuthMethod; | |
| 27 | |
| 28 import com.trilead.ssh2.Connection; | |
| 29 | |
| 30 /** | |
| 31 * | |
| 32 * @author Artem Tikhomirov | |
| 33 * @author TMate Software Ltd. | |
| 34 */ | |
| 35 public final class SshAuthMethod implements HgAuthMethod { | |
| 36 | |
| 37 private final Connection conn; | |
| 38 | |
| 39 public SshAuthMethod(Connection connection) { | |
| 40 conn = connection; | |
| 41 } | |
| 42 | |
| 43 public void tryWithUserInfo(String uriUserInfo) throws HgAuthFailedException { | |
| 44 assert uriUserInfo != null && uriUserInfo.trim().length() > 0; | |
| 45 int colon = uriUserInfo.indexOf(':'); | |
| 46 if (colon == -1) { | |
| 47 String username = uriUserInfo; | |
| 48 withPassword(username, null); | |
| 49 } else { | |
| 50 String username = uriUserInfo.substring(0, colon); | |
| 51 String password = uriUserInfo.substring(colon+1); | |
| 52 withPassword(username, password); | |
| 53 } | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 public void noCredentials() throws HgAuthFailedException { | |
| 58 try { | |
| 59 String username = System.getProperty("user.name"); | |
| 60 if (!conn.authenticateWithNone(username)) { | |
| 61 throw authFailed(username); | |
| 62 } | |
| 63 } catch (IOException ex) { | |
| 64 throw commFailed(ex); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 public void withPublicKey(String username, InputStream privateKey, String passphrase) throws HgAuthFailedException { | |
| 69 if (username == null) { | |
| 70 // FIXME AuthFailure and AuthFailed or similar distinct exceptions to tell true authentication issues from | |
| 71 // failures around it. | |
| 72 throw new HgAuthFailedException("Need username", null); | |
| 73 } | |
| 74 if (privateKey == null) { | |
| 75 throw new HgAuthFailedException("Need private key", null); | |
| 76 } | |
| 77 CharArrayWriter a = new CharArrayWriter(2048); | |
| 78 int r; | |
| 79 try { | |
| 80 while((r = privateKey.read()) != -1) { | |
| 81 a.append((char) r); | |
| 82 } | |
| 83 } catch (IOException ex) { | |
| 84 throw new HgAuthFailedException("Failed to read private key", ex); | |
| 85 } | |
| 86 try { | |
| 87 boolean success = conn.authenticateWithPublicKey(username, a.toCharArray(), passphrase); | |
| 88 if (!success) { | |
| 89 throw authFailed(username); | |
| 90 } | |
| 91 } catch (IOException ex) { | |
| 92 throw commFailed(ex); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 public void withPassword(String username, String password) throws HgAuthFailedException { | |
| 97 if (username == null) { | |
| 98 throw new HgAuthFailedException("Need username", null); | |
| 99 } | |
| 100 try { | |
| 101 boolean success; | |
| 102 if (password == null) { | |
| 103 success = conn.authenticateWithNone(username); | |
| 104 } else { | |
| 105 success = conn.authenticateWithPassword(username, password); | |
| 106 } | |
| 107 if (!success) { | |
| 108 throw authFailed(username); | |
| 109 } | |
| 110 } catch (IOException ex) { | |
| 111 throw commFailed(ex); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 public void withCertificate(X509Certificate[] clientCert) throws HgAuthFailedException { | |
| 116 } | |
| 117 | |
| 118 public boolean supportsPublicKey() { | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 public boolean supportsPassword() { | |
| 123 return true; | |
| 124 } | |
| 125 | |
| 126 public boolean supportsCertificate() { | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 private HgAuthFailedException commFailed(IOException ex) { | |
| 131 return new HgAuthFailedException("Communication failure while authenticating", ex); | |
| 132 } | |
| 133 | |
| 134 private HgAuthFailedException authFailed(String username) throws IOException { | |
| 135 final String[] authMethodsLeft = conn.getRemainingAuthMethods(username); | |
| 136 return new HgAuthFailedException(String.format("Failed to authenticate, other methods to try: %s", Arrays.toString(authMethodsLeft)), null); | |
| 137 } | |
| 138 } |
