Sample code for 30+ languages & platforms
DataFlex

Authenticate an IMAP SSH Tunnel with a Private Key

See more IMAP Examples

Demonstrates the Chilkat Imap.SshAuthenticatePk method, which authenticates the SSH tunnel using a username and an SshKey private key. The first argument is the SSH login name and the second is the private key. Call SshOpenTunnel first; the matching public key must already be authorized for the login on the SSH server.

Note: The private-key path is relative to the application's current working directory. Supply the path to your own key file.

Background: Public-key authentication is the usual choice for unattended/automated SSH access: the private key stays on the client and the server is configured to trust the corresponding public key, so no reusable password travels over the wire. The SshKey object can import keys in several formats — this example uses an OpenSSH private key.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Integer iSshPort
    Variant vPrivKey
    Handle hoPrivKey
    String sKeyStr
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.SshAuthenticatePk method, which authenticates the SSH tunnel using a
    //  username and a private key.  The 1st argument is the SSH login name and the 2nd is an SshKey
    //  private key.  Call SshOpenTunnel first.

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Move 22 To iSshPort
    Get ComSshOpenTunnel Of hoImap "ssh.example.com" iSshPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Load the SSH private key.  LoadText returns the file's text, which is then imported.
    Get Create (RefClass(cComChilkatSshKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get ComLoadText Of hoPrivKey "qa_data/id_rsa" To sKeyStr
    Get ComLastMethodSuccess Of hoPrivKey To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComFromOpenSshPrivateKey Of hoPrivKey sKeyStr To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Authenticate the SSH tunnel with the private key.  The matching public key must already be
    //  authorized for the login on the SSH server.
    Get pvComObject of hoPrivKey to vPrivKey
    Get ComSshAuthenticatePk Of hoImap "sshUser" vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  With the SSH tunnel in place, connect and log in to the IMAP server as usual.  The IMAP
    //  traffic flows through the tunnel automatically.
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSshCloseTunnel Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure