Ignore:
Timestamp:
2017-10-14T00:09:56+02:00 (7 years ago)
Author:
Don-vip
Message:

fix #15435 - do not cache incorrect login credentials when using basic auth

File:
1 edited

Legend:

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

    r12931 r12992  
    3131 */
    3232public class OsmConnection {
     33
     34    private static final String BASIC_AUTH = "Basic ";
     35
    3336    protected boolean cancel;
    3437    protected HttpClient activeConnection;
     
    7578
    7679    /**
     80     * Retrieves login from basic authentication header, if set.
     81     *
     82     * @param con the connection
     83     * @return login from basic authentication header, or {@code null}
     84     * @throws OsmTransferException if something went wrong. Check for nested exceptions
     85     * @since 12992
     86     */
     87    protected String retrieveBasicAuthorizationLogin(HttpClient con) throws OsmTransferException {
     88        String auth = con.getRequestHeader("Authorization");
     89        if (auth != null && auth.startsWith(BASIC_AUTH)) {
     90            try {
     91                String[] token = new String(Base64.getDecoder().decode(auth.substring(BASIC_AUTH.length())),
     92                        StandardCharsets.UTF_8).split(":");
     93                if (token.length == 2) {
     94                    return token[0];
     95                }
     96            } catch (IllegalArgumentException e) {
     97                Logging.error(e);
     98            }
     99        }
     100        return null;
     101    }
     102
     103    /**
    77104     * Adds an authentication header for basic authentication
    78105     *
     
    98125                String password = response.getPassword() == null ? "" : String.valueOf(response.getPassword());
    99126                String token = username + ':' + password;
    100                 con.setHeader("Authorization", "Basic "+Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)));
     127                con.setHeader("Authorization", BASIC_AUTH + Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)));
    101128            }
    102129        }
Note: See TracChangeset for help on using the changeset viewer.