Changeset 1955 in josm for trunk


Ignore:
Timestamp:
2009-08-11T13:37:25+02:00 (15 years ago)
Author:
stoecker
Message:

close #3112 - patch by bastiK - rework username/password handling to allow password hooks

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceDialog.java

    r1865 r1955  
    4141    // some common tabs
    4242    public final JPanel display = createPreferenceTab("display", tr("Display Settings"), tr("Various settings that influence the visual representation of the whole program."));
    43     public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server."));
     43    public final JPanel connection = createPreferenceTab("connection", I18n.tr("Connection Settings"), I18n.tr("Connection Settings for the OSM server."),true);
    4444    public final JPanel map = createPreferenceTab("map", I18n.tr("Map Settings"), I18n.tr("Settings for the map projection and data interpretation."));
    4545    public final JPanel audio = createPreferenceTab("audio", I18n.tr("Audio Settings"), I18n.tr("Settings for the audio player and audio markers."));
  • trunk/src/org/openstreetmap/josm/gui/preferences/ServerAccessPreference.java

    r1742 r1955  
    1212import org.openstreetmap.josm.Main;
    1313import org.openstreetmap.josm.tools.GBC;
     14import org.openstreetmap.josm.io.OsmConnection;
     15import org.openstreetmap.josm.io.CredentialsManager.PreferenceAdditions;
    1416
    1517public class ServerAccessPreference implements PreferenceSetting {
     
    2628    private JTextField osmDataServer = new JTextField(20);
    2729    /**
    28      * Editfield for the username to the OSM account.
     30     * Provide username and password input editfields.
     31     * Store the values if user hits OK.
    2932     */
    30     private JTextField osmDataUsername = new JTextField(20);
    31     /**
    32      * Passwordfield for the userpassword of the REST API.
    33      */
    34     private JPasswordField osmDataPassword = new JPasswordField(20);
     33    private PreferenceAdditions credentialsPA = OsmConnection.credentialsManager.newPreferenceAdditions();
    3534
    3635    public void addGui(PreferenceDialog gui) {
    3736        osmDataServer.setText(Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api"));
    38         osmDataUsername.setText(Main.pref.get("osm-server.username"));
    39         osmDataPassword.setText(Main.pref.get("osm-server.password"));
    40 
    4137        osmDataServer.setToolTipText(tr("The base URL for the OSM server (REST API)"));
    42         osmDataUsername.setToolTipText(tr("Login name (e-mail) to the OSM account."));
    43         osmDataPassword.setToolTipText(tr("Login password to the OSM account. Leave blank to not store any password."));
    44 
    4538        gui.connection.add(new JLabel(tr("Base Server URL")), GBC.std());
    4639        gui.connection.add(osmDataServer, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
    47         gui.connection.add(new JLabel(tr("OSM username (e-mail)")), GBC.std());
    48         gui.connection.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
    49         gui.connection.add(new JLabel(tr("OSM password")), GBC.std());
    50         gui.connection.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0));
    51         JLabel warning = new JLabel(tr("<html>" +
    52                 "WARNING: The password is stored in plain text in the preferences file.<br>" +
    53                 "The password is transferred in plain text to the server, encoded in the URL.<br>" +
    54         "<b>Do not use a valuable Password.</b></html>"));
    55         warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
    56         gui.connection.add(warning, GBC.eop().fill(GBC.HORIZONTAL));
     40        credentialsPA.addPreferenceOptions(gui.connection);
    5741    }
    5842
    5943    public boolean ok() {
    6044        Main.pref.put("osm-server.url", osmDataServer.getText());
    61         Main.pref.put("osm-server.username", osmDataUsername.getText());
    62         Main.pref.put("osm-server.password", String.valueOf(osmDataPassword.getPassword()));
     45        credentialsPA.preferencesChanged();
    6346        return false;
    6447    }
  • trunk/src/org/openstreetmap/josm/io/OsmConnection.java

    r1881 r1955  
    2525import org.openstreetmap.josm.tools.Base64;
    2626import org.openstreetmap.josm.tools.GBC;
     27import org.openstreetmap.josm.io.CredentialsManager.CMException;
    2728
    2829/**
     
    3637    protected boolean cancel = false;
    3738    protected HttpURLConnection activeConnection;
     39    /**
     40     * Handles password storage and some related gui-components.
     41     * It can be set by a plugin. This may happen at startup and
     42     * by changing the preferences.
     43     * Syncronize on this object to get or set a consistent
     44     * username/password pair.
     45     */
     46    public static CredentialsManager credentialsManager = new PlainCredentialsManager();
    3847
    3948    private static OsmAuth authentication = new OsmAuth();
     49
    4050    /**
    4151     * Initialize the http defaults and the authenticator.
     
    5565     * The authentication class handling the login requests.
    5666     */
    57     private static class OsmAuth extends Authenticator {
     67    public static class OsmAuth extends Authenticator {
    5868        /**
    5969         * Set to true, when the autenticator tried the password once.
    6070         */
    61         boolean passwordtried = false;
     71        public boolean passwordtried = false;
    6272        /**
    6373         * Whether the user cancelled the password dialog
    6474         */
    65         boolean authCancelled = false;
    66 
     75        public boolean authCancelled = false;
    6776        @Override protected PasswordAuthentication getPasswordAuthentication() {
    68             String username = Main.pref.get("osm-server.username");
    69             String password = Main.pref.get("osm-server.password");
    70             if (passwordtried || username.equals("") || password.equals("")) {
    71                 JPanel p = new JPanel(new GridBagLayout());
    72                 if (!username.equals("") && !password.equals("")) {
    73                     p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop());
    74                 }
    75                 p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0));
    76                 JTextField usernameField = new JTextField(username, 20);
    77                 p.add(usernameField, GBC.eol());
    78                 p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0));
    79                 JPasswordField passwordField = new JPasswordField(password, 20);
    80                 p.add(passwordField, GBC.eol());
    81                 JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted."));
    82                 warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
    83                 p.add(warning, GBC.eop());
    84 
    85                 JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals(""));
    86                 p.add(savePassword, GBC.eop());
    87 
    88                 int choice = new ExtendedDialog(Main.parent,
    89                         tr("Enter Password"),
    90                         p,
    91                         new String[] {tr("Login"), tr("Cancel")},
    92                         new String[] {"ok.png", "cancel.png"}).getValue();
    93 
    94                 if (choice != 1) {
    95                     authCancelled = true;
    96                     return null;
    97                 }
    98                 username = usernameField.getText();
    99                 password = String.valueOf(passwordField.getPassword());
    100                 if (savePassword.isSelected()) {
    101                     Main.pref.put("osm-server.username", username);
    102                     Main.pref.put("osm-server.password", password);
    103                 }
    104                 if (username.equals(""))
    105                     return null;
    106             }
    107             passwordtried = true;
    108             return new PasswordAuthentication(username, password.toCharArray());
     77            return credentialsManager.getPasswordAuthentication(this);
    10978        }
    11079    }
     
    141110    protected void addAuth(HttpURLConnection con) throws CharacterCodingException {
    142111        CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
    143         String auth = Main.pref.get("osm-server.username") + ":" + Main.pref.get("osm-server.password");
     112        String auth;
     113        try {
     114            synchronized (credentialsManager) {
     115                auth = credentialsManager.lookupUsername() + ":" + credentialsManager.lookupPassword();
     116            }
     117        } catch (CMException e) {
     118            auth = ":";
     119        }
    144120        ByteBuffer bytes = encoder.encode(CharBuffer.wrap(auth));
    145121        con.addRequestProperty("Authorization", "Basic "+Base64.encode(bytes));
     
    155131        return cancel;
    156132    }
     133
     134    public static class PlainCredentialsManager implements CredentialsManager {
     135        public String lookupUsername() throws CMException {
     136            String username = Main.pref.get("osm-server.username", null);
     137            if (username == null) throw new CredentialsManager.NoContentException();
     138            return username;
     139        }
     140        public String lookupPassword() throws CMException {
     141            String password = Main.pref.get("osm-server.password");
     142            if (password == null) throw new CredentialsManager.NoContentException();
     143            return password;
     144        }
     145        public void storeUsername(String username) {
     146            Main.pref.put("osm-server.username", username);
     147        }
     148        public void storePassword(String password) {
     149            Main.pref.put("osm-server.password", password);
     150        }
     151        public PasswordAuthentication getPasswordAuthentication(OsmAuth caller) {
     152            String username, password;
     153            try {
     154                username = lookupUsername();
     155            } catch (CMException e) {
     156                username = "";
     157            }
     158            try {
     159                password = lookupPassword();
     160            } catch (CMException e) {
     161                password = "";
     162            }
     163            if (caller.passwordtried || username.equals("") || password.equals("")) {
     164                JPanel p = new JPanel(new GridBagLayout());
     165                if (!username.equals("") && !password.equals("")) {
     166                    p.add(new JLabel(tr("Incorrect password or username.")), GBC.eop());
     167                }
     168                p.add(new JLabel(tr("Username")), GBC.std().insets(0,0,10,0));
     169                JTextField usernameField = new JTextField(username, 20);
     170                p.add(usernameField, GBC.eol());
     171                p.add(new JLabel(tr("Password")), GBC.std().insets(0,0,10,0));
     172                JPasswordField passwordField = new JPasswordField(password, 20);
     173                p.add(passwordField, GBC.eol());
     174                JLabel warning = new JLabel(tr("Warning: The password is transferred unencrypted."));
     175                warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
     176                p.add(warning, GBC.eop());
     177
     178                JCheckBox savePassword = new JCheckBox(tr("Save user and password (unencrypted)"), !username.equals("") && !password.equals(""));
     179                p.add(savePassword, GBC.eop());
     180
     181                int choice = new ExtendedDialog(Main.parent,
     182                        tr("Enter Password"),
     183                        p,
     184                        new String[] {tr("Login"), tr("Cancel")},
     185                        new String[] {"ok.png", "cancel.png"}).getValue();
     186
     187                if (choice != 1) {
     188                    caller.authCancelled = true;
     189                    return null;
     190                }
     191                username = usernameField.getText();
     192                password = String.valueOf(passwordField.getPassword());
     193                if (savePassword.isSelected()) {
     194                    storeUsername(username);
     195                    storePassword(password);
     196                }
     197                if (username.equals(""))
     198                    return null;
     199            }
     200            caller.passwordtried = true;
     201            return new PasswordAuthentication(username, password.toCharArray());
     202        }
     203        public PreferenceAdditions newPreferenceAdditions() {
     204            return new PreferenceAdditions() {
     205                /**
     206                 * Editfield for the username to the OSM account.
     207                 */
     208                private JTextField osmDataUsername = new JTextField(20);
     209                /**
     210                 * Passwordfield for the userpassword of the REST API.
     211                 */
     212                private JPasswordField osmDataPassword = new JPasswordField(20);
     213
     214                private String oldUsername = "";
     215                private String oldPassword = "";
     216
     217                public void addPreferenceOptions(JPanel panel) {
     218                    try {
     219                        oldUsername = lookupUsername();
     220                    } catch (CMException e) {
     221                        oldUsername = "";
     222                    }
     223                    try {
     224                        oldPassword = lookupPassword();
     225                    } catch (CMException e) {
     226                        oldPassword = "";
     227                    }
     228                    osmDataUsername.setText(oldUsername);
     229                    osmDataPassword.setText(oldPassword);
     230                    osmDataUsername.setToolTipText(tr("Login name (e-mail) to the OSM account."));
     231                    osmDataPassword.setToolTipText(tr("Login password to the OSM account. Leave blank to not store any password."));
     232                    panel.add(new JLabel(tr("OSM username (e-mail)")), GBC.std());
     233                    panel.add(osmDataUsername, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,5));
     234                    panel.add(new JLabel(tr("OSM password")), GBC.std());
     235                    panel.add(osmDataPassword, GBC.eol().fill(GBC.HORIZONTAL).insets(5,0,0,0));
     236                    JLabel warning = new JLabel(tr("<html>" +
     237                            "WARNING: The password is stored in plain text in the preferences file.<br>" +
     238                            "The password is transferred in plain text to the server, encoded in the URL.<br>" +
     239                            "<b>Do not use a valuable Password.</b></html>"));
     240                    warning.setFont(warning.getFont().deriveFont(Font.ITALIC));
     241                    panel.add(warning, GBC.eop().fill(GBC.HORIZONTAL));
     242                }
     243                public void preferencesChanged() {
     244                    String newUsername = osmDataUsername.getText();
     245                    String newPassword = String.valueOf(osmDataPassword.getPassword());
     246                    if (!oldUsername.equals(newUsername))
     247                        storeUsername(newUsername);
     248                    if (!oldPassword.equals(newPassword))
     249                        storePassword(newPassword);
     250                }
     251            };
     252        }
     253    }
    157254}
Note: See TracChangeset for help on using the changeset viewer.