Changeset 9355 in josm
- Timestamp:
- 2016-01-09T18:07:26+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java
r9220 r9355 4 4 import java.net.MalformedURLException; 5 5 import java.net.URL; 6 import java.util.Objects; 6 7 7 8 import oauth.signpost.OAuthConsumer; 8 9 import oauth.signpost.OAuthProvider; 9 10 10 import org.openstreetmap.josm.Main; 11 11 import org.openstreetmap.josm.data.Preferences; … … 27 27 */ 28 28 public static final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5"; 29 /**30 * The default OSM OAuth request token URL.31 */32 public static final String DEFAULT_REQUEST_TOKEN_URL = Main.getOSMWebsite() + "/oauth/request_token";33 /**34 * The default OSM OAuth access token URL.35 */36 public static final String DEFAULT_ACCESS_TOKEN_URL = Main.getOSMWebsite() + "/oauth/access_token";37 /**38 * The default OSM OAuth authorize URL.39 */40 public static final String DEFAULT_AUTHORISE_URL = Main.getOSMWebsite() + "/oauth/authorize";41 29 42 30 /** … … 60 48 */ 61 49 public static OAuthParameters createDefault(String apiUrl) { 62 String host = ""; 63 if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) { 50 final String consumerKey; 51 final String consumerSecret; 52 final String serverUrl; 53 54 if (apiUrl != null) { 55 // validate URL syntax 64 56 try { 65 host = new URL(apiUrl).getHost();57 new URL(apiUrl); 66 58 } catch (MalformedURLException e) { 67 // Ignored 68 if (Main.isTraceEnabled()) { 69 Main.trace(e.getMessage()); 70 } 59 apiUrl = null; 71 60 } 72 61 } 73 boolean osmDevServer = host.endsWith("dev.openstreetmap.org"); 62 63 if (apiUrl != null && !OsmApi.DEFAULT_API_URL.equals(apiUrl)) { 64 consumerKey = ""; // a custom consumer key is required 65 consumerSecret = ""; // a custom consumer secret is requireds 66 serverUrl = apiUrl.replaceAll("/api$", ""); 67 } else { 68 consumerKey = DEFAULT_JOSM_CONSUMER_KEY; 69 consumerSecret = DEFAULT_JOSM_CONSUMER_SECRET; 70 serverUrl = Main.getOSMWebsite(); 71 } 72 74 73 return new OAuthParameters( 75 DEFAULT_JOSM_CONSUMER_KEY, 76 DEFAULT_JOSM_CONSUMER_SECRET, 77 osmDevServer ? DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host) : DEFAULT_REQUEST_TOKEN_URL, 78 osmDevServer ? DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host) : DEFAULT_ACCESS_TOKEN_URL, 79 osmDevServer ? DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host) : DEFAULT_AUTHORISE_URL); 74 consumerKey, 75 consumerSecret, 76 serverUrl + "/oauth/request_token", 77 serverUrl + "/oauth/access_token", 78 serverUrl + "/oauth/authorize", 79 serverUrl + "/login", 80 serverUrl + "/logout"); 80 81 } 81 82 … … 93 94 pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()), 94 95 pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()), 95 pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()) 96 ); 96 pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()), 97 pref.get("oauth.settings.osm-login-url", parameters.getOsmLoginUrl()), 98 pref.get("oauth.settings.osm-logout-url", parameters.getOsmLogoutUrl())); 99 } 100 101 /** 102 * Remembers the current values in the preferences <code>pref</code>. 103 * 104 * @param pref the preferences. Must not be null. 105 * @throws IllegalArgumentException if pref is null. 106 */ 107 public void rememberPreferences(Preferences pref) { 108 CheckParameterUtil.ensureParameterNotNull(pref, "pref"); 109 pref.put("oauth.settings.consumer-key", getConsumerKey()); 110 pref.put("oauth.settings.consumer-secret", getConsumerSecret()); 111 pref.put("oauth.settings.request-token-url", getRequestTokenUrl()); 112 pref.put("oauth.settings.access-token-url", getAccessTokenUrl()); 113 pref.put("oauth.settings.authorise-url", getAuthoriseUrl()); 114 pref.put("oauth.settings.osm-login-url", getOsmLoginUrl()); 115 pref.put("oauth.settings.osm-logout-url", getOsmLogoutUrl()); 97 116 } 98 117 … … 102 121 private final String accessTokenUrl; 103 122 private final String authoriseUrl; 123 private final String osmLoginUrl; 124 private final String osmLogoutUrl; 104 125 105 126 /** … … 110 131 * @param accessTokenUrl access token URL 111 132 * @param authoriseUrl authorise URL 112 * 133 * @param osmLoginUrl the OSM login URL (for automatic mode) 134 * @param osmLogoutUrl the OSM logout URL (for automatic mode) 113 135 * @see #createDefault 114 136 * @see #createFromPreferences … … 116 138 */ 117 139 public OAuthParameters(String consumerKey, String consumerSecret, 118 String requestTokenUrl, String accessTokenUrl, String authoriseUrl) {140 String requestTokenUrl, String accessTokenUrl, String authoriseUrl, String osmLoginUrl, String osmLogoutUrl) { 119 141 this.consumerKey = consumerKey; 120 142 this.consumerSecret = consumerSecret; … … 122 144 this.accessTokenUrl = accessTokenUrl; 123 145 this.authoriseUrl = authoriseUrl; 146 this.osmLoginUrl = osmLoginUrl; 147 this.osmLogoutUrl = osmLogoutUrl; 124 148 } 125 149 … … 137 161 this.requestTokenUrl = other.requestTokenUrl; 138 162 this.authoriseUrl = other.authoriseUrl; 163 this.osmLoginUrl = other.osmLoginUrl; 164 this.osmLogoutUrl = other.osmLogoutUrl; 139 165 } 140 166 … … 177 203 public String getAuthoriseUrl() { 178 204 return authoriseUrl; 205 } 206 207 /** 208 * Gets the URL used to login users on the website (for automatic mode). 209 * @return The URL used to login users 210 */ 211 public String getOsmLoginUrl() { 212 return osmLoginUrl; 213 } 214 215 /** 216 * Gets the URL used to logout users on the website (for automatic mode). 217 * @return The URL used to logout users 218 */ 219 public String getOsmLogoutUrl() { 220 return osmLogoutUrl; 179 221 } 180 222 … … 205 247 206 248 @Override 249 public boolean equals(Object o) { 250 if (this == o) return true; 251 if (o == null || getClass() != o.getClass()) return false; 252 OAuthParameters that = (OAuthParameters) o; 253 return Objects.equals(consumerKey, that.consumerKey) && 254 Objects.equals(consumerSecret, that.consumerSecret) && 255 Objects.equals(requestTokenUrl, that.requestTokenUrl) && 256 Objects.equals(accessTokenUrl, that.accessTokenUrl) && 257 Objects.equals(authoriseUrl, that.authoriseUrl) && 258 Objects.equals(osmLoginUrl, that.osmLoginUrl) && 259 Objects.equals(osmLogoutUrl, that.osmLogoutUrl); 260 } 261 262 @Override 207 263 public int hashCode() { 208 final int prime = 31; 209 int result = 1; 210 result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode()); 211 result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode()); 212 result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode()); 213 result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode()); 214 result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode()); 215 return result; 216 } 217 218 @Override 219 public boolean equals(Object obj) { 220 if (this == obj) 221 return true; 222 if (obj == null) 223 return false; 224 if (getClass() != obj.getClass()) 225 return false; 226 OAuthParameters other = (OAuthParameters) obj; 227 if (accessTokenUrl == null) { 228 if (other.accessTokenUrl != null) 229 return false; 230 } else if (!accessTokenUrl.equals(other.accessTokenUrl)) 231 return false; 232 if (authoriseUrl == null) { 233 if (other.authoriseUrl != null) 234 return false; 235 } else if (!authoriseUrl.equals(other.authoriseUrl)) 236 return false; 237 if (consumerKey == null) { 238 if (other.consumerKey != null) 239 return false; 240 } else if (!consumerKey.equals(other.consumerKey)) 241 return false; 242 if (consumerSecret == null) { 243 if (other.consumerSecret != null) 244 return false; 245 } else if (!consumerSecret.equals(other.consumerSecret)) 246 return false; 247 if (requestTokenUrl == null) { 248 if (other.requestTokenUrl != null) 249 return false; 250 } else if (!requestTokenUrl.equals(other.requestTokenUrl)) 251 return false; 252 return true; 264 return Objects.hash(consumerKey, consumerSecret, requestTokenUrl, accessTokenUrl, authoriseUrl, osmLoginUrl, osmLogoutUrl); 253 265 } 254 266 } -
trunk/src/org/openstreetmap/josm/gui/oauth/AdvancedOAuthPropertiesPanel.java
r9220 r9355 35 35 * <li>Access token URL</li> 36 36 * <li>Authorize URL</li> 37 * <li>OSM login URL</li> 38 * <li>OSM logout URL</li> 37 39 * </ul> 38 40 * … … 48 50 private JosmTextField tfAccessTokenURL; 49 51 private JosmTextField tfAuthoriseURL; 52 private JosmTextField tfOsmLoginURL; 53 private JosmTextField tfOsmLogoutURL; 50 54 private transient UseDefaultItemListener ilUseDefault; 51 55 private String apiUrl; … … 120 124 SelectAllOnFocusGainedDecorator.decorate(tfAuthoriseURL); 121 125 126 127 // -- OSM login URL 128 gc.gridy = 6; 129 gc.gridx = 0; 130 gc.weightx = 0.0; 131 add(new JLabel(tr("OSM login URL:")), gc); 132 133 gc.gridx = 1; 134 gc.weightx = 1.0; 135 add(tfOsmLoginURL = new JosmTextField(), gc); 136 SelectAllOnFocusGainedDecorator.decorate(tfOsmLoginURL); 137 138 139 // -- OSM logout URL 140 gc.gridy = 7; 141 gc.gridx = 0; 142 gc.weightx = 0.0; 143 add(new JLabel(tr("OSM logout URL:")), gc); 144 145 gc.gridx = 1; 146 gc.weightx = 1.0; 147 add(tfOsmLogoutURL = new JosmTextField(), gc); 148 SelectAllOnFocusGainedDecorator.decorate(tfOsmLogoutURL); 149 122 150 cbUseDefaults.addItemListener(ilUseDefault = new UseDefaultItemListener()); 123 151 } … … 125 153 protected boolean hasCustomSettings() { 126 154 OAuthParameters params = OAuthParameters.createDefault(apiUrl); 127 return 128 !tfConsumerKey.getText().equals(params.getConsumerKey()) 129 || !tfConsumerSecret.getText().equals(params.getConsumerSecret()) 130 || !tfRequestTokenURL.getText().equals(params.getRequestTokenUrl()) 131 || !tfAccessTokenURL.getText().equals(params.getAccessTokenUrl()) 132 || !tfAuthoriseURL.getText().equals(params.getAuthoriseUrl()); 155 return !params.equals(getAdvancedParameters()); 133 156 } 134 157 … … 173 196 tfAccessTokenURL.setText(params.getAccessTokenUrl()); 174 197 tfAuthoriseURL.setText(params.getAuthoriseUrl()); 198 tfOsmLoginURL.setText(params.getOsmLoginUrl()); 199 tfOsmLogoutURL.setText(params.getOsmLogoutUrl()); 175 200 176 201 setChildComponentsEnabled(false); … … 198 223 tfRequestTokenURL.getText(), 199 224 tfAccessTokenURL.getText(), 200 tfAuthoriseURL.getText()); 225 tfAuthoriseURL.getText(), 226 tfOsmLoginURL.getText(), 227 tfOsmLogoutURL.getText()); 201 228 } 202 229 … … 220 247 tfAccessTokenURL.setText(parameters.getAccessTokenUrl() == null ? "" : parameters.getAccessTokenUrl()); 221 248 tfAuthoriseURL.setText(parameters.getAuthoriseUrl() == null ? "" : parameters.getAuthoriseUrl()); 249 tfOsmLoginURL.setText(parameters.getOsmLoginUrl() == null ? "" : parameters.getOsmLoginUrl()); 250 tfOsmLogoutURL.setText(parameters.getOsmLogoutUrl() == null ? "" : parameters.getOsmLogoutUrl()); 222 251 } 223 252 } … … 244 273 resetToDefaultSettings(); 245 274 } else { 246 cbUseDefaults.setSelected(false); 247 tfConsumerKey.setText(pref.get("oauth.settings.consumer-key", OAuthParameters.DEFAULT_JOSM_CONSUMER_KEY)); 248 tfConsumerSecret.setText(pref.get("oauth.settings.consumer-secret", OAuthParameters.DEFAULT_JOSM_CONSUMER_SECRET)); 249 tfRequestTokenURL.setText(pref.get("oauth.settings.request-token-url", OAuthParameters.DEFAULT_REQUEST_TOKEN_URL)); 250 tfAccessTokenURL.setText(pref.get("oauth.settings.access-token-url", OAuthParameters.DEFAULT_ACCESS_TOKEN_URL)); 251 tfAuthoriseURL.setText(pref.get("oauth.settings.authorise-url", OAuthParameters.DEFAULT_AUTHORISE_URL)); 252 setChildComponentsEnabled(true); 275 setAdvancedParameters(OAuthParameters.createFromPreferences(pref)); 253 276 } 254 277 ilUseDefault.setEnabled(true); … … 265 288 pref.put("oauth.settings.use-default", cbUseDefaults.isSelected()); 266 289 if (cbUseDefaults.isSelected()) { 267 pref.put("oauth.settings.consumer-key", null); 268 pref.put("oauth.settings.consumer-secret", null); 269 pref.put("oauth.settings.request-token-url", null); 270 pref.put("oauth.settings.access-token-url", null); 271 pref.put("oauth.settings.authorise-url", null); 290 new OAuthParameters(null, null, null, null, null, null, null).rememberPreferences(pref); 272 291 } else { 273 pref.put("oauth.settings.consumer-key", tfConsumerKey.getText().trim()); 274 pref.put("oauth.settings.consumer-secret", tfConsumerSecret.getText().trim()); 275 pref.put("oauth.settings.request-token-url", tfRequestTokenURL.getText().trim()); 276 pref.put("oauth.settings.access-token-url", tfAccessTokenURL.getText().trim()); 277 pref.put("oauth.settings.authorise-url", tfAuthoriseURL.getText().trim()); 292 getAdvancedParameters().rememberPreferences(pref); 278 293 } 279 294 } -
trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java
r9352 r9355 479 479 480 480 protected void alertLoginFailed(OsmLoginFailedException e) { 481 String loginUrl = null; 482 try { 483 loginUrl = authClient.buildOsmLoginUrl(); 484 } catch (OsmOAuthAuthorizationException e1) { 485 alertInvalidLoginUrl(); 486 return; 487 } 481 final String loginUrl = getAdvancedPropertiesPanel().getAdvancedParameters().getOsmLoginUrl(); 488 482 HelpAwareOptionPane.showOptionDialog( 489 483 FullyAutomaticAuthorizationUI.this, -
trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java
r9309 r9355 8 8 import java.lang.reflect.Field; 9 9 import java.net.HttpURLConnection; 10 import java.net.MalformedURLException;11 10 import java.net.URL; 12 11 import java.nio.charset.StandardCharsets; … … 247 246 248 247 /** 249 * Derives the OSM login URL from the OAuth Authorization Website URL250 *251 * @return the OSM login URL252 * @throws OsmOAuthAuthorizationException if something went wrong, in particular if the253 * URLs are malformed254 */255 public String buildOsmLoginUrl() throws OsmOAuthAuthorizationException {256 try {257 URL autUrl = new URL(oauthProviderParameters.getAuthoriseUrl());258 URL url = new URL(Main.pref.get("oauth.protocol", "https"), autUrl.getHost(), autUrl.getPort(), "/login");259 return url.toString();260 } catch (MalformedURLException e) {261 throw new OsmOAuthAuthorizationException(e);262 }263 }264 265 /**266 * Derives the OSM logout URL from the OAuth Authorization Website URL267 *268 * @return the OSM logout URL269 * @throws OsmOAuthAuthorizationException if something went wrong, in particular if the270 * URLs are malformed271 */272 protected String buildOsmLogoutUrl() throws OsmOAuthAuthorizationException {273 try {274 URL autUrl = new URL(oauthProviderParameters.getAuthoriseUrl());275 URL url = new URL(Main.pref.get("oauth.protocol", "https"), autUrl.getHost(), autUrl.getPort(), "/logout");276 return url.toString();277 } catch (MalformedURLException e) {278 throw new OsmOAuthAuthorizationException(e);279 }280 }281 282 /**283 248 * Submits a request to the OSM website for a login form. The OSM website replies a session ID in 284 249 * a cookie. … … 289 254 protected SessionId fetchOsmWebsiteSessionId() throws OsmOAuthAuthorizationException { 290 255 try { 291 StringBuilder sb = new StringBuilder(); 292 sb.append(buildOsmLoginUrl()).append("?cookie_test=true"); 293 URL url = new URL(sb.toString()); 256 final URL url = new URL(oauthProviderParameters.getOsmLoginUrl() + "?cookie_test=true"); 294 257 synchronized (this) { 295 258 connection = HttpClient.create(url); … … 341 304 protected void authenticateOsmSession(SessionId sessionId, String userName, String password) throws OsmLoginFailedException { 342 305 try { 343 URL url = new URL(buildOsmLoginUrl());306 final URL url = new URL(oauthProviderParameters.getOsmLoginUrl()); 344 307 final HttpClient client = HttpClient.create(url, "POST").useCache(false); 345 308 … … 383 346 protected void logoutOsmSession(SessionId sessionId) throws OsmOAuthAuthorizationException { 384 347 try { 385 URL url = new URL( buildOsmLogoutUrl());348 URL url = new URL(oauthProviderParameters.getOsmLogoutUrl()); 386 349 synchronized (this) { 387 350 connection = HttpClient.create(url).setMaxRedirects(-1);
Note:
See TracChangeset
for help on using the changeset viewer.