package org.apache.sshd.common.keyprovider;

import org.apache.sshd.common.config.keys.loader.KeyPairResourceParser;
import org.apache.sshd.common.session.SessionContext;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.NoSuchProviderException;

import static org.apache.sshd.common.util.security.SecurityUtils.getKeyPairResourceParser;

/**
 * Read a key directly from a given string which represents the private key.
 */
public class SimpleKeyPairProvider extends AbstractKeyPairProvider {

    // the private key
    private final String data;

    public SimpleKeyPairProvider(String data) {
        this.data = data;
    }

    @Override
    public Iterable<KeyPair> loadKeys(SessionContext session) throws IOException, GeneralSecurityException {
        KeyPairResourceParser parser = getKeyPairResourceParser();
        if (parser == null) {
            throw new NoSuchProviderException("No registered key-pair resource parser");
        }
        return parser.loadKeyPairs(session, null, null, data);
    }
}
