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

Last change on this file since 13111 was 12928, checked in by bastiK, 7 years ago

see #15229 - do not copy the entire preferences list, just to set a custom server API in OAuth wizard

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