1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Font;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.net.Authenticator;
|
---|
9 | import java.net.HttpURLConnection;
|
---|
10 | import java.net.PasswordAuthentication;
|
---|
11 | import java.nio.ByteBuffer;
|
---|
12 | import java.nio.CharBuffer;
|
---|
13 | import java.nio.charset.Charset;
|
---|
14 | import java.nio.charset.CharsetEncoder;
|
---|
15 | import java.nio.charset.CharacterCodingException;
|
---|
16 |
|
---|
17 | import javax.swing.JCheckBox;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 | import javax.swing.JPasswordField;
|
---|
21 | import javax.swing.JTextField;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
25 | import org.openstreetmap.josm.tools.Base64;
|
---|
26 | import org.openstreetmap.josm.tools.GBC;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Base class that handles common things like authentication for the reader and writer
|
---|
30 | * to the osm server.
|
---|
31 | *
|
---|
32 | * @author imi
|
---|
33 | */
|
---|
34 | public class OsmConnection {
|
---|
35 |
|
---|
36 | public static class OsmParseException extends Exception {
|
---|
37 | public OsmParseException() {super();}
|
---|
38 | public OsmParseException(String message, Throwable cause) {super(message, cause);}
|
---|
39 | public OsmParseException(String message) {super(message);}
|
---|
40 | public OsmParseException(Throwable cause) {super(cause);}
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected boolean cancel = false;
|
---|
44 | protected HttpURLConnection activeConnection;
|
---|
45 |
|
---|
46 | private static OsmAuth authentication = new OsmAuth();
|
---|
47 | /**
|
---|
48 | * Initialize the http defaults and the authenticator.
|
---|
49 | */
|
---|
50 | static {
|
---|
51 | // TODO: current authentication handling is sub-optimal in that it seems to use the same authenticator for
|
---|
52 | // any kind of request. HTTP requests executed by plugins, e.g. to password-protected WMS servers,
|
---|
53 | // will use the same username/password which is undesirable.
|
---|
54 | try {
|
---|
55 | HttpURLConnection.setFollowRedirects(true);
|
---|
56 | Authenticator.setDefault(authentication);
|
---|
57 | } catch (SecurityException e) {
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * The authentication class handling the login requests.
|
---|
63 | */
|
---|
64 | private static class OsmAuth extends Authenticator {
|
---|
65 | /**
|
---|
66 | * Set to true, when the autenticator tried the password once.
|
---|
67 | */
|
---|
68 | boolean passwordtried = false;
|
---|
69 | /**
|
---|
70 | * Whether the user cancelled the password dialog
|
---|
71 | */
|
---|
72 | boolean authCancelled = false;
|
---|
73 |
|
---|
74 | @Override protected PasswordAuthentication getPasswordAuthentication() {
|
---|
75 | String username = Main.pref.get("osm-server.username");
|
---|
76 | String password = Main.pref.get("osm-server.password");
|
---|
77 | if (passwordtried || username.equals("") || password.equals("")) {
|
---|
78 | JPanel p = new JPanel(new GridBagLayout());
|
---|
79 | if (!username.equals("") && !password.equals(""))
|
---|
80 | p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop());
|
---|
81 | p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0));
|
---|
82 | JTextField usernameField = new JTextField(username, 20);
|
---|
83 | p.add(usernameField, GBC.eol());
|
---|
84 | p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0));
|
---|
85 | JPasswordField passwordField = new JPasswordField(password, 20);
|
---|
86 | p.add(passwordField, GBC.eol());
|
---|
87 | JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted."));
|
---|
88 | warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
|
---|
89 | p.add(warning, GBC.eop());
|
---|
90 |
|
---|
91 | JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals(""));
|
---|
92 | p.add(savePassword, GBC.eop());
|
---|
93 |
|
---|
94 | int choice = new ExtendedDialog(Main.parent,
|
---|
95 | tr("Enter Password"),
|
---|
96 | p,
|
---|
97 | new String[] {tr("Login"), tr("Cancel")},
|
---|
98 | new String[] {"ok.png", "cancel.png"}).getValue();
|
---|
99 |
|
---|
100 | if (choice != 1) {
|
---|
101 | authCancelled = true;
|
---|
102 | return null;
|
---|
103 | }
|
---|
104 | username = usernameField.getText();
|
---|
105 | password = String.valueOf(passwordField.getPassword());
|
---|
106 | if (savePassword.isSelected()) {
|
---|
107 | Main.pref.put("osm-server.username", username);
|
---|
108 | Main.pref.put("osm-server.password", password);
|
---|
109 | }
|
---|
110 | if (username.equals(""))
|
---|
111 | return null;
|
---|
112 | }
|
---|
113 | passwordtried = true;
|
---|
114 | return new PasswordAuthentication(username, password.toCharArray());
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Must be called before each connection attemp to initialize the authentication.
|
---|
120 | */
|
---|
121 | protected final void initAuthentication() {
|
---|
122 | authentication.authCancelled = false;
|
---|
123 | authentication.passwordtried = false;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * @return Whether the connection was cancelled.
|
---|
128 | */
|
---|
129 | protected final boolean isAuthCancelled() {
|
---|
130 | return authentication.authCancelled;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public void cancel() {
|
---|
134 | Main.pleaseWaitDlg.currentAction.setText(tr("Aborting..."));
|
---|
135 | cancel = true;
|
---|
136 | if (activeConnection != null) {
|
---|
137 | activeConnection.setConnectTimeout(100);
|
---|
138 | activeConnection.setReadTimeout(100);
|
---|
139 | try {
|
---|
140 | Thread.sleep(100);
|
---|
141 | } catch (InterruptedException ex) {}
|
---|
142 | activeConnection.disconnect();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected void addAuth(HttpURLConnection con) throws CharacterCodingException {
|
---|
147 | CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
|
---|
148 | String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password");
|
---|
149 | ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth));
|
---|
150 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
|
---|
151 | }
|
---|
152 | }
|
---|