Changeset 18764 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2023-06-20T19:40:11+02:00 (20 months ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/oauth/OAuthAccessTokenHolder.java
r18671 r18764 130 130 IOAuthToken token = CredentialsManager.getInstance().lookupOAuthAccessToken(api); 131 131 // 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 } 134 136 } catch (CredentialsAgentException exception) { 135 137 Logging.trace(exception); -
trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java
r13901 r18764 12 12 import javax.xml.parsers.ParserConfigurationException; 13 13 14 import org.openstreetmap.josm.data.oauth.IOAuthParameters; 15 import org.openstreetmap.josm.data.oauth.IOAuthToken; 16 import org.openstreetmap.josm.data.oauth.OAuth20Token; 14 17 import org.openstreetmap.josm.data.oauth.OAuthParameters; 15 18 import org.openstreetmap.josm.data.oauth.OAuthToken; … … 42 45 */ 43 46 public 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; 46 50 private boolean canceled; 47 51 private final Component parent; … … 62 66 CheckParameterUtil.ensureParameterNotNull(parameters, "parameters"); 63 67 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; 65 91 this.oauthParameters = parameters; 66 92 this.parent = parent; … … 84 110 85 111 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 } 89 124 } 90 125 … … 114 149 } 115 150 151 final String oauthKey = getAuthKey(); 116 152 if (connection.getResponse().getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 117 153 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); 119 156 120 157 if (connection.getResponse().getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) 121 158 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); 123 160 124 161 if (connection.getResponse().getResponseCode() != HttpURLConnection.HTTP_OK) … … 146 183 + "You are accessing the OSM server as user ''{2}'' with id ''{3}''." 147 184 + "</html>", 148 token.getKey(),185 getAuthKey(), 149 186 apiUrl, 150 187 Utils.escapeReservedCharactersHTML(userInfo.getDisplayName()), … … 167 204 +"</html>", 168 205 apiUrl, 169 token.getKey()206 getAuthKey() 170 207 ), 171 208 tr("Test failed"), … … 185 222 +"</html>", 186 223 apiUrl, 187 token.getKey()224 getAuthKey() 188 225 ), 189 226 tr("Token allows restricted access"), … … 204 241 +"</html>", 205 242 apiUrl, 206 token.getKey()243 getAuthKey() 207 244 ), 208 245 tr("Test failed"), … … 221 258 +"</html>", 222 259 apiUrl, 223 token.getKey()260 getAuthKey() 224 261 ), 225 262 tr("Test failed"), … … 237 274 + "</html>", 238 275 apiUrl, 239 token.getKey()276 getAuthKey() 240 277 ), 241 278 tr("Test failed"), … … 276 313 } 277 314 } 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 } 278 325 } -
trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java
r18665 r18764 18 18 import javax.swing.JRadioButton; 19 19 20 import org.openstreetmap.josm.data.UserIdentityManager; 20 21 import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder; 21 22 import org.openstreetmap.josm.data.oauth.OAuthVersion; … … 153 154 throw new IllegalStateException("One of OAuth 2.0, OAuth 1.0a, or Basic authentication must be checked"); 154 155 } 155 Config.getPref().put("osm-server.auth-method", authMethod); 156 final boolean initUser = Config.getPref().put("osm-server.auth-method", authMethod); 156 157 if ("basic".equals(authMethod)) { 157 158 // save username and password and clear the OAuth token … … 169 170 pnlBasicAuthPreferences.saveToPreferences(); 170 171 pnlOAuth20Preferences.saveToPreferences(); 172 } 173 if (initUser) { 174 if ("basic".equals(authMethod)) { 175 UserIdentityManager.getInstance().initFromPreferences(); 176 } else { 177 UserIdentityManager.getInstance().initFromOAuth(); 178 } 171 179 } 172 180 } -
trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java
r18667 r18764 283 283 // these want the OAuth 1.0 token information 284 284 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))); 287 287 btns.add(new JButton(new RemoveAuthorisationAction())); 288 288 gc.gridy = 4; … … 423 423 */ 424 424 private class TestAuthorisationAction extends AbstractAction { 425 private final OAuthVersion oAuthVersion; 426 425 427 /** 426 428 * Constructs a new {@code TestAuthorisationAction}. 427 429 */ 428 TestAuthorisationAction() { 430 TestAuthorisationAction(OAuthVersion oAuthVersion) { 431 this.oAuthVersion = oAuthVersion; 429 432 putValue(NAME, tr("Test Access Token")); 430 433 putValue(SHORT_DESCRIPTION, tr("Click test access to the OSM server with the current access token")); … … 434 437 @Override 435 438 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 } 445 459 } 446 460 }
Note:
See TracChangeset
for help on using the changeset viewer.