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.awtui;
47
48 import java.awt.GridBagConstraints;
49 import java.awt.GridBagLayout;
50 import java.awt.Insets;
51
52 import javax.swing.JLabel;
53 import javax.swing.JOptionPane;
54 import javax.swing.JPanel;
55 import javax.swing.JPasswordField;
56 import javax.swing.JTextField;
57
58 import org.eclipse.jgit.errors.UnsupportedCredentialItem;
59 import org.eclipse.jgit.transport.ChainingCredentialsProvider;
60 import org.eclipse.jgit.transport.CredentialItem;
61 import org.eclipse.jgit.transport.CredentialsProvider;
62 import org.eclipse.jgit.transport.NetRCCredentialsProvider;
63 import org.eclipse.jgit.transport.URIish;
64
65
66 public class AwtCredentialsProvider extends CredentialsProvider {
67
68 public static void install() {
69 final AwtCredentialsProvider c = new AwtCredentialsProvider();
70 CredentialsProvider cp = new ChainingCredentialsProvider(
71 new NetRCCredentialsProvider(), c);
72 CredentialsProvider.setDefault(cp);
73 }
74
75 @Override
76 public boolean isInteractive() {
77 return true;
78 }
79
80 @Override
81 public boolean supports(CredentialItem... items) {
82 for (CredentialItem i : items) {
83 if (i instanceof CredentialItem.StringType)
84 continue;
85
86 else if (i instanceof CredentialItem.CharArrayType)
87 continue;
88
89 else if (i instanceof CredentialItem.YesNoType)
90 continue;
91
92 else if (i instanceof CredentialItem.InformationalMessage)
93 continue;
94
95 else
96 return false;
97 }
98 return true;
99 }
100
101 @Override
102 public boolean get(URIish uri, CredentialItem... items)
103 throws UnsupportedCredentialItem {
104 if (items.length == 0) {
105 return true;
106
107 } else if (items.length == 1) {
108 final CredentialItem item = items[0];
109
110 if (item instanceof CredentialItem.InformationalMessage) {
111 JOptionPane.showMessageDialog(null, item.getPromptText(),
112 UIText.get().warning, JOptionPane.INFORMATION_MESSAGE);
113 return true;
114
115 } else if (item instanceof CredentialItem.YesNoType) {
116 CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
117 int r = JOptionPane.showConfirmDialog(null, v.getPromptText(),
118 UIText.get().warning, JOptionPane.YES_NO_OPTION);
119 switch (r) {
120 case JOptionPane.YES_OPTION:
121 v.setValue(true);
122 return true;
123
124 case JOptionPane.NO_OPTION:
125 v.setValue(false);
126 return true;
127
128 case JOptionPane.CANCEL_OPTION:
129 case JOptionPane.CLOSED_OPTION:
130 default:
131 return false;
132 }
133
134 } else {
135 return interactive(uri, items);
136 }
137
138 } else {
139 return interactive(uri, items);
140 }
141 }
142
143 private static boolean interactive(URIish uri, CredentialItem[] items) {
144 final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
145 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
146 new Insets(0, 0, 0, 0), 0, 0);
147 final JPanel panel = new JPanel();
148 panel.setLayout(new GridBagLayout());
149
150 final JTextField[] texts = new JTextField[items.length];
151 for (int i = 0; i < items.length; i++) {
152 CredentialItem item = items[i];
153
154 if (item instanceof CredentialItem.StringType
155 || item instanceof CredentialItem.CharArrayType) {
156 gbc.fill = GridBagConstraints.NONE;
157 gbc.gridwidth = GridBagConstraints.RELATIVE;
158 gbc.gridx = 0;
159 panel.add(new JLabel(item.getPromptText()), gbc);
160
161 gbc.fill = GridBagConstraints.HORIZONTAL;
162 gbc.gridwidth = GridBagConstraints.RELATIVE;
163 gbc.gridx = 1;
164 if (item.isValueSecure())
165 texts[i] = new JPasswordField(20);
166 else
167 texts[i] = new JTextField(20);
168 panel.add(texts[i], gbc);
169 gbc.gridy++;
170
171 } else if (item instanceof CredentialItem.InformationalMessage) {
172 gbc.fill = GridBagConstraints.NONE;
173 gbc.gridwidth = GridBagConstraints.REMAINDER;
174 gbc.gridx = 0;
175 panel.add(new JLabel(item.getPromptText()), gbc);
176 gbc.gridy++;
177
178 } else {
179 throw new UnsupportedCredentialItem(uri, item.getPromptText());
180 }
181 }
182
183 if (JOptionPane.showConfirmDialog(null, panel,
184 UIText.get().authenticationRequired,
185 JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION)
186 return false;
187
188 for (int i = 0; i < items.length; i++) {
189 CredentialItem item = items[i];
190 JTextField f = texts[i];
191
192 if (item instanceof CredentialItem.StringType) {
193 CredentialItem.StringType v = (CredentialItem.StringType) item;
194 if (f instanceof JPasswordField)
195 v.setValue(new String(((JPasswordField) f).getPassword()));
196 else
197 v.setValue(f.getText());
198
199 } else if (item instanceof CredentialItem.CharArrayType) {
200 CredentialItem.CharArrayType v = (CredentialItem.CharArrayType) item;
201 if (f instanceof JPasswordField)
202 v.setValueNoCopy(((JPasswordField) f).getPassword());
203 else
204 v.setValueNoCopy(f.getText().toCharArray());
205 }
206 }
207 return true;
208 }
209 }