PowerBuilder
PowerBuilder
SSH Auth Failure Reason (AuthenticatePwPk)
See more SSH Examples
Demonstrates how to determine why authentication failed when a server requires both a password and a private key. If AuthenticatePwPk fails, GetLastJsonData returns diagnostic JSON whose authResult and authFailReason members reveal which factor was at fault.
Background: With multi-factor SSH, a plain failure is ambiguous — the key could be wrong, the password could be wrong, or both. The diagnostic JSON resolves that: it reports
Key is incorrect when the key fails, or a partialAuthResult showing the key succeeded before Password is incorrect. Note the ordering consequence: if the key is rejected, authentication never reaches the password check, so you cannot yet know whether the password is also wrong. Passwords and passphrases are never included in this JSON, so it is safe to log.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Key
string ls_PrivKeyText
oleobject loo_Ssh
integer li_Port
string ls_Password
oleobject loo_Json
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates how to determine why authentication failed when a server requires BOTH a
// password and a private key. Was the private key wrong, or the password?
// Load the private key used for authentication.
loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.SshKey")
if li_rc < 0 then
destroy loo_Key
MessageBox("Error","Connecting to COM object failed")
return
end if
// The key password is not hard-coded in a real application; obtain it from a secure source.
loo_Key.Password = "myKeyPassword"
ls_PrivKeyText = loo_Key.LoadText("qa_data/my_private_key_file")
if loo_Key.LastMethodSuccess = 0 then
Write-Debug loo_Key.LastErrorText
destroy loo_Key
return
end if
li_Success = loo_Key.FromOpenSshPrivateKey(ls_PrivKeyText)
if li_Success = 0 then
Write-Debug loo_Key.LastErrorText
destroy loo_Key
return
end if
loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
li_Port = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_Port)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Key
destroy loo_Ssh
return
end if
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
ls_Password = "mySshPassword"
// Authenticate using both a password and a private key.
li_Success = loo_Ssh.AuthenticatePwPk("mySshLogin",ls_Password,loo_Key)
if li_Success then
Write-Debug "Authentication is successful!"
destroy loo_Key
destroy loo_Ssh
return
end if
// Authentication failed. The diagnostic JSON explains which factor was at fault.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Ssh.GetLastJsonData(loo_Json)
loo_Json.EmitCompact = 0
// If the key is correct but the password is wrong:
// {
// "public_key_type": "rsa",
// "partialAuthResult": "publickey success. continue to authenticate with password...",
// "authResult": "failed",
// "authFailReason": "Password is incorrect"
// }
//
// If the key is incorrect. Whether the password is also wrong is unknown, because
// authentication never got far enough to check it:
// {
// "public_key_type": "rsa",
// "authResult": "failed",
// "authFailReason": "Key is incorrect"
// }
Write-Debug "authResult: " + loo_Json.StringOf("authResult")
Write-Debug "authFailReason: " + loo_Json.StringOf("authFailReason")
destroy loo_Key
destroy loo_Ssh
destroy loo_Json