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

Last change on this file since 12686 was 12686, checked in by Don-vip, 4 months ago

see #15182 - move OAuthAccessTokenHolder from gui.preferences.server to data.oauth

  • Property svn:eol-style set to native
File size: 6.8 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.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 pref the preferences. Must not be null.
146     * @param cm the credential manager. Must not be null.
147     * @throws IllegalArgumentException if cm is null
148     */
149    public void init(Preferences pref, CredentialsAgent cm) {
150        CheckParameterUtil.ensureParameterNotNull(pref, "pref");
151        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
152        OAuthToken token = null;
153        try {
154            token = cm.lookupOAuthAccessToken();
155        } catch (CredentialsAgentException e) {
156            Logging.error(e);
157            Logging.warn(tr("Failed to retrieve OAuth Access Token from credential manager"));
158            Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
159        }
160        saveToPreferences = pref.getBoolean("oauth.access-token.save-to-preferences", true);
161        if (token != null) {
162            accessTokenKey = token.getKey();
163            accessTokenSecret = token.getSecret();
164        }
165    }
166
167    /**
168     * Saves the content of this holder to the preferences and a credential store managed
169     * by a credential manager.
170     *
171     * @param preferences the preferences. Must not be null.
172     * @param cm the credentials manager. Must not be null.
173     * @throws IllegalArgumentException if preferences is null
174     * @throws IllegalArgumentException if cm is null
175     */
176    public void save(Preferences preferences, CredentialsAgent cm) {
177        CheckParameterUtil.ensureParameterNotNull(preferences, "preferences");
178        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
179        preferences.put("oauth.access-token.save-to-preferences", saveToPreferences);
180        try {
181            if (!saveToPreferences) {
182                cm.storeOAuthAccessToken(null);
183            } else {
184                cm.storeOAuthAccessToken(new OAuthToken(accessTokenKey, accessTokenSecret));
185            }
186        } catch (CredentialsAgentException e) {
187            Logging.error(e);
188            Logging.warn(tr("Failed to store OAuth Access Token to credentials manager"));
189            Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
190        }
191    }
192
193    /**
194     * Clears the content of this holder
195     */
196    public void clear() {
197        accessTokenKey = null;
198        accessTokenSecret = null;
199    }
200}
Note: See TracBrowser for help on using the repository browser.