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

Last change on this file since 13536 was 13173, checked in by Don-vip, 6 years ago

see #15310 - remove most of deprecated APIs

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