Android™
Android™
SSH Tunnel Credentials from OS Secure Storage
See more SSH Tunnel Examples
Demonstrates the EnableSecrets property, which enables automatic resolution of credentials from the operating system's secure storage. When enabled, password properties and methods may receive a "secret specification string" beginning with !! instead of a literal password.
Background: This is a cleaner alternative to reading a secret yourself and passing it in: with
EnableSecrets on, you hand Chilkat a reference like !!my_secret_name and it fetches the actual value from the platform's secure store — Windows Credential Manager, Apple Keychain, and so on. The literal password never appears in your source or configuration, which is exactly the practice recommended throughout these examples.Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
// Demonstrates the SshTunnel.EnableSecrets property, which enables automatic resolution of
// credentials from the operating system's secure storage.
CkSshTunnel tunnel = new CkSshTunnel();
// When EnableSecrets is true, supported password properties and methods may receive a "secret
// specification string" beginning with "!!" instead of a literal password. Chilkat resolves the
// secret from the OS secure store (Windows Credential Manager, Apple Keychain, etc.).
tunnel.put_EnableSecrets(true);
tunnel.put_DestHostname("db.internal.example.com");
tunnel.put_DestPort(5432);
int sshPort = 22;
success = tunnel.Connect("ssh.example.com",sshPort);
if (success == false) {
Log.i(TAG, tunnel.lastErrorText());
return;
}
// Pass a secret specification instead of a literal password. Chilkat looks up the named secret
// in the OS secure store rather than using this text directly.
String password = "!!my_ssh_password_secret";
success = tunnel.AuthenticatePw("mySshLogin",password);
if (success == false) {
Log.i(TAG, tunnel.lastErrorText());
return;
}
int listenPort = 1080;
success = tunnel.BeginAccepting(listenPort);
if (success == false) {
Log.i(TAG, tunnel.lastErrorText());
return;
}
Log.i(TAG, "Authenticated using a secret from OS secure storage.");
boolean waitForThreadExit = true;
success = tunnel.CloseTunnel(waitForThreadExit);
if (success == false) {
Log.i(TAG, tunnel.lastErrorText());
return;
}
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}