Setting ssh key for jgit

I am wondering how can I use jgit to connect to github using the specified ssh key file (i.e. not in ~ / .ssh /).

Unfortunately, I'm not sure how to use JschConfigSessionFactory correctly. I tried to create a setting, as in this article: Using keys with JGit to securely access the Git repository

I call Git with git.push().setRemote(remotePath).call(); However, I get this error (specific repository omitted from the log):

 org.eclipse.jgit.api.errors.TransportException: https://github.com/user/repo: not authorized at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:160) at gitio.GitInterface.pushToRemote(GitInterface.java:145) at engine.GitInterfaceTester.main(GitInterfaceTester.java:25) Caused by: org.eclipse.jgit.errors.TransportException: https://github.com/user/repo: not authorized at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:479) at org.eclipse.jgit.transport.TransportHttp.openPush(TransportHttp.java:396) at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:154) at org.eclipse.jgit.transport.Transport.push(Transport.java:1173) at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:156) ... 2 more 

I noticed that custom override methods in JschConfigSessionFactory never called. This is almost certainly the cause of the problem ... but I don't know why they are not being called; I pass the custom JschConfigSessionFactory to SshSessionFactory using SshSessionFactory.setInstance(sessionFactory);

Does anyone know what I'm doing wrong?

+6
source share
1 answer

GitHub uses password authentication for https URLs and public key authentication for SSH URLs (see https://gist.github.com/grawity/4392747 ).

SshSessionFactory is only used for SSH URLs such as git @ github.com: user / repo.git. To authenticate with the https url, you can use CredentialsProvider . The base class for commands that can establish connections is TransportCommand and has the setCredentialsProvider() method. If you equip PushCommand with the CredentialsProvider , which provides a username and password, you should be able to successfully establish a connection.

For more information about authentication with JGit, you can read this article: http://www.codeaffine.com/2014/12/09/jgit-authentication/

+2
source

Source: https://habr.com/ru/post/969369/


All Articles