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