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