]> source.dussan.org Git - jgit.git/blob
5053493005bd4f5555b6bb493ef831cbf71221fa
[jgit.git] /
1 /*
2  * Copyright (C) 2010, Google Inc. 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
11 package org.eclipse.jgit.internal.transport.ssh.jsch;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import org.eclipse.jgit.transport.CredentialItem;
18 import org.eclipse.jgit.transport.CredentialsProvider;
19 import org.eclipse.jgit.transport.URIish;
20
21 import com.jcraft.jsch.Session;
22 import com.jcraft.jsch.UIKeyboardInteractive;
23 import com.jcraft.jsch.UserInfo;
24
25 /**
26  * A JSch {@link com.jcraft.jsch.UserInfo} adapter for a
27  * {@link org.eclipse.jgit.transport.CredentialsProvider}.
28  */
29 public class CredentialsProviderUserInfo implements UserInfo,
30                 UIKeyboardInteractive {
31         private final URIish uri;
32
33         private final CredentialsProvider provider;
34
35         private String password;
36
37         private String passphrase;
38
39         /**
40          * Wrap a CredentialsProvider to make it suitable for use with JSch.
41          *
42          * @param session
43          *            the JSch session this UserInfo will support authentication on.
44          * @param credentialsProvider
45          *            the provider that will perform the authentication.
46          */
47         public CredentialsProviderUserInfo(Session session,
48                         CredentialsProvider credentialsProvider) {
49                 this.uri = createURI(session);
50                 this.provider = credentialsProvider;
51         }
52
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());
59                 return uri;
60         }
61
62         /** {@inheritDoc} */
63         @Override
64         public String getPassword() {
65                 return password;
66         }
67
68         /** {@inheritDoc} */
69         @Override
70         public String getPassphrase() {
71                 return passphrase;
72         }
73
74         /** {@inheritDoc} */
75         @Override
76         public boolean promptPassphrase(String msg) {
77                 CredentialItem.StringType v = newPrompt(msg);
78                 if (provider.get(uri, v)) {
79                         passphrase = v.getValue();
80                         return true;
81                 }
82                 passphrase = null;
83                 return false;
84         }
85
86         /** {@inheritDoc} */
87         @Override
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());
92                         return true;
93                 }
94                 password = null;
95                 return false;
96         }
97
98         private CredentialItem.StringType newPrompt(String msg) {
99                 return new CredentialItem.StringType(msg, true);
100         }
101
102         /** {@inheritDoc} */
103         @Override
104         public boolean promptYesNo(String msg) {
105                 CredentialItem.YesNoType v = new CredentialItem.YesNoType(msg);
106                 return provider.get(uri, v) && v.getValue();
107         }
108
109         /** {@inheritDoc} */
110         @Override
111         public void showMessage(String msg) {
112                 provider.get(uri, new CredentialItem.InformationalMessage(msg));
113         }
114
115         /** {@inheritDoc} */
116         @Override
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]);
122
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));
127
128                 if (!provider.get(uri, items))
129                         return null; // cancel
130
131                 String[] result = new String[v.length];
132                 for (int i = 0; i < v.length; i++)
133                         result[i] = v[i].getValue();
134                 return result;
135         }
136 }