| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.data.oauth; |
|---|
| 3 | |
|---|
| 4 | import oauth.signpost.OAuthConsumer; |
|---|
| 5 | import oauth.signpost.OAuthProvider; |
|---|
| 6 | import oauth.signpost.basic.DefaultOAuthConsumer; |
|---|
| 7 | import oauth.signpost.basic.DefaultOAuthProvider; |
|---|
| 8 | |
|---|
| 9 | import org.openstreetmap.josm.data.Preferences; |
|---|
| 10 | import org.openstreetmap.josm.tools.CheckParameterUtil; |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * This class manages a set of OAuth parameters. |
|---|
| 14 | * |
|---|
| 15 | */ |
|---|
| 16 | public class OAuthParameters { |
|---|
| 17 | |
|---|
| 18 | static public final String DEFAULT_JOSM_CONSUMER_KEY = "AdCRxTpvnbmfV8aPqrTLyA"; |
|---|
| 19 | static public final String DEFAULT_JOSM_CONSUMER_SECRET = "XmYOiGY9hApytcBC3xCec3e28QBqOWz5g6DSb5UpE"; |
|---|
| 20 | static public final String DEFAULT_REQUEST_TOKEN_URL = "http://www.openstreetmap.org/oauth/request_token"; |
|---|
| 21 | static public final String DEFAULT_ACCESS_TOKEN_URL = "http://www.openstreetmap.org/oauth/access_token"; |
|---|
| 22 | static public final String DEFAULT_AUTHORISE_URL = "http://www.openstreetmap.org/oauth/authorize"; |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Replies a set of default parameters for a consumer accessing the standard OSM server |
|---|
| 27 | * at http://api.openstreetmap.org/api |
|---|
| 28 | * |
|---|
| 29 | * @return a set of default parameters |
|---|
| 30 | */ |
|---|
| 31 | static public OAuthParameters createDefault() { |
|---|
| 32 | OAuthParameters parameters = new OAuthParameters(); |
|---|
| 33 | parameters.setConsumerKey(DEFAULT_JOSM_CONSUMER_KEY); |
|---|
| 34 | parameters.setConsumerSecret(DEFAULT_JOSM_CONSUMER_SECRET); |
|---|
| 35 | parameters.setRequestTokenUrl(DEFAULT_REQUEST_TOKEN_URL); |
|---|
| 36 | parameters.setAccessTokenUrl(DEFAULT_ACCESS_TOKEN_URL); |
|---|
| 37 | parameters.setAuthoriseUrl(DEFAULT_AUTHORISE_URL); |
|---|
| 38 | return parameters; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Replies a set of parameters as defined in the preferences. |
|---|
| 43 | * |
|---|
| 44 | * @param pref the preferences |
|---|
| 45 | * @return the parameters |
|---|
| 46 | */ |
|---|
| 47 | static public OAuthParameters createFromPreferences(Preferences pref) { |
|---|
| 48 | boolean useDefault = pref.getBoolean("oauth.settings.use-default", true ); |
|---|
| 49 | if (useDefault) |
|---|
| 50 | return createDefault(); |
|---|
| 51 | OAuthParameters parameters = new OAuthParameters(); |
|---|
| 52 | parameters.setConsumerKey(pref.get("oauth.settings.consumer-key", "")); |
|---|
| 53 | parameters.setConsumerSecret(pref.get("oauth.settings.consumer-secret", "")); |
|---|
| 54 | parameters.setRequestTokenUrl(pref.get("oauth.settings.request-token-url", "")); |
|---|
| 55 | parameters.setAccessTokenUrl(pref.get("oauth.settings.access-token-url", "")); |
|---|
| 56 | parameters.setAuthoriseUrl(pref.get("oauth.settings.authorise-url", "")); |
|---|
| 57 | return parameters; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Clears the preferences for OAuth parameters |
|---|
| 62 | * |
|---|
| 63 | * @param pref the preferences in which keys related to OAuth parameters are |
|---|
| 64 | * removed |
|---|
| 65 | */ |
|---|
| 66 | static public void clearPreferences(Preferences pref) { |
|---|
| 67 | pref.put("oauth.settings.consumer-key", null); |
|---|
| 68 | pref.put("oauth.settings.consumer-secret", null); |
|---|
| 69 | pref.put("oauth.settings.request-token-url", null); |
|---|
| 70 | pref.put("oauth.settings.access-token-url", null); |
|---|
| 71 | pref.put("oauth.settings.authorise-url", null); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | private String consumerKey; |
|---|
| 75 | private String consumerSecret; |
|---|
| 76 | private String requestTokenUrl; |
|---|
| 77 | private String accessTokenUrl; |
|---|
| 78 | private String authoriseUrl; |
|---|
| 79 | |
|---|
| 80 | public OAuthParameters() { |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Creates a clone of the parameters in <code>other</code>. |
|---|
| 85 | * |
|---|
| 86 | * @param other the other parameters. Must not be null. |
|---|
| 87 | * @throws IllegalArgumentException thrown if other is null |
|---|
| 88 | */ |
|---|
| 89 | public OAuthParameters(OAuthParameters other) throws IllegalArgumentException{ |
|---|
| 90 | CheckParameterUtil.ensureParameterNotNull(other, "other"); |
|---|
| 91 | this.consumerKey = other.consumerKey; |
|---|
| 92 | this.consumerSecret = other.consumerSecret; |
|---|
| 93 | this.accessTokenUrl = other.accessTokenUrl; |
|---|
| 94 | this.requestTokenUrl = other.requestTokenUrl; |
|---|
| 95 | this.authoriseUrl = other.authoriseUrl; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | public String getConsumerKey() { |
|---|
| 99 | return consumerKey; |
|---|
| 100 | } |
|---|
| 101 | public void setConsumerKey(String consumerKey) { |
|---|
| 102 | this.consumerKey = consumerKey; |
|---|
| 103 | } |
|---|
| 104 | public String getConsumerSecret() { |
|---|
| 105 | return consumerSecret; |
|---|
| 106 | } |
|---|
| 107 | public void setConsumerSecret(String consumerSecret) { |
|---|
| 108 | this.consumerSecret = consumerSecret; |
|---|
| 109 | } |
|---|
| 110 | public String getRequestTokenUrl() { |
|---|
| 111 | return requestTokenUrl; |
|---|
| 112 | } |
|---|
| 113 | public void setRequestTokenUrl(String requestTokenUrl) { |
|---|
| 114 | this.requestTokenUrl = requestTokenUrl; |
|---|
| 115 | } |
|---|
| 116 | public String getAccessTokenUrl() { |
|---|
| 117 | return accessTokenUrl; |
|---|
| 118 | } |
|---|
| 119 | public void setAccessTokenUrl(String accessTokenUrl) { |
|---|
| 120 | this.accessTokenUrl = accessTokenUrl; |
|---|
| 121 | } |
|---|
| 122 | public String getAuthoriseUrl() { |
|---|
| 123 | return authoriseUrl; |
|---|
| 124 | } |
|---|
| 125 | public void setAuthoriseUrl(String authoriseUrl) { |
|---|
| 126 | this.authoriseUrl = authoriseUrl; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * Builds an {@see OAuthConsumer} based on these parameters |
|---|
| 131 | * |
|---|
| 132 | * @return the consumer |
|---|
| 133 | */ |
|---|
| 134 | public OAuthConsumer buildConsumer() { |
|---|
| 135 | OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret); |
|---|
| 136 | return consumer; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * Builds an {@see OAuthProvider} based on these parameters and a OAuth consumer <code>consumer</code>. |
|---|
| 141 | * |
|---|
| 142 | * @param consumer the consumer. Must not be null. |
|---|
| 143 | * @return the provider |
|---|
| 144 | * @throws IllegalArgumentException thrown if consumer is null |
|---|
| 145 | */ |
|---|
| 146 | public OAuthProvider buildProvider(OAuthConsumer consumer) throws IllegalArgumentException { |
|---|
| 147 | CheckParameterUtil.ensureParameterNotNull(consumer, "consumer"); |
|---|
| 148 | return new DefaultOAuthProvider( |
|---|
| 149 | requestTokenUrl, |
|---|
| 150 | accessTokenUrl, |
|---|
| 151 | authoriseUrl |
|---|
| 152 | ); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | public void saveToPreferences(Preferences pref) { |
|---|
| 156 | if (this.equals(createDefault())) { |
|---|
| 157 | pref.put("oauth.settings.use-default", true ); |
|---|
| 158 | clearPreferences(pref); |
|---|
| 159 | return; |
|---|
| 160 | } |
|---|
| 161 | pref.put("oauth.settings.use-default", false); |
|---|
| 162 | pref.put("oauth.settings.consumer-key", consumerKey); |
|---|
| 163 | pref.put("oauth.settings.consumer-secret", consumerSecret); |
|---|
| 164 | pref.put("oauth.settings.request-token-url", requestTokenUrl); |
|---|
| 165 | pref.put("oauth.settings.access-token-url", accessTokenUrl); |
|---|
| 166 | pref.put("oauth.settings.authorise-url", authoriseUrl); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | @Override |
|---|
| 170 | public int hashCode() { |
|---|
| 171 | final int prime = 31; |
|---|
| 172 | int result = 1; |
|---|
| 173 | result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode()); |
|---|
| 174 | result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode()); |
|---|
| 175 | result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode()); |
|---|
| 176 | result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode()); |
|---|
| 177 | result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode()); |
|---|
| 178 | return result; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | @Override |
|---|
| 182 | public boolean equals(Object obj) { |
|---|
| 183 | if (this == obj) |
|---|
| 184 | return true; |
|---|
| 185 | if (obj == null) |
|---|
| 186 | return false; |
|---|
| 187 | if (getClass() != obj.getClass()) |
|---|
| 188 | return false; |
|---|
| 189 | OAuthParameters other = (OAuthParameters) obj; |
|---|
| 190 | if (accessTokenUrl == null) { |
|---|
| 191 | if (other.accessTokenUrl != null) |
|---|
| 192 | return false; |
|---|
| 193 | } else if (!accessTokenUrl.equals(other.accessTokenUrl)) |
|---|
| 194 | return false; |
|---|
| 195 | if (authoriseUrl == null) { |
|---|
| 196 | if (other.authoriseUrl != null) |
|---|
| 197 | return false; |
|---|
| 198 | } else if (!authoriseUrl.equals(other.authoriseUrl)) |
|---|
| 199 | return false; |
|---|
| 200 | if (consumerKey == null) { |
|---|
| 201 | if (other.consumerKey != null) |
|---|
| 202 | return false; |
|---|
| 203 | } else if (!consumerKey.equals(other.consumerKey)) |
|---|
| 204 | return false; |
|---|
| 205 | if (consumerSecret == null) { |
|---|
| 206 | if (other.consumerSecret != null) |
|---|
| 207 | return false; |
|---|
| 208 | } else if (!consumerSecret.equals(other.consumerSecret)) |
|---|
| 209 | return false; |
|---|
| 210 | if (requestTokenUrl == null) { |
|---|
| 211 | if (other.requestTokenUrl != null) |
|---|
| 212 | return false; |
|---|
| 213 | } else if (!requestTokenUrl.equals(other.requestTokenUrl)) |
|---|
| 214 | return false; |
|---|
| 215 | return true; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|