|
| 1 | +/* |
| 2 | + * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Distribution License v. 1.0 which is available at |
| 6 | + * https://www.eclipse.org/org/documents/edl-v10.php. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: BSD-3-Clause |
| 9 | + */ |
| 10 | +package org.eclipse.jgit.internal.transport.sshd.agent; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import org.apache.sshd.agent.SshAgent; |
| 18 | +import org.apache.sshd.agent.SshAgentFactory; |
| 19 | +import org.apache.sshd.agent.SshAgentServer; |
| 20 | +import org.apache.sshd.common.FactoryManager; |
| 21 | +import org.apache.sshd.common.channel.ChannelFactory; |
| 22 | +import org.apache.sshd.common.session.ConnectionService; |
| 23 | +import org.eclipse.jgit.annotations.NonNull; |
| 24 | +import org.eclipse.jgit.transport.sshd.agent.ConnectorFactory; |
| 25 | + |
| 26 | +/** |
| 27 | + * A factory for creating {@link SshAgentClient}s. |
| 28 | + */ |
| 29 | +public class JGitSshAgentFactory implements SshAgentFactory { |
| 30 | + |
| 31 | + private final @NonNull ConnectorFactory factory; |
| 32 | + |
| 33 | + private final File homeDir; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates a new {@link JGitSshAgentFactory}. |
| 37 | + * |
| 38 | + * @param factory |
| 39 | + * {@link JGitSshAgentFactory} to wrap |
| 40 | + * @param homeDir |
| 41 | + * for obtaining the current local user's home directory |
| 42 | + */ |
| 43 | + public JGitSshAgentFactory(@NonNull ConnectorFactory factory, |
| 44 | + File homeDir) { |
| 45 | + this.factory = factory; |
| 46 | + this.homeDir = homeDir; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public List<ChannelFactory> getChannelForwardingFactories( |
| 51 | + FactoryManager manager) { |
| 52 | + // No agent forwarding supported yet. |
| 53 | + return Collections.emptyList(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public SshAgent createClient(FactoryManager manager) throws IOException { |
| 58 | + // sshd 2.8.0 will pass us the session here. At that point, we can get |
| 59 | + // the HostConfigEntry and extract and handle the IdentityAgent setting. |
| 60 | + // For now, pass null to let the ConnectorFactory do its default |
| 61 | + // behavior (Pageant on Windows, SSH_AUTH_SOCK on Unixes with the |
| 62 | + // jgit-builtin factory). |
| 63 | + return new SshAgentClient(factory.create(null, homeDir)); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public SshAgentServer createServer(ConnectionService service) |
| 68 | + throws IOException { |
| 69 | + // This should be called in a server only. |
| 70 | + return null; |
| 71 | + } |
| 72 | +} |
0 commit comments