Ignore:
Timestamp:
2023-02-08T18:31:58+01:00 (3 years ago)
Author:
taylor.smock
Message:

Fix #20768: Add OAuth 2.0 support

This also fixes #21607: authentication buttons are unavailable when credentials
are set.

File:
1 edited

Legend:

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

    r15586 r18650  
    1616import javax.swing.JOptionPane;
    1717
     18import org.openstreetmap.josm.data.oauth.IOAuthParameters;
     19import org.openstreetmap.josm.data.oauth.OAuth20Parameters;
    1820import org.openstreetmap.josm.data.oauth.OAuthParameters;
     21import org.openstreetmap.josm.data.oauth.OAuthVersion;
    1922import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    2023import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
     
    5255    private final JosmTextField tfOsmLoginURL = new JosmTextField();
    5356    private final JosmTextField tfOsmLogoutURL = new JosmTextField();
     57    private final OAuthVersion oauthVersion;
    5458    private transient UseDefaultItemListener ilUseDefault;
    5559    private String apiUrl;
     
    5761    /**
    5862     * Constructs a new {@code AdvancedOAuthPropertiesPanel}.
    59      */
    60     public AdvancedOAuthPropertiesPanel() {
     63     * @param oauthVersion The OAuth version to make the panel for
     64     */
     65    public AdvancedOAuthPropertiesPanel(OAuthVersion oauthVersion) {
     66        this.oauthVersion = oauthVersion;
    6167        build();
    6268    }
     
    7177        gc.weightx = 1.0;
    7278        gc.insets = new Insets(0, 0, 3, 3);
    73         gc.gridwidth = 2;
     79        gc.gridwidth = 3;
    7480        add(cbUseDefaults, gc);
    7581
     
    7884        gc.weightx = 0.0;
    7985        gc.gridwidth = 1;
    80         add(new JLabel(tr("Consumer Key:")), gc);
     86        if (this.oauthVersion == OAuthVersion.OAuth10a) {
     87            add(new JLabel(tr("Consumer Key:")), gc);
     88        } else {
     89            add(new JLabel(tr("Client ID:")), gc);
     90        }
    8191
    8292        gc.gridx = 1;
     
    8696
    8797        // -- consumer secret
    88         gc.gridy = 2;
     98        gc.gridy++;
    8999        gc.gridx = 0;
    90100        gc.weightx = 0.0;
    91         add(new JLabel(tr("Consumer Secret:")), gc);
     101        if (this.oauthVersion == OAuthVersion.OAuth10a) {
     102            add(new JLabel(tr("Consumer Secret:")), gc);
     103        } else {
     104            add(new JLabel(tr("Client Secret:")), gc);
     105        }
    92106
    93107        gc.gridx = 1;
     
    97111
    98112        // -- request token URL
    99         gc.gridy = 3;
     113        gc.gridy++;
    100114        gc.gridx = 0;
    101115        gc.weightx = 0.0;
    102         add(new JLabel(tr("Request Token URL:")), gc);
     116        if (this.oauthVersion == OAuthVersion.OAuth10a) {
     117            add(new JLabel(tr("Request Token URL:")), gc);
     118        } else {
     119            add(new JLabel(tr("Redirect URL:")), gc);
     120        }
    103121
    104122        gc.gridx = 1;
     
    108126
    109127        // -- access token URL
    110         gc.gridy = 4;
     128        gc.gridy++;
    111129        gc.gridx = 0;
    112130        gc.weightx = 0.0;
     
    119137
    120138        // -- authorise URL
    121         gc.gridy = 5;
     139        gc.gridy++;
    122140        gc.gridx = 0;
    123141        gc.weightx = 0.0;
     
    129147        SelectAllOnFocusGainedDecorator.decorate(tfAuthoriseURL);
    130148
    131         // -- OSM login URL
    132         gc.gridy = 6;
    133         gc.gridx = 0;
    134         gc.weightx = 0.0;
    135         add(new JLabel(tr("OSM login URL:")), gc);
    136 
    137         gc.gridx = 1;
    138         gc.weightx = 1.0;
    139         add(tfOsmLoginURL, gc);
    140         SelectAllOnFocusGainedDecorator.decorate(tfOsmLoginURL);
    141 
    142         // -- OSM logout URL
    143         gc.gridy = 7;
    144         gc.gridx = 0;
    145         gc.weightx = 0.0;
    146         add(new JLabel(tr("OSM logout URL:")), gc);
    147 
    148         gc.gridx = 1;
    149         gc.weightx = 1.0;
    150         add(tfOsmLogoutURL, gc);
    151         SelectAllOnFocusGainedDecorator.decorate(tfOsmLogoutURL);
     149        if (this.oauthVersion == OAuthVersion.OAuth10a) {
     150            // -- OSM login URL
     151            gc.gridy++;
     152            gc.gridx = 0;
     153            gc.weightx = 0.0;
     154            add(new JLabel(tr("OSM login URL:")), gc);
     155
     156            gc.gridx = 1;
     157            gc.weightx = 1.0;
     158            add(tfOsmLoginURL, gc);
     159            SelectAllOnFocusGainedDecorator.decorate(tfOsmLoginURL);
     160
     161            // -- OSM logout URL
     162            gc.gridy++;
     163            gc.gridx = 0;
     164            gc.weightx = 0.0;
     165            add(new JLabel(tr("OSM logout URL:")), gc);
     166
     167            gc.gridx = 1;
     168            gc.weightx = 1.0;
     169            add(tfOsmLogoutURL, gc);
     170            SelectAllOnFocusGainedDecorator.decorate(tfOsmLogoutURL);
     171        }
    152172
    153173        ilUseDefault = new UseDefaultItemListener();
     
    192212    protected void resetToDefaultSettings() {
    193213        cbUseDefaults.setSelected(true);
    194         OAuthParameters params = OAuthParameters.createDefault(apiUrl);
    195         tfConsumerKey.setText(params.getConsumerKey());
    196         tfConsumerSecret.setText(params.getConsumerSecret());
    197         tfRequestTokenURL.setText(params.getRequestTokenUrl());
    198         tfAccessTokenURL.setText(params.getAccessTokenUrl());
    199         tfAuthoriseURL.setText(params.getAuthoriseUrl());
    200         tfOsmLoginURL.setText(params.getOsmLoginUrl());
    201         tfOsmLogoutURL.setText(params.getOsmLogoutUrl());
     214        IOAuthParameters iParams = OAuthParameters.createDefault(apiUrl, this.oauthVersion);
     215        switch (this.oauthVersion) {
     216            case OAuth10a:
     217                OAuthParameters params = (OAuthParameters) iParams;
     218                tfConsumerKey.setText(params.getConsumerKey());
     219                tfConsumerSecret.setText(params.getConsumerSecret());
     220                tfRequestTokenURL.setText(params.getRequestTokenUrl());
     221                tfAccessTokenURL.setText(params.getAccessTokenUrl());
     222                tfAuthoriseURL.setText(params.getAuthoriseUrl());
     223                tfOsmLoginURL.setText(params.getOsmLoginUrl());
     224                tfOsmLogoutURL.setText(params.getOsmLogoutUrl());
     225                break;
     226            case OAuth20:
     227            case OAuth21:
     228                OAuth20Parameters params20 = (OAuth20Parameters) iParams;
     229                tfConsumerKey.setText(params20.getClientId());
     230                tfConsumerSecret.setText(params20.getClientSecret());
     231                tfAccessTokenURL.setText(params20.getAccessTokenUrl());
     232                tfAuthoriseURL.setText(params20.getAuthorizationUrl());
     233                tfRequestTokenURL.setText(params20.getRedirectUri());
     234        }
    202235
    203236        setChildComponentsEnabled(false);
     
    217250     * @return the OAuth parameters
    218251     */
    219     public OAuthParameters getAdvancedParameters() {
     252    public IOAuthParameters getAdvancedParameters() {
    220253        if (cbUseDefaults.isSelected())
    221             return OAuthParameters.createDefault(apiUrl);
    222         return new OAuthParameters(
    223             tfConsumerKey.getText(),
    224             tfConsumerSecret.getText(),
    225             tfRequestTokenURL.getText(),
    226             tfAccessTokenURL.getText(),
    227             tfAuthoriseURL.getText(),
    228             tfOsmLoginURL.getText(),
    229             tfOsmLogoutURL.getText());
     254            return OAuthParameters.createDefault(apiUrl, this.oauthVersion);
     255        if (this.oauthVersion == OAuthVersion.OAuth10a) {
     256            return new OAuthParameters(
     257                    tfConsumerKey.getText(),
     258                    tfConsumerSecret.getText(),
     259                    tfRequestTokenURL.getText(),
     260                    tfAccessTokenURL.getText(),
     261                    tfAuthoriseURL.getText(),
     262                    tfOsmLoginURL.getText(),
     263                    tfOsmLogoutURL.getText());
     264        }
     265        return new OAuth20Parameters(
     266                tfConsumerKey.getText(),
     267                tfConsumerSecret.getText(),
     268                tfAuthoriseURL.getText(),
     269                tfAccessTokenURL.getText(),
     270                tfRequestTokenURL.getText()
     271                );
    230272    }
    231273
     
    236278     * @throws IllegalArgumentException if parameters is null.
    237279     */
    238     public void setAdvancedParameters(OAuthParameters parameters) {
     280    public void setAdvancedParameters(IOAuthParameters parameters) {
    239281        CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
    240         if (parameters.equals(OAuthParameters.createDefault(apiUrl))) {
     282        if (parameters.equals(OAuthParameters.createDefault(apiUrl, parameters.getOAuthVersion()))) {
    241283            cbUseDefaults.setSelected(true);
    242284            setChildComponentsEnabled(false);
     
    244286            cbUseDefaults.setSelected(false);
    245287            setChildComponentsEnabled(true);
    246             tfConsumerKey.setText(parameters.getConsumerKey() == null ? "" : parameters.getConsumerKey());
    247             tfConsumerSecret.setText(parameters.getConsumerSecret() == null ? "" : parameters.getConsumerSecret());
    248             tfRequestTokenURL.setText(parameters.getRequestTokenUrl() == null ? "" : parameters.getRequestTokenUrl());
    249             tfAccessTokenURL.setText(parameters.getAccessTokenUrl() == null ? "" : parameters.getAccessTokenUrl());
    250             tfAuthoriseURL.setText(parameters.getAuthoriseUrl() == null ? "" : parameters.getAuthoriseUrl());
    251             tfOsmLoginURL.setText(parameters.getOsmLoginUrl() == null ? "" : parameters.getOsmLoginUrl());
    252             tfOsmLogoutURL.setText(parameters.getOsmLogoutUrl() == null ? "" : parameters.getOsmLogoutUrl());
     288            if (parameters instanceof OAuthParameters) {
     289                OAuthParameters parameters10 = (OAuthParameters) parameters;
     290                tfConsumerKey.setText(parameters10.getConsumerKey() == null ? "" : parameters10.getConsumerKey());
     291                tfConsumerSecret.setText(parameters10.getConsumerSecret() == null ? "" : parameters10.getConsumerSecret());
     292                tfRequestTokenURL.setText(parameters10.getRequestTokenUrl() == null ? "" : parameters10.getRequestTokenUrl());
     293                tfAccessTokenURL.setText(parameters10.getAccessTokenUrl() == null ? "" : parameters10.getAccessTokenUrl());
     294                tfAuthoriseURL.setText(parameters10.getAuthoriseUrl() == null ? "" : parameters10.getAuthoriseUrl());
     295                tfOsmLoginURL.setText(parameters10.getOsmLoginUrl() == null ? "" : parameters10.getOsmLoginUrl());
     296                tfOsmLogoutURL.setText(parameters10.getOsmLogoutUrl() == null ? "" : parameters10.getOsmLogoutUrl());
     297            } else if (parameters instanceof OAuth20Parameters) {
     298                OAuth20Parameters parameters20 = (OAuth20Parameters) parameters;
     299                tfConsumerKey.setText(parameters20.getClientId());
     300                tfConsumerSecret.setText(parameters20.getClientSecret());
     301                tfAccessTokenURL.setText(parameters20.getAccessTokenUrl());
     302                tfAuthoriseURL.setText(parameters20.getAuthorizationUrl());
     303                tfRequestTokenURL.setText(parameters20.getRedirectUri());
     304            }
    253305        }
    254306    }
Note: See TracChangeset for help on using the changeset viewer.