[2801] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data.oauth;
|
---|
| 3 |
|
---|
[5422] | 4 | import java.net.MalformedURLException;
|
---|
| 5 | import java.net.URL;
|
---|
| 6 |
|
---|
[2801] | 7 | import oauth.signpost.OAuthConsumer;
|
---|
| 8 | import oauth.signpost.OAuthProvider;
|
---|
| 9 | import oauth.signpost.basic.DefaultOAuthConsumer;
|
---|
| 10 | import oauth.signpost.basic.DefaultOAuthProvider;
|
---|
| 11 |
|
---|
[6453] | 12 | import org.openstreetmap.josm.Main;
|
---|
[2801] | 13 | import org.openstreetmap.josm.data.Preferences;
|
---|
[5422] | 14 | import org.openstreetmap.josm.io.OsmApi;
|
---|
[2801] | 15 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * This class manages a set of OAuth parameters.
|
---|
[5422] | 19 | * @since 2747
|
---|
[2801] | 20 | */
|
---|
| 21 | public class OAuthParameters {
|
---|
| 22 |
|
---|
[5422] | 23 | /**
|
---|
[6067] | 24 | * The default JOSM OAuth consumer key (created by user josmeditor).
|
---|
[5422] | 25 | */
|
---|
[6883] | 26 | public static final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb";
|
---|
[5422] | 27 | /**
|
---|
[6067] | 28 | * The default JOSM OAuth consumer secret (created by user josmeditor).
|
---|
[5422] | 29 | */
|
---|
[6883] | 30 | public static final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5";
|
---|
[5422] | 31 | /**
|
---|
| 32 | * The default OSM OAuth request token URL.
|
---|
| 33 | */
|
---|
[6897] | 34 | public static final String DEFAULT_REQUEST_TOKEN_URL = Main.getOSMWebsite() + "/oauth/request_token";
|
---|
[5422] | 35 | /**
|
---|
| 36 | * The default OSM OAuth access token URL.
|
---|
| 37 | */
|
---|
[6897] | 38 | public static final String DEFAULT_ACCESS_TOKEN_URL = Main.getOSMWebsite() + "/oauth/access_token";
|
---|
[5422] | 39 | /**
|
---|
| 40 | * The default OSM OAuth authorize URL.
|
---|
| 41 | */
|
---|
[6897] | 42 | public static final String DEFAULT_AUTHORISE_URL = Main.getOSMWebsite() + "/oauth/authorize";
|
---|
[2801] | 43 |
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Replies a set of default parameters for a consumer accessing the standard OSM server
|
---|
[5422] | 47 | * at {@link OsmApi#DEFAULT_API_URL}.
|
---|
[3479] | 48 | *
|
---|
[2801] | 49 | * @return a set of default parameters
|
---|
| 50 | */
|
---|
[6883] | 51 | public static OAuthParameters createDefault() {
|
---|
[5422] | 52 | return createDefault(null);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Replies a set of default parameters for a consumer accessing an OSM server
|
---|
| 57 | * at the given API url. URL parameters are only set if the URL equals {@link OsmApi#DEFAULT_API_URL}
|
---|
| 58 | * or references the domain "dev.openstreetmap.org", otherwise they may be <code>null</code>.
|
---|
[6069] | 59 | *
|
---|
[5422] | 60 | * @param apiUrl The API URL for which the OAuth default parameters are created. If null or empty, the default OSM API url is used.
|
---|
| 61 | * @return a set of default parameters for the given {@code apiUrl}
|
---|
| 62 | * @since 5422
|
---|
| 63 | */
|
---|
[6883] | 64 | public static OAuthParameters createDefault(String apiUrl) {
|
---|
[2801] | 65 | OAuthParameters parameters = new OAuthParameters();
|
---|
[6076] | 66 | parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY);
|
---|
| 67 | parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET);
|
---|
| 68 | parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL);
|
---|
| 69 | parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL);
|
---|
| 70 | parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL);
|
---|
[6243] | 71 | if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
|
---|
[5422] | 72 | try {
|
---|
| 73 | String host = new URL(apiUrl).getHost();
|
---|
| 74 | if (host.endsWith("dev.openstreetmap.org")) {
|
---|
| 75 | parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host));
|
---|
| 76 | parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host));
|
---|
| 77 | parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host));
|
---|
| 78 | }
|
---|
| 79 | } catch (MalformedURLException e) {
|
---|
| 80 | // Ignored
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
[2801] | 83 | return parameters;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Replies a set of parameters as defined in the preferences.
|
---|
[3479] | 88 | *
|
---|
[2801] | 89 | * @param pref the preferences
|
---|
| 90 | * @return the parameters
|
---|
| 91 | */
|
---|
[6883] | 92 | public static OAuthParameters createFromPreferences(Preferences pref) {
|
---|
[6599] | 93 | OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
|
---|
[6077] | 94 | parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()));
|
---|
| 95 | parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()));
|
---|
[6076] | 96 | parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()));
|
---|
| 97 | parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()));
|
---|
| 98 | parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()));
|
---|
[2801] | 99 | 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 |
|
---|
[5422] | 108 | /**
|
---|
| 109 | * Constructs a new, unitialized, {@code OAuthParameters}.
|
---|
[6069] | 110 | *
|
---|
[5422] | 111 | * @see #createDefault
|
---|
| 112 | * @see #createFromPreferences
|
---|
| 113 | */
|
---|
[2801] | 114 | public OAuthParameters() {
|
---|
[8415] | 115 | // contents can be set later with setters
|
---|
[2801] | 116 | }
|
---|
| 117 |
|
---|
| 118 | /**
|
---|
| 119 | * Creates a clone of the parameters in <code>other</code>.
|
---|
[3479] | 120 | *
|
---|
[2801] | 121 | * @param other the other parameters. Must not be null.
|
---|
[8291] | 122 | * @throws IllegalArgumentException if other is null
|
---|
[2801] | 123 | */
|
---|
[8291] | 124 | public OAuthParameters(OAuthParameters other) {
|
---|
[2801] | 125 | CheckParameterUtil.ensureParameterNotNull(other, "other");
|
---|
| 126 | this.consumerKey = other.consumerKey;
|
---|
| 127 | this.consumerSecret = other.consumerSecret;
|
---|
| 128 | this.accessTokenUrl = other.accessTokenUrl;
|
---|
| 129 | this.requestTokenUrl = other.requestTokenUrl;
|
---|
| 130 | this.authoriseUrl = other.authoriseUrl;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[5422] | 133 | /**
|
---|
| 134 | * Gets the consumer key.
|
---|
| 135 | * @return The consumer key
|
---|
| 136 | */
|
---|
[2801] | 137 | public String getConsumerKey() {
|
---|
| 138 | return consumerKey;
|
---|
| 139 | }
|
---|
[6069] | 140 |
|
---|
[5422] | 141 | /**
|
---|
| 142 | * Sets the consumer key.
|
---|
| 143 | * @param consumerKey The consumer key
|
---|
| 144 | */
|
---|
[2801] | 145 | public void setConsumerKey(String consumerKey) {
|
---|
| 146 | this.consumerKey = consumerKey;
|
---|
| 147 | }
|
---|
[6069] | 148 |
|
---|
[5422] | 149 | /**
|
---|
[6069] | 150 | * Gets the consumer secret.
|
---|
[5422] | 151 | * @return The consumer secret
|
---|
| 152 | */
|
---|
[2801] | 153 | public String getConsumerSecret() {
|
---|
| 154 | return consumerSecret;
|
---|
| 155 | }
|
---|
[6069] | 156 |
|
---|
[5422] | 157 | /**
|
---|
| 158 | * Sets the consumer secret.
|
---|
| 159 | * @param consumerSecret The consumer secret
|
---|
| 160 | */
|
---|
[2801] | 161 | public void setConsumerSecret(String consumerSecret) {
|
---|
| 162 | this.consumerSecret = consumerSecret;
|
---|
| 163 | }
|
---|
[6069] | 164 |
|
---|
[5422] | 165 | /**
|
---|
| 166 | * Gets the request token URL.
|
---|
| 167 | * @return The request token URL
|
---|
| 168 | */
|
---|
[2801] | 169 | public String getRequestTokenUrl() {
|
---|
| 170 | return requestTokenUrl;
|
---|
| 171 | }
|
---|
[6069] | 172 |
|
---|
[5422] | 173 | /**
|
---|
| 174 | * Sets the request token URL.
|
---|
| 175 | * @param requestTokenUrl the request token URL
|
---|
| 176 | */
|
---|
[2801] | 177 | public void setRequestTokenUrl(String requestTokenUrl) {
|
---|
| 178 | this.requestTokenUrl = requestTokenUrl;
|
---|
| 179 | }
|
---|
[6069] | 180 |
|
---|
[5422] | 181 | /**
|
---|
| 182 | * Gets the access token URL.
|
---|
| 183 | * @return The access token URL
|
---|
| 184 | */
|
---|
[2801] | 185 | public String getAccessTokenUrl() {
|
---|
| 186 | return accessTokenUrl;
|
---|
| 187 | }
|
---|
[6069] | 188 |
|
---|
[5422] | 189 | /**
|
---|
| 190 | * Sets the access token URL.
|
---|
| 191 | * @param accessTokenUrl The access token URL
|
---|
| 192 | */
|
---|
[2801] | 193 | public void setAccessTokenUrl(String accessTokenUrl) {
|
---|
| 194 | this.accessTokenUrl = accessTokenUrl;
|
---|
| 195 | }
|
---|
[6069] | 196 |
|
---|
[5422] | 197 | /**
|
---|
| 198 | * Gets the authorise URL.
|
---|
| 199 | * @return The authorise URL
|
---|
| 200 | */
|
---|
[2801] | 201 | public String getAuthoriseUrl() {
|
---|
| 202 | return authoriseUrl;
|
---|
| 203 | }
|
---|
[6069] | 204 |
|
---|
[5422] | 205 | /**
|
---|
| 206 | * Sets the authorise URL.
|
---|
| 207 | * @param authoriseUrl The authorise URL
|
---|
| 208 | */
|
---|
[2801] | 209 | public void setAuthoriseUrl(String authoriseUrl) {
|
---|
| 210 | this.authoriseUrl = authoriseUrl;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /**
|
---|
[5422] | 214 | * Builds an {@link OAuthConsumer} based on these parameters.
|
---|
[3479] | 215 | *
|
---|
[2801] | 216 | * @return the consumer
|
---|
| 217 | */
|
---|
| 218 | public OAuthConsumer buildConsumer() {
|
---|
[5422] | 219 | return new DefaultOAuthConsumer(consumerKey, consumerSecret);
|
---|
[2801] | 220 | }
|
---|
| 221 |
|
---|
| 222 | /**
|
---|
[5266] | 223 | * Builds an {@link OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>.
|
---|
[3479] | 224 | *
|
---|
[2801] | 225 | * @param consumer the consumer. Must not be null.
|
---|
| 226 | * @return the provider
|
---|
[5422] | 227 | * @throws IllegalArgumentException if consumer is null
|
---|
[2801] | 228 | */
|
---|
[8291] | 229 | public OAuthProvider buildProvider(OAuthConsumer consumer) {
|
---|
[2801] | 230 | CheckParameterUtil.ensureParameterNotNull(consumer, "consumer");
|
---|
| 231 | return new DefaultOAuthProvider(
|
---|
| 232 | requestTokenUrl,
|
---|
| 233 | accessTokenUrl,
|
---|
| 234 | authoriseUrl
|
---|
| 235 | );
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | @Override
|
---|
| 239 | public int hashCode() {
|
---|
| 240 | final int prime = 31;
|
---|
| 241 | int result = 1;
|
---|
| 242 | result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode());
|
---|
| 243 | result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode());
|
---|
| 244 | result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode());
|
---|
| 245 | result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode());
|
---|
| 246 | result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode());
|
---|
| 247 | return result;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | @Override
|
---|
| 251 | public boolean equals(Object obj) {
|
---|
| 252 | if (this == obj)
|
---|
| 253 | return true;
|
---|
| 254 | if (obj == null)
|
---|
| 255 | return false;
|
---|
| 256 | if (getClass() != obj.getClass())
|
---|
| 257 | return false;
|
---|
| 258 | OAuthParameters other = (OAuthParameters) obj;
|
---|
| 259 | if (accessTokenUrl == null) {
|
---|
| 260 | if (other.accessTokenUrl != null)
|
---|
| 261 | return false;
|
---|
| 262 | } else if (!accessTokenUrl.equals(other.accessTokenUrl))
|
---|
| 263 | return false;
|
---|
| 264 | if (authoriseUrl == null) {
|
---|
| 265 | if (other.authoriseUrl != null)
|
---|
| 266 | return false;
|
---|
| 267 | } else if (!authoriseUrl.equals(other.authoriseUrl))
|
---|
| 268 | return false;
|
---|
| 269 | if (consumerKey == null) {
|
---|
| 270 | if (other.consumerKey != null)
|
---|
| 271 | return false;
|
---|
| 272 | } else if (!consumerKey.equals(other.consumerKey))
|
---|
| 273 | return false;
|
---|
| 274 | if (consumerSecret == null) {
|
---|
| 275 | if (other.consumerSecret != null)
|
---|
| 276 | return false;
|
---|
| 277 | } else if (!consumerSecret.equals(other.consumerSecret))
|
---|
| 278 | return false;
|
---|
| 279 | if (requestTokenUrl == null) {
|
---|
| 280 | if (other.requestTokenUrl != null)
|
---|
| 281 | return false;
|
---|
| 282 | } else if (!requestTokenUrl.equals(other.requestTokenUrl))
|
---|
| 283 | return false;
|
---|
| 284 | return true;
|
---|
| 285 | }
|
---|
| 286 | }
|
---|