1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.transport;
45
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.List;
49
50 import com.jcraft.jsch.Session;
51 import com.jcraft.jsch.UIKeyboardInteractive;
52 import com.jcraft.jsch.UserInfo;
53
54
55
56
57
58 public class CredentialsProviderUserInfo implements UserInfo,
59 UIKeyboardInteractive {
60 private final URIish uri;
61
62 private final CredentialsProvider provider;
63
64 private String password;
65
66 private String passphrase;
67
68
69
70
71
72
73
74
75
76 public CredentialsProviderUserInfo(Session session,
77 CredentialsProvider credentialsProvider) {
78 this.uri = createURI(session);
79 this.provider = credentialsProvider;
80 }
81
82 private static URIish createURI(Session session) {
83 URIish uri = new URIish();
84 uri = uri.setScheme("ssh");
85 uri = uri.setUser(session.getUserName());
86 uri = uri.setHost(session.getHost());
87 uri = uri.setPort(session.getPort());
88 return uri;
89 }
90
91
92 @Override
93 public String getPassword() {
94 return password;
95 }
96
97
98 @Override
99 public String getPassphrase() {
100 return passphrase;
101 }
102
103
104 @Override
105 public boolean promptPassphrase(String msg) {
106 CredentialItem.StringType v = newPrompt(msg);
107 if (provider.get(uri, v)) {
108 passphrase = v.getValue();
109 return true;
110 } else {
111 passphrase = null;
112 return false;
113 }
114 }
115
116
117 @Override
118 public boolean promptPassword(String msg) {
119 CredentialItem.Password p = new CredentialItem.Password(msg);
120 if (provider.get(uri, p)) {
121 password = new String(p.getValue());
122 return true;
123 } else {
124 password = null;
125 return false;
126 }
127 }
128
129 private CredentialItem.StringType newPrompt(String msg) {
130 return new CredentialItem.StringType(msg, true);
131 }
132
133
134 @Override
135 public boolean promptYesNo(String msg) {
136 CredentialItem.YesNoType v = new CredentialItem.YesNoType(msg);
137 return provider.get(uri, v) && v.getValue();
138 }
139
140
141 @Override
142 public void showMessage(String msg) {
143 provider.get(uri, new CredentialItem.InformationalMessage(msg));
144 }
145
146
147 @Override
148 public String[] promptKeyboardInteractive(String destination, String name,
149 String instruction, String[] prompt, boolean[] echo) {
150 CredentialItem.StringType[] v = new CredentialItem.StringType[prompt.length];
151 for (int i = 0; i < prompt.length; i++)
152 v[i] = new CredentialItem.StringType(prompt[i], !echo[i]);
153
154 List<CredentialItem> items = new ArrayList<>();
155 if (instruction != null && instruction.length() > 0)
156 items.add(new CredentialItem.InformationalMessage(instruction));
157 items.addAll(Arrays.asList(v));
158
159 if (!provider.get(uri, items))
160 return null;
161
162 String[] result = new String[v.length];
163 for (int i = 0; i < v.length; i++)
164 result[i] = v[i].getValue();
165 return result;
166 }
167 }