Changeset 19008 in josm


Ignore:
Timestamp:
2024-03-06T15:07:01+01:00 (2 months ago)
Author:
taylor.smock
Message:

Fix an issue with custom OAuth2 parameters where the custom parameters would be replaced by default parameters

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java

    r18991 r19008  
    197197     */
    198198    public static IOAuthParameters createFromApiUrl(String apiUrl, OAuthVersion oAuthVersion) {
    199         IOAuthParameters parameters = createDefault(apiUrl, oAuthVersion);
     199        // We actually need the host
     200        if (apiUrl.startsWith("https://") || apiUrl.startsWith("http://")) {
     201            try {
     202                apiUrl = new URI(apiUrl).getHost();
     203            } catch (URISyntaxException syntaxException) {
     204                Logging.trace(apiUrl);
     205            }
     206        }
    200207        switch (oAuthVersion) {
    201208            case OAuth20:
    202209            case OAuth21: // Right now, OAuth 2.1 will work with our OAuth 2.0 implementation
    203                 OAuth20Parameters oAuth20Parameters = (OAuth20Parameters) parameters;
    204210                try {
    205211                    IOAuthToken storedToken = CredentialsManager.getInstance().lookupOAuthAccessToken(apiUrl);
    206                     return storedToken != null ? storedToken.getParameters() : oAuth20Parameters;
     212                    if (storedToken != null) {
     213                        return storedToken.getParameters();
     214                    }
    207215                } catch (CredentialsAgentException e) {
    208216                    Logging.trace(e);
    209217                }
    210                 return oAuth20Parameters;
     218                return createDefault(apiUrl, oAuthVersion);
    211219            default:
    212220                throw new IllegalArgumentException("Unknown OAuth version: " + oAuthVersion);
  • trunk/src/org/openstreetmap/josm/gui/oauth/AdvancedOAuthPropertiesPanel.java

    r18991 r19008  
    215215                tfConsumerKey.getText(),
    216216                tfConsumerSecret.getText(),
     217                tfAccessTokenURL.getText(),
    217218                tfAuthoriseURL.getText(),
    218                 tfAccessTokenURL.getText(),
     219                apiUrl,
    219220                tfRequestTokenURL.getText()
    220221                );
     
    261262                this.resetToDefaultSettings();
    262263            } else {
    263                 setAdvancedParameters(OAuthParameters.createFromApiUrl(paramApiUrl, OAuthVersion.OAuth20));
     264                // createFromApiUrl may make a network request
     265                final IOAuthParameters parameters = OAuthParameters.createFromApiUrl(paramApiUrl, OAuthVersion.OAuth20);
     266                GuiHelper.runInEDT(() -> setAdvancedParameters(parameters));
    264267            }
    265268            GuiHelper.runInEDT(() -> ilUseDefault.setEnabled(true));
  • trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java

    r18991 r19008  
    381381                    });
    382382                }
    383             }, getApiUrl(), getOAuthVersion());
     383            }, getApiUrl(), getOAuthVersion(), getOAuthParameters());
    384384            getProgressMonitor().worked(1);
    385385        }
  • trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java

    r18991 r19008  
    8484        if ((this.oAuthVersion == OAuthVersion.OAuth20 || this.oAuthVersion == OAuthVersion.OAuth21)
    8585        && this.procedure == AuthorizationProcedure.FULLY_AUTOMATIC) {
    86             authorize(true, callback, this.apiUrl, this.oAuthVersion);
     86            authorize(true, callback, this.apiUrl, this.oAuthVersion, getOAuthParameters());
    8787        } else {
    8888            setVisible(true);
     
    9090                throw new UserCancelException();
    9191            }
    92         }
    93         OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
    94         holder.setAccessToken(apiUrl, getAccessToken());
    95         holder.setSaveToPreferences(isSaveAccessTokenToPreferences());
     92            OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();
     93            holder.setAccessToken(apiUrl, getAccessToken());
     94            holder.setSaveToPreferences(isSaveAccessTokenToPreferences());
     95        }
    9696    }
    9797
    9898    /**
    9999     * Perform the oauth dance
     100     *
    100101     * @param startRemoteControl {@code true} to start remote control if it is not already running
    101      * @param callback The callback to use to notify that the OAuth dance succeeded
    102      * @param apiUrl The API URL to get the token for
    103      * @param oAuthVersion The OAuth version that the authorization dance is force
    104      */
    105     static void authorize(boolean startRemoteControl, Consumer<Optional<IOAuthToken>> callback, String apiUrl, OAuthVersion oAuthVersion) {
     102     * @param callback           The callback to use to notify that the OAuth dance succeeded
     103     * @param apiUrl             The API URL to get the token for
     104     * @param oAuthVersion       The OAuth version that the authorization dance is force
     105     * @param oAuthParameters    The OAuth parameters to use
     106     */
     107    static void authorize(boolean startRemoteControl, Consumer<Optional<IOAuthToken>> callback, String apiUrl,
     108                          OAuthVersion oAuthVersion, IOAuthParameters oAuthParameters) {
    106109        final boolean remoteControlIsRunning = Boolean.TRUE.equals(RemoteControl.PROP_REMOTECONTROL_ENABLED.get());
    107110        // TODO: Ask user if they want to start remote control?
     
    109112            RemoteControl.start();
    110113        }
    111         new OAuth20Authorization().authorize(OAuthParameters.createDefault(apiUrl, oAuthVersion), token -> {
     114        new OAuth20Authorization().authorize(
     115                Optional.ofNullable(oAuthParameters).orElseGet(() -> OAuthParameters.createDefault(apiUrl, oAuthVersion)),
     116                token -> {
    112117                    if (!remoteControlIsRunning) {
    113118                        RemoteControl.stop();
     
    253258     * Creates the wizard.
    254259     *
    255      * @param parent the component relative to which the dialog is displayed
    256      * @param procedure the authorization procedure to use
    257      * @param apiUrl the API URL. Must not be null.
    258      * @param executor the executor used for running the HTTP requests for the authorization
    259      * @param oAuthVersion The OAuth version this wizard is for
     260     * @param parent             the component relative to which the dialog is displayed
     261     * @param procedure          the authorization procedure to use
     262     * @param apiUrl             the API URL. Must not be null.
     263     * @param executor           the executor used for running the HTTP requests for the authorization
     264     * @param oAuthVersion       The OAuth version this wizard is for
     265     * @param advancedParameters The OAuth parameters to initialize the wizard with
    260266     * @throws IllegalArgumentException if apiUrl is null
    261267     */
    262268    public OAuthAuthorizationWizard(Component parent, AuthorizationProcedure procedure, String apiUrl,
    263                                     Executor executor, OAuthVersion oAuthVersion) {
     269                                    Executor executor, OAuthVersion oAuthVersion, IOAuthParameters advancedParameters) {
    264270        super(GuiHelper.getFrameForComponent(parent), ModalityType.DOCUMENT_MODAL);
    265271        this.procedure = Objects.requireNonNull(procedure, "procedure");
     
    268274        this.oAuthVersion = oAuthVersion;
    269275        build();
     276        if (advancedParameters != null) {
     277            pnlFullyAutomaticAuthorisationUI.getAdvancedPropertiesPanel().setAdvancedParameters(advancedParameters);
     278            pnlManualAuthorisationUI.getAdvancedPropertiesPanel().setAdvancedParameters(advancedParameters);
     279        }
    270280    }
    271281
     
    362372                    AuthorizationProcedure.FULLY_AUTOMATIC,
    363373                    serverUrl.toString(), Utils.newDirectExecutor(),
    364                     OAuthVersion.OAuth20);
     374                    OAuthVersion.OAuth20, null);
    365375            wizard.showDialog(null);
    366376            return wizard;
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java

    r19007 r19008  
    331331                    final String currentApiUrl = apiUrl;
    332332                    MainApplication.worker.execute(() -> {
    333                         final String clientId = OAuthParameters.createDefault(apiUrl, oAuthVersion).getClientId();
     333                        final String clientId = OAuthParameters.createFromApiUrl(apiUrl, oAuthVersion).getClientId();
    334334                        if (Objects.equals(apiUrl, currentApiUrl)) {
    335335                            GuiHelper.runInEDT(() -> this.setEnabled(!Utils.isEmpty(clientId)));
     
    347347                    apiUrl,
    348348                    MainApplication.worker,
    349                     oAuthVersion);
     349                    oAuthVersion,
     350                    pnlAdvancedProperties.getAdvancedParameters()
     351                    );
    350352            try {
    351353                wizard.showDialog(token -> GuiHelper.runInEDT(OAuthAuthenticationPreferencesPanel.this::refreshView));
     
    407409        @Override
    408410        public void actionPerformed(ActionEvent evt) {
    409             IOAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken(OsmApi.getOsmApi().getBaseUrl(), OAuthVersion.OAuth20);
     411            IOAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken(apiUrl, OAuthVersion.OAuth20);
    410412            TestAccessTokenTask task = new TestAccessTokenTask(
    411413                    OAuthAuthenticationPreferencesPanel.this,
Note: See TracChangeset for help on using the changeset viewer.