source: josm/trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java@ 3438

Last change on this file since 3438 was 3425, checked in by stoecker, 14 years ago

fix #5244 - port to signpost 1.2 - patch by dpaleino

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