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