Search code examples
delphiauthenticationsftpdevart

Setting Authentication type SFTP connection delphi


I am using devart SecureBridge to create a connection over SFTP and I am having trouble setting the authentication type on the SSHClient.

When i try without it gives me an exception: 'The negotioation of host key algorithm is failed'. I guess it's trying to use private/public key, but I want it to use password authentication.

Here is my code, I hope someone can guide me in the right direction.

  oSSHClient    := TScSSHClient.Create(nil);
  oSFTPClient   := nil;
  oFileStorage  := nil;
  oFileStorage := TScFileStorage.Create(nil);
  oSSHClient.KeyStorage := oFileStorage;
  iColon := pos(':', edHost.text);
  oSSHClient.HostName := edHost.Text;
  if iColon > 0 then
  begin
    oSSHClient.Port := StrToIntDef(copy(edHost.Text, iColon+1, length(edHost.Text)), 22);
  end;
  oSSHClient.User := edUser.Text;
  oSSHClient.Password := edPassword.Text;

  oSSHClient.Authentication := oSSHClient.Authentication.atPassword; // How am i supposed to set this

  oSSHClient.Connect;

EDIT: WORKING CODE FOR OTHERS TO SEE:

  oSSHClient    := TScSSHClient.Create(nil);
  oFileStorage  := nil;
  try
    oFileStorage := TScFileStorage.Create(nil);
    oSSHClient.KeyStorage := oFileStorage;
    iColon := pos(':', edHost.text);
    oSSHClient.HostName := edHost.Text;
    if iColon > 0 then
    begin
      oSSHClient.Port := StrToIntDef(copy(edHost.Text, iColon+1, length(edHost.Text)), 22);
    end;
    oSSHClient.User := edUser.Text;
    oSSHClient.Password := edPassword.Text;
    oSSHClient.HostKeyAlgorithms.AsString:='ssh-rsa,ssh-dss';
    oSSHClient.OnServerKeyValidate := ScSSHClientServerKeyValidate;
    oSSHClient.Authentication := atPassword;
    try
      try
        oSSHClient.Connect;
        TestErrorTekst := GetLang('CaptConnectToServerOK');
      except
        TestErrorTekst := GetLang('CaptConnectToServerFailed'); 
      end;
    finally
      oSSHClient.Disconnect;
    end;
  finally
    edTest.Text := TestErrorTekst;
    oSSHClient.Free;
    oFileStorage.Free;
  end;

Solution

  • I suppose you should not set Authentication.

    You can try 2 things. Set correct algorithm

    oSSHClient.HostKeyAlgorithms.AsString:='ssh-rsa,ssh-dss';
    

    Or disable validaton of the key. Set Accept in ScSSHClientServerKeyValidate procedure to True.

    procedure TForm1.ScSSHClientServerKeyValidate(Sender: TObject;
      NewServerKey: TScKey; var Accept: Boolean);
    begin
      Accept:=True;
    end;