Ticket #24149: 24149.patch
| File 24149.patch, 11.4 KB (added by , 13 months ago) |
|---|
-
src/org/openstreetmap/josm/io/auth/CredentialsManager.java
Subject: [PATCH] Use host as part of the key in memoryCredentialsCache --- IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/src/org/openstreetmap/josm/io/auth/CredentialsManager.java b/src/org/openstreetmap/josm/io/auth/CredentialsManager.java
a b 133 133 } 134 134 } 135 135 // see #11914: clear cache before we store new value 136 purgeCredentialsCache(requestorType );136 purgeCredentialsCache(requestorType, host); 137 137 delegate.store(requestorType, host, credentials); 138 138 } 139 139 … … 141 141 public CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse) 142 142 throws CredentialsAgentException { 143 143 CredentialsAgentResponse credentials = delegate.getCredentials(requestorType, host, noSuccessWithLastResponse); 144 if (requestorType == RequestorType.SERVER ) {144 if (requestorType == RequestorType.SERVER && Objects.equals(OsmApi.getOsmApi().getHost(), host)) { 145 145 // see #11914 : Keep UserIdentityManager up to date 146 146 String userName = credentials.getUsername(); 147 147 userName = userName == null ? "" : userName.trim(); … … 174 174 public void purgeCredentialsCache(RequestorType requestorType) { 175 175 delegate.purgeCredentialsCache(requestorType); 176 176 } 177 178 @Override 179 public void purgeCredentialsCache(RequestorType requestorType, String host) { 180 delegate.purgeCredentialsCache(requestorType, host); 181 } 177 182 } -
test/unit/org/openstreetmap/josm/io/auth/CredentialsManagerTest.java
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/test/unit/org/openstreetmap/josm/io/auth/CredentialsManagerTest.java b/test/unit/org/openstreetmap/josm/io/auth/CredentialsManagerTest.java
a b 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.io.auth; 3 3 4 import org.junit.jupiter.api.Assertions; 5 import org.junit.jupiter.api.Test; 4 6 import org.openstreetmap.josm.testutils.annotations.HTTP; 5 7 8 import java.net.Authenticator; 9 import java.util.List; 10 6 11 /** 7 12 * Test class for {@link CredentialsManager} 8 13 */ … … 12 17 public CredentialsManager createAgent() { 13 18 return new CredentialsManager(new JosmPreferencesCredentialAgent()); 14 19 } 20 21 @Test 22 public void testMultipleUnsavedHostsLookup() throws CredentialsAgentException { 23 final AbstractCredentialsAgent aca = new JosmPreferencesCredentialAgent(); 24 // A provider that mimics user giving the credentials and choosing not to store them in preferences. 25 AbstractCredentialsAgent.setCredentialsProvider((requestorType, agent, response, username, password, host) -> { 26 response.setUsername("user" + host); 27 response.setPassword("password".toCharArray()); 28 response.setSaveCredentials(false); 29 response.setCanceled(false); 30 }); 31 final CredentialsManager agent = new CredentialsManager(aca); 32 33 String host1 = "example.com"; 34 String host2 = "example.org"; 35 for (String host : List.of(host1, host2)) { 36 // Try to get credentials after "failure" => provider gives the credentials. 37 agent.getCredentials(Authenticator.RequestorType.SERVER, host, true); 38 } 39 // Both hosts should receive their respective credentials. 40 CredentialsAgentResponse response = agent.getCredentials(Authenticator.RequestorType.SERVER, host1, false); 41 Assertions.assertEquals("user" + host1, response.getUsername()); 42 response = agent.getCredentials(Authenticator.RequestorType.SERVER, host2, false); 43 Assertions.assertEquals("user" + host2, response.getUsername()); 44 } 15 45 } -
src/org/openstreetmap/josm/io/OsmServerReader.java
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/src/org/openstreetmap/josm/io/OsmServerReader.java b/src/org/openstreetmap/josm/io/OsmServerReader.java
a b 207 207 } 208 208 try { 209 209 if (response.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { 210 CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER );210 CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER, OsmApi.getOsmApi().getHost()); 211 211 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null); 212 212 } 213 213 -
src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java b/src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java
a b 3 3 4 4 import java.net.Authenticator.RequestorType; 5 5 import java.net.PasswordAuthentication; 6 import java.util. EnumMap;6 import java.util.HashMap; 7 7 import java.util.Map; 8 8 import java.util.Objects; 9 9 10 10 import org.openstreetmap.josm.tools.Logging; 11 import org.openstreetmap.josm.tools.Pair; 11 12 12 13 /** 13 14 * Partial implementation of the {@link CredentialsAgent} interface. … … 47 48 credentialsProvider = Objects.requireNonNull(provider, "provider"); 48 49 } 49 50 50 protected Map< RequestorType, PasswordAuthentication> memoryCredentialsCache = new EnumMap<>(RequestorType.class);51 protected Map<Pair<RequestorType, String>, PasswordAuthentication> memoryCredentialsCache = new HashMap<>(); 51 52 52 53 @Override 53 54 public CredentialsAgentResponse getCredentials(final RequestorType requestorType, final String host, boolean noSuccessWithLastResponse) … … 64 65 * Last request was successful and there was no credentials stored in file (or only the username is stored). 65 66 * -> Try to recall credentials that have been entered manually in this session. 66 67 */ 67 if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(requestorType) && 68 Pair<RequestorType, String> mccKey = Pair.create(requestorType, host); 69 if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(mccKey) && 68 70 (credentials == null || credentials.getPassword() == null || credentials.getPassword().length == 0)) { 69 PasswordAuthentication pa = memoryCredentialsCache.get( requestorType);71 PasswordAuthentication pa = memoryCredentialsCache.get(mccKey); 70 72 response.setUsername(pa.getUserName()); 71 73 response.setPassword(pa.getPassword()); 72 74 response.setCanceled(false); … … 88 90 )); 89 91 } else { 90 92 // User decides not to save credentials to file. Keep it in memory so we don't have to ask over and over again. 91 memoryCredentialsCache.put( requestorType, new PasswordAuthentication(response.getUsername(), response.getPassword()));93 memoryCredentialsCache.put(mccKey, new PasswordAuthentication(response.getUsername(), response.getPassword())); 92 94 } 93 95 } else { 94 96 // We got it from file. … … 101 103 102 104 @Override 103 105 public final void purgeCredentialsCache(RequestorType requestorType) { 104 memoryCredentialsCache.remove(requestorType); 106 memoryCredentialsCache.keySet().removeIf(pair -> pair.a == requestorType); 107 } 108 109 @Override 110 public void purgeCredentialsCache(RequestorType requestorType, String host) { 111 memoryCredentialsCache.remove(Pair.create(requestorType, host)); 105 112 } 106 113 107 114 /** -
src/org/openstreetmap/josm/io/OsmApi.java
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/src/org/openstreetmap/josm/io/OsmApi.java b/src/org/openstreetmap/josm/io/OsmApi.java
a b 824 824 throw new OsmApiException(retCode, errorHeader, errorBody); 825 825 case HttpURLConnection.HTTP_UNAUTHORIZED: 826 826 case HttpURLConnection.HTTP_FORBIDDEN: 827 CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER );827 CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER, getHost()); 828 828 throw new OsmApiException(retCode, errorHeader, errorBody, activeConnection.getURL().toString(), 829 829 doAuthenticate ? retrieveBasicAuthorizationLogin(client) : null, response.getContentType()); 830 830 default: -
src/org/openstreetmap/josm/io/auth/CredentialsAgent.java
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/src/org/openstreetmap/josm/io/auth/CredentialsAgent.java b/src/org/openstreetmap/josm/io/auth/CredentialsAgent.java
a b 83 83 /** 84 84 * Purges the internal credentials cache for the given requestor type. 85 85 * @param requestorType the type of service. 86 * {@link RequestorType# SERVER} for the OSM API server, {@link RequestorType#PROXY} for a proxy server86 * {@link RequestorType#PROXY} for a proxy server, {@link RequestorType#SERVER} for other servers. 87 87 * @since 12992 88 88 */ 89 89 void purgeCredentialsCache(RequestorType requestorType); 90 90 91 /** 92 * Purges the internal credentials cache for the given requestor type and host. 93 * @param requestorType the type of service. 94 * @param host the host. 95 * {@link RequestorType#PROXY} for a proxy server, {@link RequestorType#SERVER} for other servers. 96 */ 97 default void purgeCredentialsCache(RequestorType requestorType, String host) { 98 purgeCredentialsCache(requestorType); 99 } 100 91 101 /** 92 102 * Provide a Panel that is shown below the API password / username fields 93 103 * in the JOSM Preferences. (E.g. a warning that password is saved unencrypted.)
