1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.oauth;
|
---|
3 |
|
---|
4 | import java.net.MalformedURLException;
|
---|
5 | import java.net.URL;
|
---|
6 |
|
---|
7 | import oauth.signpost.OAuthConsumer;
|
---|
8 | import oauth.signpost.OAuthProvider;
|
---|
9 | import oauth.signpost.basic.DefaultOAuthConsumer;
|
---|
10 | import oauth.signpost.basic.DefaultOAuthProvider;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.Preferences;
|
---|
14 | import org.openstreetmap.josm.io.OsmApi;
|
---|
15 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * This class manages a set of OAuth parameters.
|
---|
19 | * @since 2747
|
---|
20 | */
|
---|
21 | public class OAuthParameters {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The default JOSM OAuth consumer key (created by user josmeditor).
|
---|
25 | */
|
---|
26 | public static final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb";
|
---|
27 | /**
|
---|
28 | * The default JOSM OAuth consumer secret (created by user josmeditor).
|
---|
29 | */
|
---|
30 | public static final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5";
|
---|
31 | /**
|
---|
32 | * The default OSM OAuth request token URL.
|
---|
33 | */
|
---|
34 | public static final String DEFAULT_REQUEST_TOKEN_URL = Main.getOSMWebsite() + "/oauth/request_token";
|
---|
35 | /**
|
---|
36 | * The default OSM OAuth access token URL.
|
---|
37 | */
|
---|
38 | public static final String DEFAULT_ACCESS_TOKEN_URL = Main.getOSMWebsite() + "/oauth/access_token";
|
---|
39 | /**
|
---|
40 | * The default OSM OAuth authorize URL.
|
---|
41 | */
|
---|
42 | public static final String DEFAULT_AUTHORISE_URL = Main.getOSMWebsite() + "/oauth/authorize";
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Replies a set of default parameters for a consumer accessing the standard OSM server
|
---|
47 | * at {@link OsmApi#DEFAULT_API_URL}.
|
---|
48 | *
|
---|
49 | * @return a set of default parameters
|
---|
50 | */
|
---|
51 | public static OAuthParameters createDefault() {
|
---|
52 | return createDefault(null);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Replies a set of default parameters for a consumer accessing an OSM server
|
---|
57 | * at the given API url. URL parameters are only set if the URL equals {@link OsmApi#DEFAULT_API_URL}
|
---|
58 | * or references the domain "dev.openstreetmap.org", otherwise they may be <code>null</code>.
|
---|
59 | *
|
---|
60 | * @param apiUrl The API URL for which the OAuth default parameters are created. If null or empty, the default OSM API url is used.
|
---|
61 | * @return a set of default parameters for the given {@code apiUrl}
|
---|
62 | * @since 5422
|
---|
63 | */
|
---|
64 | public static OAuthParameters createDefault(String apiUrl) {
|
---|
65 | OAuthParameters parameters = new OAuthParameters();
|
---|
66 | parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY);
|
---|
67 | parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET);
|
---|
68 | parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL);
|
---|
69 | parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL);
|
---|
70 | parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL);
|
---|
71 | if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
|
---|
72 | try {
|
---|
73 | String host = new URL(apiUrl).getHost();
|
---|
74 | if (host.endsWith("dev.openstreetmap.org")) {
|
---|
75 | parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host));
|
---|
76 | parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host));
|
---|
77 | parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host));
|
---|
78 | }
|
---|
79 | } catch (MalformedURLException e) {
|
---|
80 | // Ignored
|
---|
81 | }
|
---|
82 | }
|
---|
83 | return parameters;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Replies a set of parameters as defined in the preferences.
|
---|
88 | *
|
---|
89 | * @param pref the preferences
|
---|
90 | * @return the parameters
|
---|
91 | */
|
---|
92 | public static OAuthParameters createFromPreferences(Preferences pref) {
|
---|
93 | OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
|
---|
94 | parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()));
|
---|
95 | parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()));
|
---|
96 | parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()));
|
---|
97 | parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()));
|
---|
98 | parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()));
|
---|
99 | 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 | }
|
---|