Changeset 9355 in josm for trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java
- Timestamp:
- 2016-01-09T18:07:26+01:00 (8 years ago)
- File:
-
- 1 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 }
Note:
See TracChangeset
for help on using the changeset viewer.