commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject svn commit: r417178 - in /jakarta/commons/proper/vfs/trunk: src/java/org/apache/commons/vfs/ src/java/org/apache/commons/vfs/auth/ src/java/org/apache/commons/vfs/impl/ src/java/org/apache/commons/vfs/provider/ftp/ src/java/org/apache/commons/vfs/provi...
Date Mon, 26 Jun 2006 12:31:42 GMT
Author: imario
Date: Mon Jun 26 05:31:41 2006
New Revision: 417178

URL: http://svn.apache.org/viewvc?rev=417178&view=rev
Log:
hackaton - UserAuthenticator: callback to get user credentials. With this you can avoid putting
credentials into the url.

Added:
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticationData.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticator.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/auth/
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemConfigBuilder.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/util/UserAuthenticatorUtils.java
Modified:
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystemConfigBuilder.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileProvider.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavClientFactory.java
    jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileProvider.java
    jakarta/commons/proper/vfs/trunk/xdocs/api.xml

Added: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticationData.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticationData.java?rev=417178&view=auto
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticationData.java
(added)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticationData.java
Mon Jun 26 05:31:41 2006
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs;
+
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.Iterator;
+
+/**
+ * Container for various authentication data
+ */
+public class UserAuthenticationData
+{
+	public static class Type implements Comparable
+	{
+		private final String type;
+
+		public Type(String type)
+		{
+			this.type = type;
+		}
+
+		public boolean equals(Object o)
+		{
+			if (this == o)
+			{
+				return true;
+			}
+			if (o == null || getClass() != o.getClass())
+			{
+				return false;
+			}
+
+			Type type1 = (Type) o;
+
+			if (type != null ? !type.equals(type1.type) : type1.type != null)
+			{
+				return false;
+			}
+
+			return true;
+		}
+
+		public int compareTo(Object o)
+		{
+			Type t = (Type) o;
+
+			return type.compareTo(t.type);
+		}
+	}
+
+	public static final Type USERNAME = new Type("username");
+	public static final Type PASSWORD = new Type("password");
+	public static final Type DOMAIN = new Type("domain");
+
+	private Map authenticationData = new TreeMap();
+
+	public UserAuthenticationData()
+	{
+	}
+
+	/**
+	 * set a data to this collection
+	 */
+	public void setData(Type type, char[] data)
+	{
+		authenticationData.put(type, data);
+	}
+
+	/**
+	 * get a data from the collection
+	 */
+	public char[] getData(Type type)
+	{
+		return (char[]) authenticationData.get(type);
+	}
+
+	/**
+	 * deleted all data stored within this authenticator
+	 */
+	public void cleanup()
+	{
+		// step 1: nullify character buffers
+		Iterator iterAuthenticationData = authenticationData.values().iterator();
+		while (iterAuthenticationData.hasNext())
+		{
+			char[] data = (char[]) iterAuthenticationData.next();
+			if (data == null || data.length < 0)
+			{
+				continue;
+			}
+
+			for (int i = 0; i<data.length; i++)
+			{
+				data[i]=0;
+			}
+		}
+		// step 2: allow data itself to gc
+		authenticationData.clear();
+	}
+}

Added: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticator.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticator.java?rev=417178&view=auto
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticator.java
(added)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/UserAuthenticator.java
Mon Jun 26 05:31:41 2006
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs;
+
+/**
+ * The user authenticator is used to query credentials from the user
+ */
+public interface UserAuthenticator
+{
+	/**
+	 * queries the given type from the user
+	 */
+	public UserAuthenticationData requestAuthentication(UserAuthenticationData.Type[] types);
+}

Added: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java?rev=417178&view=auto
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java
(added)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/auth/StaticUserAuthenticator.java
Mon Jun 26 05:31:41 2006
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.auth;
+
+import org.apache.commons.vfs.UserAuthenticationData;
+import org.apache.commons.vfs.UserAuthenticator;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
+
+/**
+ * provides always the same credential data passed in with the constructor.
+ */
+public class StaticUserAuthenticator implements UserAuthenticator
+{
+	private final String username;
+	private final String password;
+	private final String domain;
+
+	public StaticUserAuthenticator(String domain, String username, String password)
+	{
+		this.username = username;
+		this.password = password;
+		this.domain = domain;
+	}
+
+	public UserAuthenticationData requestAuthentication(UserAuthenticationData.Type[] types)
+	{
+		UserAuthenticationData data = new UserAuthenticationData();
+		data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain));
+		data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(username));
+		data.setData(UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(password));
+		return data;
+	}
+}

Added: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemConfigBuilder.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemConfigBuilder.java?rev=417178&view=auto
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemConfigBuilder.java
(added)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/impl/DefaultFileSystemConfigBuilder.java
Mon Jun 26 05:31:41 2006
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.impl;
+
+import org.apache.commons.vfs.FileSystemConfigBuilder;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.UserAuthenticator;
+
+/**
+ * Default options usable for all filesystems
+ */
+public class DefaultFileSystemConfigBuilder extends FileSystemConfigBuilder
+{
+	private final static DefaultFileSystemConfigBuilder builder = new DefaultFileSystemConfigBuilder();
+
+	public static DefaultFileSystemConfigBuilder getInstance()
+	{
+		return builder;
+	}
+
+	/**
+	 * Sets the user authenticator to get authentication informations
+	 */
+	public void setUserAuthenticator(FileSystemOptions opts, UserAuthenticator userAuthenticator)
throws FileSystemException
+	{
+		setParam(opts, "userAuthenticator", userAuthenticator);
+	}
+
+	/**
+	 * @see #setUserAuthenticator
+	 */
+	public UserAuthenticator getUserAuthenticator(FileSystemOptions opts)
+	{
+		return (UserAuthenticator) getParam(opts, "userAuthenticator");
+	}
+
+	protected Class getConfigClass()
+	{
+		return DefaultFileSystemConfigBuilder.class;
+	}
+}

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java
Mon Jun 26 05:31:41 2006
@@ -19,7 +19,10 @@
 import org.apache.commons.net.ftp.FTPFile;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.UserAuthenticationData;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 import org.apache.commons.vfs.provider.GenericFileName;
+import org.apache.commons.vfs.provider.sftp.SftpFileProvider;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -57,15 +60,25 @@
     {
         final GenericFileName rootName = getRoot();
 
-        return FtpClientFactory.createConnection(rootName.getHostName(),
-            rootName.getPort(),
-            rootName.getUserName(),
-            rootName.getPassword(),
-            rootName.getPath(),
-            getFileSystemOptions());
-    }
+		UserAuthenticationData authData = null;
+		try
+		{
+			authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, FtpFileProvider.AUTHENTICATOR_TYPES);
+
+			return FtpClientFactory.createConnection(rootName.getHostName(),
+				rootName.getPort(),
+				UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())),
+				UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())),
+				rootName.getPath(),
+				getFileSystemOptions());
+		}
+		finally
+		{
+			UserAuthenticatorUtils.cleanup(authData);
+		}
+	}
 
-    private FTPClient getFtpClient() throws FileSystemException
+	private FTPClient getFtpClient() throws FileSystemException
     {
         if (ftpClient == null)
         {

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClientFactory.java
Mon Jun 26 05:31:41 2006
@@ -22,6 +22,7 @@
 import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 
 import java.io.IOException;
 
@@ -40,17 +41,17 @@
     /**
      * Creates a new connection to the server.
      */
-    public static FTPClient createConnection(String hostname, int port, String username,
String password, String workingDirectory, FileSystemOptions fileSystemOptions) throws FileSystemException
+    public static FTPClient createConnection(String hostname, int port, char[] username,
char[] password, String workingDirectory, FileSystemOptions fileSystemOptions) throws FileSystemException
     {
         // Determine the username and password to use
         if (username == null)
         {
-            username = "anonymous";
+            username = "anonymous".toCharArray();
         }
 
         if (password == null)
         {
-            password = "anonymous";
+            password = "anonymous".toCharArray();
         }
 
         try
@@ -117,7 +118,9 @@
                 }
 
                 // Login
-                if (!client.login(username, password))
+                if (!client.login(
+					UserAuthenticatorUtils.toString(username),
+					UserAuthenticatorUtils.toString(password)))
                 {
                     throw new FileSystemException("vfs.provider.ftp/login.error", new Object[]{hostname,
username}, null);
                 }

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java
Mon Jun 26 05:31:41 2006
@@ -21,6 +21,7 @@
 import org.apache.commons.vfs.FileSystemConfigBuilder;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.UserAuthenticationData;
 import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
 import org.apache.commons.vfs.provider.GenericFileName;
 
@@ -54,7 +55,12 @@
         Capability.RANDOM_ACCESS_READ,
     }));
 
-    public FtpFileProvider()
+	public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
+		{
+			UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
+		};
+
+	public FtpFileProvider()
     {
         super();
         setFileNameParser(FtpFileNameParser.getInstance());

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java
Mon Jun 26 05:31:41 2006
@@ -22,8 +22,10 @@
 import org.apache.commons.vfs.FileSystemConfigBuilder;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.UserAuthenticationData;
 import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
 import org.apache.commons.vfs.provider.GenericFileName;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -49,7 +51,12 @@
         Capability.RANDOM_ACCESS_READ
     }));
 
-    public HttpFileProvider()
+	public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
+		{
+			UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
+		};
+
+	public HttpFileProvider()
     {
         super();
         setFileNameParser(HttpFileNameParser.getInstance());
@@ -64,13 +71,24 @@
         // Create the file system
         final GenericFileName rootName = (GenericFileName) name;
 
-        HttpClient httpClient = HttpClientFactory.createConnection(rootName.getHostName(),
-            rootName.getPort(),
-            rootName.getUserName(),
-            rootName.getPassword(),
-            fileSystemOptions);
+		UserAuthenticationData authData = null;
+		HttpClient httpClient;
+		try
+		{
+			authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
+
+			httpClient = HttpClientFactory.createConnection(rootName.getHostName(),
+				rootName.getPort(),
+				UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
UserAuthenticatorUtils.toChar(rootName.getUserName()))),
+				UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME,
UserAuthenticatorUtils.toChar(rootName.getPassword()))),
+				fileSystemOptions);
+		}
+		finally
+		{
+			UserAuthenticatorUtils.cleanup(authData);
+		}
 
-        return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
+		return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
     }
 
     public FileSystemConfigBuilder getConfigBuilder()

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java
Mon Jun 26 05:31:41 2006
@@ -43,7 +43,7 @@
     /**
      * Creates a new connection to the server.
      */
-    public static Session createConnection(String hostname, int port, String username, String
password, FileSystemOptions fileSystemOptions) throws FileSystemException
+    public static Session createConnection(String hostname, int port, char[] username, char[]
password, FileSystemOptions fileSystemOptions) throws FileSystemException
     {
         JSch jsch = new JSch();
 
@@ -125,10 +125,10 @@
         Session session;
         try
         {
-            session = jsch.getSession(username,
+            session = jsch.getSession(new String(username),
                     hostname,
                     port);
-            session.setPassword(password);
+            session.setPassword(new String(password));
 
             Integer timeout = SftpFileSystemConfigBuilder.getInstance().getTimeout(fileSystemOptions);
             if (timeout != null)

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java
Mon Jun 26 05:31:41 2006
@@ -22,6 +22,8 @@
 import org.apache.commons.vfs.FileSystemConfigBuilder;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.UserAuthenticationData;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
 import org.apache.commons.vfs.provider.GenericFileName;
 
@@ -54,8 +56,13 @@
     }));
 
     public final static String ATTR_USER_INFO = "UI";
+	
+	public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
+		{
+			UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
+		};
 
-    // private JSch jSch = new JSch();
+	// private JSch jSch = new JSch();
 
     public SftpFileProvider()
     {
@@ -74,13 +81,17 @@
         final GenericFileName rootName = (GenericFileName) name;
 
         Session session;
+		UserAuthenticationData authData = null;
         try
         {
-            session = SftpClientFactory.createConnection(rootName.getHostName(),
-                rootName.getPort(),
-                rootName.getUserName(),
-                rootName.getPassword(),
-                fileSystemOptions);
+			authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
+
+			session = SftpClientFactory.createConnection(
+				rootName.getHostName(),
+				rootName.getPort(),
+				UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())),
+				UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())),
+				fileSystemOptions);
         }
         catch (final Exception e)
         {
@@ -88,8 +99,12 @@
                 name,
                 e);
         }
+		finally
+		{
+			UserAuthenticatorUtils.cleanup(authData);
+		}
 
-        return new SftpFileSystem(rootName, session, fileSystemOptions);
+		return new SftpFileSystem(rootName, session, fileSystemOptions);
     }
 
 

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java
Mon Jun 26 05:31:41 2006
@@ -24,6 +24,8 @@
 import org.apache.commons.vfs.FileSystem;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.UserAuthenticationData;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 import org.apache.commons.vfs.provider.AbstractFileSystem;
 import org.apache.commons.vfs.provider.GenericFileName;
 
@@ -40,7 +42,8 @@
     extends AbstractFileSystem
     implements FileSystem
 {
-    private Session session;
+
+	private Session session;
     // private final JSch jSch;
     private ChannelSftp idleChannel;
 
@@ -76,15 +79,19 @@
         {
             // channel closed. e.g. by freeUnusedResources, but now we need it again
             Session session;
-            try
+			UserAuthenticationData authData = null;
+			try
             {
                 final GenericFileName rootName = (GenericFileName) getRootName();
 
-                session = SftpClientFactory.createConnection(rootName.getHostName(),
-                    rootName.getPort(),
-                    rootName.getUserName(),
-                    rootName.getPassword(),
-                    getFileSystemOptions());
+				authData = UserAuthenticatorUtils.authenticate(getFileSystemOptions(), SftpFileProvider.AUTHENTICATOR_TYPES);
+
+				session = SftpClientFactory.createConnection(
+					rootName.getHostName(),
+					rootName.getPort(),
+					UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())),
+					UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())),
+					getFileSystemOptions());
             }
             catch (final Exception e)
             {
@@ -92,8 +99,12 @@
                     getRootName(),
                     e);
             }
+			finally
+			{
+				UserAuthenticatorUtils.cleanup(authData);
+			}
 
-            this.session = session;
+			this.session = session;
         }
 
         try

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystemConfigBuilder.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystemConfigBuilder.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystemConfigBuilder.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystemConfigBuilder.java
Mon Jun 26 05:31:41 2006
@@ -183,7 +183,7 @@
      * set the timeout value on jsch session
      *
      * @param opts
-     * @param userDirIsRoot
+     * @param timeout
      */
     public void setTimeout(FileSystemOptions opts, Integer timeout)
     {

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
Mon Jun 26 05:31:41 2006
@@ -25,9 +25,11 @@
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileType;
 import org.apache.commons.vfs.RandomAccessContent;
+import org.apache.commons.vfs.UserAuthenticationData;
 import org.apache.commons.vfs.provider.AbstractFileObject;
 import org.apache.commons.vfs.provider.UriParser;
 import org.apache.commons.vfs.util.RandomAccessMode;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -77,16 +79,43 @@
 
         String path = smbFileName.getUriWithoutAuth();
 
-        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
-            smbFileName.getDomain(), smbFileName.getUserName(), smbFileName.getPassword());
-        SmbFile file = new SmbFile(path, auth);
+		UserAuthenticationData authData = null;
+		SmbFile file;
+		NtlmPasswordAuthentication auth;
+		try
+		{
+			authData = UserAuthenticatorUtils.authenticate(getFileSystem().getFileSystemOptions(),
SmbFileProvider.AUTHENTICATOR_TYPES);
+
+			auth = new NtlmPasswordAuthentication(
+				UserAuthenticatorUtils.toString(
+					UserAuthenticatorUtils.getData(
+						authData,
+						UserAuthenticationData.DOMAIN,
+						UserAuthenticatorUtils.toChar(smbFileName.getDomain()))),
+				UserAuthenticatorUtils.toString(
+					UserAuthenticatorUtils.getData(
+						authData,
+						UserAuthenticationData.USERNAME,
+						UserAuthenticatorUtils.toChar(smbFileName.getUserName()))),
+				UserAuthenticatorUtils.toString(
+					UserAuthenticatorUtils.getData(
+						authData,
+						UserAuthenticationData.USERNAME,
+						UserAuthenticatorUtils.toChar(smbFileName.getPassword()))));
+
+			file = new SmbFile(path, auth);
+		}
+		finally
+		{
+			UserAuthenticatorUtils.cleanup(authData);
+		}
+
+		if (file.isDirectory() && !file.toString().endsWith("/"))
+		{
+			file = new SmbFile(path + "/", auth);
+		}
 
-        if (file.isDirectory() && !file.toString().endsWith("/"))
-        {
-            file = new SmbFile(path + "/", auth);
-        }
-
-        return file;
+		return file;
     }
 
     /**

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileProvider.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileProvider.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileProvider.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileProvider.java
Mon Jun 26 05:31:41 2006
@@ -20,6 +20,7 @@
 import org.apache.commons.vfs.FileSystem;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.UserAuthenticationData;
 import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
 import org.apache.commons.vfs.provider.FileProvider;
 
@@ -53,7 +54,12 @@
         Capability.RANDOM_ACCESS_WRITE
     }));
 
-    public SmbFileProvider()
+	public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
+		{
+			UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD, UserAuthenticationData.DOMAIN
+		};
+
+	public SmbFileProvider()
     {
         super();
         setFileNameParser(SmbFileNameParser.getInstance());

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavClientFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavClientFactory.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavClientFactory.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavClientFactory.java
Mon Jun 26 05:31:41 2006
@@ -19,6 +19,7 @@
 import org.apache.commons.httpclient.HttpURL;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 import org.apache.webdav.lib.WebdavResource;
 
 import java.io.IOException;
@@ -38,14 +39,15 @@
     /**
      * Creates a new connection to the server.
      */
-    public static HttpClient createConnection(String hostname, int port, String username,
String password, FileSystemOptions fileSystemOptions) throws FileSystemException
+    public static HttpClient createConnection(String hostname, int port, char[] username,
char[] password, FileSystemOptions fileSystemOptions) throws FileSystemException
     {
         // Create an Http client
         HttpClient client;
         try
         {
-            final HttpURL url = new HttpURL(username,
-                password,
+            final HttpURL url = new HttpURL(
+				UserAuthenticatorUtils.toString(username),
+                UserAuthenticatorUtils.toString(password),
                 hostname,
                 port,
                 "/");

Modified: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileProvider.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileProvider.java?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileProvider.java
(original)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileProvider.java
Mon Jun 26 05:31:41 2006
@@ -21,6 +21,8 @@
 import org.apache.commons.vfs.FileSystem;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.UserAuthenticationData;
+import org.apache.commons.vfs.util.UserAuthenticatorUtils;
 import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
 import org.apache.commons.vfs.provider.GenericFileName;
 import org.apache.commons.vfs.provider.http.HttpFileNameParser;
@@ -53,7 +55,12 @@
         Capability.RANDOM_ACCESS_READ
     }));
 
-    public WebdavFileProvider()
+	public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
+		{
+			UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
+		};
+
+	public WebdavFileProvider()
     {
         super();
 
@@ -69,13 +76,25 @@
         // Create the file system
         final GenericFileName rootName = (GenericFileName) name;
 
-        HttpClient httpClient = WebdavClientFactory.createConnection(rootName.getHostName(),
-            rootName.getPort(),
-            rootName.getUserName(),
-            rootName.getPassword(),
-            fileSystemOptions);
+		UserAuthenticationData authData = null;
+		HttpClient httpClient;
+		try
+		{
+			authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
+
+			httpClient = WebdavClientFactory.createConnection(
+				rootName.getHostName(),
+				rootName.getPort(),
+				UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())),
+				UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getPassword())),
+				fileSystemOptions);
+		}
+		finally
+		{
+			UserAuthenticatorUtils.cleanup(authData);
+		}
 
-        return new WebDavFileSystem(rootName, httpClient, fileSystemOptions);
+		return new WebDavFileSystem(rootName, httpClient, fileSystemOptions);
     }
 
     public Collection getCapabilities()

Added: jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/util/UserAuthenticatorUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/util/UserAuthenticatorUtils.java?rev=417178&view=auto
==============================================================================
--- jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/util/UserAuthenticatorUtils.java
(added)
+++ jakarta/commons/proper/vfs/trunk/src/java/org/apache/commons/vfs/util/UserAuthenticatorUtils.java
Mon Jun 26 05:31:41 2006
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.util;
+
+import org.apache.commons.vfs.UserAuthenticator;
+import org.apache.commons.vfs.UserAuthenticationData;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
+
+/**
+ * some helper
+ */
+public class UserAuthenticatorUtils
+{
+	/**
+	 * gets data of given type from the UserAuthenticationData or null if there is no data or
data of this type available
+	 */
+	public static char[] getData(UserAuthenticationData data, UserAuthenticationData.Type type,
char[] defaultValue)
+	{
+		if (data == null)
+		{
+			return defaultValue;
+		}
+
+		char[] ret = data.getData(type);
+		if (ret == null)
+		{
+			return defaultValue;
+		}
+
+		return ret;
+	}
+
+	/**
+	 * if there is a authenticator the authentication will take place, else null will be reutrned
+	 */
+	public static UserAuthenticationData authenticate(FileSystemOptions opts, UserAuthenticationData.Type[]
authenticatorTypes)
+	{
+		UserAuthenticator auth = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(opts);
+		if (auth == null)
+		{
+			return null;
+		}
+
+		return auth.requestAuthentication(authenticatorTypes);
+	}
+
+	/**
+	 * converts a string to a char array (null safe)
+	 */
+	public static char[] toChar(String string)
+	{
+		if (string == null)
+		{
+			return null;
+		}
+
+		return string.toCharArray();
+	}
+
+	/**
+	 * cleanup the data in the UerAuthenticationData (null safe)
+	 */
+	public static void cleanup(UserAuthenticationData authData)
+	{
+		if (authData == null)
+		{
+			return;
+		}
+
+		authData.cleanup();
+	}
+
+	/**
+	 * converts the given data to a string (null safe)
+	 */
+	public static String toString(char[] data)
+	{
+		if (data == null)
+		{
+			return null;
+		}
+
+		return new String(data);
+	}
+}

Modified: jakarta/commons/proper/vfs/trunk/xdocs/api.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/xdocs/api.xml?rev=417178&r1=417177&r2=417178&view=diff
==============================================================================
--- jakarta/commons/proper/vfs/trunk/xdocs/api.xml (original)
+++ jakarta/commons/proper/vfs/trunk/xdocs/api.xml Mon Jun 26 05:31:41 2006
@@ -69,9 +69,48 @@
                     hold a reference to this object. If the FileObject is no longer reachable,
and the jvm needs some memory,
                     it will be released.
                 </p>
-            </subsection>
+				<p>
+					There is also a internal cache of each file object avoid the need to access the network
layer. Now its possible
+					to configure this behviour through the use of <a href="apidocs/org/apache/commons/vfs/CacheStrategy.html">CacheStrategy</a>.
+					<br />
+					Do this on the DefaultFileSystemManager. For example:
+					<code>
+						((DefaultFileSystemManager) VFS.getManager()).setCacheStrategy(CacheStrategy.ON_CALL)
+					</code>
+				</p>
+			</subsection>
 
-            <subsection name="Examples">
+			<subsection name="User Authentication">
+				<p>
+					You cann put the credentials into th url, but the drawback here is, that it is easily
possible to get access to the password.
+				</p>
+				<p>
+					To solve you can use the <a href="apidocs/org/apache/commons/vfs/UserAuthenticator.html">UserAuthenticator</a>
+				</p>
+				<p>
+					For example:
+					<code>
+						StaticUserAuthenticator auth = new StaticUserAuthenticator("username", "password",
null);
+						FileSystemOptions opts = new FileSystemOptions();
+						DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
+
+						FileObject fo = VFS.getManager().resolveFile("smb://host/anyshare/dir", opts);
+					</code>
+				</p>
+				<p>
+					Internally the UserAuthenticator uses char arrays which will be zeroed before its freed
for garbage collection.
+					Unhappily none of the current libraries use char arrays and so VFS has to create a string.
Thus, the main advantage
+					of this solution - security - is lost, but hey, thats not VFS fault ;-)
+				</p>
+				<p>
+					VFS calls <code>UserAuthenticator.requestAuthentication</code> each time
it requires credentials, it depends on the
+					filesystem implementation how often this might be. For example, with FTP this is on
every connection, in SMB/JCIFS
+					this is for EVERY OBJECT. It is up to you how long you will cache credentials of if
you would like to provide a
+					"save credentials" checkbox.
+				</p>
+			</subsection>
+
+			<subsection name="Examples">
                 <p>
                     For an example of using the API, take a look at the classes
                     in the



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message