1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Font;
|
---|
7 | import java.net.PasswordAuthentication;
|
---|
8 | import java.net.Authenticator.RequestorType;
|
---|
9 |
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JPasswordField;
|
---|
12 | import javax.swing.JTextField;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
16 | import org.openstreetmap.josm.io.auth.CredentialsManagerException;
|
---|
17 | import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
|
---|
18 | import org.openstreetmap.josm.tools.GBC;
|
---|
19 |
|
---|
20 | public class ServerAccessPreference implements PreferenceSetting {
|
---|
21 |
|
---|
22 | public static class Factory implements PreferenceSettingFactory {
|
---|
23 | public PreferenceSetting createPreferenceSetting() {
|
---|
24 | return new ServerAccessPreference();
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Editfield for the Base url to the REST API from OSM.
|
---|
30 | */
|
---|
31 | final private JTextField osmDataServerURL = new JTextField(20);
|
---|
32 | /**
|
---|
33 | * Editfield for the username to the OSM account.
|
---|
34 | */
|
---|
35 | final private JTextField osmDataUsername = new JTextField(20);
|
---|
36 | /**
|
---|
37 | * Passwordfield for the userpassword of the REST API.
|
---|
38 | */
|
---|
39 | final private JPasswordField osmDataPassword = new JPasswordField(20);
|
---|
40 |
|
---|
41 | public void addGui(PreferenceDialog gui) {
|
---|
42 | CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
|
---|
43 | String oldServerURL = Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api");
|
---|
44 | String oldUsername;
|
---|
45 | String oldPassword;
|
---|
46 | try {
|
---|
47 | PasswordAuthentication credentials = cm.lookup(RequestorType.SERVER);
|
---|
48 | oldUsername = (credentials == null | credentials.getUserName() == null) ? "" : credentials.getUserName();
|
---|
49 | oldPassword = (credentials == null | credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
|
---|
50 | } catch(CredentialsManagerException e) {
|
---|
51 | e.printStackTrace();
|
---|
52 | oldUsername = "";
|
---|
53 | oldPassword = "";
|
---|
54 | }
|
---|
55 |
|
---|
56 | osmDataServerURL.setText(oldServerURL);
|
---|
57 | osmDataUsername.setText(oldUsername);
|
---|
58 | osmDataPassword.setText(oldPassword);
|
---|
59 | osmDataServerURL.setToolTipText(tr("The base URL for the OSM server (REST API)"));
|
---|
60 | osmDataUsername.setToolTipText(tr("Login name (e-mail) to the OSM account."));
|
---|
61 | osmDataPassword.setToolTipText(tr("Login password to the OSM account. Leave blank to not store any password."));
|
---|
62 |
|
---|
63 | gui.connection.add(new JLabel(tr("Base Server URL")), GBC.std());
|
---|
64 | gui.connection.add(osmDataServerURL, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
|
---|
65 | gui.connection.add(new JLabel(tr("OSM username (e-mail)")), GBC.std());
|
---|
66 | gui.connection.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
|
---|
67 | gui.connection.add(new JLabel(tr("OSM password")), GBC.std());
|
---|
68 | gui.connection.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0));
|
---|
69 | JLabel warning = new JLabel(tr("<html>" +
|
---|
70 | "WARNING: The password is stored in plain text in the preferences file.<br>" +
|
---|
71 | "The password is transferred in plain text to the server, encoded in the URL.<br>" +
|
---|
72 | "<b>Do not use a valuable Password.</b></html>"));
|
---|
73 | warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
|
---|
74 | gui.connection.add(warning, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
75 | }
|
---|
76 |
|
---|
77 | public boolean ok() {
|
---|
78 | CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
|
---|
79 | Main.pref.put("osm-server.url", osmDataServerURL.getText());
|
---|
80 | try {
|
---|
81 | cm.store(RequestorType.SERVER, new PasswordAuthentication(
|
---|
82 | osmDataUsername.getText(),
|
---|
83 | osmDataPassword.getPassword()
|
---|
84 | ));
|
---|
85 | } catch(CredentialsManagerException e) {
|
---|
86 | // FIXME: Message dialog with an error message?
|
---|
87 | e.printStackTrace();
|
---|
88 | }
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 | }
|
---|