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    public static final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb";
027    /**
028     * The default JOSM OAuth consumer secret (created by user josmeditor).
029     */
030    public static final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5";
031    /**
032     * The default OSM OAuth request token URL.
033     */
034    public static final String DEFAULT_REQUEST_TOKEN_URL = Main.getOSMWebsite() + "/oauth/request_token";
035    /**
036     * The default OSM OAuth access token URL.
037     */
038    public static final String DEFAULT_ACCESS_TOKEN_URL = Main.getOSMWebsite() + "/oauth/access_token";
039    /**
040     * The default OSM OAuth authorize URL.
041     */
042    public static final String DEFAULT_AUTHORISE_URL = Main.getOSMWebsite() + "/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    public static 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    public static 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                if (Main.isTraceEnabled()) {
082                    Main.trace(e.getMessage());
083                }
084            }
085        }
086        return parameters;
087    }
088
089    /**
090     * Replies a set of parameters as defined in the preferences.
091     *
092     * @param pref the preferences
093     * @return the parameters
094     */
095    public static OAuthParameters createFromPreferences(Preferences pref) {
096        OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
097        parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()));
098        parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()));
099        parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()));
100        parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()));
101        parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()));
102        return parameters;
103    }
104
105    private String consumerKey;
106    private String consumerSecret;
107    private String requestTokenUrl;
108    private String accessTokenUrl;
109    private String authoriseUrl;
110
111    /**
112     * Constructs a new, unitialized, {@code OAuthParameters}.
113     *
114     * @see #createDefault
115     * @see #createFromPreferences
116     */
117    public OAuthParameters() {
118        // contents can be set later with setters
119    }
120
121    /**
122     * Creates a clone of the parameters in <code>other</code>.
123     *
124     * @param other the other parameters. Must not be null.
125     * @throws IllegalArgumentException if other is null
126     */
127    public OAuthParameters(OAuthParameters other) {
128        CheckParameterUtil.ensureParameterNotNull(other, "other");
129        this.consumerKey = other.consumerKey;
130        this.consumerSecret = other.consumerSecret;
131        this.accessTokenUrl = other.accessTokenUrl;
132        this.requestTokenUrl = other.requestTokenUrl;
133        this.authoriseUrl = other.authoriseUrl;
134    }
135
136    /**
137     * Gets the consumer key.
138     * @return The consumer key
139     */
140    public String getConsumerKey() {
141        return consumerKey;
142    }
143
144    /**
145     * Sets the consumer key.
146     * @param consumerKey The consumer key
147     */
148    public void setConsumerKey(String consumerKey) {
149        this.consumerKey = consumerKey;
150    }
151
152    /**
153     * Gets the consumer secret.
154     * @return The consumer secret
155     */
156    public String getConsumerSecret() {
157        return consumerSecret;
158    }
159
160    /**
161     * Sets the consumer secret.
162     * @param consumerSecret The consumer secret
163     */
164    public void setConsumerSecret(String consumerSecret) {
165        this.consumerSecret = consumerSecret;
166    }
167
168    /**
169     * Gets the request token URL.
170     * @return The request token URL
171     */
172    public String getRequestTokenUrl() {
173        return requestTokenUrl;
174    }
175
176    /**
177     * Sets the request token URL.
178     * @param requestTokenUrl the request token URL
179     */
180    public void setRequestTokenUrl(String requestTokenUrl) {
181        this.requestTokenUrl = requestTokenUrl;
182    }
183
184    /**
185     * Gets the access token URL.
186     * @return The access token URL
187     */
188    public String getAccessTokenUrl() {
189        return accessTokenUrl;
190    }
191
192    /**
193     * Sets the access token URL.
194     * @param accessTokenUrl The access token URL
195     */
196    public void setAccessTokenUrl(String accessTokenUrl) {
197        this.accessTokenUrl = accessTokenUrl;
198    }
199
200    /**
201     * Gets the authorise URL.
202     * @return The authorise URL
203     */
204    public String getAuthoriseUrl() {
205        return authoriseUrl;
206    }
207
208    /**
209     * Sets the authorise URL.
210     * @param authoriseUrl The authorise URL
211     */
212    public void setAuthoriseUrl(String authoriseUrl) {
213        this.authoriseUrl = authoriseUrl;
214    }
215
216    /**
217     * Builds an {@link OAuthConsumer} based on these parameters.
218     *
219     * @return the consumer
220     */
221    public OAuthConsumer buildConsumer() {
222        return new DefaultOAuthConsumer(consumerKey, consumerSecret);
223    }
224
225    /**
226     * Builds an {@link OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>.
227     *
228     * @param consumer the consumer. Must not be null.
229     * @return the provider
230     * @throws IllegalArgumentException if consumer is null
231     */
232    public OAuthProvider buildProvider(OAuthConsumer consumer) {
233        CheckParameterUtil.ensureParameterNotNull(consumer, "consumer");
234        return new DefaultOAuthProvider(
235                requestTokenUrl,
236                accessTokenUrl,
237                authoriseUrl
238        );
239    }
240
241    @Override
242    public int hashCode() {
243        final int prime = 31;
244        int result = 1;
245        result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode());
246        result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode());
247        result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode());
248        result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode());
249        result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode());
250        return result;
251    }
252
253    @Override
254    public boolean equals(Object obj) {
255        if (this == obj)
256            return true;
257        if (obj == null)
258            return false;
259        if (getClass() != obj.getClass())
260            return false;
261        OAuthParameters other = (OAuthParameters) obj;
262        if (accessTokenUrl == null) {
263            if (other.accessTokenUrl != null)
264                return false;
265        } else if (!accessTokenUrl.equals(other.accessTokenUrl))
266            return false;
267        if (authoriseUrl == null) {
268            if (other.authoriseUrl != null)
269                return false;
270        } else if (!authoriseUrl.equals(other.authoriseUrl))
271            return false;
272        if (consumerKey == null) {
273            if (other.consumerKey != null)
274                return false;
275        } else if (!consumerKey.equals(other.consumerKey))
276            return false;
277        if (consumerSecret == null) {
278            if (other.consumerSecret != null)
279                return false;
280        } else if (!consumerSecret.equals(other.consumerSecret))
281            return false;
282        if (requestTokenUrl == null) {
283            if (other.requestTokenUrl != null)
284                return false;
285        } else if (!requestTokenUrl.equals(other.requestTokenUrl))
286            return false;
287        return true;
288    }
289}