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 public class CredentialsProviderUserInfo implements UserInfo,
56 UIKeyboardInteractive {
57 private final URIish uri;
58
59 private final CredentialsProvider provider;
60
61 private String password;
62
63 private String passphrase;
64
65
66
67
68
69
70
71
72
73 public CredentialsProviderUserInfo(Session session,
74 CredentialsProvider credentialsProvider) {
75 this.uri = createURI(session);
76 this.provider = credentialsProvider;
77 }
78
79 private static URIish createURI(Session session) {
80 URIish uri = new URIish();
81 uri = uri.setScheme("ssh");
82 uri = uri.setUser(session.getUserName());
83 uri = uri.setHost(session.getHost());
84 uri = uri.setPort(session.getPort());
85 return uri;
86 }
87
88 @Override
89 public String getPassword() {
90 return password;
91 }
92
93 @Override
94 public String getPassphrase() {
95 return passphrase;
96 }
97
98 @Override
99 public boolean promptPassphrase(String msg) {
100 CredentialItem.StringType v = newPrompt(msg);
101 if (provider.get(uri, v)) {
102 passphrase = v.getValue();
103 return true;
104 } else {
105 passphrase = null;
106 return false;
107 }
108 }
109
110 @Override
111 public boolean promptPassword(String msg) {
112 CredentialItem.Password p = new CredentialItem.Password(msg);
113 if (provider.get(uri, p)) {
114 password = new String(p.getValue());
115 return true;
116 } else {
117 password = null;
118 return false;
119 }
120 }
121
122 private CredentialItem.StringType newPrompt(String msg) {
123 return new CredentialItem.StringType(msg, true);
124 }
125
126 @Override
127 public boolean promptYesNo(String msg) {
128 CredentialItem.YesNoType v = new CredentialItem.YesNoType(msg);
129 return provider.get(uri, v) && v.getValue();
130 }
131
132 @Override
133 public void showMessage(String msg) {
134 provider.get(uri, new CredentialItem.InformationalMessage(msg));
135 }
136
137 @Override
138 public String[] promptKeyboardInteractive(String destination, String name,
139 String instruction, String[] prompt, boolean[] echo) {
140 CredentialItem.StringType[] v = new CredentialItem.StringType[prompt.length];
141 for (int i = 0; i < prompt.length; i++)
142 v[i] = new CredentialItem.StringType(prompt[i], !echo[i]);
143
144 List<CredentialItem> items = new ArrayList<>();
145 if (instruction != null && instruction.length() > 0)
146 items.add(new CredentialItem.InformationalMessage(instruction));
147 items.addAll(Arrays.asList(v));
148
149 if (!provider.get(uri, items))
150 return null;
151
152 String[] result = new String[v.length];
153 for (int i = 0; i < v.length; i++)
154 result[i] = v[i].getValue();
155 return result;
156 }
157 }