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.auth;
013:
014: import java.io.File;
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import org.tmatesoft.svn.core.SVNErrorMessage;
019: import org.tmatesoft.svn.core.SVNException;
020: import org.tmatesoft.svn.core.SVNURL;
021: import org.tmatesoft.svn.core.internal.wc.SVNErrorManager;
022: import org.tmatesoft.svn.core.io.SVNRepository;
023:
024: /**
025: * The <b>BasicAuthenticationManager</b> is a simple implementation of
026: * <b>ISVNAuthenticationManager</b> for storing and providing credentials without
027: * using auth providers. A basic manager simply keeps the user credentials provided.
028: * Also this manager may store a single proxy server options context (for HHTP requests
029: * to go through a particular proxy server).
030: *
031: * <p>
032: * This manager does not use authentication providers (<b>ISVNAuthenticationProvider</b>) but only
033: * those credentials that was supplied to its constructor. Also this manager never
034: * caches credentials.
035: *
036: * <p>
037: * This manager is not used in SVNKit internals. You may use a default
038: * manager (how to get it read javadoc for {@link ISVNAuthenticationManager}),
039: * this basic manager or implement your own one.
040: *
041: * @version 1.1.1
042: * @author TMate Software Ltd.
043: * @see ISVNAuthenticationProvider
044: */
045: public class BasicAuthenticationManager implements
046: ISVNAuthenticationManager, ISVNProxyManager {
047:
048: private List myPasswordAuthentications;
049: private List mySSHAuthentications;
050: private List myUserNameAuthentications;
051: private int mySSHIndex;
052: private int myPasswordIndex;
053: private int myUserNameIndex;
054:
055: private String myProxyHost;
056: private int myProxyPort;
057: private String myProxyUserName;
058: private String myProxyPassword;
059: private boolean myIsAuthenticationForced;
060:
061: /**
062: * Creates an auth manager given a user credential - a username
063: * and password.
064: *
065: * @param userName a username
066: * @param password a password
067: */
068: public BasicAuthenticationManager(String userName, String password) {
069: setAuthentications(new SVNAuthentication[] {
070: new SVNPasswordAuthentication(userName, password, false),
071: new SVNSSHAuthentication(userName, password, -1, false),
072: new SVNUserNameAuthentication(userName, false), });
073: }
074:
075: /**
076: * Creates an auth manager given a user credential - a username and
077: * an ssh private key.
078: *
079: * @param userName a username
080: * @param keyFile a private key file
081: * @param passphrase a password to the private key
082: * @param portNumber a port number over which an ssh tunnel is established
083: */
084: public BasicAuthenticationManager(String userName, File keyFile,
085: String passphrase, int portNumber) {
086: setAuthentications(new SVNAuthentication[] {
087: new SVNSSHAuthentication(userName, keyFile, passphrase,
088: portNumber, false),
089: new SVNUserNameAuthentication(userName, false), });
090: }
091:
092: /**
093: * Creates an auth manager given user credentials to use.
094: *
095: * @param authentications user credentials
096: */
097: public BasicAuthenticationManager(
098: SVNAuthentication[] authentications) {
099: setAuthentications(authentications);
100: }
101:
102: /**
103: * Sets the given user credentials to this manager.
104: *
105: * @param authentications user credentials
106: */
107: public void setAuthentications(SVNAuthentication[] authentications) {
108: myPasswordAuthentications = new ArrayList();
109: mySSHAuthentications = new ArrayList();
110: myUserNameAuthentications = new ArrayList();
111: myPasswordIndex = 0;
112: mySSHIndex = 0;
113: for (int i = 0; authentications != null
114: && i < authentications.length; i++) {
115: SVNAuthentication auth = authentications[i];
116: if (auth instanceof SVNPasswordAuthentication) {
117: myPasswordAuthentications.add(auth);
118: } else if (auth instanceof SVNSSHAuthentication) {
119: mySSHAuthentications.add(auth);
120: } else if (auth instanceof SVNUserNameAuthentication) {
121: myUserNameAuthentications.add(auth);
122: }
123: }
124: }
125:
126: /**
127: * Sets a proxy server context to this manager.
128: *
129: * @param proxyHost a proxy server hostname
130: * @param proxyPort a proxy server port
131: * @param proxyUserName a username to supply to a proxy machine
132: * @param proxyPassword a password to supply to a proxy machine
133: */
134: public void setProxy(String proxyHost, int proxyPort,
135: String proxyUserName, String proxyPassword) {
136: myProxyHost = proxyHost;
137: myProxyPort = proxyPort >= 0 ? proxyPort : 3128;
138: myProxyUserName = proxyUserName;
139: myProxyPassword = proxyPassword;
140: }
141:
142: public SVNAuthentication getFirstAuthentication(String kind,
143: String realm, SVNURL url) throws SVNException {
144: if (ISVNAuthenticationManager.SSH.equals(kind)
145: && mySSHAuthentications.size() > 0) {
146: mySSHIndex = 0;
147: return (SVNAuthentication) mySSHAuthentications.get(0);
148: } else if (ISVNAuthenticationManager.PASSWORD.equals(kind)
149: && myPasswordAuthentications.size() > 0) {
150: myPasswordIndex = 0;
151: return (SVNAuthentication) myPasswordAuthentications.get(0);
152: } else if (ISVNAuthenticationManager.USERNAME.equals(kind)
153: && myUserNameAuthentications.size() > 0) {
154: myUserNameIndex = 0;
155: return (SVNAuthentication) myUserNameAuthentications.get(0);
156: }
157: if (ISVNAuthenticationManager.USERNAME.equals(kind)) {
158: if (url.getUserInfo() != null
159: && !"".equals(url.getUserInfo())) {
160: return new SVNUserNameAuthentication(url.getUserInfo(),
161: false);
162: }
163: // client will use default.
164: return new SVNUserNameAuthentication(null, false);
165: }
166: SVNErrorManager.authenticationFailed(
167: "Authentication required for ''{0}''", realm);
168: return null;
169: }
170:
171: public SVNAuthentication getNextAuthentication(String kind,
172: String realm, SVNURL url) throws SVNException {
173: if (ISVNAuthenticationManager.SSH.equals(kind)
174: && mySSHIndex + 1 < mySSHAuthentications.size()) {
175: mySSHIndex++;
176: return (SVNAuthentication) mySSHAuthentications
177: .get(mySSHIndex);
178: } else if (ISVNAuthenticationManager.PASSWORD.equals(kind)
179: && myPasswordIndex + 1 < myPasswordAuthentications
180: .size()) {
181: myPasswordIndex++;
182: return (SVNAuthentication) myPasswordAuthentications
183: .get(myPasswordIndex);
184: } else if (ISVNAuthenticationManager.USERNAME.equals(kind)
185: && myUserNameIndex + 1 < myUserNameAuthentications
186: .size()) {
187: myUserNameIndex++;
188: return (SVNAuthentication) myUserNameAuthentications
189: .get(myUserNameIndex);
190: }
191: SVNErrorManager.authenticationFailed(
192: "Authentication required for ''{0}''", realm);
193: return null;
194: }
195:
196: /**
197: * Does nothing.
198: *
199: * @param provider
200: */
201: public void setAuthenticationProvider(
202: ISVNAuthenticationProvider provider) {
203: }
204:
205: /**
206: * Returns itself as a proxy manager.
207: *
208: * @param url a repository location that will be accessed
209: * over the proxy server for which a manager is needed
210: * @return a proxy manager
211: * @throws SVNException
212: */
213: public ISVNProxyManager getProxyManager(SVNURL url)
214: throws SVNException {
215: return this ;
216: }
217:
218: /**
219: * Returns <span class="javakeyword">null</span>.
220: *
221: * @param url
222: * @return <span class="javakeyword">null</span>
223: * @throws SVNException
224: */
225: public ISVNSSLManager getSSLManager(SVNURL url) throws SVNException {
226: return null;
227: }
228:
229: /**
230: * Does nothing.
231: *
232: * @param accepted
233: * @param kind
234: * @param realm
235: * @param errorMessage
236: * @param authentication
237: */
238: public void acknowledgeAuthentication(boolean accepted,
239: String kind, String realm, SVNErrorMessage errorMessage,
240: SVNAuthentication authentication) {
241: }
242:
243: /**
244: * Does nothing.
245: *
246: * @param storage
247: */
248: public void setRuntimeStorage(ISVNAuthenticationStorage storage) {
249: }
250:
251: public boolean isAuthenticationForced() {
252: return myIsAuthenticationForced;
253: }
254:
255: public void setAuthenticationForced(boolean forced) {
256: myIsAuthenticationForced = forced;
257: }
258:
259: public String getProxyHost() {
260: return myProxyHost;
261: }
262:
263: public int getProxyPort() {
264: return myProxyPort;
265: }
266:
267: public String getProxyUserName() {
268: return myProxyUserName;
269: }
270:
271: public String getProxyPassword() {
272: return myProxyPassword;
273: }
274:
275: /**
276: * Does nothing.
277: *
278: * @param accepted
279: * @param errorMessage
280: */
281: public void acknowledgeProxyContext(boolean accepted,
282: SVNErrorMessage errorMessage) {
283: }
284:
285: public long getHTTPTimeout(SVNRepository repository) {
286: return 3600 * 1000;
287: }
288:
289: }
|