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

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

Introduction

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

Prototype

ExtendedTransformer USERNAME_EXTRACTOR

To view the source code for org.apache.commons.net.ssh.keys CryptoKeyEntry USERNAME_EXTRACTOR.

Click Source Link

Document

An ExtendedTransformer that invokes CryptoKeyEntry#getUser()

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)
 *///w  ww  . j  a  va2 s  .  co  m
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;
            }
        }
    };
}