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

Last change on this file since 12928 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
RevLine 
[2801]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.oauth;
3
[9355]4import java.util.Objects;
[5422]5
[6453]6import org.openstreetmap.josm.Main;
[2801]7import org.openstreetmap.josm.data.Preferences;
[5422]8import org.openstreetmap.josm.io.OsmApi;
[12928]9import org.openstreetmap.josm.spi.preferences.Config;
[2801]10import org.openstreetmap.josm.tools.CheckParameterUtil;
[10294]11import org.openstreetmap.josm.tools.Utils;
[2801]12
[10294]13import oauth.signpost.OAuthConsumer;
14import oauth.signpost.OAuthProvider;
15
[2801]16/**
[9220]17 * This class manages an immutable set of OAuth parameters.
[5422]18 * @since 2747
[2801]19 */
20public class OAuthParameters {
21
[5422]22 /**
[6067]23 * The default JOSM OAuth consumer key (created by user josmeditor).
[5422]24 */
[6883]25 public static final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb";
[5422]26 /**
[6067]27 * The default JOSM OAuth consumer secret (created by user josmeditor).
[5422]28 */
[6883]29 public static final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5";
[2801]30
31 /**
32 * Replies a set of default parameters for a consumer accessing the standard OSM server
[5422]33 * at {@link OsmApi#DEFAULT_API_URL}.
[3479]34 *
[2801]35 * @return a set of default parameters
36 */
[6883]37 public static OAuthParameters createDefault() {
[5422]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>.
[6069]45 *
[5422]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 */
[6883]50 public static OAuthParameters createDefault(String apiUrl) {
[9355]51 final String consumerKey;
52 final String consumerSecret;
53 final String serverUrl;
54
[10294]55 if (!Utils.isValidUrl(apiUrl)) {
56 apiUrl = null;
[5422]57 }
[9355]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
[9220]69 return new OAuthParameters(
[9355]70 consumerKey,
71 consumerSecret,
72 serverUrl + "/oauth/request_token",
73 serverUrl + "/oauth/access_token",
74 serverUrl + "/oauth/authorize",
75 serverUrl + "/login",
76 serverUrl + "/logout");
[2801]77 }
78
79 /**
80 * Replies a set of parameters as defined in the preferences.
[3479]81 *
[12928]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 *
[2801]100 * @param pref the preferences
101 * @return the parameters
[12928]102 * @deprecated (since 12928) replaced by {@link #createFromApiUrl(java.lang.String)}
[2801]103 */
[12928]104 @Deprecated
[6883]105 public static OAuthParameters createFromPreferences(Preferences pref) {
[6599]106 OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
[9220]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()),
[9355]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()));
[2801]115 }
116
[9355]117 /**
[12928]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 /**
[9355]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.
[12928]135 * @deprecated (since 12928) replaced by {@link #rememberPreferences()}
[9355]136 */
[12928]137 @Deprecated
[9355]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
[9220]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;
[9355]154 private final String osmLoginUrl;
155 private final String osmLogoutUrl;
[2801]156
[5422]157 /**
[9220]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
[9355]164 * @param osmLoginUrl the OSM login URL (for automatic mode)
165 * @param osmLogoutUrl the OSM logout URL (for automatic mode)
[5422]166 * @see #createDefault
[12928]167 * @see #createFromApiUrl
[9220]168 * @since 9220
[5422]169 */
[9220]170 public OAuthParameters(String consumerKey, String consumerSecret,
[9355]171 String requestTokenUrl, String accessTokenUrl, String authoriseUrl, String osmLoginUrl, String osmLogoutUrl) {
[9220]172 this.consumerKey = consumerKey;
173 this.consumerSecret = consumerSecret;
174 this.requestTokenUrl = requestTokenUrl;
175 this.accessTokenUrl = accessTokenUrl;
176 this.authoriseUrl = authoriseUrl;
[9355]177 this.osmLoginUrl = osmLoginUrl;
178 this.osmLogoutUrl = osmLogoutUrl;
[2801]179 }
180
181 /**
182 * Creates a clone of the parameters in <code>other</code>.
[3479]183 *
[2801]184 * @param other the other parameters. Must not be null.
[8291]185 * @throws IllegalArgumentException if other is null
[2801]186 */
[8291]187 public OAuthParameters(OAuthParameters other) {
[2801]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;
[9355]194 this.osmLoginUrl = other.osmLoginUrl;
195 this.osmLogoutUrl = other.osmLogoutUrl;
[2801]196 }
197
[5422]198 /**
199 * Gets the consumer key.
200 * @return The consumer key
201 */
[2801]202 public String getConsumerKey() {
203 return consumerKey;
204 }
[6069]205
[5422]206 /**
[6069]207 * Gets the consumer secret.
[5422]208 * @return The consumer secret
209 */
[2801]210 public String getConsumerSecret() {
211 return consumerSecret;
212 }
[6069]213
[5422]214 /**
215 * Gets the request token URL.
216 * @return The request token URL
217 */
[2801]218 public String getRequestTokenUrl() {
219 return requestTokenUrl;
220 }
[6069]221
[5422]222 /**
223 * Gets the access token URL.
224 * @return The access token URL
225 */
[2801]226 public String getAccessTokenUrl() {
227 return accessTokenUrl;
228 }
[6069]229
[5422]230 /**
231 * Gets the authorise URL.
232 * @return The authorise URL
233 */
[2801]234 public String getAuthoriseUrl() {
235 return authoriseUrl;
236 }
[6069]237
[5422]238 /**
[9355]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 /**
[5422]255 * Builds an {@link OAuthConsumer} based on these parameters.
[3479]256 *
[2801]257 * @return the consumer
258 */
259 public OAuthConsumer buildConsumer() {
[9172]260 return new SignpostAdapters.OAuthConsumer(consumerKey, consumerSecret);
[2801]261 }
262
263 /**
[5266]264 * Builds an {@link OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>.
[3479]265 *
[2801]266 * @param consumer the consumer. Must not be null.
267 * @return the provider
[5422]268 * @throws IllegalArgumentException if consumer is null
[2801]269 */
[8291]270 public OAuthProvider buildProvider(OAuthConsumer consumer) {
[2801]271 CheckParameterUtil.ensureParameterNotNull(consumer, "consumer");
[9172]272 return new SignpostAdapters.OAuthProvider(
[2801]273 requestTokenUrl,
274 accessTokenUrl,
275 authoriseUrl
276 );
277 }
278
279 @Override
[9355]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);
[2801]291 }
292
293 @Override
[9355]294 public int hashCode() {
295 return Objects.hash(consumerKey, consumerSecret, requestTokenUrl, accessTokenUrl, authoriseUrl, osmLoginUrl, osmLogoutUrl);
[2801]296 }
297}
Note: See TracBrowser for help on using the repository browser.