Changeset 18764 in josm for trunk


Ignore:
Timestamp:
2023-06-20T19:40:11+02:00 (14 months ago)
Author:
taylor.smock
Message:

Fix #22952, #23013: Improve methods for seeing who is logged in to OSM

This adds a Test Authentication button to the OAuth 2 panel and updates the
current UserIdentityManager to have the appropriate details. In addition, this
fixes a problem whereby a user logged in via OAuth 2 would not be able to log in
via OAuth 1.

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

Legend:

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

    r18671 r18764  
    130130            IOAuthToken token = CredentialsManager.getInstance().lookupOAuthAccessToken(api);
    131131            // We *do* want to set the API token to null, if it doesn't exist. Just to avoid unnecessary lookups.
    132             this.setAccessToken(api, token);
    133             return token;
     132            if (token == null || token.getOAuthType() == version) {
     133                this.setAccessToken(api, token);
     134                return token;
     135            }
    134136        } catch (CredentialsAgentException exception) {
    135137            Logging.trace(exception);
  • trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java

    r13901 r18764  
    1212import javax.xml.parsers.ParserConfigurationException;
    1313
     14import org.openstreetmap.josm.data.oauth.IOAuthParameters;
     15import org.openstreetmap.josm.data.oauth.IOAuthToken;
     16import org.openstreetmap.josm.data.oauth.OAuth20Token;
    1417import org.openstreetmap.josm.data.oauth.OAuthParameters;
    1518import org.openstreetmap.josm.data.oauth.OAuthToken;
     
    4245 */
    4346public class TestAccessTokenTask extends PleaseWaitRunnable {
    44     private final OAuthToken token;
    45     private final OAuthParameters oauthParameters;
     47    private final OAuthToken tokenOAuth1;
     48    private final IOAuthToken tokenOAuth2;
     49    private final IOAuthParameters oauthParameters;
    4650    private boolean canceled;
    4751    private final Component parent;
     
    6266        CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
    6367        CheckParameterUtil.ensureParameterNotNull(accessToken, "accessToken");
    64         this.token = accessToken;
     68        this.tokenOAuth1 = accessToken;
     69        this.tokenOAuth2 = null;
     70        this.oauthParameters = parameters;
     71        this.parent = parent;
     72        this.apiUrl = apiUrl;
     73    }
     74
     75    /**
     76     * Create the task
     77     *
     78     * @param parent the parent component relative to which the  {@link PleaseWaitRunnable}-Dialog is displayed
     79     * @param apiUrl the API URL. Must not be null.
     80     * @param parameters the OAuth parameters. Must not be null.
     81     * @param accessToken the Access Token. Must not be null.
     82     * @since xxx
     83     */
     84    public TestAccessTokenTask(Component parent, String apiUrl, IOAuthParameters parameters, IOAuthToken accessToken) {
     85        super(parent, tr("Testing OAuth Access Token"), false /* don't ignore exceptions */);
     86        CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl");
     87        CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
     88        CheckParameterUtil.ensureParameterNotNull(accessToken, "accessToken");
     89        this.tokenOAuth1 = null;
     90        this.tokenOAuth2 = accessToken;
    6591        this.oauthParameters = parameters;
    6692        this.parent = parent;
     
    84110
    85111    protected void sign(HttpClient con) throws OAuthException {
    86         OAuthConsumer consumer = oauthParameters.buildConsumer();
    87         consumer.setTokenWithSecret(token.getKey(), token.getSecret());
    88         consumer.sign(con);
     112        if (oauthParameters instanceof OAuthParameters) {
     113            OAuthConsumer consumer = ((OAuthParameters) oauthParameters).buildConsumer();
     114            consumer.setTokenWithSecret(tokenOAuth1.getKey(), tokenOAuth1.getSecret());
     115            consumer.sign(con);
     116        } else {
     117            try {
     118                this.tokenOAuth2.sign(con);
     119            } catch (org.openstreetmap.josm.data.oauth.OAuthException e) {
     120                // Adapt our OAuthException to the SignPost OAuth exception
     121                throw new OAuthException(e) {};
     122            }
     123        }
    89124    }
    90125
     
    114149            }
    115150
     151            final String oauthKey = getAuthKey();
    116152            if (connection.getResponse().getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
    117153                throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED,
    118                         tr("Retrieving user details with Access Token Key ''{0}'' was rejected.", token.getKey()), null);
     154                        tr("Retrieving user details with Access Token Key ''{0}'' was rejected.",
     155                                oauthKey), null);
    119156
    120157            if (connection.getResponse().getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN)
    121158                throw new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN,
    122                         tr("Retrieving user details with Access Token Key ''{0}'' was forbidden.", token.getKey()), null);
     159                        tr("Retrieving user details with Access Token Key ''{0}'' was forbidden.", oauthKey), null);
    123160
    124161            if (connection.getResponse().getResponseCode() != HttpURLConnection.HTTP_OK)
     
    146183                        + "You are accessing the OSM server as user ''{2}'' with id ''{3}''."
    147184                        + "</html>",
    148                         token.getKey(),
     185                        getAuthKey(),
    149186                        apiUrl,
    150187                        Utils.escapeReservedCharactersHTML(userInfo.getDisplayName()),
     
    167204                        +"</html>",
    168205                        apiUrl,
    169                         token.getKey()
     206                        getAuthKey()
    170207                ),
    171208                tr("Test failed"),
     
    185222                        +"</html>",
    186223                        apiUrl,
    187                         token.getKey()
     224                        getAuthKey()
    188225                ),
    189226                tr("Token allows restricted access"),
     
    204241                        +"</html>",
    205242                        apiUrl,
    206                         token.getKey()
     243                        getAuthKey()
    207244                ),
    208245                tr("Test failed"),
     
    221258                        +"</html>",
    222259                        apiUrl,
    223                         token.getKey()
     260                        getAuthKey()
    224261                ),
    225262                tr("Test failed"),
     
    237274                        + "</html>",
    238275                        apiUrl,
    239                         token.getKey()
     276                        getAuthKey()
    240277                ),
    241278                tr("Test failed"),
     
    276313        }
    277314    }
     315
     316    private String getAuthKey() {
     317        if (this.tokenOAuth1 != null) {
     318            return this.tokenOAuth1.getKey();
     319        }
     320        if (this.tokenOAuth2 instanceof OAuth20Token) {
     321            return ((OAuth20Token) this.tokenOAuth2).getBearerToken();
     322        }
     323        throw new IllegalArgumentException("Only OAuth1 and OAuth2 tokens are understood: " + this.tokenOAuth2);
     324    }
    278325}
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java

    r18665 r18764  
    1818import javax.swing.JRadioButton;
    1919
     20import org.openstreetmap.josm.data.UserIdentityManager;
    2021import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
    2122import org.openstreetmap.josm.data.oauth.OAuthVersion;
     
    153154            throw new IllegalStateException("One of OAuth 2.0, OAuth 1.0a, or Basic authentication must be checked");
    154155        }
    155         Config.getPref().put("osm-server.auth-method", authMethod);
     156        final boolean initUser = Config.getPref().put("osm-server.auth-method", authMethod);
    156157        if ("basic".equals(authMethod)) {
    157158            // save username and password and clear the OAuth token
     
    169170            pnlBasicAuthPreferences.saveToPreferences();
    170171            pnlOAuth20Preferences.saveToPreferences();
     172        }
     173        if (initUser) {
     174            if ("basic".equals(authMethod)) {
     175                UserIdentityManager.getInstance().initFromPreferences();
     176            } else {
     177                UserIdentityManager.getInstance().initFromOAuth();
     178            }
    171179        }
    172180    }
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java

    r18667 r18764  
    283283                // these want the OAuth 1.0 token information
    284284                btns.add(new JButton(new RenewAuthorisationAction(AuthorizationProcedure.FULLY_AUTOMATIC)));
    285                 btns.add(new JButton(new TestAuthorisationAction()));
    286             }
     285            }
     286            btns.add(new JButton(new TestAuthorisationAction(oAuthVersion)));
    287287            btns.add(new JButton(new RemoveAuthorisationAction()));
    288288            gc.gridy = 4;
     
    423423     */
    424424    private class TestAuthorisationAction extends AbstractAction {
     425        private final OAuthVersion oAuthVersion;
     426
    425427        /**
    426428         * Constructs a new {@code TestAuthorisationAction}.
    427429         */
    428         TestAuthorisationAction() {
     430        TestAuthorisationAction(OAuthVersion oAuthVersion) {
     431            this.oAuthVersion = oAuthVersion;
    429432            putValue(NAME, tr("Test Access Token"));
    430433            putValue(SHORT_DESCRIPTION, tr("Click test access to the OSM server with the current access token"));
     
    434437        @Override
    435438        public void actionPerformed(ActionEvent evt) {
    436             OAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken();
    437             OAuthParameters parameters = OAuthParameters.createFromApiUrl(OsmApi.getOsmApi().getServerUrl());
    438             TestAccessTokenTask task = new TestAccessTokenTask(
    439                     OAuthAuthenticationPreferencesPanel.this,
    440                     apiUrl,
    441                     parameters,
    442                     token
    443             );
    444             MainApplication.worker.submit(task);
     439            if (this.oAuthVersion == OAuthVersion.OAuth10a) {
     440                OAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken();
     441                OAuthParameters parameters = OAuthParameters.createFromApiUrl(OsmApi.getOsmApi().getServerUrl());
     442                TestAccessTokenTask task = new TestAccessTokenTask(
     443                        OAuthAuthenticationPreferencesPanel.this,
     444                        apiUrl,
     445                        parameters,
     446                        token
     447                );
     448                MainApplication.worker.submit(task);
     449            } else {
     450                IOAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken(OsmApi.getOsmApi().getBaseUrl(), OAuthVersion.OAuth20);
     451                TestAccessTokenTask task = new TestAccessTokenTask(
     452                        OAuthAuthenticationPreferencesPanel.this,
     453                        apiUrl,
     454                        token.getParameters(),
     455                        token
     456                );
     457                MainApplication.worker.submit(task);
     458            }
    445459        }
    446460    }
Note: See TracChangeset for help on using the changeset viewer.