1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.net.Authenticator.RequestorType;
|
---|
7 | import java.net.HttpURLConnection;
|
---|
8 | import java.nio.ByteBuffer;
|
---|
9 | import java.nio.CharBuffer;
|
---|
10 | import java.nio.charset.CharacterCodingException;
|
---|
11 | import java.nio.charset.Charset;
|
---|
12 | import java.nio.charset.CharsetEncoder;
|
---|
13 |
|
---|
14 | import oauth.signpost.OAuthConsumer;
|
---|
15 | import oauth.signpost.exception.OAuthException;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.data.oauth.OAuthParameters;
|
---|
19 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
|
---|
20 | import org.openstreetmap.josm.io.auth.CredentialsAgentException;
|
---|
21 | import org.openstreetmap.josm.io.auth.CredentialsAgentResponse;
|
---|
22 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
23 | import org.openstreetmap.josm.tools.Base64;
|
---|
24 | import org.openstreetmap.josm.tools.Utils;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Base class that handles common things like authentication for the reader and writer
|
---|
28 | * to the osm server.
|
---|
29 | *
|
---|
30 | * @author imi
|
---|
31 | */
|
---|
32 | public class OsmConnection {
|
---|
33 | protected boolean cancel = false;
|
---|
34 | protected HttpURLConnection activeConnection;
|
---|
35 | protected OAuthParameters oauthParameters;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Initialize the http defaults and the authenticator.
|
---|
39 | */
|
---|
40 | static {
|
---|
41 | try {
|
---|
42 | HttpURLConnection.setFollowRedirects(true);
|
---|
43 | } catch (SecurityException e) {
|
---|
44 | e.printStackTrace();
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void cancel() {
|
---|
49 | cancel = true;
|
---|
50 | synchronized (this) {
|
---|
51 | if (activeConnection != null) {
|
---|
52 | activeConnection.setConnectTimeout(100);
|
---|
53 | activeConnection.setReadTimeout(100);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | try {
|
---|
57 | Thread.sleep(100);
|
---|
58 | } catch (InterruptedException ex) {
|
---|
59 | Main.warn("InterruptedException in "+getClass().getSimpleName()+" during cancel");
|
---|
60 | }
|
---|
61 |
|
---|
62 | synchronized (this) {
|
---|
63 | if (activeConnection != null) {
|
---|
64 | activeConnection.disconnect();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Adds an authentication header for basic authentication
|
---|
71 | *
|
---|
72 | * @param con the connection
|
---|
73 | * @throws OsmTransferException thrown if something went wrong. Check for nested exceptions
|
---|
74 | */
|
---|
75 | protected void addBasicAuthorizationHeader(HttpURLConnection con) throws OsmTransferException {
|
---|
76 | CharsetEncoder encoder = Utils.UTF_8.newEncoder();
|
---|
77 | CredentialsAgentResponse response;
|
---|
78 | String token;
|
---|
79 | try {
|
---|
80 | synchronized (CredentialsManager.getInstance()) {
|
---|
81 | response = CredentialsManager.getInstance().getCredentials(RequestorType.SERVER,
|
---|
82 | con.getURL().getHost(), false /* don't know yet whether the credentials will succeed */);
|
---|
83 | }
|
---|
84 | } catch (CredentialsAgentException e) {
|
---|
85 | throw new OsmTransferException(e);
|
---|
86 | }
|
---|
87 | if (response == null) {
|
---|
88 | token = ":";
|
---|
89 | } else if (response.isCanceled()) {
|
---|
90 | cancel = true;
|
---|
91 | return;
|
---|
92 | } else {
|
---|
93 | String username= response.getUsername() == null ? "" : response.getUsername();
|
---|
94 | String password = response.getPassword() == null ? "" : String.valueOf(response.getPassword());
|
---|
95 | token = username + ":" + password;
|
---|
96 | try {
|
---|
97 | ByteBuffer bytes = encoder.encode(CharBuffer.wrap(token));
|
---|
98 | con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
|
---|
99 | } catch(CharacterCodingException e) {
|
---|
100 | throw new OsmTransferException(e);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Signs the connection with an OAuth authentication header
|
---|
107 | *
|
---|
108 | * @param connection the connection
|
---|
109 | *
|
---|
110 | * @throws OsmTransferException thrown if there is currently no OAuth Access Token configured
|
---|
111 | * @throws OsmTransferException thrown if signing fails
|
---|
112 | */
|
---|
113 | protected void addOAuthAuthorizationHeader(HttpURLConnection connection) throws OsmTransferException {
|
---|
114 | if (oauthParameters == null) {
|
---|
115 | oauthParameters = OAuthParameters.createFromPreferences(Main.pref);
|
---|
116 | }
|
---|
117 | OAuthConsumer consumer = oauthParameters.buildConsumer();
|
---|
118 | OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
|
---|
119 | if (! holder.containsAccessToken())
|
---|
120 | throw new MissingOAuthAccessTokenException();
|
---|
121 | consumer.setTokenWithSecret(holder.getAccessTokenKey(), holder.getAccessTokenSecret());
|
---|
122 | try {
|
---|
123 | consumer.sign(connection);
|
---|
124 | } catch(OAuthException e) {
|
---|
125 | throw new OsmTransferException(tr("Failed to sign a HTTP connection with an OAuth Authentication header"), e);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected void addAuth(HttpURLConnection connection) throws OsmTransferException {
|
---|
130 | String authMethod = Main.pref.get("osm-server.auth-method", "basic");
|
---|
131 | if (authMethod.equals("basic")) {
|
---|
132 | addBasicAuthorizationHeader(connection);
|
---|
133 | } else if (authMethod.equals("oauth")) {
|
---|
134 | addOAuthAuthorizationHeader(connection);
|
---|
135 | } else {
|
---|
136 | String msg = tr("Unexpected value for preference ''{0}''. Got ''{1}''.", "osm-server.auth-method", authMethod);
|
---|
137 | Main.warn(msg);
|
---|
138 | throw new OsmTransferException(msg);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Replies true if this connection is canceled
|
---|
144 | *
|
---|
145 | * @return true if this connection is canceled
|
---|
146 | */
|
---|
147 | public boolean isCanceled() {
|
---|
148 | return cancel;
|
---|
149 | }
|
---|
150 | }
|
---|