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; 009 010import org.openstreetmap.josm.Main; 011import org.openstreetmap.josm.data.Preferences; 012import org.openstreetmap.josm.io.OsmApi; 013import org.openstreetmap.josm.tools.CheckParameterUtil; 014 015/** 016 * This class manages an immutable set of OAuth parameters. 017 * @since 2747 018 */ 019public class OAuthParameters { 020 021 /** 022 * The default JOSM OAuth consumer key (created by user josmeditor). 023 */ 024 public static final String DEFAULT_JOSM_CONSUMER_KEY = "F7zPYlVCqE2BUH9Hr4SsWZSOnrKjpug1EgqkbsSb"; 025 /** 026 * The default JOSM OAuth consumer secret (created by user josmeditor). 027 */ 028 public static final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5"; 029 /** 030 * The default OSM OAuth request token URL. 031 */ 032 public static final String DEFAULT_REQUEST_TOKEN_URL = Main.getOSMWebsite() + "/oauth/request_token"; 033 /** 034 * The default OSM OAuth access token URL. 035 */ 036 public static final String DEFAULT_ACCESS_TOKEN_URL = Main.getOSMWebsite() + "/oauth/access_token"; 037 /** 038 * The default OSM OAuth authorize URL. 039 */ 040 public static final String DEFAULT_AUTHORISE_URL = Main.getOSMWebsite() + "/oauth/authorize"; 041 042 /** 043 * Replies a set of default parameters for a consumer accessing the standard OSM server 044 * at {@link OsmApi#DEFAULT_API_URL}. 045 * 046 * @return a set of default parameters 047 */ 048 public static OAuthParameters createDefault() { 049 return createDefault(null); 050 } 051 052 /** 053 * Replies a set of default parameters for a consumer accessing an OSM server 054 * at the given API url. URL parameters are only set if the URL equals {@link OsmApi#DEFAULT_API_URL} 055 * or references the domain "dev.openstreetmap.org", otherwise they may be <code>null</code>. 056 * 057 * @param apiUrl The API URL for which the OAuth default parameters are created. If null or empty, the default OSM API url is used. 058 * @return a set of default parameters for the given {@code apiUrl} 059 * @since 5422 060 */ 061 public static OAuthParameters createDefault(String apiUrl) { 062 String host = ""; 063 if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) { 064 try { 065 host = new URL(apiUrl).getHost(); 066 } catch (MalformedURLException e) { 067 // Ignored 068 if (Main.isTraceEnabled()) { 069 Main.trace(e.getMessage()); 070 } 071 } 072 } 073 boolean osmDevServer = host.endsWith("dev.openstreetmap.org"); 074 return new OAuthParameters( 075 DEFAULT_JOSM_CONSUMER_KEY, 076 DEFAULT_JOSM_CONSUMER_SECRET, 077 osmDevServer ? DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host) : DEFAULT_REQUEST_TOKEN_URL, 078 osmDevServer ? DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host) : DEFAULT_ACCESS_TOKEN_URL, 079 osmDevServer ? DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host) : DEFAULT_AUTHORISE_URL); 080 } 081 082 /** 083 * Replies a set of parameters as defined in the preferences. 084 * 085 * @param pref the preferences 086 * @return the parameters 087 */ 088 public static OAuthParameters createFromPreferences(Preferences pref) { 089 OAuthParameters parameters = createDefault(pref.get("osm-server.url")); 090 return new OAuthParameters( 091 pref.get("oauth.settings.consumer-key", parameters.getConsumerKey()), 092 pref.get("oauth.settings.consumer-secret", parameters.getConsumerSecret()), 093 pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()), 094 pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()), 095 pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()) 096 ); 097 } 098 099 private final String consumerKey; 100 private final String consumerSecret; 101 private final String requestTokenUrl; 102 private final String accessTokenUrl; 103 private final String authoriseUrl; 104 105 /** 106 * Constructs a new {@code OAuthParameters}. 107 * @param consumerKey consumer key 108 * @param consumerSecret consumer secret 109 * @param requestTokenUrl request token URL 110 * @param accessTokenUrl access token URL 111 * @param authoriseUrl authorise URL 112 * 113 * @see #createDefault 114 * @see #createFromPreferences 115 * @since 9220 116 */ 117 public OAuthParameters(String consumerKey, String consumerSecret, 118 String requestTokenUrl, String accessTokenUrl, String authoriseUrl) { 119 this.consumerKey = consumerKey; 120 this.consumerSecret = consumerSecret; 121 this.requestTokenUrl = requestTokenUrl; 122 this.accessTokenUrl = accessTokenUrl; 123 this.authoriseUrl = authoriseUrl; 124 } 125 126 /** 127 * Creates a clone of the parameters in <code>other</code>. 128 * 129 * @param other the other parameters. Must not be null. 130 * @throws IllegalArgumentException if other is null 131 */ 132 public OAuthParameters(OAuthParameters other) { 133 CheckParameterUtil.ensureParameterNotNull(other, "other"); 134 this.consumerKey = other.consumerKey; 135 this.consumerSecret = other.consumerSecret; 136 this.accessTokenUrl = other.accessTokenUrl; 137 this.requestTokenUrl = other.requestTokenUrl; 138 this.authoriseUrl = other.authoriseUrl; 139 } 140 141 /** 142 * Gets the consumer key. 143 * @return The consumer key 144 */ 145 public String getConsumerKey() { 146 return consumerKey; 147 } 148 149 /** 150 * Gets the consumer secret. 151 * @return The consumer secret 152 */ 153 public String getConsumerSecret() { 154 return consumerSecret; 155 } 156 157 /** 158 * Gets the request token URL. 159 * @return The request token URL 160 */ 161 public String getRequestTokenUrl() { 162 return requestTokenUrl; 163 } 164 165 /** 166 * Gets the access token URL. 167 * @return The access token URL 168 */ 169 public String getAccessTokenUrl() { 170 return accessTokenUrl; 171 } 172 173 /** 174 * Gets the authorise URL. 175 * @return The authorise URL 176 */ 177 public String getAuthoriseUrl() { 178 return authoriseUrl; 179 } 180 181 /** 182 * Builds an {@link OAuthConsumer} based on these parameters. 183 * 184 * @return the consumer 185 */ 186 public OAuthConsumer buildConsumer() { 187 return new SignpostAdapters.OAuthConsumer(consumerKey, consumerSecret); 188 } 189 190 /** 191 * Builds an {@link OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>. 192 * 193 * @param consumer the consumer. Must not be null. 194 * @return the provider 195 * @throws IllegalArgumentException if consumer is null 196 */ 197 public OAuthProvider buildProvider(OAuthConsumer consumer) { 198 CheckParameterUtil.ensureParameterNotNull(consumer, "consumer"); 199 return new SignpostAdapters.OAuthProvider( 200 requestTokenUrl, 201 accessTokenUrl, 202 authoriseUrl 203 ); 204 } 205 206 @Override 207 public int hashCode() { 208 final int prime = 31; 209 int result = 1; 210 result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode()); 211 result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode()); 212 result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode()); 213 result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode()); 214 result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode()); 215 return result; 216 } 217 218 @Override 219 public boolean equals(Object obj) { 220 if (this == obj) 221 return true; 222 if (obj == null) 223 return false; 224 if (getClass() != obj.getClass()) 225 return false; 226 OAuthParameters other = (OAuthParameters) obj; 227 if (accessTokenUrl == null) { 228 if (other.accessTokenUrl != null) 229 return false; 230 } else if (!accessTokenUrl.equals(other.accessTokenUrl)) 231 return false; 232 if (authoriseUrl == null) { 233 if (other.authoriseUrl != null) 234 return false; 235 } else if (!authoriseUrl.equals(other.authoriseUrl)) 236 return false; 237 if (consumerKey == null) { 238 if (other.consumerKey != null) 239 return false; 240 } else if (!consumerKey.equals(other.consumerKey)) 241 return false; 242 if (consumerSecret == null) { 243 if (other.consumerSecret != null) 244 return false; 245 } else if (!consumerSecret.equals(other.consumerSecret)) 246 return false; 247 if (requestTokenUrl == null) { 248 if (other.requestTokenUrl != null) 249 return false; 250 } else if (!requestTokenUrl.equals(other.requestTokenUrl)) 251 return false; 252 return true; 253 } 254}