001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.oauth;
003
004import java.net.MalformedURLException;
005import java.net.URL;
006
007import oauth.signpost.OAuthConsumer;
008import oauth.signpost.OAuthProvider;
009import oauth.signpost.basic.DefaultOAuthConsumer;
010import oauth.signpost.basic.DefaultOAuthProvider;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.data.Preferences;
014import org.openstreetmap.josm.io.OsmApi;
015import org.openstreetmap.josm.tools.CheckParameterUtil;
016
017/**
018 * This class manages a set of OAuth parameters.
019 * @since 2747
020 */
021public class OAuthParameters {
022
023    /**
024     * The default JOSM OAuth consumer key (created by user josmeditor).
025     */
026    static public final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb";
027    /**
028     * The default JOSM OAuth consumer secret (created by user josmeditor).
029     */
030    static public final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5";
031    /**
032     * The default OSM OAuth request token URL.
033     */
034    static public final String DEFAULT_REQUEST_TOKEN_URL = Main.OSM_WEBSITE + "/oauth/request_token";
035    /**
036     * The default OSM OAuth access token URL.
037     */
038    static public final String DEFAULT_ACCESS_TOKEN_URL = Main.OSM_WEBSITE + "/oauth/access_token";
039    /**
040     * The default OSM OAuth authorize URL.
041     */
042    static public final String DEFAULT_AUTHORISE_URL = Main.OSM_WEBSITE + "/oauth/authorize";
043
044
045    /**
046     * Replies a set of default parameters for a consumer accessing the standard OSM server
047     * at {@link OsmApi#DEFAULT_API_URL}.
048     *
049     * @return a set of default parameters
050     */
051    static public OAuthParameters createDefault() {
052        return createDefault(null);
053    }
054
055    /**
056     * Replies a set of default parameters for a consumer accessing an OSM server
057     * at the given API url. URL parameters are only set if the URL equals {@link OsmApi#DEFAULT_API_URL}
058     * or references the domain "dev.openstreetmap.org", otherwise they may be <code>null</code>.
059     *
060     * @param apiUrl The API URL for which the OAuth default parameters are created. If null or empty, the default OSM API url is used.
061     * @return a set of default parameters for the given {@code apiUrl}
062     * @since 5422
063     */
064    static public OAuthParameters createDefault(String apiUrl) {
065        OAuthParameters parameters = new OAuthParameters();
066        parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY);
067        parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET);
068        parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL);
069        parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL);
070        parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL);
071        if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
072            try {
073                String host = new URL(apiUrl).getHost();
074                if (host.endsWith("dev.openstreetmap.org")) {
075                    parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host));
076                    parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host));
077                    parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host));
078                }
079            } catch (MalformedURLException e) {
080                // Ignored
081            }
082        }
083        return parameters;
084    }
085
086    /**
087     * Replies a set of parameters as defined in the preferences.
088     *
089     * @param pref the preferences
090     * @return the parameters
091     */
092    static public OAuthParameters createFromPreferences(Preferences pref) {
093        OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
094        parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()));
095        parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()));
096        parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()));
097        parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()));
098        parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()));
099        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    /**
238     * Saves these OAuth parameters to the given {@code Preferences}.
239     * @param pref The Preferences into which are saved these OAuth parameters with the prefix "oauth.settings"
240     */
241    public void saveToPreferences(Preferences pref) {
242        pref.put("oauth.settings.consumer-key", consumerKey);
243        pref.put("oauth.settings.consumer-secret", consumerSecret);
244        pref.put("oauth.settings.request-token-url", requestTokenUrl);
245        pref.put("oauth.settings.access-token-url", accessTokenUrl);
246        pref.put("oauth.settings.authorise-url", authoriseUrl);
247    }
248
249    @Override
250    public int hashCode() {
251        final int prime = 31;
252        int result = 1;
253        result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode());
254        result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode());
255        result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode());
256        result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode());
257        result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode());
258        return result;
259    }
260
261    @Override
262    public boolean equals(Object obj) {
263        if (this == obj)
264            return true;
265        if (obj == null)
266            return false;
267        if (getClass() != obj.getClass())
268            return false;
269        OAuthParameters other = (OAuthParameters) obj;
270        if (accessTokenUrl == null) {
271            if (other.accessTokenUrl != null)
272                return false;
273        } else if (!accessTokenUrl.equals(other.accessTokenUrl))
274            return false;
275        if (authoriseUrl == null) {
276            if (other.authoriseUrl != null)
277                return false;
278        } else if (!authoriseUrl.equals(other.authoriseUrl))
279            return false;
280        if (consumerKey == null) {
281            if (other.consumerKey != null)
282                return false;
283        } else if (!consumerKey.equals(other.consumerKey))
284            return false;
285        if (consumerSecret == null) {
286            if (other.consumerSecret != null)
287                return false;
288        } else if (!consumerSecret.equals(other.consumerSecret))
289            return false;
290        if (requestTokenUrl == null) {
291            if (other.requestTokenUrl != null)
292                return false;
293        } else if (!requestTokenUrl.equals(other.requestTokenUrl))
294            return false;
295        return true;
296    }
297}