Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Demonstrates the SshTunnel.EnableSecrets property, which enables automatic resolution of
    //  credentials from the operating system's secure storage.

    let tunnel = CkoSshTunnel()!

    //  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.enableSecrets = true

    tunnel.destHostname = "db.internal.example.com"
    tunnel.destPort = 5432

    var sshPort: Int = 22
    success = tunnel.connect(hostname: "ssh.example.com", port: sshPort)
    if success == false {
        print("\(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.
    var password: String? = "!!my_ssh_password_secret"
    success = tunnel.authenticatePw(login: "mySshLogin", password: password)
    if success == false {
        print("\(tunnel.lastErrorText!)")
        return
    }

    var listenPort: Int = 1080
    success = tunnel.beginAccepting(listenPort: listenPort)
    if success == false {
        print("\(tunnel.lastErrorText!)")
        return
    }

    print("Authenticated using a secret from OS secure storage.")

    var waitForThreadExit: Bool = true
    success = tunnel.closeTunnel(waitForThreads: waitForThreadExit)
    if success == false {
        print("\(tunnel.lastErrorText!)")
        return
    }


}