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.GridBagConstraints;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.Insets;
|
---|
9 | import java.net.PasswordAuthentication;
|
---|
10 | import java.net.Authenticator.RequestorType;
|
---|
11 |
|
---|
12 | import javax.swing.BorderFactory;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 | import javax.swing.JPasswordField;
|
---|
16 | import javax.swing.JTextField;
|
---|
17 | import javax.swing.text.html.HTMLEditorKit;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
22 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
23 | import org.openstreetmap.josm.io.auth.CredentialsManagerException;
|
---|
24 | import org.openstreetmap.josm.io.auth.CredentialsManagerFactory;
|
---|
25 | import org.openstreetmap.josm.io.auth.JosmPreferencesCredentialManager;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * The preferences panel for parameters necessary for the Basic Authentication
|
---|
29 | * Scheme.
|
---|
30 | *
|
---|
31 | */
|
---|
32 | public class BasicAuthenticationPreferencesPanel extends JPanel {
|
---|
33 |
|
---|
34 | /** the OSM user name */
|
---|
35 | private JTextField tfOsmUserName;
|
---|
36 | private UserNameValidator valUserName;
|
---|
37 | /** the OSM password */
|
---|
38 | private JPasswordField tfOsmPassword;
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * builds the UI
|
---|
43 | */
|
---|
44 | protected void build() {
|
---|
45 | setLayout(new GridBagLayout());
|
---|
46 | setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
|
---|
47 | GridBagConstraints gc = new GridBagConstraints();
|
---|
48 |
|
---|
49 | // -- OSM user name
|
---|
50 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
51 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
52 | gc.weightx = 0.0;
|
---|
53 | gc.insets = new Insets(0,0,3,3);
|
---|
54 | add(new JLabel(tr("OSM username:")), gc);
|
---|
55 |
|
---|
56 | gc.gridx = 1;
|
---|
57 | gc.weightx = 1.0;
|
---|
58 | add(tfOsmUserName = new JTextField(), gc);
|
---|
59 | SelectAllOnFocusGainedDecorator.decorate(tfOsmUserName);
|
---|
60 | valUserName = new UserNameValidator(tfOsmUserName);
|
---|
61 | valUserName.validate();
|
---|
62 |
|
---|
63 | // -- OSM password
|
---|
64 | gc.gridx = 0;
|
---|
65 | gc.gridy = 1;
|
---|
66 | gc.weightx = 0.0;
|
---|
67 | add(new JLabel(tr("OSM password:")), gc);
|
---|
68 |
|
---|
69 | gc.gridx = 1;
|
---|
70 | gc.weightx = 1.0;
|
---|
71 | add(tfOsmPassword = new JPasswordField(), gc);
|
---|
72 | SelectAllOnFocusGainedDecorator.decorate(tfOsmPassword);
|
---|
73 | tfOsmPassword.setToolTipText(tr("Please enter your OSM password"));
|
---|
74 |
|
---|
75 | // -- an info panel with a warning message
|
---|
76 | gc.gridx = 0;
|
---|
77 | gc.gridy = 2;
|
---|
78 | gc.gridwidth = 2;
|
---|
79 | gc.weightx = 1.0;
|
---|
80 | gc.weighty = 1.0;
|
---|
81 | gc.insets = new Insets(5,0,0,0);
|
---|
82 | gc.fill = GridBagConstraints.BOTH;
|
---|
83 | HtmlPanel pnlMessage = new HtmlPanel();
|
---|
84 | HTMLEditorKit kit = (HTMLEditorKit)pnlMessage.getEditorPane().getEditorKit();
|
---|
85 | kit.getStyleSheet().addRule(".warning-body {background-color:rgb(253,255,221);padding: 10pt; border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
|
---|
86 | pnlMessage.setText(
|
---|
87 | tr(
|
---|
88 | "<html><body>"
|
---|
89 | + "<p class=\"warning-body\">"
|
---|
90 | + "<strong>Warning:</strong> The password is stored in plain text in the JOSM preferences file. "
|
---|
91 | + "Furthermore, it is transferred <strong>unencrypted</strong> in every request sent to the OSM server. "
|
---|
92 | + "<strong>Do not use a valuable password.</strong>"
|
---|
93 | + "</p>"
|
---|
94 | + "</body></html>"
|
---|
95 | )
|
---|
96 | );
|
---|
97 | add(pnlMessage, gc);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public BasicAuthenticationPreferencesPanel() {
|
---|
101 | build();
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void initFromPreferences() {
|
---|
105 | CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
|
---|
106 | try {
|
---|
107 | PasswordAuthentication pa = cm.lookup(RequestorType.SERVER);
|
---|
108 | if (pa == null) {
|
---|
109 | tfOsmUserName.setText("");
|
---|
110 | tfOsmPassword.setText("");
|
---|
111 | } else {
|
---|
112 | tfOsmUserName.setText(pa.getUserName() == null? "" : pa.getUserName());
|
---|
113 | tfOsmPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
|
---|
114 | }
|
---|
115 | } catch(CredentialsManagerException e) {
|
---|
116 | e.printStackTrace();
|
---|
117 | System.err.println(tr("Warning: failed to retrieve OSM credentials from credential manager."));
|
---|
118 | System.err.println(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
|
---|
119 | tfOsmUserName.setText("");
|
---|
120 | tfOsmPassword.setText("");
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void saveToPreferences() {
|
---|
125 | CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
|
---|
126 | try {
|
---|
127 | PasswordAuthentication pa = new PasswordAuthentication(
|
---|
128 | tfOsmUserName.getText().trim(),
|
---|
129 | tfOsmPassword.getPassword()
|
---|
130 | );
|
---|
131 | cm.store(RequestorType.SERVER, pa);
|
---|
132 | // always save the username to the preferences if it isn't already saved there
|
---|
133 | // by the credential manager
|
---|
134 | if (! (cm instanceof JosmPreferencesCredentialManager)) {
|
---|
135 | Main.pref.put("osm-server.username", tfOsmUserName.getText().trim());
|
---|
136 | }
|
---|
137 | } catch(CredentialsManagerException e) {
|
---|
138 | e.printStackTrace();
|
---|
139 | System.err.println(tr("Warning: failed to save OSM credentials to credential manager."));
|
---|
140 | System.err.println(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | public void clearPassword() {
|
---|
145 | tfOsmPassword.setText("");
|
---|
146 | }
|
---|
147 | }
|
---|