Ignore:
Timestamp:
2016-01-09T16:25:08+01:00 (8 years ago)
Author:
simon04
Message:

fix #7612 - Prefer OAuth, provide authorization at upload

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmConnection.java

    r9309 r9352  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.lang.reflect.InvocationTargetException;
    67import java.net.Authenticator.RequestorType;
     8import java.net.MalformedURLException;
     9import java.net.URL;
    710import java.nio.ByteBuffer;
    811import java.nio.CharBuffer;
     
    1013import java.nio.charset.CharsetEncoder;
    1114import java.nio.charset.StandardCharsets;
     15import java.util.Objects;
     16import java.util.concurrent.Callable;
     17import java.util.concurrent.FutureTask;
    1218
    1319import org.openstreetmap.josm.Main;
    1420import org.openstreetmap.josm.data.oauth.OAuthParameters;
     21import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
    1522import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
    1623import org.openstreetmap.josm.io.auth.CredentialsAgentException;
     
    2229import oauth.signpost.OAuthConsumer;
    2330import oauth.signpost.exception.OAuthException;
     31import org.openstreetmap.josm.tools.Utils;
     32
     33import javax.swing.SwingUtilities;
    2434
    2535/**
     
    96106        OAuthConsumer consumer = oauthParameters.buildConsumer();
    97107        OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
    98         if (!holder.containsAccessToken())
     108        if (!holder.containsAccessToken()) {
     109            obtainAccessToken(connection);
     110        }
     111        if (!holder.containsAccessToken()) { // check if wizard completed
    99112            throw new MissingOAuthAccessTokenException();
     113        }
    100114        consumer.setTokenWithSecret(holder.getAccessTokenKey(), holder.getAccessTokenSecret());
    101115        try {
     
    106120    }
    107121
     122    /**
     123     * Obtains an OAuth access token for the connection. Afterwards, the token is accessible via {@link OAuthAccessTokenHolder}.
     124     * @param connection connection for which the access token should be obtained
     125     * @throws MissingOAuthAccessTokenException if the process cannot be completec successfully
     126     */
     127    protected void obtainAccessToken(final HttpClient connection) throws MissingOAuthAccessTokenException {
     128        try {
     129            final URL apiUrl = new URL(Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL));
     130            if (!Objects.equals(apiUrl.getHost(), connection.getURL().getHost())) {
     131                throw new MissingOAuthAccessTokenException();
     132            }
     133            final Runnable authTask = new FutureTask<>(new Callable<OAuthAuthorizationWizard>() {
     134                @Override
     135                public OAuthAuthorizationWizard call() throws Exception {
     136                    // Concerning Utils.newDirectExecutor: Main.worker cannot be used since this connection is already
     137                    // executed via Main.worker. The OAuth connections would block otherwise.
     138                    final OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
     139                            Main.parent, apiUrl.toExternalForm(), Utils.newDirectExecutor());
     140                    wizard.showDialog();
     141                    OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true);
     142                    OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
     143                    return wizard;
     144                }
     145            });
     146            // exception handling differs from implementation at GuiHelper.runInEDTAndWait()
     147            if (SwingUtilities.isEventDispatchThread()) {
     148                authTask.run();
     149            } else {
     150                SwingUtilities.invokeAndWait(authTask);
     151            }
     152        } catch (MalformedURLException | InterruptedException | InvocationTargetException e) {
     153            throw new MissingOAuthAccessTokenException();
     154        }
     155    }
     156
    108157    protected void addAuth(HttpClient connection) throws OsmTransferException {
    109         String authMethod = Main.pref.get("osm-server.auth-method", "basic");
     158        final String authMethod = OsmApi.getAuthMethod();
    110159        if ("basic".equals(authMethod)) {
    111160            addBasicAuthorizationHeader(connection);
Note: See TracChangeset for help on using the changeset viewer.