Changeset 19345 in josm for trunk/src


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

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

Location:
trunk/src/org/openstreetmap/josm/io
Files:
5 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}
Note: See TracChangeset for help on using the changeset viewer.