Search in sources :

Example 1 with Cryptor

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

the class EncryptedUserAuthenticator method requestAuthentication.

public UserAuthenticationData requestAuthentication(UserAuthenticationData.Type[] types) {
    UserAuthenticationData data = new UserAuthenticationData();
    for (Type type : types) {
        if (type == UserAuthenticationData.DOMAIN) {
            data.setData(UserAuthenticationData.DOMAIN, UserAuthenticatorUtils.toChar(domain));
        } else if (type == UserAuthenticationData.USERNAME) {
            data.setData(UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(username));
        } else if (type == UserAuthenticationData.PASSWORD) {
            try {
                // unfortunately, we have to pass it in plaintext, but the original password
                // could be encrypted from the get-go using the global Cryptor
                String passwd = getCryptor().decrypt(encryptedPassword);
                char[] chars = UserAuthenticatorUtils.toChar(passwd);
                data.setData(UserAuthenticationData.PASSWORD, chars);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return data;
}
Also used : UserAuthenticationData(org.apache.commons.vfs2.UserAuthenticationData) Type(org.apache.commons.vfs2.UserAuthenticationData.Type)

Example 2 with Cryptor

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

the class HostFileNameParser method extractToPath.

/**
 * Extracts the scheme, userinfo, hostname and port components of a generic URI.
 *
 * @param context component context.
 * @param uri The absolute URI to parse.
 * @param name Used to return the remainder of the URI.
 * @return Authority extracted host authority, never null.
 * @throws FileSystemException if authority cannot be extracted.
 */
protected Authority extractToPath(final VfsComponentContext context, final String uri, final StringBuilder name) throws FileSystemException {
    final Authority auth = new Authority();
    final FileSystemManager fsm;
    if (context != null) {
        fsm = context.getFileSystemManager();
    } else {
        fsm = VFS.getManager();
    }
    // Extract the scheme
    auth.scheme = UriParser.extractScheme(fsm.getSchemes(), uri, name);
    // Expecting "//"
    if (name.length() < 2 || name.charAt(0) != '/' || name.charAt(1) != '/') {
        throw new FileSystemException("vfs.provider/missing-double-slashes.error", uri);
    }
    name.delete(0, 2);
    // Extract userinfo, and split into username and password
    final String userInfo = extractUserInfo(name);
    final String userName;
    final String password;
    if (userInfo != null) {
        final int idx = userInfo.indexOf(':');
        if (idx == -1) {
            userName = userInfo;
            password = null;
        } else {
            userName = userInfo.substring(0, idx);
            password = userInfo.substring(idx + 1);
        }
    } else {
        userName = null;
        password = null;
    }
    auth.userName = UriParser.decode(userName);
    auth.password = UriParser.decode(password);
    if (auth.password != null && auth.password.startsWith("{") && auth.password.endsWith("}")) {
        try {
            final Cryptor cryptor = CryptorFactory.getCryptor();
            auth.password = cryptor.decrypt(auth.password.substring(1, auth.password.length() - 1));
        } catch (final Exception ex) {
            throw new FileSystemException("Unable to decrypt password", ex);
        }
    }
    // Extract hostname, and normalize (lowercase)
    final String hostName = extractHostName(name);
    if (hostName == null) {
        throw new FileSystemException("vfs.provider/missing-hostname.error", uri);
    }
    auth.hostName = hostName.toLowerCase();
    // Extract port
    auth.port = extractPort(name, uri);
    // Expecting '/' or empty name
    if (name.length() > 0 && name.charAt(0) != '/') {
        throw new FileSystemException("vfs.provider/missing-hostname-path-sep.error", uri);
    }
    return auth;
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) Cryptor(org.apache.commons.vfs2.util.Cryptor) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) FileSystemException(org.apache.commons.vfs2.FileSystemException)

Aggregations

FileSystemException (org.apache.commons.vfs2.FileSystemException)1 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)1 UserAuthenticationData (org.apache.commons.vfs2.UserAuthenticationData)1 Type (org.apache.commons.vfs2.UserAuthenticationData.Type)1 Cryptor (org.apache.commons.vfs2.util.Cryptor)1