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

Last change on this file since 13173 was 13173, checked in by Don-vip, 6 years ago

see #15310 - remove most of deprecated APIs

  • Property svn:eol-style set to native
File size: 9.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.io.OsmApi;
8import org.openstreetmap.josm.spi.preferences.Config;
9import org.openstreetmap.josm.tools.CheckParameterUtil;
10import org.openstreetmap.josm.tools.Utils;
11
12import oauth.signpost.OAuthConsumer;
13import oauth.signpost.OAuthProvider;
14
15/**
16 * This class manages an immutable 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 /**
31 * Replies a set of default parameters for a consumer accessing the standard OSM server
32 * at {@link OsmApi#DEFAULT_API_URL}.
33 *
34 * @return a set of default parameters
35 */
36 public static OAuthParameters createDefault() {
37 return createDefault(null);
38 }
39
40 /**
41 * Replies a set of default parameters for a consumer accessing an OSM server
42 * at the given API url. URL parameters are only set if the URL equals {@link OsmApi#DEFAULT_API_URL}
43 * or references the domain "dev.openstreetmap.org", otherwise they may be <code>null</code>.
44 *
45 * @param apiUrl The API URL for which the OAuth default parameters are created. If null or empty, the default OSM API url is used.
46 * @return a set of default parameters for the given {@code apiUrl}
47 * @since 5422
48 */
49 public static OAuthParameters createDefault(String apiUrl) {
50 final String consumerKey;
51 final String consumerSecret;
52 final String serverUrl;
53
54 if (!Utils.isValidUrl(apiUrl)) {
55 apiUrl = null;
56 }
57
58 if (apiUrl != null && !OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
59 consumerKey = ""; // a custom consumer key is required
60 consumerSecret = ""; // a custom consumer secret is requireds
61 serverUrl = apiUrl.replaceAll("/api$", "");
62 } else {
63 consumerKey = DEFAULT_JOSM_CONSUMER_KEY;
64 consumerSecret = DEFAULT_JOSM_CONSUMER_SECRET;
65 serverUrl = Main.getOSMWebsite();
66 }
67
68 return new OAuthParameters(
69 consumerKey,
70 consumerSecret,
71 serverUrl + "/oauth/request_token",
72 serverUrl + "/oauth/access_token",
73 serverUrl + "/oauth/authorize",
74 serverUrl + "/login",
75 serverUrl + "/logout");
76 }
77
78 /**
79 * Replies a set of parameters as defined in the preferences.
80 *
81 * @param apiUrl the API URL. Must not be null.
82 * @return the parameters
83 */
84 public static OAuthParameters createFromApiUrl(String apiUrl) {
85 OAuthParameters parameters = createDefault(apiUrl);
86 return new OAuthParameters(
87 Config.getPref().get("oauth.settings.consumer-key", parameters.getConsumerKey()),
88 Config.getPref().get("oauth.settings.consumer-secret", parameters.getConsumerSecret()),
89 Config.getPref().get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()),
90 Config.getPref().get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()),
91 Config.getPref().get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()),
92 Config.getPref().get("oauth.settings.osm-login-url", parameters.getOsmLoginUrl()),
93 Config.getPref().get("oauth.settings.osm-logout-url", parameters.getOsmLogoutUrl()));
94 }
95
96 /**
97 * Remembers the current values in the preferences.
98 */
99 public void rememberPreferences() {
100 Config.getPref().put("oauth.settings.consumer-key", getConsumerKey());
101 Config.getPref().put("oauth.settings.consumer-secret", getConsumerSecret());
102 Config.getPref().put("oauth.settings.request-token-url", getRequestTokenUrl());
103 Config.getPref().put("oauth.settings.access-token-url", getAccessTokenUrl());
104 Config.getPref().put("oauth.settings.authorise-url", getAuthoriseUrl());
105 Config.getPref().put("oauth.settings.osm-login-url", getOsmLoginUrl());
106 Config.getPref().put("oauth.settings.osm-logout-url", getOsmLogoutUrl());
107 }
108
109 private final String consumerKey;
110 private final String consumerSecret;
111 private final String requestTokenUrl;
112 private final String accessTokenUrl;
113 private final String authoriseUrl;
114 private final String osmLoginUrl;
115 private final String osmLogoutUrl;
116
117 /**
118 * Constructs a new {@code OAuthParameters}.
119 * @param consumerKey consumer key
120 * @param consumerSecret consumer secret
121 * @param requestTokenUrl request token URL
122 * @param accessTokenUrl access token URL
123 * @param authoriseUrl authorise URL
124 * @param osmLoginUrl the OSM login URL (for automatic mode)
125 * @param osmLogoutUrl the OSM logout URL (for automatic mode)
126 * @see #createDefault
127 * @see #createFromApiUrl
128 * @since 9220
129 */
130 public OAuthParameters(String consumerKey, String consumerSecret,
131 String requestTokenUrl, String accessTokenUrl, String authoriseUrl, String osmLoginUrl, String osmLogoutUrl) {
132 this.consumerKey = consumerKey;
133 this.consumerSecret = consumerSecret;
134 this.requestTokenUrl = requestTokenUrl;
135 this.accessTokenUrl = accessTokenUrl;
136 this.authoriseUrl = authoriseUrl;
137 this.osmLoginUrl = osmLoginUrl;
138 this.osmLogoutUrl = osmLogoutUrl;
139 }
140
141 /**
142 * Creates a clone of the parameters in <code>other</code>.
143 *
144 * @param other the other parameters. Must not be null.
145 * @throws IllegalArgumentException if other is null
146 */
147 public OAuthParameters(OAuthParameters other) {
148 CheckParameterUtil.ensureParameterNotNull(other, "other");
149 this.consumerKey = other.consumerKey;
150 this.consumerSecret = other.consumerSecret;
151 this.accessTokenUrl = other.accessTokenUrl;
152 this.requestTokenUrl = other.requestTokenUrl;
153 this.authoriseUrl = other.authoriseUrl;
154 this.osmLoginUrl = other.osmLoginUrl;
155 this.osmLogoutUrl = other.osmLogoutUrl;
156 }
157
158 /**
159 * Gets the consumer key.
160 * @return The consumer key
161 */
162 public String getConsumerKey() {
163 return consumerKey;
164 }
165
166 /**
167 * Gets the consumer secret.
168 * @return The consumer secret
169 */
170 public String getConsumerSecret() {
171 return consumerSecret;
172 }
173
174 /**
175 * Gets the request token URL.
176 * @return The request token URL
177 */
178 public String getRequestTokenUrl() {
179 return 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 * Gets the authorise URL.
192 * @return The authorise URL
193 */
194 public String getAuthoriseUrl() {
195 return authoriseUrl;
196 }
197
198 /**
199 * Gets the URL used to login users on the website (for automatic mode).
200 * @return The URL used to login users
201 */
202 public String getOsmLoginUrl() {
203 return osmLoginUrl;
204 }
205
206 /**
207 * Gets the URL used to logout users on the website (for automatic mode).
208 * @return The URL used to logout users
209 */
210 public String getOsmLogoutUrl() {
211 return osmLogoutUrl;
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 boolean equals(Object o) {
241 if (this == o) return true;
242 if (o == null || getClass() != o.getClass()) return false;
243 OAuthParameters that = (OAuthParameters) o;
244 return Objects.equals(consumerKey, that.consumerKey) &&
245 Objects.equals(consumerSecret, that.consumerSecret) &&
246 Objects.equals(requestTokenUrl, that.requestTokenUrl) &&
247 Objects.equals(accessTokenUrl, that.accessTokenUrl) &&
248 Objects.equals(authoriseUrl, that.authoriseUrl) &&
249 Objects.equals(osmLoginUrl, that.osmLoginUrl) &&
250 Objects.equals(osmLogoutUrl, that.osmLogoutUrl);
251 }
252
253 @Override
254 public int hashCode() {
255 return Objects.hash(consumerKey, consumerSecret, requestTokenUrl, accessTokenUrl, authoriseUrl, osmLoginUrl, osmLogoutUrl);
256 }
257}
Note: See TracBrowser for help on using the repository browser.