Changeset 4690 in josm


Ignore:
Timestamp:
2011-12-21T23:12:59+01:00 (8 years ago)
Author:
stoecker
Message:

see #7086 - fix passing auth information to wrong server

Location:
trunk/src/org/openstreetmap/josm
Files:
11 edited

Legend:

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

    r4645 r4690  
    4242public class CredentialDialog extends JDialog {
    4343
    44     static public CredentialDialog getOsmApiCredentialDialog(String username, String password, String saveUsernameAndPasswordCheckboxText) {
     44    static public CredentialDialog getOsmApiCredentialDialog(String username, String password, String host, String saveUsernameAndPasswordCheckboxText) {
    4545        CredentialDialog dialog = new CredentialDialog(saveUsernameAndPasswordCheckboxText);
    46         dialog.prepareForOsmApiCredentials(username, password);
     46        if(OsmApi.getOsmApi().getHost().equals(host)) {
     47            dialog.prepareForOsmApiCredentials(username, password);
     48        } else {
     49            dialog.prepareForOtherHostCredentials(username, password, host);
     50        }
    4751        dialog.pack();
    4852        return dialog;
    4953    }
    5054
    51     static public CredentialDialog getHttpProxyCredentialDialog(String username, String password, String saveUsernameAndPasswordCheckboxText) {
     55    static public CredentialDialog getHttpProxyCredentialDialog(String username, String password, String host, String saveUsernameAndPasswordCheckboxText) {
    5256        CredentialDialog dialog = new CredentialDialog(saveUsernameAndPasswordCheckboxText);
    5357        dialog.prepareForProxyCredentials(username, password);
     
    109113        setTitle(tr("Enter credentials for OSM API"));
    110114        getContentPane().add(pnlCredentials = new OsmApiCredentialsPanel(this), BorderLayout.CENTER);
     115        pnlCredentials.init(username, password);
     116        validate();
     117    }
     118
     119    public void prepareForOtherHostCredentials(String username, String password, String host) {
     120        setTitle(tr("Enter credentials for host"));
     121        getContentPane().add(pnlCredentials = new OtherHostCredentialsPanel(this, host), BorderLayout.CENTER);
    111122        pnlCredentials.init(username, password);
    112123        validate();
     
    257268    }
    258269
     270    private static class OtherHostCredentialsPanel extends CredentialPanel {
     271
     272        String host;
     273
     274        @Override
     275        protected void build() {
     276            super.build();
     277            tfUserName.setToolTipText(tr("Please enter the user name of your account"));
     278            tfPassword.setToolTipText(tr("Please enter the password of your account"));
     279            lblHeading.setText(
     280                    "<html>" + tr("Authenticating at the host ''{0}'' failed. Please enter a valid username and a valid password.",
     281                            host) + "</html>");
     282            lblWarning.setText(tr("Warning: The password is transferred unencrypted."));
     283        }
     284
     285        public OtherHostCredentialsPanel(CredentialDialog owner, String host) {
     286            super(owner);
     287            this.host = host;
     288            build();
     289        }
     290    }
     291
    259292    private static class HttpProxyCredentialsPanel extends CredentialPanel {
    260293        @Override
  • trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java

    r4253 r4690  
    4242import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
    4343import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
     44import org.openstreetmap.josm.io.OsmApi;
    4445import org.openstreetmap.josm.io.OsmTransferException;
    4546import org.openstreetmap.josm.io.auth.CredentialsAgent;
     
    184185        CredentialsAgent cm = CredentialsManager.getInstance();
    185186        try {
    186             PasswordAuthentication pa = cm.lookup(RequestorType.SERVER);
     187            PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
    187188            if (pa == null) {
    188189                tfUserName.setText("");
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/BasicAuthenticationPreferencesPanel.java

    r4263 r4690  
    2323import org.openstreetmap.josm.io.auth.CredentialsManager;
    2424import org.openstreetmap.josm.io.auth.JosmPreferencesCredentialAgent;
     25import org.openstreetmap.josm.io.OsmApi;
    2526
    2627/**
     
    9495            decorationPanel.removeAll();
    9596            decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER);
    96             PasswordAuthentication pa = cm.lookup(RequestorType.SERVER);
     97            PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
    9798            if (pa == null) {
    9899                tfOsmUserName.setText("");
     
    118119                    tfOsmPassword.getPassword()
    119120            );
    120             cm.store(RequestorType.SERVER, pa);
     121            cm.store(RequestorType.SERVER, OsmApi.getOsmApi().getHost(), pa);
    121122        } catch(CredentialsAgentException e) {
    122123            e.printStackTrace();
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java

    r4246 r4690  
    325325        CredentialsAgent cm = CredentialsManager.getInstance();
    326326        try {
    327             PasswordAuthentication pa = cm.lookup(RequestorType.PROXY);
     327            PasswordAuthentication pa = cm.lookup(RequestorType.PROXY, tfProxyHttpHost.getText());
    328328            if (pa == null) {
    329329                tfProxyHttpUser.setText("");
     
    404404                    tfProxyHttpPassword.getPassword()
    405405            );
    406             cm.store(RequestorType.PROXY, pa);
     406            cm.store(RequestorType.PROXY, tfProxyHttpHost.getText(), pa);
    407407        } catch(CredentialsAgentException e) {
    408408            e.printStackTrace();
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r4645 r4690  
    1717import java.net.ConnectException;
    1818import java.net.HttpURLConnection;
     19import java.net.MalformedURLException;
    1920import java.net.SocketTimeoutException;
    2021import java.net.URL;
     
    141142    public String getVersion() {
    142143        return version;
     144    }
     145
     146    public String getHost() {
     147        String host = null;
     148        try {
     149            host = (new URL(serverUrl)).getHost();
     150        } catch (MalformedURLException e) {
     151        }
     152        return host;
    143153    }
    144154
  • trunk/src/org/openstreetmap/josm/io/OsmConnection.java

    r4245 r4690  
    7777        try {
    7878            synchronized (CredentialsManager.getInstance()) {
    79                 response = CredentialsManager.getInstance().getCredentials(RequestorType.SERVER, false /* don't know yet whether the credentials will succeed */);
     79                response = CredentialsManager.getInstance().getCredentials(RequestorType.SERVER,
     80                con.getURL().getHost(), false /* don't know yet whether the credentials will succeed */);
    8081            }
    8182        } catch (CredentialsAgentException e) {
  • trunk/src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java

    r4249 r4690  
    1717     */
    1818    @Override
    19     public CredentialsAgentResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsAgentException{
     19    public CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse) throws CredentialsAgentException{
    2020        if (requestorType == null)
    2121            return null;
    22         PasswordAuthentication credentials =  lookup(requestorType);
     22        PasswordAuthentication credentials =  lookup(requestorType, host);
    2323        String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
    2424        String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
     
    4747            CredentialDialog dialog = null;
    4848            switch(requestorType) {
    49             case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password, getSaveUsernameAndPasswordCheckboxText()); break;
    50             case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password, getSaveUsernameAndPasswordCheckboxText()); break;
     49            case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password, host, getSaveUsernameAndPasswordCheckboxText()); break;
     50            case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password, host, getSaveUsernameAndPasswordCheckboxText()); break;
    5151            }
    5252            dialog.setVisible(true);
     
    5757            response.setPassword(dialog.getPassword());
    5858            if (dialog.isSaveCredentials()) {
    59                 store(requestorType, new PasswordAuthentication(
     59                store(requestorType, host, new PasswordAuthentication(
    6060                        response.getUsername(),
    6161                        response.getPassword()
  • trunk/src/org/openstreetmap/josm/io/auth/CredentialsAgent.java

    r4249 r4690  
    2929     * @throws CredentialsAgentException thrown if a problem occurs in a implementation of this interface
    3030     */
    31     PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsAgentException;
     31    PasswordAuthentication lookup(RequestorType requestorType, String host) throws CredentialsAgentException;
    3232
    3333    /**
     
    3939     * @throws CredentialsManagerException thrown if a problem occurs in a implementation of this interface
    4040     */
    41     void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsAgentException;
     41    void store(RequestorType requestorType, String host, PasswordAuthentication credentials) throws CredentialsAgentException;
    4242
    4343    /**
     
    5050
    5151     */
    52     CredentialsAgentResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsAgentException;
     52    CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse) throws CredentialsAgentException;
    5353
    5454    /**
  • trunk/src/org/openstreetmap/josm/io/auth/CredentialsManager.java

    r4264 r4690  
    88import org.openstreetmap.josm.data.oauth.OAuthToken;
    99import org.openstreetmap.josm.gui.JosmUserIdentityManager;
     10import org.openstreetmap.josm.io.OsmApi;
    1011import org.openstreetmap.josm.tools.Utils;
    1112
     
    6667
    6768    public String getUsername() {
     69        return getUsername(OsmApi.getOsmApi().getHost());
     70    }
     71
     72    public String getUsername(String host) {
    6873        String username = null;
    6974        try {
    70             PasswordAuthentication auth = lookup(RequestorType.SERVER);
     75            PasswordAuthentication auth = lookup(RequestorType.SERVER, host);
    7176            if (auth != null) {
    7277                username = auth.getUserName();
     
    8186
    8287    @Override
    83     public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsAgentException {
    84         return delegate.lookup(requestorType);
     88    public PasswordAuthentication lookup(RequestorType requestorType, String host) throws CredentialsAgentException {
     89        return delegate.lookup(requestorType, host);
    8590    }
    8691
    8792    @Override
    88     public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsAgentException {
    89         if (requestorType == RequestorType.SERVER && credentials.getUserName() != null && !credentials.getUserName().trim().isEmpty()) {
    90             JosmUserIdentityManager.getInstance().setPartiallyIdentified(credentials.getUserName());
     93    public void store(RequestorType requestorType, String host, PasswordAuthentication credentials) throws CredentialsAgentException {
     94        if (requestorType == RequestorType.SERVER && OsmApi.getOsmApi().getHost().equals(host)) {
     95            String username = credentials.getUserName();
     96            if(username != null && !username.trim().isEmpty()) {
     97                JosmUserIdentityManager.getInstance().setPartiallyIdentified(username);
     98            }
    9199        }
    92         delegate.store(requestorType, credentials);
     100        delegate.store(requestorType, host, credentials);
    93101    }
    94102
    95103    @Override
    96     public CredentialsAgentResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsAgentException {
    97         return delegate.getCredentials(requestorType, noSuccessWithLastResponse);
     104    public CredentialsAgentResponse getCredentials(RequestorType requestorType, String host, boolean noSuccessWithLastResponse) throws CredentialsAgentException {
     105        return delegate.getCredentials(requestorType, host, noSuccessWithLastResponse);
    98106    }
    99107
  • trunk/src/org/openstreetmap/josm/io/auth/DefaultAuthenticator.java

    r4249 r4690  
    4949            }
    5050            boolean tried = credentialsTried.get(getRequestorType()) != null;
    51             CredentialsAgentResponse response = CredentialsManager.getInstance().getCredentials(getRequestorType(), tried);
     51            CredentialsAgentResponse response = CredentialsManager.getInstance().getCredentials(getRequestorType(), getRequestingHost(), tried);
    5252            if (response == null || response.isCanceled())
    5353                return null;
  • trunk/src/org/openstreetmap/josm/io/auth/JosmPreferencesCredentialAgent.java

    r4249 r4690  
    1414import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
    1515import org.openstreetmap.josm.gui.widgets.HtmlPanel;
     16import org.openstreetmap.josm.io.OsmApi;
    1617
    1718/**
     
    2627     */
    2728    @Override
    28     public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsAgentException{
     29    public PasswordAuthentication lookup(RequestorType requestorType, String host) throws CredentialsAgentException{
    2930        if (requestorType == null)
    3031            return null;
     
    3334        switch(requestorType) {
    3435        case SERVER:
    35             user = Main.pref.get("osm-server.username", null);
    36             password = Main.pref.get("osm-server.password", null);
     36            if(OsmApi.getOsmApi().getHost().equals(host)) {
     37                user = Main.pref.get("osm-server.username", null);
     38                password = Main.pref.get("osm-server.password", null);
     39            } else {
     40                user = null;
     41                password = null;
     42            }
    3743            if (user == null)
    3844                return null;
     
    5258     */
    5359    @Override
    54     public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsAgentException {
     60    public void store(RequestorType requestorType, String host, PasswordAuthentication credentials) throws CredentialsAgentException {
    5561        if (requestorType == null)
    5662            return;
    5763        switch(requestorType) {
    5864        case SERVER:
    59             Main.pref.put("osm-server.username", credentials.getUserName());
    60             if (credentials.getPassword() == null) {
    61                 Main.pref.put("osm-server.password", null);
    62             } else {
    63                 Main.pref.put("osm-server.password", String.valueOf(credentials.getPassword()));
     65            if(OsmApi.getOsmApi().getHost().equals(host)) {
     66                Main.pref.put("osm-server.username", credentials.getUserName());
     67                if (credentials.getPassword() == null) {
     68                    Main.pref.put("osm-server.password", null);
     69                } else {
     70                    Main.pref.put("osm-server.password", String.valueOf(credentials.getPassword()));
     71                }
    6472            }
    6573            break;
Note: See TracChangeset for help on using the changeset viewer.