Search in sources :

Example 1 with UserAuthenticator

use of org.apache.commons.vfs2.UserAuthenticator in project artisynth_core by artisynth.

the class EncryptedUserAuthenticator method equals.

public boolean equals(UserAuthenticator obj) {
    UserAuthenticationData data = obj.requestAuthentication(ALL_AUTH_DATA);
    String str = new String(data.getData(UserAuthenticationData.DOMAIN));
    if (!equals(str, domain)) {
        return false;
    }
    str = new String(data.getData(UserAuthenticationData.USERNAME));
    if (!equals(str, username)) {
        return false;
    }
    str = new String(data.getData(UserAuthenticationData.PASSWORD));
    try {
        // encrypt password
        str = getCryptor().encrypt(str);
        if (!equals(str, encryptedPassword)) {
            return false;
        }
    } catch (Exception e) {
    }
    return true;
}
Also used : UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData)

Example 2 with UserAuthenticator

use of org.apache.commons.vfs2.UserAuthenticator in project hop by apache.

the class S3NFileSystemTest method testGetS3Service.

@Test
public void testGetS3Service() throws Exception {
    assertNotNull(fileSystem.getS3Client());
    FileSystemOptions options = new FileSystemOptions();
    UserAuthenticator authenticator = mock(UserAuthenticator.class);
    UserAuthenticationData authData = mock(UserAuthenticationData.class);
    when(authenticator.requestAuthentication(S3FileProvider.AUTHENTICATOR_TYPES)).thenReturn(authData);
    when(authData.getData(UserAuthenticationData.USERNAME)).thenReturn("username".toCharArray());
    when(authData.getData(UserAuthenticationData.PASSWORD)).thenReturn("password".toCharArray());
    DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(options, authenticator);
    fileSystem = new S3NFileSystem(fileName, options);
    assertNotNull(fileSystem.getS3Client());
}
Also used : S3NFileSystem(org.apache.hop.vfs.s3.s3n.vfs.S3NFileSystem) UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData) UserAuthenticator(org.apache.commons.vfs2.UserAuthenticator) FileSystemOptions(org.apache.commons.vfs2.FileSystemOptions) Test(org.junit.Test)

Example 3 with UserAuthenticator

use of org.apache.commons.vfs2.UserAuthenticator in project commons-vfs by apache.

the class StaticUserAuthenticatorTest method testEquality.

@Test
public void testEquality() {
    final UserAuthenticator userAuthenticator = new StaticUserAuthenticator("DOMAIN", "USER", "PWD");
    assertEquals(new StaticUserAuthenticator("DOMAIN", "USER", "PWD"), userAuthenticator);
    assertNotEquals(new StaticUserAuthenticator("DOMAIN", "USER", null), userAuthenticator);
    assertNotEquals(new StaticUserAuthenticator("DOMAIN", null, "PWD"), userAuthenticator);
    assertNotEquals(new StaticUserAuthenticator(null, "USER", "PWD"), userAuthenticator);
    assertEquals(new StaticUserAuthenticator("DOMAIN", "USER", "PWD").hashCode(), userAuthenticator.hashCode());
}
Also used : UserAuthenticator(org.apache.commons.vfs2.UserAuthenticator) Test(org.junit.jupiter.api.Test)

Example 4 with UserAuthenticator

use of org.apache.commons.vfs2.UserAuthenticator in project commons-vfs by apache.

the class HttpClientFactory method createConnection.

/**
 * Creates a new connection to the server.
 *
 * @param builder The HttpFileSystemConfigBuilder.
 * @param scheme The protocol.
 * @param hostname The hostname.
 * @param port The port number.
 * @param username The username.
 * @param password The password
 * @param fileSystemOptions The file system options.
 * @return a new HttpClient connection.
 * @throws FileSystemException if an error occurs.
 * @since 2.0
 */
public static HttpClient createConnection(final HttpFileSystemConfigBuilder builder, final String scheme, final String hostname, final int port, final String username, final String password, final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final HttpClient client;
    try {
        final HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
        final HttpConnectionManagerParams connectionMgrParams = mgr.getParams();
        client = new HttpClient(mgr);
        final HostConfiguration config = new HostConfiguration();
        config.setHost(hostname, port, scheme);
        if (fileSystemOptions != null) {
            final String proxyHost = builder.getProxyHost(fileSystemOptions);
            final int proxyPort = builder.getProxyPort(fileSystemOptions);
            if (!StringUtils.isEmpty(proxyHost) && proxyPort > 0) {
                config.setProxy(proxyHost, proxyPort);
            }
            final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
            if (proxyAuth != null) {
                final UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth, new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD });
                if (authData != null) {
                    final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)), UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null)));
                    final AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
                    client.getState().setProxyCredentials(scope, proxyCreds);
                }
                if (builder.isPreemptiveAuth(fileSystemOptions)) {
                    final HttpClientParams httpClientParams = new HttpClientParams();
                    httpClientParams.setAuthenticationPreemptive(true);
                    client.setParams(httpClientParams);
                }
            }
            final Cookie[] cookies = builder.getCookies(fileSystemOptions);
            if (cookies != null) {
                client.getState().addCookies(cookies);
            }
        }
        /*
             * ConnectionManager set methods must be called after the host & port and proxy host & port are set in the
             * HostConfiguration. They are all used as part of the key when HttpConnectionManagerParams tries to locate
             * the host configuration.
             */
        connectionMgrParams.setMaxConnectionsPerHost(config, builder.getMaxConnectionsPerHost(fileSystemOptions));
        connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));
        connectionMgrParams.setConnectionTimeout(DurationUtils.toMillisInt(builder.getConnectionTimeoutDuration(fileSystemOptions)));
        connectionMgrParams.setSoTimeout(DurationUtils.toMillisInt(builder.getSoTimeoutDuration(fileSystemOptions)));
        client.setHostConfiguration(config);
        if (username != null) {
            final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
            final AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
            client.getState().setCredentials(scope, creds);
        }
    } catch (final Exception exc) {
        throw new FileSystemException("vfs.provider.http/connect.error", exc, hostname);
    }
    return client;
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) UserAuthenticator(org.apache.commons.vfs2.UserAuthenticator) FileSystemException(org.apache.commons.vfs2.FileSystemException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData) FileSystemException(org.apache.commons.vfs2.FileSystemException) HttpConnectionManagerParams(org.apache.commons.httpclient.params.HttpConnectionManagerParams) HttpClient(org.apache.commons.httpclient.HttpClient) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) AuthScope(org.apache.commons.httpclient.auth.AuthScope) HttpClientParams(org.apache.commons.httpclient.params.HttpClientParams) HttpConnectionManager(org.apache.commons.httpclient.HttpConnectionManager) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)

Example 5 with UserAuthenticator

use of org.apache.commons.vfs2.UserAuthenticator in project commons-vfs by apache.

the class Http4FileProvider method createHttpClientContext.

/**
 * Create an {@link HttpClientContext} object for an http4 file system.
 *
 * @param builder Configuration options builder for http4 provider
 * @param rootName The root path
 * @param fileSystemOptions The FileSystem options
 * @param authData The {@code UserAuthentiationData} object
 * @return an {@link HttpClientContext} object
 */
protected HttpClientContext createHttpClientContext(final Http4FileSystemConfigBuilder builder, final GenericFileName rootName, final FileSystemOptions fileSystemOptions, final UserAuthenticationData authData) {
    final HttpClientContext clientContext = HttpClientContext.create();
    final CredentialsProvider credsProvider = new BasicCredentialsProvider();
    clientContext.setCredentialsProvider(credsProvider);
    final String username = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())));
    final String password = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())));
    if (!StringUtils.isEmpty(username)) {
        credsProvider.setCredentials(new AuthScope(rootName.getHostName(), rootName.getPort()), new UsernamePasswordCredentials(username, password));
    }
    final HttpHost proxyHost = getProxyHttpHost(builder, fileSystemOptions);
    if (proxyHost != null) {
        final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
        if (proxyAuth != null) {
            final UserAuthenticationData proxyAuthData = UserAuthenticatorUtils.authenticate(proxyAuth, new UserAuthenticationData.Type[] { UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD });
            if (proxyAuthData != null) {
                final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(proxyAuthData, UserAuthenticationData.USERNAME, null)), UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(proxyAuthData, UserAuthenticationData.PASSWORD, null)));
                credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), proxyCreds);
            }
            if (builder.isPreemptiveAuth(fileSystemOptions)) {
                final AuthCache authCache = new BasicAuthCache();
                final BasicScheme basicAuth = new BasicScheme();
                authCache.put(proxyHost, basicAuth);
                clientContext.setAuthCache(authCache);
            }
        }
    }
    return clientContext;
}
Also used : UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) UserAuthenticator(org.apache.commons.vfs2.UserAuthenticator) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

UserAuthenticator (org.apache.commons.vfs2.UserAuthenticator)9 FileSystemOptions (org.apache.commons.vfs2.FileSystemOptions)6 UserAuthenticationData (org.apache.commons.vfs2.UserAuthenticationData)6 StaticUserAuthenticator (org.apache.commons.vfs2.auth.StaticUserAuthenticator)4 FileSystemException (org.apache.commons.vfs2.FileSystemException)3 Test (org.junit.Test)3 AWSCredentials (com.amazonaws.auth.AWSCredentials)2 Test (org.junit.jupiter.api.Test)2 SdkClientException (com.amazonaws.SdkClientException)1 File (java.io.File)1 Cookie (org.apache.commons.httpclient.Cookie)1 HostConfiguration (org.apache.commons.httpclient.HostConfiguration)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 HttpConnectionManager (org.apache.commons.httpclient.HttpConnectionManager)1 MultiThreadedHttpConnectionManager (org.apache.commons.httpclient.MultiThreadedHttpConnectionManager)1 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)1 AuthScope (org.apache.commons.httpclient.auth.AuthScope)1 HttpClientParams (org.apache.commons.httpclient.params.HttpClientParams)1 HttpConnectionManagerParams (org.apache.commons.httpclient.params.HttpConnectionManagerParams)1 LocalFile (org.apache.commons.vfs2.provider.local.LocalFile)1