use of org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator in project ebad by informatique-cdc.
the class ShellServiceTest method setupSSHServer.
private void setupSSHServer() throws IOException {
sshd = SshServer.setUpDefaultServer();
final PublicKey allowedKey;
sshd.setPasswordAuthenticator((username, password, session) -> StringUtils.equals(username, USERNAME) && StringUtils.equals(password, PASSWORD));
sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
@Override
public boolean authenticate(String username, PublicKey key, ServerSession session) {
return false;
// return key.equals(allowedKey);
}
});
sshd.setPort(2048);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Files.createTempFile("host_file", ".ser")));
sshd.setSubsystemFactories(Collections.<SubsystemFactory>singletonList(new SftpSubsystemFactory()));
sshd.setFileSystemFactory(new VirtualFileSystemFactory(Files.createTempDirectory("SFTP_TEMP")));
sshd.setCommandFactory(new ProcessShellCommandFactory());
sshd.start();
}
use of org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator in project nomulus by google.
the class TestSftpServer method createSftpServer.
public static FtpServer createSftpServer(final String authorizedUser, @Nullable final String authorizedPassword, @Nullable final PublicKey authorizedPublicKey, int port, final File home) {
ServerBuilder serverBuilder = ServerBuilder.builder();
serverBuilder.randomFactory(secureRandomFactory);
if (authorizedPublicKey != null) {
// This authenticator checks that the user is presenting the right key. If authenticate
// returns true, then the server will make sure that the user can prove they have that key.
// Not that you would know this from the Apache javadocs.
serverBuilder.publickeyAuthenticator(new PublickeyAuthenticator() {
@Override
public boolean authenticate(String username, PublicKey publicKey, ServerSession session) {
return Arrays.equals(publicKey.getEncoded(), authorizedPublicKey.getEncoded());
}
});
}
serverBuilder.fileSystemFactory(new VirtualFileSystemFactory(home.toPath()));
SshServer server = serverBuilder.build();
server.setCommandFactory(new ScpCommandFactory());
server.setPort(port);
NamedFactory<Command> sftpSubsystemFactory = new SftpSubsystemFactory.Builder().build();
server.setSubsystemFactories(ImmutableList.of(sftpSubsystemFactory));
if (authorizedPassword != null) {
server.setPasswordAuthenticator(new PasswordAuthenticator() {
@Override
public boolean authenticate(String username, String password, ServerSession session) {
return username.equals(authorizedUser) && password.equals(authorizedPassword);
}
});
}
KeyPairProvider keyPairProvider = new KeyPairProvider() {
final ImmutableMap<String, KeyPair> keyPairByTypeMap = ImmutableMap.of(KEY_TYPE, HOST_KEY_PAIR);
@Override
public Iterable<KeyPair> loadKeys() {
return keyPairByTypeMap.values();
}
@Override
public Iterable<String> getKeyTypes() {
return keyPairByTypeMap.keySet();
}
@Override
public KeyPair loadKey(final String type) {
return keyPairByTypeMap.get(type);
}
};
server.setKeyPairProvider(keyPairProvider);
return new TestSftpServer(server);
}
use of org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator in project equinox.bundles by eclipse-equinox.
the class SshServ method createSimpleAuthorizedKeysAuthenticator.
private PublickeyAuthenticator createSimpleAuthorizedKeysAuthenticator() {
// use authorized keys file if property is set
final String authorizedKeysFile = System.getProperty(SSH_AUTHORIZED_KEYS_FILE_PROP);
if (null != authorizedKeysFile) {
AuthorizedKeysFileAuthenticator authenticator = new AuthorizedKeysFileAuthenticator();
authenticator.setAuthorizedKeysFile(authorizedKeysFile);
return authenticator;
}
final String customPublicKeysAuthentication = System.getProperty(SSH_CUSTOM_PUBLIC_KEY_AUTHENTICATION);
// fall back to dynamic provider based on available OSGi services only if explicitly specified
if ("true".equals(customPublicKeysAuthentication)) {
return (username, key, session) -> {
// find available services
try {
for (ServiceReference<PublickeyAuthenticator> reference : context.getServiceReferences(PublickeyAuthenticator.class, null)) {
PublickeyAuthenticator authenticator = null;
try {
authenticator = context.getService(reference);
// first positive match wins; continue looking otherwise
if (authenticator.authenticate(username, key, session))
return true;
} finally {
if (null != authenticator)
context.ungetService(reference);
}
}
} catch (InvalidSyntaxException e) {
// no filter is used
}
return false;
};
}
return null;
}
Aggregations