001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.oauth; 003 004import java.net.MalformedURLException; 005import java.net.URL; 006 007import oauth.signpost.OAuthConsumer; 008import oauth.signpost.OAuthProvider; 009import oauth.signpost.basic.DefaultOAuthConsumer; 010import oauth.signpost.basic.DefaultOAuthProvider; 011 012import org.openstreetmap.josm.Main; 013import org.openstreetmap.josm.data.Preferences; 014import org.openstreetmap.josm.io.OsmApi; 015import org.openstreetmap.josm.tools.CheckParameterUtil; 016 017/** 018 * This class manages a set of OAuth parameters. 019 * @since 2747 020 */ 021public class OAuthParameters { 022 023 /** 024 * The default JOSM OAuth consumer key (created by user josmeditor). 025 */ 026 public static final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb"; 027 /** 028 * The default JOSM OAuth consumer secret (created by user josmeditor). 029 */ 030 public static final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5"; 031 /** 032 * The default OSM OAuth request token URL. 033 */ 034 public static final String DEFAULT_REQUEST_TOKEN_URL = Main.getOSMWebsite() + "/oauth/request_token"; 035 /** 036 * The default OSM OAuth access token URL. 037 */ 038 public static final String DEFAULT_ACCESS_TOKEN_URL = Main.getOSMWebsite() + "/oauth/access_token"; 039 /** 040 * The default OSM OAuth authorize URL. 041 */ 042 public static final String DEFAULT_AUTHORISE_URL = Main.getOSMWebsite() + "/oauth/authorize"; 043 044 045 /** 046 * Replies a set of default parameters for a consumer accessing the standard OSM server 047 * at {@link OsmApi#DEFAULT_API_URL}. 048 * 049 * @return a set of default parameters 050 */ 051 public static OAuthParameters createDefault() { 052 return createDefault(null); 053 } 054 055 /** 056 * Replies a set of default parameters for a consumer accessing an OSM server 057 * at the given API url. URL parameters are only set if the URL equals {@link OsmApi#DEFAULT_API_URL} 058 * or references the domain "dev.openstreetmap.org", otherwise they may be <code>null</code>. 059 * 060 * @param apiUrl The API URL for which the OAuth default parameters are created. If null or empty, the default OSM API url is used. 061 * @return a set of default parameters for the given {@code apiUrl} 062 * @since 5422 063 */ 064 public static OAuthParameters createDefault(String apiUrl) { 065 OAuthParameters parameters = new OAuthParameters(); 066 parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY); 067 parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET); 068 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL); 069 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL); 070 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL); 071 if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) { 072 try { 073 String host = new URL(apiUrl).getHost(); 074 if (host.endsWith("dev.openstreetmap.org")) { 075 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host)); 076 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host)); 077 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host)); 078 } 079 } catch (MalformedURLException e) { 080 // Ignored 081 } 082 } 083 return parameters; 084 } 085 086 /** 087 * Replies a set of parameters as defined in the preferences. 088 * 089 * @param pref the preferences 090 * @return the parameters 091 */ 092 public static OAuthParameters createFromPreferences(Preferences pref) { 093 OAuthParameters parameters = createDefault(pref.get("osm-server.url")); 094 parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", parameters.getConsumerKey())); 095 parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret())); 096 parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl())); 097 parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl())); 098 parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl())); 099 return parameters; 100 } 101 102 private String consumerKey; 103 private String consumerSecret; 104 private String requestTokenUrl; 105 private String accessTokenUrl; 106 private String authoriseUrl; 107 108 /** 109 * Constructs a new, unitialized, {@code OAuthParameters}. 110 * 111 * @see #createDefault 112 * @see #createFromPreferences 113 */ 114 public OAuthParameters() { 115 } 116 117 /** 118 * Creates a clone of the parameters in <code>other</code>. 119 * 120 * @param other the other parameters. Must not be null. 121 * @throws IllegalArgumentException thrown if other is null 122 */ 123 public OAuthParameters(OAuthParameters other) throws IllegalArgumentException{ 124 CheckParameterUtil.ensureParameterNotNull(other, "other"); 125 this.consumerKey = other.consumerKey; 126 this.consumerSecret = other.consumerSecret; 127 this.accessTokenUrl = other.accessTokenUrl; 128 this.requestTokenUrl = other.requestTokenUrl; 129 this.authoriseUrl = other.authoriseUrl; 130 } 131 132 /** 133 * Gets the consumer key. 134 * @return The consumer key 135 */ 136 public String getConsumerKey() { 137 return consumerKey; 138 } 139 140 /** 141 * Sets the consumer key. 142 * @param consumerKey The consumer key 143 */ 144 public void setConsumerKey(String consumerKey) { 145 this.consumerKey = consumerKey; 146 } 147 148 /** 149 * Gets the consumer secret. 150 * @return The consumer secret 151 */ 152 public String getConsumerSecret() { 153 return consumerSecret; 154 } 155 156 /** 157 * Sets the consumer secret. 158 * @param consumerSecret The consumer secret 159 */ 160 public void setConsumerSecret(String consumerSecret) { 161 this.consumerSecret = consumerSecret; 162 } 163 164 /** 165 * Gets the request token URL. 166 * @return The request token URL 167 */ 168 public String getRequestTokenUrl() { 169 return requestTokenUrl; 170 } 171 172 /** 173 * Sets the request token URL. 174 * @param requestTokenUrl the request token URL 175 */ 176 public void setRequestTokenUrl(String requestTokenUrl) { 177 this.requestTokenUrl = requestTokenUrl; 178 } 179 180 /** 181 * Gets the access token URL. 182 * @return The access token URL 183 */ 184 public String getAccessTokenUrl() { 185 return accessTokenUrl; 186 } 187 188 /** 189 * Sets the access token URL. 190 * @param accessTokenUrl The access token URL 191 */ 192 public void setAccessTokenUrl(String accessTokenUrl) { 193 this.accessTokenUrl = accessTokenUrl; 194 } 195 196 /** 197 * Gets the authorise URL. 198 * @return The authorise URL 199 */ 200 public String getAuthoriseUrl() { 201 return authoriseUrl; 202 } 203 204 /** 205 * Sets the authorise URL. 206 * @param authoriseUrl The authorise URL 207 */ 208 public void setAuthoriseUrl(String authoriseUrl) { 209 this.authoriseUrl = authoriseUrl; 210 } 211 212 /** 213 * Builds an {@link OAuthConsumer} based on these parameters. 214 * 215 * @return the consumer 216 */ 217 public OAuthConsumer buildConsumer() { 218 return new DefaultOAuthConsumer(consumerKey, consumerSecret); 219 } 220 221 /** 222 * Builds an {@link OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>. 223 * 224 * @param consumer the consumer. Must not be null. 225 * @return the provider 226 * @throws IllegalArgumentException if consumer is null 227 */ 228 public OAuthProvider buildProvider(OAuthConsumer consumer) throws IllegalArgumentException { 229 CheckParameterUtil.ensureParameterNotNull(consumer, "consumer"); 230 return new DefaultOAuthProvider( 231 requestTokenUrl, 232 accessTokenUrl, 233 authoriseUrl 234 ); 235 } 236 237 @Override 238 public int hashCode() { 239 final int prime = 31; 240 int result = 1; 241 result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode()); 242 result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode()); 243 result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode()); 244 result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode()); 245 result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode()); 246 return result; 247 } 248 249 @Override 250 public boolean equals(Object obj) { 251 if (this == obj) 252 return true; 253 if (obj == null) 254 return false; 255 if (getClass() != obj.getClass()) 256 return false; 257 OAuthParameters other = (OAuthParameters) obj; 258 if (accessTokenUrl == null) { 259 if (other.accessTokenUrl != null) 260 return false; 261 } else if (!accessTokenUrl.equals(other.accessTokenUrl)) 262 return false; 263 if (authoriseUrl == null) { 264 if (other.authoriseUrl != null) 265 return false; 266 } else if (!authoriseUrl.equals(other.authoriseUrl)) 267 return false; 268 if (consumerKey == null) { 269 if (other.consumerKey != null) 270 return false; 271 } else if (!consumerKey.equals(other.consumerKey)) 272 return false; 273 if (consumerSecret == null) { 274 if (other.consumerSecret != null) 275 return false; 276 } else if (!consumerSecret.equals(other.consumerSecret)) 277 return false; 278 if (requestTokenUrl == null) { 279 if (other.requestTokenUrl != null) 280 return false; 281 } else if (!requestTokenUrl.equals(other.requestTokenUrl)) 282 return false; 283 return true; 284 } 285}