Changeset 9352 in josm


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

fix #7612 - Prefer OAuth, provide authorization at upload

Location:
trunk/src/org/openstreetmap/josm
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java

    r9350 r9352  
    1515import java.net.Authenticator.RequestorType;
    1616import java.net.PasswordAuthentication;
     17import java.util.concurrent.Executor;
    1718
    1819import javax.swing.AbstractAction;
     
    6768    private JPanel pnlActionButtonsPanel;
    6869    private JPanel pnlResult;
     70    private final Executor executor;
    6971
    7072    /**
     
    296298     * Constructs a new {@code FullyAutomaticAuthorizationUI} for the given API URL.
    297299     * @param apiUrl The OSM API URL
     300     * @param executor the executor used for running the HTTP requests for the authorization
    298301     * @since 5422
    299302     */
    300     public FullyAutomaticAuthorizationUI(String apiUrl) {
     303    public FullyAutomaticAuthorizationUI(String apiUrl, Executor executor) {
    301304        super(apiUrl);
     305        this.executor = executor;
    302306        build();
    303307    }
     
    327331        @Override
    328332        public void actionPerformed(ActionEvent evt) {
    329             Main.worker.submit(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this));
     333            executor.execute(new FullyAutomaticAuthorisationTask(FullyAutomaticAuthorizationUI.this));
    330334        }
    331335
     
    377381        @Override
    378382        public void actionPerformed(ActionEvent arg0) {
    379             Main.worker.submit(new TestAccessTokenTask(
     383            executor.execute(new TestAccessTokenTask(
    380384                    FullyAutomaticAuthorizationUI.this,
    381385                    getApiUrl(),
  • trunk/src/org/openstreetmap/josm/gui/oauth/ManualAuthorizationUI.java

    r9059 r9352  
    1212import java.beans.PropertyChangeEvent;
    1313import java.beans.PropertyChangeListener;
     14import java.util.concurrent.Executor;
    1415
    1516import javax.swing.AbstractAction;
     
    2324import javax.swing.text.JTextComponent;
    2425
    25 import org.openstreetmap.josm.Main;
    2626import org.openstreetmap.josm.data.oauth.OAuthToken;
    2727import org.openstreetmap.josm.gui.SideButton;
     
    4747    private JCheckBox cbSaveToPreferences;
    4848    private HtmlPanel pnlMessage;
     49    private final Executor executor;
    4950
    5051    protected JPanel buildAccessTokenPanel() {
     
    163164     * Constructs a new {@code ManualAuthorizationUI} for the given API URL.
    164165     * @param apiUrl The OSM API URL
     166     * @param executor the executor used for running the HTTP requests for the authorization
    165167     * @since 5422
    166168     */
    167     public ManualAuthorizationUI(String apiUrl) {
     169    public ManualAuthorizationUI(String apiUrl, Executor executor) {
    168170        super(apiUrl);
     171        this.executor = executor;
    169172        build();
    170173    }
     
    261264                    getAccessToken()
    262265            );
    263             Main.worker.submit(task);
     266            executor.execute(task);
    264267        }
    265268
  • trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java

    r9300 r9352  
    2222import java.beans.PropertyChangeEvent;
    2323import java.beans.PropertyChangeListener;
     24import java.util.concurrent.Executor;
    2425
    2526import javax.swing.AbstractAction;
     
    4445import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
    4546import org.openstreetmap.josm.gui.help.HelpUtil;
     47import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
    4648import org.openstreetmap.josm.gui.util.GuiHelper;
    4749import org.openstreetmap.josm.gui.widgets.HtmlPanel;
     
    4951import org.openstreetmap.josm.tools.ImageProvider;
    5052import org.openstreetmap.josm.tools.OpenBrowser;
     53import org.openstreetmap.josm.tools.UserCancelException;
    5154import org.openstreetmap.josm.tools.WindowGeometry;
    5255
     
    6568    private ManualAuthorizationUI pnlManualAuthorisationUI;
    6669    private JScrollPane spAuthorisationProcedureUI;
     70    private final Executor executor;
     71
     72    /**
     73     * Launches the wizard, {@link OAuthAccessTokenHolder#setAccessToken(OAuthToken) sets the token}
     74     * and {@link OAuthAccessTokenHolder#setSaveToPreferences(boolean) saves to preferences}.
     75     * @throws UserCancelException if user cancels the operation
     76     */
     77    public void showDialog() throws UserCancelException {
     78        setVisible(true);
     79        if (isCanceled()) {
     80            throw new UserCancelException();
     81        }
     82        OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
     83        holder.setAccessToken(getAccessToken());
     84        holder.setSaveToPreferences(isSaveAccessTokenToPreferences());
     85    }
    6786
    6887    /**
     
    170189        this.setMinimumSize(new Dimension(600, 420));
    171190
    172         pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl);
    173         pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl);
    174         pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl);
     191        pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorizationUI(apiUrl, executor);
     192        pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorizationUI(apiUrl, executor);
     193        pnlManualAuthorisationUI = new ManualAuthorizationUI(apiUrl, executor);
    175194
    176195        spAuthorisationProcedureUI = GuiHelper.embedInVerticalScrollPane(new JPanel());
     
    209228     * Creates the wizard.
    210229     *
    211      * @param apiUrl the API URL. Must not be null.
    212      * @throws IllegalArgumentException if apiUrl is null
    213      */
    214     public OAuthAuthorizationWizard(String apiUrl) {
    215         this(Main.parent, apiUrl);
    216     }
    217 
    218     /**
    219      * Creates the wizard.
    220      *
    221230     * @param parent the component relative to which the dialog is displayed
    222231     * @param apiUrl the API URL. Must not be null.
     232     * @param executor the executor used for running the HTTP requests for the authorization
    223233     * @throws IllegalArgumentException if apiUrl is null
    224234     */
    225     public OAuthAuthorizationWizard(Component parent, String apiUrl) {
     235    public OAuthAuthorizationWizard(Component parent, String apiUrl, Executor executor) {
    226236        super(JOptionPane.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
    227237        CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl");
    228238        this.apiUrl = apiUrl;
     239        this.executor = executor;
    229240        build();
    230241    }
  • trunk/src/org/openstreetmap/josm/gui/oauth/SemiAutomaticAuthorizationUI.java

    r9256 r9352  
    1414import java.awt.event.ItemEvent;
    1515import java.awt.event.ItemListener;
     16import java.util.concurrent.Executor;
    1617
    1718import javax.swing.AbstractAction;
     
    2122import javax.swing.JPanel;
    2223
    23 import org.openstreetmap.josm.Main;
    2424import org.openstreetmap.josm.data.oauth.OAuthToken;
    2525import org.openstreetmap.josm.gui.SideButton;
     
    4747    private RetrieveAccessTokenPanel pnlRetrieveAccessToken;
    4848    private ShowAccessTokenPanel pnlShowAccessToken;
    49 
     49    private final Executor executor;
     50   
    5051    /**
    5152     * build the UI
     
    6364     * Constructs a new {@code SemiAutomaticAuthorizationUI} for the given API URL.
    6465     * @param apiUrl The OSM API URL
     66     * @param executor the executor used for running the HTTP requests for the authorization
    6567     * @since 5422
    6668     */
    67     public SemiAutomaticAuthorizationUI(String apiUrl) {
     69    public SemiAutomaticAuthorizationUI(String apiUrl, Executor executor) {
    6870        super(apiUrl);
     71        this.executor = executor;
    6972        build();
    7073    }
     
    396399                    getAdvancedPropertiesPanel().getAdvancedParameters()
    397400            );
    398             Main.worker.submit(task);
     401            executor.execute(task);
    399402            Runnable r  = new Runnable() {
    400403                @Override
     
    411414                }
    412415            };
    413             Main.worker.submit(r);
     416            executor.execute(r);
    414417        }
    415418    }
     
    433436                    requestToken
    434437            );
    435             Main.worker.submit(task);
     438            executor.execute(task);
    436439            Runnable r  = new Runnable() {
    437440                @Override
     
    448451                }
    449452            };
    450             Main.worker.submit(r);
     453            executor.execute(r);
    451454        }
    452455    }
     
    471474                    getAccessToken()
    472475            );
    473             Main.worker.submit(task);
     476            executor.execute(task);
    474477        }
    475478    }
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java

    r9059 r9352  
    2121import org.openstreetmap.josm.gui.help.HelpUtil;
    2222import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
     23import org.openstreetmap.josm.io.OsmApi;
    2324import org.openstreetmap.josm.io.auth.CredentialsManager;
    2425
     
    5758        gc.anchor = GridBagConstraints.NORTHWEST;
    5859        gc.fill = GridBagConstraints.HORIZONTAL;
    59         gc.weightx = 0.0;
     60        gc.gridx = 1;
     61        gc.weightx = 1.0;
    6062        gc.insets = new Insets(0, 0, 0, 3);
    6163        add(rbBasicAuthentication = new JRadioButton(), gc);
     
    6567
    6668        //-- radio button for OAuth
    67         gc.gridx = 1;
    68         gc.weightx = 1.0;
     69        gc.gridx = 0;
     70        gc.weightx = 0.0;
    6971        add(rbOAuth = new JRadioButton(), gc);
    7072        rbOAuth.setText(tr("Use OAuth"));
     
    118120     */
    119121    public final void initFromPreferences() {
    120         String authMethod = Main.pref.get("osm-server.auth-method", "basic");
     122        final String authMethod = OsmApi.getAuthMethod();
    121123        if ("basic".equals(authMethod)) {
    122124            rbBasicAuthentication.setSelected(true);
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java

    r8836 r9352  
    3535import org.openstreetmap.josm.io.auth.CredentialsManager;
    3636import org.openstreetmap.josm.tools.ImageProvider;
     37import org.openstreetmap.josm.tools.UserCancelException;
    3738
    3839/**
     
    325326            OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
    326327                    OAuthAuthenticationPreferencesPanel.this,
    327                     apiUrl
    328             );
    329             wizard.setVisible(true);
    330             if (wizard.isCanceled()) return;
    331             OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
    332             holder.setAccessToken(wizard.getAccessToken());
    333             holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());
     328                    apiUrl,
     329                    Main.worker);
     330            try {
     331                wizard.showDialog();
     332            } catch (UserCancelException ignore) {
     333                Main.trace(ignore.toString());
     334                return;
     335            }
    334336            pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
    335337            refreshView();
     
    340342     * Launches the OAuthAuthorisationWizard to generate a new Access Token
    341343     */
    342     private class RenewAuthorisationAction extends AbstractAction {
     344    private class RenewAuthorisationAction extends AuthoriseNowAction {
    343345        /**
    344346         * Constructs a new {@code RenewAuthorisationAction}.
     
    348350            putValue(SHORT_DESCRIPTION, tr("Click to step through the OAuth authorization process and generate a new Access Token"));
    349351            putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth-small"));
    350 
    351         }
    352 
    353         @Override
    354         public void actionPerformed(ActionEvent arg0) {
    355             OAuthAuthorizationWizard wizard = new OAuthAuthorizationWizard(
    356                     OAuthAuthenticationPreferencesPanel.this,
    357                     apiUrl
    358             );
    359             wizard.setVisible(true);
    360             if (wizard.isCanceled()) return;
    361             OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
    362             holder.setAccessToken(wizard.getAccessToken());
    363             holder.setSaveToPreferences(wizard.isSaveAccessTokenToPreferences());
    364             pnlAdvancedProperties.setAdvancedParameters(wizard.getOAuthParameters());
    365             refreshView();
    366352        }
    367353    }
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r9309 r9352  
    582582     * @since 6349
    583583     */
    584     public static final boolean isUsingOAuth() {
    585         return "oauth".equals(Main.pref.get("osm-server.auth-method", "basic"));
     584    public static boolean isUsingOAuth() {
     585        return "oauth".equals(getAuthMethod());
     586    }
     587
     588    /**
     589     * Returns the authentication method set in the preferences
     590     * @return the authentication method
     591     */
     592    public static String getAuthMethod() {
     593        return Main.pref.get("osm-server.auth-method", "oauth");
    586594    }
    587595
  • 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);
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r9351 r9352  
    4949import java.util.Locale;
    5050import java.util.Objects;
    51 import java.util.concurrent.ExecutorService;
    52 import java.util.concurrent.Executors;
     51import java.util.concurrent.Executor;
    5352import java.util.concurrent.ForkJoinPool;
    5453import java.util.concurrent.ForkJoinWorkerThread;
     
    14311430            }
    14321431        }, null, true);
     1432    }
     1433
     1434    /**
     1435     * Returns an executor which executes commands in the calling thread
     1436     * @return an executor
     1437     */
     1438    public static Executor newDirectExecutor() {
     1439        return new Executor() {
     1440            @Override
     1441            public void execute(Runnable command) {
     1442                command.run();
     1443            }
     1444        };
    14331445    }
    14341446
Note: See TracChangeset for help on using the changeset viewer.