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.JOptionPane;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.JPasswordField;
|
---|
22 | import javax.swing.JTextField;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
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: refactor this crap (maybe just insert the damn auth http-header by yourself)
|
---|
52 | try {
|
---|
53 | HttpURLConnection.setFollowRedirects(true);
|
---|
54 | Authenticator.setDefault(authentication);
|
---|
55 | } catch (SecurityException e) {
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * The authentication class handling the login requests.
|
---|
61 | */
|
---|
62 | private static class OsmAuth extends Authenticator {
|
---|
63 | /**
|
---|
64 | * Set to true, when the autenticator tried the password once.
|
---|
65 | */
|
---|
66 | boolean passwordtried = false;
|
---|
67 | /**
|
---|
68 | * Whether the user cancelled the password dialog
|
---|
69 | */
|
---|
70 | boolean authCancelled = false;
|
---|
71 |
|
---|
72 | @Override protected PasswordAuthentication getPasswordAuthentication() {
|
---|
73 | String username = Main.pref.get("osm-server.username");
|
---|
74 | String password = Main.pref.get("osm-server.password");
|
---|
75 | if (passwordtried || username.equals("") || password.equals("")) {
|
---|
76 | JPanel p = new JPanel(new GridBagLayout());
|
---|
77 | if (!username.equals("") && !password.equals(""))
|
---|
78 | p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop());
|
---|
79 | p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0));
|
---|
80 | JTextField usernameField = new JTextField(username, 20);
|
---|
81 | p.add(usernameField, GBC.eol());
|
---|
82 | p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0));
|
---|
83 | JPasswordField passwordField = new JPasswordField(password, 20);
|
---|
84 | p.add(passwordField, GBC.eol());
|
---|
85 | JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted."));
|
---|
86 | warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
|
---|
87 | p.add(warning, GBC.eop());
|
---|
88 |
|
---|
89 | JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals(""));
|
---|
90 | p.add(savePassword, GBC.eop());
|
---|
91 |
|
---|
92 | int choice = JOptionPane.showConfirmDialog(Main.parent, p, tr("Enter Password"), JOptionPane.OK_CANCEL_OPTION);
|
---|
93 | if (choice == JOptionPane.CANCEL_OPTION) {
|
---|
94 | authCancelled = true;
|
---|
95 | return null;
|
---|
96 | }
|
---|
97 | username = usernameField.getText();
|
---|
98 | password = String.valueOf(passwordField.getPassword());
|
---|
99 | if (savePassword.isSelected()) {
|
---|
100 | Main.pref.put("osm-server.username", username);
|
---|
101 | Main.pref.put("osm-server.password", password);
|
---|
102 | }
|
---|
103 | if (username.equals(""))
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 | passwordtried = true;
|
---|
107 | return new PasswordAuthentication(username, password.toCharArray());
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Must be called before each connection attemp to initialize the authentication.
|
---|
113 | */
|
---|
114 | protected final void initAuthentication() {
|
---|
115 | authentication.authCancelled = false;
|
---|
116 | authentication.passwordtried = false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * @return Whether the connection was cancelled.
|
---|
121 | */
|
---|
122 | protected final boolean isAuthCancelled() {
|
---|
123 | return authentication.authCancelled;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void cancel() {
|
---|
127 | Main.pleaseWaitDlg.currentAction.setText(tr("Aborting..."));
|
---|
128 | cancel = true;
|
---|
129 | if (activeConnection != null) {
|
---|
130 | activeConnection.setConnectTimeout(100);
|
---|
131 | activeConnection.setReadTimeout(100);
|
---|
132 | try {
|
---|
133 | Thread.sleep(100);
|
---|
134 | } catch (InterruptedException ex) {}
|
---|
135 | activeConnection.disconnect();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected void addAuth(HttpURLConnection con) throws CharacterCodingException {
|
---|
140 | CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
|
---|
141 | String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password");
|
---|
142 | ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth));
|
---|
143 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
|
---|
144 | }
|
---|
145 | }
|
---|