PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkImap.pb"
IncludeFile "CkSshKey.pb"
Procedure ChilkatExample()
success.i = 0
; 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.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
sshPort.i = 22
success = CkImap::ckSshOpenTunnel(imap,"ssh.example.com",sshPort)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Load the SSH private key. LoadText returns the file's text, which is then imported.
privKey.i = CkSshKey::ckCreate()
If privKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
keyStr.s = CkSshKey::ckLoadText(privKey,"qa_data/id_rsa")
If CkSshKey::ckLastMethodSuccess(privKey) = 0
Debug CkSshKey::ckLastErrorText(privKey)
CkImap::ckDispose(imap)
CkSshKey::ckDispose(privKey)
ProcedureReturn
EndIf
success = CkSshKey::ckFromOpenSshPrivateKey(privKey,keyStr)
If success = 0
Debug CkSshKey::ckLastErrorText(privKey)
CkImap::ckDispose(imap)
CkSshKey::ckDispose(privKey)
ProcedureReturn
EndIf
; Authenticate the SSH tunnel with the private key. The matching public key must already be
; authorized for the login on the SSH server.
success = CkImap::ckSshAuthenticatePk(imap,"sshUser",privKey)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSshKey::ckDispose(privKey)
ProcedureReturn
EndIf
; With the SSH tunnel in place, connect and log in to the IMAP server as usual. The IMAP
; traffic flows through the tunnel automatically.
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
success = CkImap::ckConnect(imap,"imap.example.com")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSshKey::ckDispose(privKey)
ProcedureReturn
EndIf
success = CkImap::ckLogin(imap,"user@example.com","myPassword")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSshKey::ckDispose(privKey)
ProcedureReturn
EndIf
success = CkImap::ckDisconnect(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSshKey::ckDispose(privKey)
ProcedureReturn
EndIf
success = CkImap::ckSshCloseTunnel(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkSshKey::ckDispose(privKey)
ProcedureReturn
EndIf
CkImap::ckDispose(imap)
CkSshKey::ckDispose(privKey)
ProcedureReturn
EndProcedure