source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java

Last change on this file was 19008, checked in by taylor.smock, 7 weeks ago

Fix an issue with custom OAuth2 parameters where the custom parameters would be replaced by default parameters

  • Property svn:eol-style set to native
File size: 14.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.FlowLayout;
9import java.awt.Font;
10import java.awt.GridBagConstraints;
11import java.awt.GridBagLayout;
12import java.awt.Insets;
13import java.awt.event.ActionEvent;
14import java.io.IOException;
15import java.util.concurrent.Executor;
16
17import javax.swing.AbstractAction;
18import javax.swing.BorderFactory;
19import javax.swing.JButton;
20import javax.swing.JLabel;
21import javax.swing.JOptionPane;
22import javax.swing.JPanel;
23import javax.swing.JTabbedPane;
24import javax.swing.text.html.HTMLEditorKit;
25
26import org.openstreetmap.josm.data.oauth.IOAuthToken;
27import org.openstreetmap.josm.data.oauth.OAuthVersion;
28import org.openstreetmap.josm.gui.HelpAwareOptionPane;
29import org.openstreetmap.josm.gui.PleaseWaitRunnable;
30import org.openstreetmap.josm.gui.help.HelpUtil;
31import org.openstreetmap.josm.gui.util.GuiHelper;
32import org.openstreetmap.josm.gui.widgets.HtmlPanel;
33import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
34import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
35import org.openstreetmap.josm.io.OsmTransferException;
36import org.openstreetmap.josm.tools.ImageProvider;
37import org.openstreetmap.josm.tools.Logging;
38import org.xml.sax.SAXException;
39
40/**
41 * This is a UI which supports a JOSM user to get an OAuth Access Token in a fully
42 * automatic process.
43 *
44 * @since 2746
45 */
46public class FullyAutomaticAuthorizationUI extends AbstractAuthorizationUI {
47 private final AccessTokenInfoPanel pnlAccessTokenInfo = new AccessTokenInfoPanel();
48 private OsmPrivilegesPanel pnlOsmPrivileges;
49 private JPanel pnlPropertiesPanel;
50 private JPanel pnlActionButtonsPanel;
51 private JPanel pnlResult;
52 private final transient Executor executor;
53
54 /**
55 * Builds the panel with the three privileges the user can grant JOSM
56 *
57 * @return constructed panel for the privileges
58 */
59 protected VerticallyScrollablePanel buildGrantsPanel() {
60 pnlOsmPrivileges = new OsmPrivilegesPanel();
61 return pnlOsmPrivileges;
62 }
63
64 /**
65 * Builds the panel for entering the username and password
66 *
67 * @return constructed panel for the credentials
68 */
69 protected VerticallyScrollablePanel buildUserNamePasswordPanel() {
70 VerticallyScrollablePanel pnl = new VerticallyScrollablePanel(new GridBagLayout());
71 GridBagConstraints gc = new GridBagConstraints();
72 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
73
74 gc.anchor = GridBagConstraints.NORTHWEST;
75 gc.fill = GridBagConstraints.HORIZONTAL;
76 gc.weightx = 1.0;
77 gc.gridwidth = 2;
78 HtmlPanel pnlMessage = new HtmlPanel();
79 HTMLEditorKit kit = (HTMLEditorKit) pnlMessage.getEditorPane().getEditorKit();
80 kit.getStyleSheet().addRule(
81 ".warning-body {background-color:#DDFFDD; padding: 10pt; " +
82 "border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
83 kit.getStyleSheet().addRule("ol {margin-left: 1cm}");
84 pnlMessage.setText("<html><body><p class=\"warning-body\">"
85 + tr("Please enter your OSM user name and password. The password will <strong>not</strong> be saved "
86 + "in clear text in the JOSM preferences and it will be submitted to the OSM server <strong>only once</strong>. "
87 + "Subsequent data upload requests don''t use your password any more.").replace(". ", ".<br>")
88 + "</p>"
89 + "</body></html>");
90 pnl.add(pnlMessage, gc);
91
92 // the user name input field
93 gc.gridy = 1;
94 gc.gridwidth = 1;
95 gc.anchor = GridBagConstraints.NORTHWEST;
96 gc.fill = GridBagConstraints.HORIZONTAL;
97 gc.weightx = 0.0;
98 gc.insets = new Insets(0, 0, 3, 3);
99 pnl.add(new JLabel(tr("Username: ")), gc);
100
101 // the password input field
102 gc.anchor = GridBagConstraints.NORTHWEST;
103 gc.fill = GridBagConstraints.HORIZONTAL;
104 gc.gridy = 2;
105 gc.gridx = 0;
106 gc.weightx = 0.0;
107 pnl.add(new JLabel(tr("Password:")), gc);
108
109 // filler - grab remaining space
110 gc.gridx = 1;
111 gc.gridy = 4;
112 gc.gridwidth = 2;
113 gc.fill = GridBagConstraints.BOTH;
114 gc.weightx = 1.0;
115 gc.weighty = 1.0;
116 pnl.add(new JPanel(), gc);
117
118 return pnl;
119 }
120
121 protected JPanel buildPropertiesPanel() {
122 JPanel pnl = new JPanel(new BorderLayout());
123
124 JTabbedPane tpProperties = new JTabbedPane();
125 tpProperties.add(buildUserNamePasswordPanel().getVerticalScrollPane());
126 tpProperties.add(buildGrantsPanel().getVerticalScrollPane());
127 tpProperties.add(getAdvancedPropertiesPanel().getVerticalScrollPane());
128 tpProperties.setTitleAt(0, tr("Basic"));
129 tpProperties.setTitleAt(1, tr("Granted rights"));
130 tpProperties.setTitleAt(2, tr("Advanced OAuth properties"));
131
132 pnl.add(tpProperties, BorderLayout.CENTER);
133 return pnl;
134 }
135
136 /**
137 * Builds the panel with the action button for starting the authorisation
138 *
139 * @return constructed button panel
140 */
141 protected JPanel buildActionButtonPanel() {
142 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
143
144 RunAuthorisationAction runAuthorisationAction = new RunAuthorisationAction();
145 pnl.add(new JButton(runAuthorisationAction));
146 return pnl;
147 }
148
149 /**
150 * Builds the panel which displays the generated Access Token.
151 *
152 * @return constructed panel for the results
153 */
154 protected JPanel buildResultsPanel() {
155 JPanel pnl = new JPanel(new GridBagLayout());
156 GridBagConstraints gc = new GridBagConstraints();
157 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
158
159 // the message panel
160 gc.anchor = GridBagConstraints.NORTHWEST;
161 gc.fill = GridBagConstraints.HORIZONTAL;
162 gc.weightx = 1.0;
163 JMultilineLabel msg = new JMultilineLabel("");
164 msg.setFont(msg.getFont().deriveFont(Font.PLAIN));
165 String lbl = tr("Accept Access Token");
166 msg.setText(tr("<html>"
167 + "You have successfully retrieved an OAuth Access Token from the OSM website. "
168 + "Click on <strong>{0}</strong> to accept the token. JOSM will use it in "
169 + "subsequent requests to gain access to the OSM API."
170 + "</html>", lbl));
171 pnl.add(msg, gc);
172
173 // infos about the access token
174 gc.gridy = 1;
175 gc.insets = new Insets(5, 0, 0, 0);
176 pnl.add(pnlAccessTokenInfo, gc);
177
178 // the actions
179 JPanel pnl1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
180 pnl1.add(new JButton(new BackAction()));
181 pnl1.add(new JButton(new TestAccessTokenAction()));
182 gc.gridy = 2;
183 pnl.add(pnl1, gc);
184
185 // filler - grab the remaining space
186 gc.gridy = 3;
187 gc.fill = GridBagConstraints.BOTH;
188 gc.weightx = 1.0;
189 gc.weighty = 1.0;
190 pnl.add(new JPanel(), gc);
191
192 return pnl;
193 }
194
195 protected final void build() {
196 setLayout(new BorderLayout());
197 pnlPropertiesPanel = buildPropertiesPanel();
198 pnlActionButtonsPanel = buildActionButtonPanel();
199 pnlResult = buildResultsPanel();
200
201 prepareUIForEnteringRequest();
202 }
203
204 /**
205 * Prepares the UI for the first step in the automatic process: entering the authentication
206 * and authorisation parameters.
207 *
208 */
209 protected void prepareUIForEnteringRequest() {
210 removeAll();
211 add(pnlPropertiesPanel, BorderLayout.CENTER);
212 add(pnlActionButtonsPanel, BorderLayout.SOUTH);
213 pnlPropertiesPanel.revalidate();
214 pnlActionButtonsPanel.revalidate();
215 validate();
216 repaint();
217
218 setAccessToken(null);
219 }
220
221 /**
222 * Prepares the UI for the second step in the automatic process: displaying the access token
223 *
224 */
225 protected void prepareUIForResultDisplay() {
226 removeAll();
227 add(pnlResult, BorderLayout.CENTER);
228 validate();
229 repaint();
230 }
231
232 /**
233 * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL.
234 * @param apiUrl The OSM API URL
235 * @param executor the executor used for running the HTTP requests for the authorization
236 * @since 5422
237 * @deprecated since 18991
238 */
239 @Deprecated
240 public FullyAutomaticAuthorizationUI(String apiUrl, Executor executor) {
241 this(apiUrl, executor, OAuthVersion.OAuth10a);
242 }
243
244 /**
245 * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL.
246 * @param apiUrl The OSM API URL
247 * @param executor the executor used for running the HTTP requests for the authorization
248 * @param oAuthVersion The OAuth version to use for this UI
249 * @since 18991
250 */
251 public FullyAutomaticAuthorizationUI(String apiUrl, Executor executor, OAuthVersion oAuthVersion) {
252 super(apiUrl, oAuthVersion);
253 this.executor = executor;
254 build();
255 }
256
257 @Override
258 public boolean isSaveAccessTokenToPreferences() {
259 return pnlAccessTokenInfo.isSaveToPreferences();
260 }
261
262 @Override
263 protected void setAccessToken(IOAuthToken accessToken) {
264 super.setAccessToken(accessToken);
265 pnlAccessTokenInfo.setAccessToken(accessToken);
266 }
267
268 /**
269 * Starts the authorisation process
270 */
271 class RunAuthorisationAction extends AbstractAction {
272 RunAuthorisationAction() {
273 putValue(NAME, tr("Authorize now"));
274 new ImageProvider("oauth", "oauth-small").getResource().attachImageIcon(this);
275 putValue(SHORT_DESCRIPTION, tr("Click to redirect you to the authorization form on the JOSM web site"));
276 }
277
278 @Override
279 public void actionPerformed(ActionEvent evt) {
280 executor.execute(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this));
281 }
282 }
283
284 /**
285 * Action to go back to step 1 in the process
286 */
287 class BackAction extends AbstractAction {
288 BackAction() {
289 putValue(NAME, tr("Back"));
290 putValue(SHORT_DESCRIPTION, tr("Run the automatic authorization steps again"));
291 new ImageProvider("dialogs", "previous").getResource().attachImageIcon(this);
292 }
293
294 @Override
295 public void actionPerformed(ActionEvent arg0) {
296 prepareUIForEnteringRequest();
297 }
298 }
299
300 /**
301 * Action to test an access token.
302 */
303 class TestAccessTokenAction extends AbstractAction {
304 TestAccessTokenAction() {
305 putValue(NAME, tr("Test Access Token"));
306 new ImageProvider("logo").getResource().attachImageIcon(this);
307 }
308
309 @Override
310 public void actionPerformed(ActionEvent arg0) {
311 executor.execute(new TestAccessTokenTask(
312 FullyAutomaticAuthorizationUI.this,
313 getApiUrl(),
314 getAccessToken()
315 ));
316 }
317 }
318
319 class FullyAutomaticAuthorisationTask extends PleaseWaitRunnable {
320 private boolean canceled;
321
322 FullyAutomaticAuthorisationTask(Component parent) {
323 super(parent, tr("Authorize JOSM to access the OSM API"), false /* don't ignore exceptions */);
324 }
325
326 @Override
327 protected void cancel() {
328 canceled = true;
329 }
330
331 @Override
332 protected void finish() {
333 // Do nothing
334 }
335
336 protected void alertAuthorisationFailed() {
337 HelpAwareOptionPane.showOptionDialog(
338 FullyAutomaticAuthorizationUI.this,
339 tr("<html>"
340 + "The automatic process for retrieving an OAuth Access Token<br>"
341 + "from the OSM server failed.<br><br>"
342 + "Please try again or choose another kind of authorization process,<br>"
343 + "i.e. semi-automatic or manual authorization."
344 +"</html>"),
345 tr("OAuth authorization failed"),
346 JOptionPane.ERROR_MESSAGE,
347 HelpUtil.ht("/Dialog/OAuthAuthorisationWizard#FullyAutomaticProcessFailed")
348 );
349 }
350
351 protected void alertInvalidLoginUrl() {
352 HelpAwareOptionPane.showOptionDialog(
353 FullyAutomaticAuthorizationUI.this,
354 tr("<html>"
355 + "The automatic process for retrieving an OAuth Access Token<br>"
356 + "from the OSM server failed because JOSM was not able to build<br>"
357 + "a valid login URL from the OAuth Authorize Endpoint URL ''{0}''.<br><br>"
358 + "Please check your advanced setting and try again."
359 + "</html>",
360 getAdvancedPropertiesPanel().getAdvancedParameters().getAuthorizationUrl()),
361 tr("OAuth authorization failed"),
362 JOptionPane.ERROR_MESSAGE,
363 HelpUtil.ht("/Dialog/OAuthAuthorisationWizard#FullyAutomaticProcessFailed")
364 );
365 }
366
367 protected void handleException(final OsmOAuthAuthorizationException e) {
368 Logging.error(e);
369 GuiHelper.runInEDT(this::alertAuthorisationFailed);
370 }
371
372 @Override
373 protected void realRun() throws SAXException, IOException, OsmTransferException {
374 getProgressMonitor().setTicksCount(2);
375 OAuthAuthorizationWizard.authorize(true, token -> {
376 if (!canceled) {
377 getProgressMonitor().worked(1);
378 GuiHelper.runInEDT(() -> {
379 prepareUIForResultDisplay();
380 setAccessToken(token.orElse(null));
381 });
382 }
383 }, getApiUrl(), getOAuthVersion(), getOAuthParameters());
384 getProgressMonitor().worked(1);
385 }
386 }
387}
Note: See TracBrowser for help on using the repository browser.