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