001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html.
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012: package org.tmatesoft.svn.cli;
013:
014: import java.io.BufferedReader;
015: import java.io.File;
016: import java.io.IOException;
017: import java.io.InputStreamReader;
018: import java.security.cert.X509Certificate;
019:
020: import org.tmatesoft.svn.core.SVNErrorMessage;
021: import org.tmatesoft.svn.core.SVNURL;
022: import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
023: import org.tmatesoft.svn.core.auth.ISVNAuthenticationProvider;
024: import org.tmatesoft.svn.core.auth.SVNAuthentication;
025: import org.tmatesoft.svn.core.auth.SVNPasswordAuthentication;
026: import org.tmatesoft.svn.core.auth.SVNSSHAuthentication;
027: import org.tmatesoft.svn.core.auth.SVNSSLAuthentication;
028: import org.tmatesoft.svn.core.auth.SVNUserNameAuthentication;
029: import org.tmatesoft.svn.core.internal.util.SVNSSLUtil;
030:
031: /**
032: * @version 1.0
033: * @author TMate Software Ltd.
034: */
035: public class SVNConsoleAuthenticationProvider implements
036: ISVNAuthenticationProvider {
037:
038: public int acceptServerAuthentication(SVNURL url, String realm,
039: Object certificate, boolean resultMayBeStored) {
040: if (!(certificate instanceof X509Certificate)) {
041: return ISVNAuthenticationProvider.ACCEPTED_TEMPORARY;
042: }
043: String hostName = url.getHost();
044: X509Certificate cert = (X509Certificate) certificate;
045: StringBuffer prompt = SVNSSLUtil.getServerCertificatePrompt(
046: cert, realm, hostName);
047: if (resultMayBeStored) {
048: prompt
049: .append("\n(R)eject, accept (t)emporarily or accept (p)ermanently? ");
050: } else {
051: prompt.append("\n(R)eject or accept (t)emporarily? ");
052: }
053: System.out.print(prompt.toString());
054: System.out.flush();
055: int r = -1;
056: while (true) {
057: try {
058: r = System.in.read();
059: if (r < 0) {
060: return ISVNAuthenticationProvider.REJECTED;
061: }
062: char ch = (char) (r & 0xFF);
063: if (ch == 'R' || ch == 'r') {
064: return ISVNAuthenticationProvider.REJECTED;
065: } else if (ch == 't' || ch == 'T') {
066: return ISVNAuthenticationProvider.ACCEPTED_TEMPORARY;
067: } else if (resultMayBeStored
068: && (ch == 'p' || ch == 'P')) {
069: return ISVNAuthenticationProvider.ACCEPTED;
070: }
071: } catch (IOException e) {
072: return ISVNAuthenticationProvider.REJECTED;
073: }
074: }
075: }
076:
077: public SVNAuthentication requestClientAuthentication(String kind,
078: SVNURL url, String realm, SVNErrorMessage errorMessage,
079: SVNAuthentication previousAuth, boolean authMayBeStored) {
080: if (ISVNAuthenticationManager.PASSWORD.equals(kind)) {
081: String name = null;
082: printRealm(realm);
083: while (name == null) {
084: name = prompt("Username");
085: if ("".equals(name)) {
086: name = null;
087: }
088: }
089: String password = prompt("Password for '" + name + "'");
090: if (password == null) {
091: password = "";
092: }
093: return new SVNPasswordAuthentication(name, password,
094: authMayBeStored);
095: } else if (ISVNAuthenticationManager.SSH.equals(kind)) {
096: String name = null;
097: printRealm(realm);
098: while (name == null) {
099: name = prompt("Username");
100: if ("".equals(name)) {
101: name = null;
102: }
103: }
104: String password = prompt("Password for '"
105: + url.getHost()
106: + "' (leave blank if you are going to use private key)");
107: if ("".equals(password)) {
108: password = null;
109: }
110: String keyFile = null;
111: String passphrase = null;
112: if (password == null) {
113: while (keyFile == null) {
114: keyFile = prompt("Private key for '"
115: + url.getHost() + "' (OpenSSH format)");
116: if ("".equals(keyFile)) {
117: name = null;
118: }
119: File file = new File(keyFile);
120: if (!file.isFile() && !file.canRead()) {
121: continue;
122: }
123: passphrase = prompt("Private key passphrase [none]");
124: if ("".equals(passphrase)) {
125: passphrase = null;
126: }
127: }
128: }
129: int port = 22;
130: String portValue = prompt("Port number for '"
131: + url.getHost() + "' [22]");
132: if (portValue != null && !"".equals(portValue)) {
133: try {
134: port = Integer.parseInt(portValue);
135: } catch (NumberFormatException e) {
136: }
137: }
138: if (password != null) {
139: return new SVNSSHAuthentication(name, password, port,
140: authMayBeStored);
141: } else if (keyFile != null) {
142: return new SVNSSHAuthentication(name,
143: new File(keyFile), passphrase, port,
144: authMayBeStored);
145: }
146: } else if (ISVNAuthenticationManager.USERNAME.equals(kind)) {
147: printRealm(realm);
148: String name = null;
149: while (name == null) {
150: name = prompt(!"file".equals(url.getProtocol()) ? "Author name ["
151: + System.getProperty("user.name") + "]"
152: : "Username ["
153: + System.getProperty("user.name") + "]");
154: if ("".equals(name) || name == null) {
155: name = System.getProperty("user.name");
156: }
157: }
158: return new SVNUserNameAuthentication(name, authMayBeStored);
159: } else if (ISVNAuthenticationManager.SSL.equals(kind)) {
160: printRealm(realm);
161: String path = null;
162: while (path == null) {
163: path = prompt("Client certificate filename");
164: if ("".equals(path)) {
165: path = null;
166: }
167: }
168: String password = prompt("Passphrase for '" + realm + "'");
169: if (password == null) {
170: password = "";
171: }
172: return new SVNSSLAuthentication(new File(path), password,
173: authMayBeStored);
174: }
175: return null;
176: }
177:
178: private static void printRealm(String realm) {
179: if (realm != null) {
180: System.out.println("Authentication realm: " + realm);
181: System.out.flush();
182: }
183: }
184:
185: private static String prompt(String label) {
186: System.out.print(label + ": ");
187: System.out.flush();
188: BufferedReader reader = new BufferedReader(
189: new InputStreamReader(System.in));
190: try {
191: return reader.readLine();
192: } catch (IOException e) {
193: return null;
194: }
195: }
196: }
|