1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.oauth;
|
---|
3 |
|
---|
4 | import java.util.Objects;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.Main;
|
---|
7 | import org.openstreetmap.josm.data.Preferences;
|
---|
8 | import org.openstreetmap.josm.io.OsmApi;
|
---|
9 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
10 | import org.openstreetmap.josm.tools.Utils;
|
---|
11 |
|
---|
12 | import oauth.signpost.OAuthConsumer;
|
---|
13 | import oauth.signpost.OAuthProvider;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This class manages an immutable set of OAuth parameters.
|
---|
17 | * @since 2747
|
---|
18 | */
|
---|
19 | public 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 pref the preferences
|
---|
82 | * @return the parameters
|
---|
83 | */
|
---|
84 | public static OAuthParameters createFromPreferences(Preferences pref) {
|
---|
85 | OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
|
---|
86 | return new OAuthParameters(
|
---|
87 | pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()),
|
---|
88 | pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()),
|
---|
89 | pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()),
|
---|
90 | pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()),
|
---|
91 | pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()),
|
---|
92 | pref.get("oauth.settings.osm-login-url", parameters.getOsmLoginUrl()),
|
---|
93 | pref.get("oauth.settings.osm-logout-url", parameters.getOsmLogoutUrl()));
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Remembers the current values in the preferences <code>pref</code>.
|
---|
98 | *
|
---|
99 | * @param pref the preferences. Must not be null.
|
---|
100 | * @throws IllegalArgumentException if pref is null.
|
---|
101 | */
|
---|
102 | public void rememberPreferences(Preferences pref) {
|
---|
103 | CheckParameterUtil.ensureParameterNotNull(pref, "pref");
|
---|
104 | pref.put("oauth.settings.consumer-key", getConsumerKey());
|
---|
105 | pref.put("oauth.settings.consumer-secret", getConsumerSecret());
|
---|
106 | pref.put("oauth.settings.request-token-url", getRequestTokenUrl());
|
---|
107 | pref.put("oauth.settings.access-token-url", getAccessTokenUrl());
|
---|
108 | pref.put("oauth.settings.authorise-url", getAuthoriseUrl());
|
---|
109 | pref.put("oauth.settings.osm-login-url", getOsmLoginUrl());
|
---|
110 | pref.put("oauth.settings.osm-logout-url", getOsmLogoutUrl());
|
---|
111 | }
|
---|
112 |
|
---|
113 | private final String consumerKey;
|
---|
114 | private final String consumerSecret;
|
---|
115 | private final String requestTokenUrl;
|
---|
116 | private final String accessTokenUrl;
|
---|
117 | private final String authoriseUrl;
|
---|
118 | private final String osmLoginUrl;
|
---|
119 | private final String osmLogoutUrl;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Constructs a new {@code OAuthParameters}.
|
---|
123 | * @param consumerKey consumer key
|
---|
124 | * @param consumerSecret consumer secret
|
---|
125 | * @param requestTokenUrl request token URL
|
---|
126 | * @param accessTokenUrl access token URL
|
---|
127 | * @param authoriseUrl authorise URL
|
---|
128 | * @param osmLoginUrl the OSM login URL (for automatic mode)
|
---|
129 | * @param osmLogoutUrl the OSM logout URL (for automatic mode)
|
---|
130 | * @see #createDefault
|
---|
131 | * @see #createFromPreferences
|
---|
132 | * @since 9220
|
---|
133 | */
|
---|
134 | public OAuthParameters(String consumerKey, String consumerSecret,
|
---|
135 | String requestTokenUrl, String accessTokenUrl, String authoriseUrl, String osmLoginUrl, String osmLogoutUrl) {
|
---|
136 | this.consumerKey = consumerKey;
|
---|
137 | this.consumerSecret = consumerSecret;
|
---|
138 | this.requestTokenUrl = requestTokenUrl;
|
---|
139 | this.accessTokenUrl = accessTokenUrl;
|
---|
140 | this.authoriseUrl = authoriseUrl;
|
---|
141 | this.osmLoginUrl = osmLoginUrl;
|
---|
142 | this.osmLogoutUrl = osmLogoutUrl;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Creates a clone of the parameters in <code>other</code>.
|
---|
147 | *
|
---|
148 | * @param other the other parameters. Must not be null.
|
---|
149 | * @throws IllegalArgumentException if other is null
|
---|
150 | */
|
---|
151 | public OAuthParameters(OAuthParameters other) {
|
---|
152 | CheckParameterUtil.ensureParameterNotNull(other, "other");
|
---|
153 | this.consumerKey = other.consumerKey;
|
---|
154 | this.consumerSecret = other.consumerSecret;
|
---|
155 | this.accessTokenUrl = other.accessTokenUrl;
|
---|
156 | this.requestTokenUrl = other.requestTokenUrl;
|
---|
157 | this.authoriseUrl = other.authoriseUrl;
|
---|
158 | this.osmLoginUrl = other.osmLoginUrl;
|
---|
159 | this.osmLogoutUrl = other.osmLogoutUrl;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Gets the consumer key.
|
---|
164 | * @return The consumer key
|
---|
165 | */
|
---|
166 | public String getConsumerKey() {
|
---|
167 | return consumerKey;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Gets the consumer secret.
|
---|
172 | * @return The consumer secret
|
---|
173 | */
|
---|
174 | public String getConsumerSecret() {
|
---|
175 | return consumerSecret;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Gets the request token URL.
|
---|
180 | * @return The request token URL
|
---|
181 | */
|
---|
182 | public String getRequestTokenUrl() {
|
---|
183 | return requestTokenUrl;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Gets the access token URL.
|
---|
188 | * @return The access token URL
|
---|
189 | */
|
---|
190 | public String getAccessTokenUrl() {
|
---|
191 | return accessTokenUrl;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Gets the authorise URL.
|
---|
196 | * @return The authorise URL
|
---|
197 | */
|
---|
198 | public String getAuthoriseUrl() {
|
---|
199 | return authoriseUrl;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Gets the URL used to login users on the website (for automatic mode).
|
---|
204 | * @return The URL used to login users
|
---|
205 | */
|
---|
206 | public String getOsmLoginUrl() {
|
---|
207 | return osmLoginUrl;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Gets the URL used to logout users on the website (for automatic mode).
|
---|
212 | * @return The URL used to logout users
|
---|
213 | */
|
---|
214 | public String getOsmLogoutUrl() {
|
---|
215 | return osmLogoutUrl;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Builds an {@link OAuthConsumer} based on these parameters.
|
---|
220 | *
|
---|
221 | * @return the consumer
|
---|
222 | */
|
---|
223 | public OAuthConsumer buildConsumer() {
|
---|
224 | return new SignpostAdapters.OAuthConsumer(consumerKey, consumerSecret);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Builds an {@link OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>.
|
---|
229 | *
|
---|
230 | * @param consumer the consumer. Must not be null.
|
---|
231 | * @return the provider
|
---|
232 | * @throws IllegalArgumentException if consumer is null
|
---|
233 | */
|
---|
234 | public OAuthProvider buildProvider(OAuthConsumer consumer) {
|
---|
235 | CheckParameterUtil.ensureParameterNotNull(consumer, "consumer");
|
---|
236 | return new SignpostAdapters.OAuthProvider(
|
---|
237 | requestTokenUrl,
|
---|
238 | accessTokenUrl,
|
---|
239 | authoriseUrl
|
---|
240 | );
|
---|
241 | }
|
---|
242 |
|
---|
243 | @Override
|
---|
244 | public boolean equals(Object o) {
|
---|
245 | if (this == o) return true;
|
---|
246 | if (o == null || getClass() != o.getClass()) return false;
|
---|
247 | OAuthParameters that = (OAuthParameters) o;
|
---|
248 | return Objects.equals(consumerKey, that.consumerKey) &&
|
---|
249 | Objects.equals(consumerSecret, that.consumerSecret) &&
|
---|
250 | Objects.equals(requestTokenUrl, that.requestTokenUrl) &&
|
---|
251 | Objects.equals(accessTokenUrl, that.accessTokenUrl) &&
|
---|
252 | Objects.equals(authoriseUrl, that.authoriseUrl) &&
|
---|
253 | Objects.equals(osmLoginUrl, that.osmLoginUrl) &&
|
---|
254 | Objects.equals(osmLogoutUrl, that.osmLogoutUrl);
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Override
|
---|
258 | public int hashCode() {
|
---|
259 | return Objects.hash(consumerKey, consumerSecret, requestTokenUrl, accessTokenUrl, authoriseUrl, osmLoginUrl, osmLogoutUrl);
|
---|
260 | }
|
---|
261 | }
|
---|