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.core.javahl;
013:
014: import java.io.File;
015: import java.security.cert.X509Certificate;
016:
017: import org.tigris.subversion.javahl.PromptUserPassword;
018: import org.tigris.subversion.javahl.PromptUserPassword2;
019: import org.tigris.subversion.javahl.PromptUserPassword3;
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.1.1
033: * @author TMate Software Ltd.
034: */
035: class JavaHLAuthenticationProvider implements
036: ISVNAuthenticationProvider {
037:
038: private static final String ADAPTER_DEFAULT_PROMPT_CLASS = "org.tigris.subversion.svnclientadapter.javahl.AbstractJhlClientAdapter$DefaultPromptUserPassword";
039: private PromptUserPassword myPrompt;
040:
041: public JavaHLAuthenticationProvider(PromptUserPassword prompt) {
042: myPrompt = prompt;
043: }
044:
045: public SVNAuthentication requestClientAuthentication(String kind,
046: SVNURL url, String realm, SVNErrorMessage errorMessage,
047: SVNAuthentication previousAuth, boolean authMayBeStored) {
048: if (ISVNAuthenticationManager.SSH.equals(kind)
049: && myPrompt instanceof PromptUserPasswordSSH) {
050: PromptUserPasswordSSH prompt4 = (PromptUserPasswordSSH) myPrompt;
051: String userName = previousAuth != null
052: && previousAuth.getUserName() != null ? previousAuth
053: .getUserName()
054: : getUserName(null, url);
055: int port = url != null ? url.getPort() : -1;
056: if (prompt4.promptSSH(realm, userName, port,
057: authMayBeStored)) {
058: String password = prompt4.getPassword();
059: String keyPath = prompt4.getSSHPrivateKeyPath();
060: String passphrase = prompt4
061: .getSSHPrivateKeyPassphrase();
062: userName = getUserName(prompt4.getUsername(), url);
063: if ("".equals(passphrase)) {
064: passphrase = null;
065: }
066: port = prompt4.getSSHPort();
067: if (port < 0 && url != null) {
068: port = url.getPort();
069: }
070: if (port < 0) {
071: port = 22;
072: }
073: boolean save = prompt4.userAllowedSave();
074: if (keyPath != null && !"".equals(keyPath)) {
075: return new SVNSSHAuthentication(userName, new File(
076: keyPath), passphrase, port, save);
077: } else if (password != null) {
078: return new SVNSSHAuthentication(userName, password,
079: port, save);
080: }
081: }
082: return null;
083: } else if (ISVNAuthenticationManager.SSL.equals(kind)
084: && myPrompt instanceof PromptUserPasswordSSL) {
085: PromptUserPasswordSSL prompt4 = (PromptUserPasswordSSL) myPrompt;
086: if (prompt4.promptSSL(realm, authMayBeStored)) {
087: String cert = prompt4.getSSLClientCertPath();
088: String password = prompt4.getSSLClientCertPassword();
089: if (cert != null) {
090: if ("".equals(password)) {
091: password = null;
092: }
093: boolean save = prompt4.userAllowedSave();
094: return new SVNSSLAuthentication(new File(cert),
095: password, save);
096: }
097: }
098: return null;
099: }
100: if (ISVNAuthenticationManager.SSH.equals(kind)
101: && previousAuth == null) {
102: // use configuration file here? but it was already used once...
103: String keyPath = System.getProperty("svnkit.ssh2.key",
104: System.getProperty("javasvn.ssh2.key"));
105: String userName = getUserName(System.getProperty(
106: "svnkit.ssh2.username", System
107: .getProperty("javasvn.ssh2.username")), url);
108: String passPhrase = System.getProperty(
109: "svnkit.ssh2.passphrase", System
110: .getProperty("javasvn.ssh2.passphrase"));
111: if (userName == null) {
112: return null;
113: }
114: if (keyPath != null && previousAuth == null) {
115: // use port number from configuration file?
116: return new SVNSSHAuthentication(userName, new File(
117: keyPath), passPhrase, -1, true);
118: }
119: // try to get password for ssh from the user.
120: } else if (ISVNAuthenticationManager.USERNAME.equals(kind)) {
121: String userName = previousAuth != null
122: && previousAuth.getUserName() != null ? previousAuth
123: .getUserName()
124: : getUserName(null, url);
125: if (myPrompt instanceof PromptUserPasswordUser) {
126: PromptUserPasswordUser prompt3 = (PromptUserPasswordUser) myPrompt;
127: if (prompt3
128: .promptUser(realm, userName, authMayBeStored)) {
129: return new SVNUserNameAuthentication(prompt3
130: .getUsername(), prompt3.userAllowedSave());
131: }
132: return getDefaultUserNameCredentials(userName);
133: } else if (myPrompt instanceof PromptUserPassword3) {
134: PromptUserPassword3 prompt3 = (PromptUserPassword3) myPrompt;
135: if (prompt3.prompt(realm, userName, authMayBeStored)) {
136: return new SVNUserNameAuthentication(prompt3
137: .getUsername(), prompt3.userAllowedSave());
138: }
139: return getDefaultUserNameCredentials(userName);
140: }
141: if (myPrompt.prompt(realm, userName)) {
142: return new SVNUserNameAuthentication(myPrompt
143: .getUsername(), false);
144: }
145: return getDefaultUserNameCredentials(userName);
146: } else if (!ISVNAuthenticationManager.PASSWORD.equals(kind)) {
147: return null;
148: }
149: String userName = previousAuth != null
150: && previousAuth.getUserName() != null ? previousAuth
151: .getUserName() : getUserName(null, url);
152: if (myPrompt instanceof PromptUserPassword3) {
153: PromptUserPassword3 prompt3 = (PromptUserPassword3) myPrompt;
154: if (prompt3.prompt(realm, userName, authMayBeStored)) {
155: if (ISVNAuthenticationManager.SSH.equals(kind)) {
156: // use default port number from configuration file (should be in previous auth).
157: int portNumber = (previousAuth instanceof SVNSSHAuthentication) ? ((SVNSSHAuthentication) previousAuth)
158: .getPortNumber()
159: : -1;
160: return new SVNSSHAuthentication(prompt3
161: .getUsername(), prompt3.getPassword(),
162: portNumber, prompt3.userAllowedSave());
163: }
164: return new SVNPasswordAuthentication(prompt3
165: .getUsername(), prompt3.getPassword(), prompt3
166: .userAllowedSave());
167: }
168: } else {
169: if (myPrompt.prompt(realm, userName)) {
170: if (ISVNAuthenticationManager.SSH.equals(kind)) {
171: return new SVNSSHAuthentication(userName, myPrompt
172: .getPassword(), -1, true);
173: }
174: return new SVNPasswordAuthentication(myPrompt
175: .getUsername(), myPrompt.getPassword(), true);
176: }
177: }
178: return null;
179: }
180:
181: private SVNAuthentication getDefaultUserNameCredentials(
182: String userName) {
183: if (ADAPTER_DEFAULT_PROMPT_CLASS.equals(myPrompt.getClass()
184: .getName())) {
185: // return default username, despite prompt was 'cancelled'.
186: return new SVNUserNameAuthentication(userName, false);
187: }
188: return null;
189: }
190:
191: public int acceptServerAuthentication(SVNURL url, String realm,
192: Object serverAuth, boolean resultMayBeStored) {
193: if (serverAuth != null
194: && myPrompt instanceof PromptUserPassword2) {
195: PromptUserPassword2 sslPrompt = (PromptUserPassword2) myPrompt;
196: serverAuth = serverAuth instanceof X509Certificate ? SVNSSLUtil
197: .getServerCertificatePrompt(
198: (X509Certificate) serverAuth, realm, url
199: .getHost())
200: : serverAuth;
201: if (serverAuth == null) {
202: serverAuth = "Unsupported certificate type '"
203: + (serverAuth != null ? serverAuth.getClass()
204: .getName() : "null") + "'";
205: }
206: return sslPrompt.askTrustSSLServer(serverAuth.toString(),
207: resultMayBeStored);
208: }
209: return ACCEPTED;
210: }
211:
212: private static String getUserName(String userName, SVNURL url) {
213: if (userName == null || "".equals(userName.trim())) {
214: userName = url != null ? url.getUserInfo() : null;
215: }
216: if (userName == null || "".equals(userName.trim())) {
217: userName = System.getProperty("user.name");
218: }
219: return userName;
220: }
221:
222: }
|