Example usage for org.apache.commons.net.ssh.keys CryptoKeyEntry getKeyData

List of usage examples for org.apache.commons.net.ssh.keys CryptoKeyEntry getKeyData

Introduction

In this page you can find the example usage for org.apache.commons.net.ssh.keys CryptoKeyEntry getKeyData.

Prototype

public byte[] getKeyData() 

Source Link

Usage

From source file:org.apache.sshd.server.PublickeyAuthenticatorUtils.java

/**
 * @param entries A {@link Collection} of {@link CryptoKeyEntry}-ies
 * @return A {@link PublickeyAuthenticator} that matches the received encoded
 * public key bytes to one of the authorized keys published by the user
 * @see CryptoKeyEntry#readAuthorizedKeys(File)
 *///from ww w  . ja v a  2 s  .  com
public static final PublickeyAuthenticator authorizedKeysAuthenticator(
        Collection<? extends CryptoKeyEntry> entries) {
    final Map<String, ? extends Collection<CryptoKeyEntry>> keysMap = ExtendedMapUtils.mapCollectionMultiValues(
            CryptoKeyEntry.USERNAME_EXTRACTOR, ExtendedCollectionUtils.<CryptoKeyEntry>linkedListFactory(),
            entries);
    return new PublickeyAuthenticator() {
        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            Collection<CryptoKeyEntry> keySet = keysMap.get(username);
            if (ExtendedCollectionUtils.isEmpty(keySet)) {
                return false;
            }

            final byte[] keyBytes = key.getEncoded();
            if (ArrayUtils.isEmpty(keyBytes)) {
                return false; // TODO consider throwing an exception
            }

            CryptoKeyEntry entry = CollectionUtils.find(keySet,
                    new AbstractExtendedPredicate<CryptoKeyEntry>(CryptoKeyEntry.class) {
                        @Override
                        public boolean evaluate(CryptoKeyEntry e) {
                            byte[] entryBytes = e.getKeyData();
                            if (Arrays.equals(keyBytes, entryBytes)) {
                                return true;
                            } else {
                                return false; // debug breakpoint;
                            }
                        }
                    });
            if (entry == null) {
                return false;
            } else {
                return true;
            }
        }
    };
}