2 * Copyright (C) 2010, Google Inc. and others
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.
8 * SPDX-License-Identifier: BSD-3-Clause
11 package org.eclipse.jgit.internal.transport.ssh.jsch;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
17 import org.eclipse.jgit.transport.CredentialItem;
18 import org.eclipse.jgit.transport.CredentialsProvider;
19 import org.eclipse.jgit.transport.URIish;
21 import com.jcraft.jsch.Session;
22 import com.jcraft.jsch.UIKeyboardInteractive;
23 import com.jcraft.jsch.UserInfo;
26 * A JSch {@link com.jcraft.jsch.UserInfo} adapter for a
27 * {@link org.eclipse.jgit.transport.CredentialsProvider}.
29 public class CredentialsProviderUserInfo implements UserInfo,
30 UIKeyboardInteractive {
31 private final URIish uri;
33 private final CredentialsProvider provider;
35 private String password;
37 private String passphrase;
40 * Wrap a CredentialsProvider to make it suitable for use with JSch.
43 * the JSch session this UserInfo will support authentication on.
44 * @param credentialsProvider
45 * the provider that will perform the authentication.
47 public CredentialsProviderUserInfo(Session session,
48 CredentialsProvider credentialsProvider) {
49 this.uri = createURI(session);
50 this.provider = credentialsProvider;
53 private static URIish createURI(Session session) {
54 URIish uri = new URIish();
55 uri = uri.setScheme("ssh"); //$NON-NLS-1$
56 uri = uri.setUser(session.getUserName());
57 uri = uri.setHost(session.getHost());
58 uri = uri.setPort(session.getPort());
64 public String getPassword() {
70 public String getPassphrase() {
76 public boolean promptPassphrase(String msg) {
77 CredentialItem.StringType v = newPrompt(msg);
78 if (provider.get(uri, v)) {
79 passphrase = v.getValue();
88 public boolean promptPassword(String msg) {
89 CredentialItem.Password p = new CredentialItem.Password(msg);
90 if (provider.get(uri, p)) {
91 password = new String(p.getValue());
98 private CredentialItem.StringType newPrompt(String msg) {
99 return new CredentialItem.StringType(msg, true);
104 public boolean promptYesNo(String msg) {
105 CredentialItem.YesNoType v = new CredentialItem.YesNoType(msg);
106 return provider.get(uri, v) && v.getValue();
111 public void showMessage(String msg) {
112 provider.get(uri, new CredentialItem.InformationalMessage(msg));
117 public String[] promptKeyboardInteractive(String destination, String name,
118 String instruction, String[] prompt, boolean[] echo) {
119 CredentialItem.StringType[] v = new CredentialItem.StringType[prompt.length];
120 for (int i = 0; i < prompt.length; i++)
121 v[i] = new CredentialItem.StringType(prompt[i], !echo[i]);
123 List<CredentialItem> items = new ArrayList<>();
124 if (instruction != null && instruction.length() > 0)
125 items.add(new CredentialItem.InformationalMessage(instruction));
126 items.addAll(Arrays.asList(v));
128 if (!provider.get(uri, items))
129 return null; // cancel
131 String[] result = new String[v.length];
132 for (int i = 0; i < v.length; i++)
133 result[i] = v[i].getValue();