1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.server;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.GridBagConstraints;
|
---|
11 | import java.awt.GridBagLayout;
|
---|
12 | import java.awt.Insets;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.ItemEvent;
|
---|
15 | import java.awt.event.ItemListener;
|
---|
16 | import java.beans.PropertyChangeEvent;
|
---|
17 | import java.beans.PropertyChangeListener;
|
---|
18 | import java.util.logging.Logger;
|
---|
19 |
|
---|
20 | import javax.swing.AbstractAction;
|
---|
21 | import javax.swing.BorderFactory;
|
---|
22 | import javax.swing.JCheckBox;
|
---|
23 | import javax.swing.JLabel;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 | import javax.swing.JTextField;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.data.oauth.OAuthParameters;
|
---|
29 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
30 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
31 | import org.openstreetmap.josm.gui.SideButton;
|
---|
32 | import org.openstreetmap.josm.gui.oauth.AdvancedOAuthPropertiesPanel;
|
---|
33 | import org.openstreetmap.josm.gui.oauth.OAuthAuthorisationWizard;
|
---|
34 | import org.openstreetmap.josm.gui.oauth.TestAccessTokenTask;
|
---|
35 | import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
|
---|
36 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The preferences panel for the OAuth preferences. This just a summary panel
|
---|
40 | * showing the current Access Token Key and Access Token Secret, if the
|
---|
41 | * user already has an Access Token.
|
---|
42 | *
|
---|
43 | * For initial authorisation see {@see OAuthAuthorisationWizard}.
|
---|
44 | *
|
---|
45 | */
|
---|
46 | public class OAuthAuthenticationPreferencesPanel extends JPanel implements PropertyChangeListener {
|
---|
47 | private static final Logger logger = Logger.getLogger(OAuthAuthenticationPreferencesPanel.class.getName());
|
---|
48 |
|
---|
49 | private JPanel pnlAuthorisationMessage;
|
---|
50 | private NotYetAuthorisedPanel pnlNotYetAuthorised;
|
---|
51 | private AlreadyAuthorisedPanel pnlAlreadyAuthorised;
|
---|
52 | private AdvancedOAuthPropertiesPanel pnlAdvancedProperties;
|
---|
53 | private String apiUrl;
|
---|
54 | private JCheckBox cbShowAdvancedParameters;
|
---|
55 | private JCheckBox cbSaveToPreferences;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Builds the panel for entering the advanced OAuth parameters
|
---|
59 | *
|
---|
60 | * @return
|
---|
61 | */
|
---|
62 | protected JPanel buildAdvancedPropertiesPanel() {
|
---|
63 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
64 | GridBagConstraints gc= new GridBagConstraints();
|
---|
65 |
|
---|
66 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
67 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
68 | gc.weightx = 0.0;
|
---|
69 | gc.insets = new Insets(0,0,0,3);
|
---|
70 | pnl.add(cbShowAdvancedParameters = new JCheckBox(), gc);
|
---|
71 | cbShowAdvancedParameters.setSelected(false);
|
---|
72 | cbShowAdvancedParameters.addItemListener(
|
---|
73 | new ItemListener() {
|
---|
74 | public void itemStateChanged(ItemEvent evt) {
|
---|
75 | pnlAdvancedProperties.setVisible(evt.getStateChange() == ItemEvent.SELECTED);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | );
|
---|
79 |
|
---|
80 | gc.gridx = 1;
|
---|
81 | gc.weightx = 1.0;
|
---|
82 | JMultilineLabel lbl = new JMultilineLabel(tr("Display Advanced OAuth Parameters"));
|
---|
83 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
84 | pnl.add(lbl, gc);
|
---|
85 |
|
---|
86 | gc.gridy = 1;
|
---|
87 | gc.gridx = 1;
|
---|
88 | gc.insets = new Insets(3,0,3,0);
|
---|
89 | gc.fill = GridBagConstraints.BOTH;
|
---|
90 | gc.weightx = 1.0;
|
---|
91 | gc.weighty = 1.0;
|
---|
92 | pnl.add(pnlAdvancedProperties = new AdvancedOAuthPropertiesPanel(), gc);
|
---|
93 | pnlAdvancedProperties.initFromPreferences(Main.pref);
|
---|
94 | pnlAdvancedProperties.setBorder(
|
---|
95 | BorderFactory.createCompoundBorder(
|
---|
96 | BorderFactory.createLineBorder(Color.GRAY, 1),
|
---|
97 | BorderFactory.createEmptyBorder(3,3,3,3)
|
---|
98 | )
|
---|
99 | );
|
---|
100 | pnlAdvancedProperties.setVisible(false);
|
---|
101 | return pnl;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * builds the UI
|
---|
106 | */
|
---|
107 | protected void build() {
|
---|
108 | setLayout(new GridBagLayout());
|
---|
109 | setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
110 | GridBagConstraints gc = new GridBagConstraints();
|
---|
111 |
|
---|
112 | // the panel for the OAuth parameters. pnlAuthorisationMessage is an
|
---|
113 | // empty panel. It is going to be filled later, depending on the
|
---|
114 | // current OAuth state in JOSM.
|
---|
115 | gc.fill = GridBagConstraints.BOTH;
|
---|
116 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
117 | gc.weighty = 1.0;
|
---|
118 | gc.weightx = 1.0;
|
---|
119 | gc.insets = new Insets(10,0,0,0);
|
---|
120 | add(pnlAuthorisationMessage = new JPanel(), gc);
|
---|
121 | pnlAuthorisationMessage.setLayout(new BorderLayout());
|
---|
122 |
|
---|
123 | // create these two panels, they are going to be used later in refreshView
|
---|
124 | //
|
---|
125 | pnlAlreadyAuthorised = new AlreadyAuthorisedPanel();
|
---|
126 | pnlNotYetAuthorised = new NotYetAuthorisedPanel();
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected void refreshView() {
|
---|
130 | pnlAuthorisationMessage.removeAll();
|
---|
131 | if (OAuthAccessTokenHolder.getInstance().containsAccessToken()) {
|
---|
132 | pnlAuthorisationMessage.add(pnlAlreadyAuthorised, BorderLayout.CENTER);
|
---|
133 | pnlAlreadyAuthorised.refreshView();
|
---|
134 | pnlAlreadyAuthorised.revalidate();
|
---|
135 | } else {
|
---|
136 | pnlAuthorisationMessage.add(pnlNotYetAuthorised, BorderLayout.CENTER);
|
---|
137 | pnlNotYetAuthorised.revalidate();
|
---|
138 | }
|
---|
139 | repaint();
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Create the panel
|
---|
144 | */
|
---|
145 | public OAuthAuthenticationPreferencesPanel() {
|
---|
146 | build();
|
---|
147 | refreshView();
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void setApiUrl(String apiUrl) {
|
---|
151 | this.apiUrl = apiUrl;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Initializes the panel from preferences
|
---|
156 | */
|
---|
157 | public void initFromPreferences() {
|
---|
158 | refreshView();
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Saves the current values to preferences
|
---|
163 | */
|
---|
164 | public void saveToPreferences() {
|
---|
165 | OAuthAccessTokenHolder.getInstance().setSaveToPreferences(cbSaveToPreferences.isSelected());
|
---|
166 | OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManagerFactory.getCredentialManager());
|
---|
167 | pnlAdvancedProperties.getAdvancedParameters().saveToPreferences(Main.pref);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * The preferences panel displayed if there is currently no Access Token available.
|
---|
172 | * This means that the user didn't run through the OAuth authorisation procedure yet.
|
---|
173 | *
|
---|
174 | */
|
---|
175 | private class NotYetAuthorisedPanel extends JPanel {
|
---|
176 | protected void build() {
|
---|
177 | setLayout(new GridBagLayout());
|
---|
178 | GridBagConstraints gc = new GridBagConstraints();
|
---|
179 |
|
---|
180 | // A message explaining that the user isn't authorised yet
|
---|
181 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
182 | gc.insets = new Insets(0,0,3,0);
|
---|
183 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
184 | gc.weightx = 1.0;
|
---|
185 | JLabel lbl;
|
---|
186 | add(lbl = new JMultilineLabel(tr("You do not have an Access Token yet to access the OSM server using OAuth. Please authorise first.")), gc);
|
---|
187 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
188 |
|
---|
189 | // Action for authorising now
|
---|
190 | gc.gridy = 1;
|
---|
191 | gc.fill = GridBagConstraints.NONE;
|
---|
192 | gc.weightx = 0.0;
|
---|
193 | add(new SideButton(new AuthoriseNowAction()), gc);
|
---|
194 |
|
---|
195 | // filler - grab remaining space
|
---|
196 | gc.gridy = 2;
|
---|
197 | gc.fill = GridBagConstraints.BOTH;
|
---|
198 | gc.weightx = 1.0;
|
---|
199 | gc.weighty = 1.0;
|
---|
200 | add(new JPanel(), gc);
|
---|
201 | }
|
---|
202 |
|
---|
203 | public NotYetAuthorisedPanel() {
|
---|
204 | build();
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * The preferences panel displayed if there is currently an AccessToken available.
|
---|
210 | *
|
---|
211 | */
|
---|
212 | private class AlreadyAuthorisedPanel extends JPanel {
|
---|
213 | private JTextField tfAccessTokenKey;
|
---|
214 | private JTextField tfAccessTokenSecret;
|
---|
215 |
|
---|
216 | protected void build() {
|
---|
217 | setLayout(new GridBagLayout());
|
---|
218 | GridBagConstraints gc = new GridBagConstraints();
|
---|
219 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
220 | gc.insets = new Insets(0,0,3,3);
|
---|
221 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
222 | gc.weightx = 1.0;
|
---|
223 | gc.gridwidth = 2;
|
---|
224 | JLabel lbl;
|
---|
225 | add(lbl = new JMultilineLabel(tr("You already have an Access Token to access the OSM server using OAuth.")), gc);
|
---|
226 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
227 |
|
---|
228 | // -- access token key
|
---|
229 | gc.gridy = 1;
|
---|
230 | gc.gridx = 0;
|
---|
231 | gc.gridwidth = 1;
|
---|
232 | gc.weightx = 0.0;
|
---|
233 | add(new JLabel(tr("Access Token Key:")), gc);
|
---|
234 |
|
---|
235 | gc.gridx = 1;
|
---|
236 | gc.weightx = 1.0;
|
---|
237 | add(tfAccessTokenKey = new JTextField(), gc);
|
---|
238 | tfAccessTokenKey.setEditable(false);
|
---|
239 |
|
---|
240 | // -- access token secret
|
---|
241 | gc.gridy = 2;
|
---|
242 | gc.gridx = 0;
|
---|
243 | gc.gridwidth = 1;
|
---|
244 | gc.weightx = 0.0;
|
---|
245 | add(new JLabel(tr("Access Token Secret:")), gc);
|
---|
246 |
|
---|
247 | gc.gridx = 1;
|
---|
248 | gc.weightx = 1.0;
|
---|
249 | add(tfAccessTokenSecret = new JTextField(), gc);
|
---|
250 | tfAccessTokenSecret.setEditable(false);
|
---|
251 |
|
---|
252 | // -- access token secret
|
---|
253 | gc.gridy = 3;
|
---|
254 | gc.gridx = 0;
|
---|
255 | gc.gridwidth = 2;
|
---|
256 | gc.weightx = 1.0;
|
---|
257 | add(cbSaveToPreferences = new JCheckBox(tr("Save to preferences")), gc);
|
---|
258 | cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
|
---|
259 |
|
---|
260 | // -- action buttons
|
---|
261 | JPanel btns = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
262 | btns.add(new SideButton(new RenewAuthorisationAction()));
|
---|
263 | btns.add(new SideButton(new TestAuthorisationAction()));
|
---|
264 | gc.gridy = 4;
|
---|
265 | gc.gridx = 0;
|
---|
266 | gc.gridwidth = 2;
|
---|
267 | gc.weightx = 1.0;
|
---|
268 | add(btns, gc);
|
---|
269 |
|
---|
270 | // the panel with the advanced options
|
---|
271 | gc.gridy = 5;
|
---|
272 | gc.gridx = 0;
|
---|
273 | gc.gridwidth = 2;
|
---|
274 | gc.weightx = 1.0;
|
---|
275 | add(buildAdvancedPropertiesPanel(), gc);
|
---|
276 |
|
---|
277 | // filler - grab the remaining space
|
---|
278 | gc.gridy = 6;
|
---|
279 | gc.fill = GridBagConstraints.BOTH;
|
---|
280 | gc.weightx = 1.0;
|
---|
281 | gc.weighty = 1.0;
|
---|
282 | add(new JPanel(), gc);
|
---|
283 |
|
---|
284 | }
|
---|
285 |
|
---|
286 | public void refreshView() {
|
---|
287 | String v = OAuthAccessTokenHolder.getInstance().getAccessTokenKey();
|
---|
288 | tfAccessTokenKey.setText(v == null? "" : v);
|
---|
289 | v = OAuthAccessTokenHolder.getInstance().getAccessTokenSecret();
|
---|
290 | tfAccessTokenSecret.setText(v == null? "" : v);
|
---|
291 | cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
|
---|
292 | }
|
---|
293 |
|
---|
294 | public AlreadyAuthorisedPanel() {
|
---|
295 | build();
|
---|
296 | refreshView();
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Action to authorise the current user
|
---|
302 | */
|
---|
303 | private class AuthoriseNowAction extends AbstractAction {
|
---|
304 | public AuthoriseNowAction() {
|
---|
305 | putValue(NAME, tr("Authorise now"));
|
---|
306 | putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorisation process"));
|
---|
307 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth"));
|
---|
308 |
|
---|
309 | }
|
---|
310 | public void actionPerformed(ActionEvent arg0) {
|
---|
311 | OAuthAuthorisationWizard wizard = new OAuthAuthorisationWizard(
|
---|
312 | OAuthAuthenticationPreferencesPanel.this,
|
---|
313 | apiUrl
|
---|
314 | );
|
---|
315 | wizard.setVisible(true);
|
---|
316 | if (wizard.isCanceled()) return;
|
---|
317 | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
|
---|
318 | holder.setAccessToken(wizard.getAccessToken());
|
---|
319 | holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());
|
---|
320 | pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
|
---|
321 | refreshView();
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Launches the OAuthAuthorisationWizard to generate a new Access Token
|
---|
327 | */
|
---|
328 | private class RenewAuthorisationAction extends AbstractAction {
|
---|
329 | public RenewAuthorisationAction() {
|
---|
330 | putValue(NAME, tr("New Access Token"));
|
---|
331 | putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorisation process and generate a new Access Token"));
|
---|
332 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth"));
|
---|
333 |
|
---|
334 | }
|
---|
335 | public void actionPerformed(ActionEvent arg0) {
|
---|
336 | OAuthAuthorisationWizard wizard = new OAuthAuthorisationWizard(
|
---|
337 | OAuthAuthenticationPreferencesPanel.this,
|
---|
338 | apiUrl
|
---|
339 | );
|
---|
340 | wizard.setVisible(true);
|
---|
341 | if (wizard.isCanceled()) return;
|
---|
342 | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
|
---|
343 | holder.setAccessToken(wizard.getAccessToken());
|
---|
344 | holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());
|
---|
345 | pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
|
---|
346 | refreshView();
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Runs a test whether we can access the OSM server with the current Access Token
|
---|
352 | */
|
---|
353 | private class TestAuthorisationAction extends AbstractAction {
|
---|
354 | public TestAuthorisationAction() {
|
---|
355 | putValue(NAME, tr("Test Access Token"));
|
---|
356 | putValue(SHORT_DESCRIPTION, tr("Click test access to the OSM server with the current access token"));
|
---|
357 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth"));
|
---|
358 |
|
---|
359 | }
|
---|
360 |
|
---|
361 | public void actionPerformed(ActionEvent evt) {
|
---|
362 | OAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken();
|
---|
363 | OAuthParameters parameters = OAuthParameters.createFromPreferences(Main.pref);
|
---|
364 | TestAccessTokenTask task = new TestAccessTokenTask(
|
---|
365 | OAuthAuthenticationPreferencesPanel.this,
|
---|
366 | apiUrl,
|
---|
367 | parameters,
|
---|
368 | token
|
---|
369 | );
|
---|
370 | Main.worker.submit(task);
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
375 | if (! evt.getPropertyName().equals(OsmApiUrlInputPanel.API_URL_PROP))
|
---|
376 | return;
|
---|
377 | apiUrl = (String)evt.getNewValue();
|
---|
378 | }
|
---|
379 | }
|
---|