source: josm/trunk/src/org/openstreetmap/josm/data/oauth/OAuthAccessTokenHolder.java@ 12928

Last change on this file since 12928 was 12928, checked in by bastiK, 7 years ago

see #15229 - do not copy the entire preferences list, just to set a custom server API in OAuth wizard

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.oauth;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import org.openstreetmap.josm.data.Preferences;
7import org.openstreetmap.josm.io.auth.CredentialsAgent;
8import org.openstreetmap.josm.io.auth.CredentialsAgentException;
9import org.openstreetmap.josm.spi.preferences.Config;
10import org.openstreetmap.josm.tools.CheckParameterUtil;
11import org.openstreetmap.josm.tools.Logging;
12
13/**
14 * Class holding OAuth access token key and secret.
15 * @since 12686 (moved from {@code gui.preferences.server} package)
16 */
17public class OAuthAccessTokenHolder {
18 private static OAuthAccessTokenHolder instance;
19
20 /**
21 * Replies the unique instance.
22 * @return The unique instance of {@code OAuthAccessTokenHolder}
23 */
24 public static synchronized OAuthAccessTokenHolder getInstance() {
25 if (instance == null) {
26 instance = new OAuthAccessTokenHolder();
27 }
28 return instance;
29 }
30
31 private boolean saveToPreferences;
32 private String accessTokenKey;
33 private String accessTokenSecret;
34
35 /**
36 * Replies true if current access token should be saved to the preferences file.
37 *
38 * @return true if current access token should be saved to the preferences file.
39 */
40 public boolean isSaveToPreferences() {
41 return saveToPreferences;
42 }
43
44 /**
45 * Sets whether the current access token should be saved to the preferences file.
46 *
47 * If true, the access token is saved in clear text to the preferences file. The same
48 * access token can therefore be used in multiple JOSM sessions.
49 *
50 * If false, the access token isn't saved to the preferences file. If JOSM is closed,
51 * the access token is lost and new token has to be generated by the OSM server the
52 * next time JOSM is used.
53 *
54 * @param saveToPreferences {@code true} to save to preferences file
55 */
56 public void setSaveToPreferences(boolean saveToPreferences) {
57 this.saveToPreferences = saveToPreferences;
58 }
59
60 /**
61 * Replies the access token key. null, if no access token key is currently set.
62 *
63 * @return the access token key
64 */
65 public String getAccessTokenKey() {
66 return accessTokenKey;
67 }
68
69 /**
70 * Sets the access token key. Pass in null to remove the current access token key.
71 *
72 * @param accessTokenKey the access token key
73 */
74 public void setAccessTokenKey(String accessTokenKey) {
75 this.accessTokenKey = accessTokenKey;
76 }
77
78 /**
79 * Replies the access token secret. null, if no access token secret is currently set.
80 *
81 * @return the access token secret
82 */
83 public String getAccessTokenSecret() {
84 return accessTokenSecret;
85 }
86
87 /**
88 * Sets the access token secret. Pass in null to remove the current access token secret.
89 *
90 * @param accessTokenSecret access token secret, or null
91 */
92 public void setAccessTokenSecret(String accessTokenSecret) {
93 this.accessTokenSecret = accessTokenSecret;
94 }
95
96 /**
97 * Replies the access token.
98 * @return the access token, can be {@code null}
99 */
100 public OAuthToken getAccessToken() {
101 if (!containsAccessToken())
102 return null;
103 return new OAuthToken(accessTokenKey, accessTokenSecret);
104 }
105
106 /**
107 * Sets the access token hold by this holder.
108 *
109 * @param accessTokenKey the access token key
110 * @param accessTokenSecret the access token secret
111 */
112 public void setAccessToken(String accessTokenKey, String accessTokenSecret) {
113 this.accessTokenKey = accessTokenKey;
114 this.accessTokenSecret = accessTokenSecret;
115 }
116
117 /**
118 * Sets the access token hold by this holder.
119 *
120 * @param token the access token. Can be null to clear the content in this holder.
121 */
122 public void setAccessToken(OAuthToken token) {
123 if (token == null) {
124 this.accessTokenKey = null;
125 this.accessTokenSecret = null;
126 } else {
127 this.accessTokenKey = token.getKey();
128 this.accessTokenSecret = token.getSecret();
129 }
130 }
131
132 /**
133 * Replies true if this holder contains an complete access token, consisting of an
134 * Access Token Key and an Access Token Secret.
135 *
136 * @return true if this holder contains an complete access token
137 */
138 public boolean containsAccessToken() {
139 return accessTokenKey != null && accessTokenSecret != null;
140 }
141
142 /**
143 * Initializes the content of this holder from the Access Token managed by the
144 * credential manager.
145 *
146 * @param cm the credential manager. Must not be null.
147 * @throws IllegalArgumentException if cm is null
148 */
149 public void init(CredentialsAgent cm) {
150 CheckParameterUtil.ensureParameterNotNull(cm, "cm");
151 OAuthToken token = null;
152 try {
153 token = cm.lookupOAuthAccessToken();
154 } catch (CredentialsAgentException e) {
155 Logging.error(e);
156 Logging.warn(tr("Failed to retrieve OAuth Access Token from credential manager"));
157 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
158 }
159 saveToPreferences = Config.getPref().getBoolean("oauth.access-token.save-to-preferences", true);
160 if (token != null) {
161 accessTokenKey = token.getKey();
162 accessTokenSecret = token.getSecret();
163 }
164 }
165
166 /**
167 * Initializes the content of this holder from the Access Token managed by the
168 * credential manager.
169 *
170 * @param pref the preferences. Must not be null.
171 * @param cm the credential manager. Must not be null.
172 * @throws IllegalArgumentException if cm is null
173 * @deprecated (since 12928) replaced by {@link #init(org.openstreetmap.josm.io.auth.CredentialsAgent)}
174 */
175 @Deprecated
176 public void init(Preferences pref, CredentialsAgent cm) {
177 CheckParameterUtil.ensureParameterNotNull(pref, "pref");
178 CheckParameterUtil.ensureParameterNotNull(cm, "cm");
179 OAuthToken token = null;
180 try {
181 token = cm.lookupOAuthAccessToken();
182 } catch (CredentialsAgentException e) {
183 Logging.error(e);
184 Logging.warn(tr("Failed to retrieve OAuth Access Token from credential manager"));
185 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
186 }
187 saveToPreferences = pref.getBoolean("oauth.access-token.save-to-preferences", true);
188 if (token != null) {
189 accessTokenKey = token.getKey();
190 accessTokenSecret = token.getSecret();
191 }
192 }
193
194 /**
195 * Saves the content of this holder to the preferences and a credential store managed
196 * by a credential manager.
197 *
198 * @param cm the credentials manager. Must not be null.
199 * @throws IllegalArgumentException if cm is null
200 */
201 public void save(CredentialsAgent cm) {
202 CheckParameterUtil.ensureParameterNotNull(cm, "cm");
203 Config.getPref().putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
204 try {
205 if (!saveToPreferences) {
206 cm.storeOAuthAccessToken(null);
207 } else {
208 cm.storeOAuthAccessToken(new OAuthToken(accessTokenKey, accessTokenSecret));
209 }
210 } catch (CredentialsAgentException e) {
211 Logging.error(e);
212 Logging.warn(tr("Failed to store OAuth Access Token to credentials manager"));
213 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
214 }
215 }
216
217 /**
218 * Saves the content of this holder to the preferences and a credential store managed
219 * by a credential manager.
220 *
221 * @param preferences the preferences. Must not be null.
222 * @param cm the credentials manager. Must not be null.
223 * @throws IllegalArgumentException if preferences is null
224 * @throws IllegalArgumentException if cm is null
225 * @deprecated (since 12928) replaced by {@link #save(org.openstreetmap.josm.io.auth.CredentialsAgent)}
226 */
227 @Deprecated
228 public void save(Preferences preferences, CredentialsAgent cm) {
229 CheckParameterUtil.ensureParameterNotNull(preferences, "preferences");
230 CheckParameterUtil.ensureParameterNotNull(cm, "cm");
231 preferences.putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
232 try {
233 if (!saveToPreferences) {
234 cm.storeOAuthAccessToken(null);
235 } else {
236 cm.storeOAuthAccessToken(new OAuthToken(accessTokenKey, accessTokenSecret));
237 }
238 } catch (CredentialsAgentException e) {
239 Logging.error(e);
240 Logging.warn(tr("Failed to store OAuth Access Token to credentials manager"));
241 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
242 }
243 }
244
245 /**
246 * Clears the content of this holder
247 */
248 public void clear() {
249 accessTokenKey = null;
250 accessTokenSecret = null;
251 }
252}
Note: See TracBrowser for help on using the repository browser.