1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.console;
14
15 import java.io.Console;
16
17 import org.eclipse.jgit.errors.UnsupportedCredentialItem;
18 import org.eclipse.jgit.pgm.internal.CLIText;
19 import org.eclipse.jgit.transport.ChainingCredentialsProvider;
20 import org.eclipse.jgit.transport.CredentialItem;
21 import org.eclipse.jgit.transport.CredentialsProvider;
22 import org.eclipse.jgit.transport.NetRCCredentialsProvider;
23 import org.eclipse.jgit.transport.URIish;
24
25
26
27
28
29
30 public class ConsoleCredentialsProvider extends CredentialsProvider {
31
32
33
34 public static void install() {
35 final ConsoleCredentialsProvider c = new ConsoleCredentialsProvider();
36 if (c.cons == null)
37 throw new NoClassDefFoundError(
38 CLIText.get().noSystemConsoleAvailable);
39 CredentialsProvider cp = new ChainingCredentialsProvider(
40 new NetRCCredentialsProvider(), c);
41 CredentialsProvider.setDefault(cp);
42 }
43
44 private final Console cons = System.console();
45
46
47 @Override
48 public boolean isInteractive() {
49 return true;
50 }
51
52
53 @Override
54 public boolean supports(CredentialItem... items) {
55 for (CredentialItem i : items) {
56 if (i instanceof CredentialItem.StringType)
57 continue;
58
59 else if (i instanceof CredentialItem.CharArrayType)
60 continue;
61
62 else if (i instanceof CredentialItem.YesNoType)
63 continue;
64
65 else if (i instanceof CredentialItem.InformationalMessage)
66 continue;
67
68 else
69 return false;
70 }
71 return true;
72 }
73
74
75 @Override
76 public boolean get(URIish uri, CredentialItem... items)
77 throws UnsupportedCredentialItem {
78 boolean ok = true;
79 for (int i = 0; i < items.length && ok; i++) {
80 CredentialItem item = items[i];
81
82 if (item instanceof CredentialItem.StringType)
83 ok = get((CredentialItem.StringType) item);
84
85 else if (item instanceof CredentialItem.CharArrayType)
86 ok = get((CredentialItem.CharArrayType) item);
87
88 else if (item instanceof CredentialItem.YesNoType)
89 ok = get((CredentialItem.YesNoType) item);
90
91 else if (item instanceof CredentialItem.InformationalMessage)
92 ok = get((CredentialItem.InformationalMessage) item);
93
94 else
95 throw new UnsupportedCredentialItem(uri, item.getPromptText());
96 }
97 return ok;
98 }
99
100 private boolean get(CredentialItem.StringType item) {
101 if (item.isValueSecure()) {
102 char[] v = cons.readPassword("%s: ", item.getPromptText());
103 if (v != null) {
104 item.setValue(new String(v));
105 return true;
106 }
107 return false;
108 }
109 String v = cons.readLine("%s: ", item.getPromptText());
110 if (v != null) {
111 item.setValue(v);
112 return true;
113 }
114 return false;
115 }
116
117 private boolean get(CredentialItem.CharArrayType item) {
118 if (item.isValueSecure()) {
119 char[] v = cons.readPassword("%s: ", item.getPromptText());
120 if (v != null) {
121 item.setValueNoCopy(v);
122 return true;
123 }
124 return false;
125 }
126 String v = cons.readLine("%s: ", item.getPromptText());
127 if (v != null) {
128 item.setValueNoCopy(v.toCharArray());
129 return true;
130 }
131 return false;
132 }
133
134 private boolean get(CredentialItem.InformationalMessage item) {
135 cons.printf("%s\n", item.getPromptText());
136 cons.flush();
137 return true;
138 }
139
140 private boolean get(CredentialItem.YesNoType item) {
141 String r = cons.readLine("%s [%s/%s]? ", item.getPromptText(),
142 CLIText.get().answerYes, CLIText.get().answerNo);
143 if (r != null) {
144 item.setValue(CLIText.get().answerYes.equalsIgnoreCase(r));
145 return true;
146 }
147 return false;
148 }
149 }