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