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
45
46 package org.eclipse.jgit.console;
47
48 import java.io.Console;
49
50 import org.eclipse.jgit.errors.UnsupportedCredentialItem;
51 import org.eclipse.jgit.pgm.internal.CLIText;
52 import org.eclipse.jgit.transport.ChainingCredentialsProvider;
53 import org.eclipse.jgit.transport.CredentialItem;
54 import org.eclipse.jgit.transport.CredentialsProvider;
55 import org.eclipse.jgit.transport.NetRCCredentialsProvider;
56 import org.eclipse.jgit.transport.URIish;
57
58
59
60
61
62
63 public class ConsoleCredentialsProvider extends CredentialsProvider {
64
65
66
67 public static void install() {
68 final ConsoleCredentialsProviderProvider.html#ConsoleCredentialsProvider">ConsoleCredentialsProvider c = new ConsoleCredentialsProvider();
69 if (c.cons == null)
70 throw new NoClassDefFoundError(
71 CLIText.get().noSystemConsoleAvailable);
72 CredentialsProvider cp = new ChainingCredentialsProvider(
73 new NetRCCredentialsProvider(), c);
74 CredentialsProvider.setDefault(cp);
75 }
76
77 private final Console cons = System.console();
78
79
80 @Override
81 public boolean isInteractive() {
82 return true;
83 }
84
85
86 @Override
87 public boolean supports(CredentialItem... items) {
88 for (CredentialItem i : items) {
89 if (i instanceof CredentialItem.StringType)
90 continue;
91
92 else if (i instanceof CredentialItem.CharArrayType)
93 continue;
94
95 else if (i instanceof CredentialItem.YesNoType)
96 continue;
97
98 else if (i instanceof CredentialItem.InformationalMessage)
99 continue;
100
101 else
102 return false;
103 }
104 return true;
105 }
106
107
108 @Override
109 public boolean get(URIish uri, CredentialItem... items)
110 throws UnsupportedCredentialItem {
111 boolean ok = true;
112 for (int i = 0; i < items.length && ok; i++) {
113 CredentialItem item = items[i];
114
115 if (item instanceof CredentialItem.StringType)
116 ok = get((CredentialItem.StringType) item);
117
118 else if (item instanceof CredentialItem.CharArrayType)
119 ok = get((CredentialItem.CharArrayType) item);
120
121 else if (item instanceof CredentialItem.YesNoType)
122 ok = get((CredentialItem.YesNoType) item);
123
124 else if (item instanceof CredentialItem.InformationalMessage)
125 ok = get((CredentialItem.InformationalMessage) item);
126
127 else
128 throw new UnsupportedCredentialItem(uri, item.getPromptText());
129 }
130 return ok;
131 }
132
133 private boolean get(CredentialItem.StringType item) {
134 if (item.isValueSecure()) {
135 char[] v = cons.readPassword("%s: ", item.getPromptText());
136 if (v != null) {
137 item.setValue(new String(v));
138 return true;
139 } else {
140 return false;
141 }
142 } else {
143 String v = cons.readLine("%s: ", item.getPromptText());
144 if (v != null) {
145 item.setValue(v);
146 return true;
147 } else {
148 return false;
149 }
150 }
151 }
152
153 private boolean get(CredentialItem.CharArrayType item) {
154 if (item.isValueSecure()) {
155 char[] v = cons.readPassword("%s: ", item.getPromptText());
156 if (v != null) {
157 item.setValueNoCopy(v);
158 return true;
159 } else {
160 return false;
161 }
162 } else {
163 String v = cons.readLine("%s: ", item.getPromptText());
164 if (v != null) {
165 item.setValueNoCopy(v.toCharArray());
166 return true;
167 } else {
168 return false;
169 }
170 }
171 }
172
173 private boolean get(CredentialItem.InformationalMessage item) {
174 cons.printf("%s\n", item.getPromptText());
175 cons.flush();
176 return true;
177 }
178
179 private boolean get(CredentialItem.YesNoType item) {
180 String r = cons.readLine("%s [%s/%s]? ", item.getPromptText(),
181 CLIText.get().answerYes, CLIText.get().answerNo);
182 if (r != null) {
183 item.setValue(CLIText.get().answerYes.equalsIgnoreCase(r));
184 return true;
185 } else {
186 return false;
187 }
188 }
189 }