Changeset 19345 in josm


Ignore:
Timestamp:
2025-03-10T17:38:42+01:00 (8 months ago)
Author:
stoecker
Message:

don't send authentication oinformation to wrong server, fix #24149, patch by ssundell

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r19101 r19345  
    825825                case HttpURLConnection.HTTP_UNAUTHORIZED:
    826826                case HttpURLConnection.HTTP_FORBIDDEN:
    827                     CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER);
     827                    CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER, getHost());
    828828                    throw new OsmApiException(retCode, errorHeader, errorBody, activeConnection.getURL().toString(),
    829829                            doAuthenticate ? retrieveBasicAuthorizationLogin(client) : null, response.getContentType());
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r18650 r19345  
    208208            try {
    209209                if (response.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
    210                     CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER);
     210                    CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
    211211                    throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);
    212212                }
  • trunk/src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java

    r12992 r19345  
    44import java.net.Authenticator.RequestorType;
    55import java.net.PasswordAuthentication;
    6 import java.util.EnumMap;
     6import java.util.HashMap;
    77import java.util.Map;
    88import java.util.Objects;
    99
    1010import org.openstreetmap.josm.tools.Logging;
     11import org.openstreetmap.josm.tools.Pair;
    1112
    1213/**
     
    4849    }
    4950
    50     protected Map<RequestorType, PasswordAuthentication> memoryCredentialsCache = new EnumMap<>(RequestorType.class);
     51    protected Map<Pair<RequestorType, String>, PasswordAuthentication> memoryCredentialsCache = new HashMap<>();
    5152
    5253    @Override
     
    6566         * -> Try to recall credentials that have been entered manually in this session.
    6667         */
    67         if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(requestorType) &&
     68        Pair<RequestorType, String> mccKey = Pair.create(requestorType, host);
     69        if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(mccKey) &&
    6870                (credentials == null || credentials.getPassword() == null || credentials.getPassword().length == 0)) {
    69             PasswordAuthentication pa = memoryCredentialsCache.get(requestorType);
     71            PasswordAuthentication pa = memoryCredentialsCache.get(mccKey);
    7072            response.setUsername(pa.getUserName());
    7173            response.setPassword(pa.getPassword());
     
    8991            } else {
    9092                // 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()));
    9294            }
    9395        } else {
     
    102104    @Override
    103105    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));
    105112    }
    106113
  • trunk/src/org/openstreetmap/josm/io/auth/CredentialsAgent.java

    r19320 r19345  
    8484     * Purges the internal credentials cache for the given requestor type.
    8585     * @param requestorType the type of service.
    86      * {@link RequestorType#SERVER} for the OSM API server, {@link RequestorType#PROXY} for a proxy server
     86     * {@link RequestorType#PROXY} for a proxy server, {@link RequestorType#SERVER} for other servers.
    8787     * @since 12992
    8888     */
    8989    void purgeCredentialsCache(RequestorType requestorType);
     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    }
    90100
    91101    /**
  • trunk/src/org/openstreetmap/josm/io/auth/CredentialsManager.java

    r19080 r19345  
    134134        }
    135135        // see #11914: clear cache before we store new value
    136         purgeCredentialsCache(requestorType);
     136        purgeCredentialsCache(requestorType, host);
    137137        delegate.store(requestorType, host, credentials);
    138138    }
     
    142142            throws CredentialsAgentException {
    143143        CredentialsAgentResponse credentials = delegate.getCredentials(requestorType, host, noSuccessWithLastResponse);
    144         if (requestorType == RequestorType.SERVER) {
     144        if (requestorType == RequestorType.SERVER && Objects.equals(OsmApi.getOsmApi().getHost(), host)) {
    145145            // see #11914 : Keep UserIdentityManager up to date
    146146            String userName = credentials.getUsername();
     
    175175        delegate.purgeCredentialsCache(requestorType);
    176176    }
     177
     178    @Override
     179    public void purgeCredentialsCache(RequestorType requestorType, String host) {
     180        delegate.purgeCredentialsCache(requestorType, host);
     181    }
    177182}
  • trunk/test/unit/org/openstreetmap/josm/io/auth/CredentialsManagerTest.java

    r18650 r19345  
    22package org.openstreetmap.josm.io.auth;
    33
     4import org.junit.jupiter.api.Assertions;
     5import org.junit.jupiter.api.Test;
    46import org.openstreetmap.josm.testutils.annotations.HTTP;
     7
     8import java.net.Authenticator;
     9import java.util.List;
    510
    611/**
     
    1318        return new CredentialsManager(new JosmPreferencesCredentialAgent());
    1419    }
     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    }
    1545}
Note: See TracChangeset for help on using the changeset viewer.