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