Changeset 4249 in josm for trunk/src/org


Ignore:
Timestamp:
2011-07-15T20:00:28+02:00 (13 years ago)
Author:
bastiK
Message:

improve plugin hook for credentials handling

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

Legend:

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

    r4245 r4249  
    188188        Main.pref.updateSystemProperties();
    189189
    190         DefaultAuthenticator.createInstance(CredentialsManager.getInstance());
     190        DefaultAuthenticator.createInstance();
    191191        Authenticator.setDefault(DefaultAuthenticator.getInstance());
    192192        ProxySelector.setDefault(new DefaultProxySelector(ProxySelector.getDefault()));
  • trunk/src/org/openstreetmap/josm/gui/io/CredentialDialog.java

    r4110 r4249  
    4141public class CredentialDialog extends JDialog {
    4242
    43     static public CredentialDialog getOsmApiCredentialDialog(String username, String password) {
    44         CredentialDialog dialog = new CredentialDialog();
     43    static public CredentialDialog getOsmApiCredentialDialog(String username, String password, String saveUsernameAndPasswordCheckboxText) {
     44        CredentialDialog dialog = new CredentialDialog(saveUsernameAndPasswordCheckboxText);
    4545        dialog.prepareForOsmApiCredentials(username, password);
    4646        dialog.pack();
     
    4848    }
    4949
    50     static public CredentialDialog getHttpProxyCredentialDialog(String username, String password) {
    51         CredentialDialog dialog = new CredentialDialog();
     50    static public CredentialDialog getHttpProxyCredentialDialog(String username, String password, String saveUsernameAndPasswordCheckboxText) {
     51        CredentialDialog dialog = new CredentialDialog(saveUsernameAndPasswordCheckboxText);
    5252        dialog.prepareForProxyCredentials(username, password);
    5353        dialog.pack();
     
    5757    private boolean canceled;
    5858    private CredentialPanel pnlCredentials;
     59    String saveUsernameAndPasswordCheckboxText;
    5960
    6061    public boolean isCanceled() {
     
    9394    }
    9495
    95     public CredentialDialog() {
     96    public CredentialDialog(String saveUsernameAndPasswordCheckboxText) {
     97        this.saveUsernameAndPasswordCheckboxText = saveUsernameAndPasswordCheckboxText;
    9698        setModalityType(ModalityType.DOCUMENT_MODAL);
    9799        try {
     
    147149            tfUserName.addKeyListener(new TFKeyListener(owner, tfUserName, tfPassword));
    148150            tfPassword.addKeyListener(new TFKeyListener(owner, tfPassword, tfUserName));
    149             cbSaveCredentials =  new JCheckBox(tr("Save user and password (unencrypted)"));
     151            cbSaveCredentials =  new JCheckBox(owner.saveUsernameAndPasswordCheckboxText);
    150152
    151153            setLayout(new GridBagLayout());
     
    301303        }
    302304
     305        @Override
    303306        public void keyPressed(KeyEvent e) {
    304307            if(e.getKeyChar() == KeyEvent.VK_ENTER) {
     
    317320        }
    318321
     322        @Override
    319323        public void keyReleased ( KeyEvent e ){
    320324        }
    321325
     326        @Override
    322327        public void keyTyped ( KeyEvent e ){
    323328        }
     
    331336        }
    332337
     338        @Override
    333339        public void actionPerformed(ActionEvent arg0) {
    334340            setCanceled(false);
     
    349355        }
    350356
     357        @Override
    351358        public void actionPerformed(ActionEvent arg0) {
    352359            cancel();
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/BasicAuthenticationPreferencesPanel.java

    r4245 r4249  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.BorderLayout;
    67import java.awt.GridBagConstraints;
    78import java.awt.GridBagLayout;
     
    1516import javax.swing.JPasswordField;
    1617import javax.swing.JTextField;
    17 import javax.swing.text.html.HTMLEditorKit;
    1818
    1919import org.openstreetmap.josm.Main;
    20 import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    2120import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
    2221import org.openstreetmap.josm.io.auth.CredentialsAgent;
     
    3736    /** the OSM password */
    3837    private JPasswordField tfOsmPassword;
    39 
     38    /** a panel with further information, e.g. some warnings */
     39    private JPanel decorationPanel;
    4040
    4141    /**
     
    8181        gc.insets = new Insets(5,0,0,0);
    8282        gc.fill = GridBagConstraints.BOTH;
    83         HtmlPanel pnlMessage = new HtmlPanel();
    84         HTMLEditorKit kit = (HTMLEditorKit)pnlMessage.getEditorPane().getEditorKit();
    85         kit.getStyleSheet().addRule(".warning-body {background-color:rgb(253,255,221);padding: 10pt; border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
    86         pnlMessage.setText(
    87                 tr(
    88                         "<html><body>"
    89                         + "<p class=\"warning-body\">"
    90                         + "<strong>Warning:</strong> The password is stored in plain text in the JOSM preferences file. "
    91                         + "Furthermore, it is transferred <strong>unencrypted</strong> in every request sent to the OSM server. "
    92                         + "<strong>Do not use a valuable password.</strong>"
    93                         + "</p>"
    94                         + "</body></html>"
    95                 )
    96         );
    97         add(pnlMessage, gc);
     83        decorationPanel = new JPanel(new BorderLayout());
     84        add(decorationPanel, gc);
    9885    }
    9986
     
    10592        CredentialsAgent cm = CredentialsManager.getInstance();
    10693        try {
     94            decorationPanel.removeAll();
     95            decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER);
    10796            PasswordAuthentication pa = cm.lookup(RequestorType.SERVER);
    10897            if (pa == null) {
  • trunk/src/org/openstreetmap/josm/io/auth/AbstractCredentialsAgent.java

    r4246 r4249  
    4747            CredentialDialog dialog = null;
    4848            switch(requestorType) {
    49             case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password); break;
    50             case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password); break;
     49            case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password, getSaveUsernameAndPasswordCheckboxText()); break;
     50            case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password, getSaveUsernameAndPasswordCheckboxText()); break;
    5151            }
    5252            dialog.setVisible(true);
     
    8080    }
    8181
     82    /**
     83     * Provide the text for a checkbox that offers to save the
     84     * username and password that has been entered by the user.
     85     */
     86    public abstract String getSaveUsernameAndPasswordCheckboxText();
    8287}
  • trunk/src/org/openstreetmap/josm/io/auth/CredentialsAgent.java

    r4245 r4249  
    22package org.openstreetmap.josm.io.auth;
    33
     4import java.awt.Component;
    45import java.net.PasswordAuthentication;
    56import java.net.Authenticator.RequestorType;
     
    2829     * @throws CredentialsAgentException thrown if a problem occurs in a implementation of this interface
    2930     */
    30     public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsAgentException;
     31    PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsAgentException;
    3132
    3233    /**
     
    3839     * @throws CredentialsManagerException thrown if a problem occurs in a implementation of this interface
    3940     */
    40     public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsAgentException;
     41    void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsAgentException;
    4142
    4243    /**
     
    4950
    5051     */
    51     public CredentialsAgentResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsAgentException;
     52    CredentialsAgentResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsAgentException;
    5253
    5354    /**
     
    5859     * @throws CredentialsAgentException thrown if something goes wrong
    5960     */
    60     public OAuthToken lookupOAuthAccessToken() throws CredentialsAgentException;
     61    OAuthToken lookupOAuthAccessToken() throws CredentialsAgentException;
    6162
    6263    /**
     
    6667     * @throws CredentialsAgentException thrown if something goes wrong
    6768     */
    68     public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsAgentException;
     69    void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsAgentException;
     70
     71
     72    /**
     73     * Provide a Panel that is shown below the API password / username fields
     74     * in the JOSM Preferences. (E.g. a warning that password is saved unencrypted.)
     75     */
     76    Component getPreferencesDecorationPanel();
     77
    6978}
  • trunk/src/org/openstreetmap/josm/io/auth/CredentialsManager.java

    r4246 r4249  
    22package org.openstreetmap.josm.io.auth;
    33
     4import java.awt.Component;
    45import java.net.Authenticator.RequestorType;
    56import java.net.PasswordAuthentication;
     
    4445     * Plugins can register a CredentialsAgentFactory, thereby overriding
    4546     * JOSM's default credentials agent.
     47     * @param agentFactory The Factory that provides the custom CredentialsAgent.
     48     * Can be null to clear the factory and switch back to default behavior.
    4649     */
    4750    public static void registerCredentialsAgentFactory(CredentialsAgentFactory agentFactory) {
     
    8487        delegate.storeOAuthAccessToken(accessToken);
    8588    }
     89
     90    @Override
     91    public Component getPreferencesDecorationPanel() {
     92        return delegate.getPreferencesDecorationPanel();
     93    }
    8694}
  • trunk/src/org/openstreetmap/josm/io/auth/DefaultAuthenticator.java

    r4245 r4249  
    2222    }
    2323
    24     public static void createInstance(CredentialsAgent credentialManager) {
    25         instance = new DefaultAuthenticator(credentialManager);
     24    public static void createInstance() {
     25        instance = new DefaultAuthenticator();
    2626    }
    2727
    28     private CredentialsAgent credentialsAgent;
    2928    private final Map<RequestorType, Boolean> credentialsTried = new HashMap<RequestorType, Boolean>();
    3029    private boolean enabled = true;
    3130
    32     /**
    33      *
    34      * @param credentialsAgent the credential manager
    35      */
    36     private DefaultAuthenticator(CredentialsAgent credentialsAgent) {
    37         this.credentialsAgent = credentialsAgent;
     31    private DefaultAuthenticator() {
    3832    }
    3933
     
    5549            }
    5650            boolean tried = credentialsTried.get(getRequestorType()) != null;
    57             CredentialsAgentResponse response = credentialsAgent.getCredentials(getRequestorType(), tried);
     51            CredentialsAgentResponse response = CredentialsManager.getInstance().getCredentials(getRequestorType(), tried);
    5852            if (response == null || response.isCanceled())
    5953                return null;
  • trunk/src/org/openstreetmap/josm/io/auth/JosmPreferencesCredentialAgent.java

    r4246 r4249  
    22package org.openstreetmap.josm.io.auth;
    33
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.awt.Component;
    47import java.net.PasswordAuthentication;
    58import java.net.Authenticator.RequestorType;
     9
     10import javax.swing.text.html.HTMLEditorKit;
    611
    712import org.openstreetmap.josm.Main;
    813import org.openstreetmap.josm.data.oauth.OAuthToken;
    914import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
     15import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    1016
    1117/**
     
    101107        }
    102108    }
     109
     110    @Override
     111    public Component getPreferencesDecorationPanel() {
     112        HtmlPanel pnlMessage = new HtmlPanel();
     113        HTMLEditorKit kit = (HTMLEditorKit)pnlMessage.getEditorPane().getEditorKit();
     114        kit.getStyleSheet().addRule(".warning-body {background-color:rgb(253,255,221);padding: 10pt; border-color:rgb(128,128,128);border-style: solid;border-width: 1px;}");
     115        pnlMessage.setText(
     116                tr(
     117                        "<html><body>"
     118                        + "<p class=\"warning-body\">"
     119                        + "<strong>Warning:</strong> The password is stored in plain text in the JOSM preferences file. "
     120                        + "Furthermore, it is transferred <strong>unencrypted</strong> in every request sent to the OSM server. "
     121                        + "<strong>Do not use a valuable password.</strong>"
     122                        + "</p>"
     123                        + "</body></html>"
     124                )
     125        );
     126        return pnlMessage;
     127    }
     128   
     129    @Override
     130    public String getSaveUsernameAndPasswordCheckboxText() {
     131        return tr("Save user and password (unencrypted)");
     132    }
     133   
    103134}
Note: See TracChangeset for help on using the changeset viewer.