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