[2801] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[12686] | 2 | package org.openstreetmap.josm.data.oauth;
|
---|
[2801] | 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import org.openstreetmap.josm.data.Preferences;
|
---|
[4245] | 7 | import org.openstreetmap.josm.io.auth.CredentialsAgent;
|
---|
| 8 | import org.openstreetmap.josm.io.auth.CredentialsAgentException;
|
---|
[2801] | 9 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[12620] | 10 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2801] | 11 |
|
---|
[6529] | 12 | /**
|
---|
| 13 | * Class holding OAuth access token key and secret.
|
---|
[12686] | 14 | * @since 12686 (moved from {@code gui.preferences.server} package)
|
---|
[6529] | 15 | */
|
---|
[2801] | 16 | public class OAuthAccessTokenHolder {
|
---|
[10378] | 17 | private static OAuthAccessTokenHolder instance;
|
---|
[2801] | 18 |
|
---|
[6529] | 19 | /**
|
---|
| 20 | * Replies the unique instance.
|
---|
| 21 | * @return The unique instance of {@code OAuthAccessTokenHolder}
|
---|
| 22 | */
|
---|
[8126] | 23 | public static synchronized OAuthAccessTokenHolder getInstance() {
|
---|
[2801] | 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.
|
---|
[3530] | 45 | *
|
---|
[2801] | 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.
|
---|
[3530] | 48 | *
|
---|
[2801] | 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.
|
---|
[3530] | 52 | *
|
---|
[8470] | 53 | * @param saveToPreferences {@code true} to save to preferences file
|
---|
[2801] | 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.
|
---|
[3530] | 61 | *
|
---|
[2801] | 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.
|
---|
[3530] | 70 | *
|
---|
[2801] | 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.
|
---|
[3530] | 79 | *
|
---|
[2801] | 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.
|
---|
[3530] | 88 | *
|
---|
[8470] | 89 | * @param accessTokenSecret access token secret, or null
|
---|
[2801] | 90 | */
|
---|
| 91 | public void setAccessTokenSecret(String accessTokenSecret) {
|
---|
| 92 | this.accessTokenSecret = accessTokenSecret;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[6529] | 95 | /**
|
---|
| 96 | * Replies the access token.
|
---|
| 97 | * @return the access token, can be {@code null}
|
---|
| 98 | */
|
---|
[2801] | 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.
|
---|
[3530] | 107 | *
|
---|
[2801] | 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.
|
---|
[3530] | 118 | *
|
---|
[2801] | 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.
|
---|
[3530] | 134 | *
|
---|
[2801] | 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.
|
---|
[3530] | 144 | *
|
---|
[2801] | 145 | * @param pref the preferences. Must not be null.
|
---|
| 146 | * @param cm the credential manager. Must not be null.
|
---|
[8291] | 147 | * @throws IllegalArgumentException if cm is null
|
---|
[2801] | 148 | */
|
---|
[8291] | 149 | public void init(Preferences pref, CredentialsAgent cm) {
|
---|
[2801] | 150 | CheckParameterUtil.ensureParameterNotNull(pref, "pref");
|
---|
| 151 | CheckParameterUtil.ensureParameterNotNull(cm, "cm");
|
---|
| 152 | OAuthToken token = null;
|
---|
| 153 | try {
|
---|
| 154 | token = cm.lookupOAuthAccessToken();
|
---|
[8510] | 155 | } catch (CredentialsAgentException e) {
|
---|
[12620] | 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()));
|
---|
[2801] | 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.
|
---|
[3530] | 170 | *
|
---|
[2801] | 171 | * @param preferences the preferences. Must not be null.
|
---|
| 172 | * @param cm the credentials manager. Must not be null.
|
---|
[8291] | 173 | * @throws IllegalArgumentException if preferences is null
|
---|
| 174 | * @throws IllegalArgumentException if cm is null
|
---|
[2801] | 175 | */
|
---|
[8291] | 176 | public void save(Preferences preferences, CredentialsAgent cm) {
|
---|
[2801] | 177 | CheckParameterUtil.ensureParameterNotNull(preferences, "preferences");
|
---|
| 178 | CheckParameterUtil.ensureParameterNotNull(cm, "cm");
|
---|
[12841] | 179 | preferences.putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
|
---|
[2801] | 180 | try {
|
---|
| 181 | if (!saveToPreferences) {
|
---|
| 182 | cm.storeOAuthAccessToken(null);
|
---|
| 183 | } else {
|
---|
| 184 | cm.storeOAuthAccessToken(new OAuthToken(accessTokenKey, accessTokenSecret));
|
---|
| 185 | }
|
---|
[8510] | 186 | } catch (CredentialsAgentException e) {
|
---|
[12620] | 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()));
|
---|
[2801] | 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
| 194 | * Clears the content of this holder
|
---|
| 195 | */
|
---|
| 196 | public void clear() {
|
---|
| 197 | accessTokenKey = null;
|
---|
| 198 | accessTokenSecret = null;
|
---|
| 199 | }
|
---|
| 200 | }
|
---|