source: josm/trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java@ 9172

Last change on this file since 9172 was 9172, checked in by simon04, 8 years ago

see #12231 - Use HttpClient for OSM API calls

This requires adaptors to the OAuth library: SignpostAdapters

  • Property svn:eol-style set to native
File size: 9.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.oauth;
3
4import java.net.MalformedURLException;
5import java.net.URL;
6
7import oauth.signpost.OAuthConsumer;
8import oauth.signpost.OAuthProvider;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.Preferences;
12import org.openstreetmap.josm.io.OsmApi;
13import org.openstreetmap.josm.tools.CheckParameterUtil;
14
15/**
16 * This class manages a set of OAuth parameters.
17 * @since 2747
18 */
19public class OAuthParameters {
20
21 /**
22 * The default JOSM OAuth consumer key (created by user josmeditor).
23 */
24 public static final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb";
25 /**
26 * The default JOSM OAuth consumer secret (created by user josmeditor).
27 */
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
42
43 /**
44 * Replies a set of default parameters for a consumer accessing the standard OSM server
45 * at {@link OsmApi#DEFAULT_API_URL}.
46 *
47 * @return a set of default parameters
48 */
49 public static OAuthParameters createDefault() {
50 return createDefault(null);
51 }
52
53 /**
54 * Replies a set of default parameters for a consumer accessing an OSM server
55 * at the given API url. URL parameters are only set if the URL equals {@link OsmApi#DEFAULT_API_URL}
56 * or references the domain "dev.openstreetmap.org", otherwise they may be <code>null</code>.
57 *
58 * @param apiUrl The API URL for which the OAuth default parameters are created. If null or empty, the default OSM API url is used.
59 * @return a set of default parameters for the given {@code apiUrl}
60 * @since 5422
61 */
62 public static OAuthParameters createDefault(String apiUrl) {
63 OAuthParameters parameters = new OAuthParameters();
64 parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY);
65 parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET);
66 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL);
67 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL);
68 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL);
69 if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
70 try {
71 String host = new URL(apiUrl).getHost();
72 if (host.endsWith("dev.openstreetmap.org")) {
73 parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host));
74 parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host));
75 parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host));
76 }
77 } catch (MalformedURLException e) {
78 // Ignored
79 if (Main.isTraceEnabled()) {
80 Main.trace(e.getMessage());
81 }
82 }
83 }
84 return parameters;
85 }
86
87 /**
88 * Replies a set of parameters as defined in the preferences.
89 *
90 * @param pref the preferences
91 * @return the parameters
92 */
93 public static OAuthParameters createFromPreferences(Preferences pref) {
94 OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
95 parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()));
96 parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()));
97 parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()));
98 parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()));
99 parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()));
100 return parameters;
101 }
102
103 private String consumerKey;
104 private String consumerSecret;
105 private String requestTokenUrl;
106 private String accessTokenUrl;
107 private String authoriseUrl;
108
109 /**
110 * Constructs a new, unitialized, {@code OAuthParameters}.
111 *
112 * @see #createDefault
113 * @see #createFromPreferences
114 */
115 public OAuthParameters() {
116 // contents can be set later with setters
117 }
118
119 /**
120 * Creates a clone of the parameters in <code>other</code>.
121 *
122 * @param other the other parameters. Must not be null.
123 * @throws IllegalArgumentException if other is null
124 */
125 public OAuthParameters(OAuthParameters other) {
126 CheckParameterUtil.ensureParameterNotNull(other, "other");
127 this.consumerKey = other.consumerKey;
128 this.consumerSecret = other.consumerSecret;
129 this.accessTokenUrl = other.accessTokenUrl;
130 this.requestTokenUrl = other.requestTokenUrl;
131 this.authoriseUrl = other.authoriseUrl;
132 }
133
134 /**
135 * Gets the consumer key.
136 * @return The consumer key
137 */
138 public String getConsumerKey() {
139 return consumerKey;
140 }
141
142 /**
143 * Sets the consumer key.
144 * @param consumerKey The consumer key
145 */
146 public void setConsumerKey(String consumerKey) {
147 this.consumerKey = consumerKey;
148 }
149
150 /**
151 * Gets the consumer secret.
152 * @return The consumer secret
153 */
154 public String getConsumerSecret() {
155 return consumerSecret;
156 }
157
158 /**
159 * Sets the consumer secret.
160 * @param consumerSecret The consumer secret
161 */
162 public void setConsumerSecret(String consumerSecret) {
163 this.consumerSecret = consumerSecret;
164 }
165
166 /**
167 * Gets the request token URL.
168 * @return The request token URL
169 */
170 public String getRequestTokenUrl() {
171 return requestTokenUrl;
172 }
173
174 /**
175 * Sets the request token URL.
176 * @param requestTokenUrl the request token URL
177 */
178 public void setRequestTokenUrl(String requestTokenUrl) {
179 this.requestTokenUrl = requestTokenUrl;
180 }
181
182 /**
183 * Gets the access token URL.
184 * @return The access token URL
185 */
186 public String getAccessTokenUrl() {
187 return accessTokenUrl;
188 }
189
190 /**
191 * Sets the access token URL.
192 * @param accessTokenUrl The access token URL
193 */
194 public void setAccessTokenUrl(String accessTokenUrl) {
195 this.accessTokenUrl = accessTokenUrl;
196 }
197
198 /**
199 * Gets the authorise URL.
200 * @return The authorise URL
201 */
202 public String getAuthoriseUrl() {
203 return authoriseUrl;
204 }
205
206 /**
207 * Sets the authorise URL.
208 * @param authoriseUrl The authorise URL
209 */
210 public void setAuthoriseUrl(String authoriseUrl) {
211 this.authoriseUrl = authoriseUrl;
212 }
213
214 /**
215 * Builds an {@link OAuthConsumer} based on these parameters.
216 *
217 * @return the consumer
218 */
219 public OAuthConsumer buildConsumer() {
220 return new SignpostAdapters.OAuthConsumer(consumerKey, consumerSecret);
221 }
222
223 /**
224 * Builds an {@link OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>.
225 *
226 * @param consumer the consumer. Must not be null.
227 * @return the provider
228 * @throws IllegalArgumentException if consumer is null
229 */
230 public OAuthProvider buildProvider(OAuthConsumer consumer) {
231 CheckParameterUtil.ensureParameterNotNull(consumer, "consumer");
232 return new SignpostAdapters.OAuthProvider(
233 requestTokenUrl,
234 accessTokenUrl,
235 authoriseUrl
236 );
237 }
238
239 @Override
240 public int hashCode() {
241 final int prime = 31;
242 int result = 1;
243 result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode());
244 result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode());
245 result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode());
246 result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode());
247 result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode());
248 return result;
249 }
250
251 @Override
252 public boolean equals(Object obj) {
253 if (this == obj)
254 return true;
255 if (obj == null)
256 return false;
257 if (getClass() != obj.getClass())
258 return false;
259 OAuthParameters other = (OAuthParameters) obj;
260 if (accessTokenUrl == null) {
261 if (other.accessTokenUrl != null)
262 return false;
263 } else if (!accessTokenUrl.equals(other.accessTokenUrl))
264 return false;
265 if (authoriseUrl == null) {
266 if (other.authoriseUrl != null)
267 return false;
268 } else if (!authoriseUrl.equals(other.authoriseUrl))
269 return false;
270 if (consumerKey == null) {
271 if (other.consumerKey != null)
272 return false;
273 } else if (!consumerKey.equals(other.consumerKey))
274 return false;
275 if (consumerSecret == null) {
276 if (other.consumerSecret != null)
277 return false;
278 } else if (!consumerSecret.equals(other.consumerSecret))
279 return false;
280 if (requestTokenUrl == null) {
281 if (other.requestTokenUrl != null)
282 return false;
283 } else if (!requestTokenUrl.equals(other.requestTokenUrl))
284 return false;
285 return true;
286 }
287}
Note: See TracBrowser for help on using the repository browser.