0

I am using JGit in Java and have the following code:

SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
  @Override
  protected void configure( Host host, Session session ) {
    session.setPassword( "password" );
  }
} );

CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI( "ssh://user@example.com/repo.git" );
cloneCommand.setTransportConfigCallback( new TransportConfigCallback() {
  @Override
  public void configure( Transport transport ) {
    SshTransport sshTransport = ( SshTransport )transport;
    sshTransport.setSshSessionFactory( sshSessionFactory );
  }
} );

I was trying to clone a repository, which is secured with SSH. I would like to force JGit to use my credentials, like username and password.

In my case, JGit internally checks my system environments and finding my .ssh folder, where the public key is stored and uses it to authenticate.

So, there is no need to use credentials like password and username. But, I want to force JGit to use credentials and not checking my environment variables and finding my public key.

Is there a way, to clone an ssh-repository and force JGit to use my credentials instead of the public key?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Schaumkuesschen
  • 139
  • 4
  • 18
  • Did you try to override `JschConfigSessionFactory::configureJSch`? This will give you access to a `JSch` instance. Perhaps removing all identities with `removeAllIdentity` may lead to the desired behaviour.. – Rüdiger Herrmann Feb 28 '20 at 10:47
  • Hallo @RüdigerHerrmann no, I did not try yet. I read your article: https://www.codeaffine.com/2014/12/09/jgit-authentication/ and played around, to force JGit to use my credentials .. but it was not successful. Maybe I really need to try it out with "removeAllIdentity". – Schaumkuesschen Feb 28 '20 at 10:56
  • @RüdigerHerrmann if I removeAllIdentity, how can I pass then the "credentials"? Is it with the "CredentialsProvider"? Thanks – Schaumkuesschen Feb 28 '20 at 11:17
  • I'd think you need to add an identity that holds username and password. That's what I would try first. Maybe this will also help: https://stackoverflow.com/questions/18835756/how-do-i-authenticate-programmatically-using-jsch – Rüdiger Herrmann Feb 28 '20 at 11:49
  • When I override the createDefaultJSch(FS fs) and create an instance JSch and call the method "removeAllIdentity", I am getting now an Auth fail, thats, why I think I have to pass the credentials somewhere. Because I want to avoid the auth. via ssh key, instead just passing the credentials somewhere. – Schaumkuesschen Feb 28 '20 at 11:53

0 Answers0