001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.oauth; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.net.URI; 007import java.util.EnumMap; 008import java.util.HashMap; 009import java.util.Map; 010import java.util.Objects; 011import java.util.Optional; 012 013import org.openstreetmap.josm.io.auth.CredentialsAgent; 014import org.openstreetmap.josm.io.auth.CredentialsAgentException; 015import org.openstreetmap.josm.io.auth.CredentialsManager; 016import org.openstreetmap.josm.spi.preferences.Config; 017import org.openstreetmap.josm.tools.CheckParameterUtil; 018import org.openstreetmap.josm.tools.Logging; 019 020/** 021 * Class holding OAuth access token key and secret. 022 * @since 12686 (moved from {@code gui.preferences.server} package) 023 */ 024public class OAuthAccessTokenHolder { 025 private static OAuthAccessTokenHolder instance; 026 027 /** 028 * Replies the unique instance. 029 * @return The unique instance of {@code OAuthAccessTokenHolder} 030 */ 031 public static synchronized OAuthAccessTokenHolder getInstance() { 032 if (instance == null) { 033 instance = new OAuthAccessTokenHolder(); 034 } 035 return instance; 036 } 037 038 private boolean saveToPreferences; 039 040 private final Map<String, Map<OAuthVersion, IOAuthToken>> tokenMap = new HashMap<>(); 041 042 /** 043 * Replies true if current access token should be saved to the preferences file. 044 * 045 * @return true if current access token should be saved to the preferences file. 046 */ 047 public boolean isSaveToPreferences() { 048 return saveToPreferences; 049 } 050 051 /** 052 * Sets whether the current access token should be saved to the preferences file. 053 * <p> 054 * If true, the access token is saved in clear text to the preferences file. The same 055 * access token can therefore be used in multiple JOSM sessions. 056 * <p> 057 * If false, the access token isn't saved to the preferences file. If JOSM is closed, 058 * the access token is lost and new token has to be generated by the OSM server the 059 * next time JOSM is used. 060 * 061 * @param saveToPreferences {@code true} to save to preferences file 062 */ 063 public void setSaveToPreferences(boolean saveToPreferences) { 064 this.saveToPreferences = saveToPreferences; 065 } 066 067 /** 068 * Replies the access token. 069 * @param api The api the token is for 070 * @param version The OAuth version the token is for 071 * @return the access token, can be {@code null} 072 * @since 18650 073 */ 074 public IOAuthToken getAccessToken(String api, OAuthVersion version) { 075 // Sometimes the api might be sent as the host 076 api = Optional.ofNullable(URI.create(api).getHost()).orElse(api); 077 if (this.tokenMap.containsKey(api) && this.tokenMap.get(api).containsKey(version)) { 078 Map<OAuthVersion, IOAuthToken> map = this.tokenMap.get(api); 079 return map.get(version); 080 } 081 try { 082 IOAuthToken token = CredentialsManager.getInstance().lookupOAuthAccessToken(api); 083 // We *do* want to set the API token to null, if it doesn't exist. Just to avoid unnecessary lookups. 084 if (token == null || token.getOAuthType() == version) { 085 this.setAccessToken(api, token); 086 return token; 087 } 088 } catch (CredentialsAgentException exception) { 089 Logging.trace(exception); 090 } 091 return null; 092 } 093 094 /** 095 * Sets the access token hold by this holder. 096 * 097 * @param api The api the token is for 098 * @param token the access token. Can be null to clear the content in this holder. 099 * @since 18650 100 */ 101 public void setAccessToken(String api, IOAuthToken token) { 102 Objects.requireNonNull(api, "api url"); 103 // Sometimes the api might be sent as the host 104 api = Optional.ofNullable(URI.create(api).getHost()).orElse(api); 105 if (token == null) { 106 if (this.tokenMap.containsKey(api)) { 107 this.tokenMap.get(api).clear(); 108 } 109 } else { 110 this.tokenMap.computeIfAbsent(api, key -> new EnumMap<>(OAuthVersion.class)).put(token.getOAuthType(), token); 111 } 112 } 113 114 /** 115 * Initializes the content of this holder from the Access Token managed by the 116 * credential manager. 117 * 118 * @param cm the credential manager. Must not be null. 119 * @throws IllegalArgumentException if cm is null 120 */ 121 public void init(CredentialsAgent cm) { 122 CheckParameterUtil.ensureParameterNotNull(cm, "cm"); 123 saveToPreferences = Config.getPref().getBoolean("oauth.access-token.save-to-preferences", true); 124 } 125 126 /** 127 * Saves the content of this holder to the preferences and a credential store managed 128 * by a credential manager. 129 * 130 * @param cm the credentials manager. Must not be null. 131 * @throws IllegalArgumentException if cm is null 132 */ 133 public void save(CredentialsAgent cm) { 134 CheckParameterUtil.ensureParameterNotNull(cm, "cm"); 135 Config.getPref().putBoolean("oauth.access-token.save-to-preferences", saveToPreferences); 136 try { 137 if (!saveToPreferences) { 138 for (String host : this.tokenMap.keySet()) { 139 cm.storeOAuthAccessToken(host, null); 140 } 141 } else { 142 for (Map.Entry<String, Map<OAuthVersion, IOAuthToken>> entry : this.tokenMap.entrySet()) { 143 if (entry.getValue().isEmpty()) { 144 cm.storeOAuthAccessToken(entry.getKey(), null); 145 continue; 146 } 147 for (OAuthVersion version : OAuthVersion.values()) { 148 if (entry.getValue().containsKey(version)) { 149 cm.storeOAuthAccessToken(entry.getKey(), entry.getValue().get(version)); 150 } 151 } 152 } 153 } 154 } catch (CredentialsAgentException e) { 155 Logging.error(e); 156 Logging.warn(tr("Failed to store OAuth Access Token to credentials manager")); 157 Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName())); 158 } 159 } 160 161 /** 162 * Clears the content of this holder 163 */ 164 public void clear() { 165 this.tokenMap.clear(); 166 } 167}