Changeset 4246 in josm


Ignore:
Timestamp:
2011-07-14T21:38:33+02:00 (13 years ago)
Author:
bastiK
Message:

plugin hook for credentials handling

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
6 edited

Legend:

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

    r3719 r4246  
    5353 * So for getValue(), setDefaultButton(int) and setCancelButton(int) the
    5454 * first button has index 1.
     55 *
     56 * Simple example:
     57 * <code>
     58 *  ExtendedDialog ed = new ExtendedDialog(
     59 *          Main.parent, tr("Dialog Title"),
     60 *          new String[] {tr("Ok"), tr("Cancel")});
     61 *  ed.setButtonIcons(new String[] {"ok", "cancel"});   // optional
     62 *  ed.setIcon(JOptionPane.WARNING_MESSAGE);            // optional
     63 *  ed.setContent(tr("Really proceed? Interesting things may happen..."));
     64 *  ed.showDialog();
     65 *  if (ed.getValue() == 1) { // user clicked first button "Ok"
     66 *      // proceed...
     67 *  }
     68 * </code>
    5569 */
    5670public class ExtendedDialog extends JDialog {
     
    282296        JPanel buttonsPanel = new JPanel(new GridBagLayout());
    283297
    284         for(int i=0; i < bTexts.length; i++) {
     298        for (int i=0; i < bTexts.length; i++) {
    285299            final int final_i = i;
    286300            Action action = new AbstractAction(bTexts[i]) {
    287                 public void actionPerformed(ActionEvent evt) {
     301                @Override public void actionPerformed(ActionEvent evt) {
    288302                    buttonAction(final_i, evt);
    289303                }
     
    415429    private void setupEscListener() {
    416430        Action actionListener = new AbstractAction() {
    417             public void actionPerformed(ActionEvent actionEvent) {
     431            @Override public void actionPerformed(ActionEvent actionEvent) {
    418432                // 0 means that the dialog has been closed otherwise.
    419433                // We need to set it to zero again, in case the dialog has been re-used
     
    535549        if (toggleable && defaultButton != null) {
    536550            SwingUtilities.invokeLater(new Runnable() {
    537                 public void run() {
     551                @Override public void run() {
    538552                    defaultButton.requestFocusInWindow();
    539553                }
     
    611625        }
    612626
    613         public void actionPerformed(ActionEvent e) {
     627        @Override public void actionPerformed(ActionEvent e) {
    614628            HelpBrowser.setUrlForHelpTopic(helpTopic);
    615629        }
  • trunk/src/org/openstreetmap/josm/gui/JosmUserIdentityManager.java

    r3083 r4246  
    2525 *
    2626 * The current user is fully identified if JOSM knows both the user name and the unique
    27  * id of the users OSM account. The later is retrieved from the OSM server with a
     27 * id of the users OSM account. The latter is retrieved from the OSM server with a
    2828 * <tt>GET /api/0.6/user/details</tt> request, submitted with the user name and password
    2929 * of the current user.
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/OsmApiUrlInputPanel.java

    r3934 r4246  
    3838    static public final String API_URL_PROP = OsmApiUrlInputPanel.class.getName() + ".apiUrl";
    3939
    40     static private final String defaulturl = "http://api.openstreetmap.org/api";
     40    static public final String defaulturl = "http://api.openstreetmap.org/api";
    4141    private JLabel lblValid;
    4242    private JLabel lblApiUrl;
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java

    r4245 r4246  
    292292        String policy = Main.pref.get(PROXY_POLICY, null);
    293293        ProxyPolicy pp = ProxyPolicy.fromName(policy);
    294         pp = pp == null? ProxyPolicy.NO_PROXY: pp;
     294        if (pp == null) {
     295            pp = ProxyPolicy.NO_PROXY;
     296        }
    295297        rbProxyPolicy.get(pp).setSelected(true);
    296298        String value = Main.pref.get("proxy.host", null);
     
    353355
    354356    class ProxyPolicyChangeListener implements ItemListener {
     357        @Override
    355358        public void itemStateChanged(ItemEvent arg0) {
    356359            updateEnabledState();
  • trunk/src/org/openstreetmap/josm/io/auth/CredentialsManager.java

    r4245 r4246  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.io.auth;
     3
     4import java.net.Authenticator.RequestorType;
     5import java.net.PasswordAuthentication;
     6
     7import org.openstreetmap.josm.data.oauth.OAuthToken;
    38
    49/**
     
    813 *
    914 */
    10 public class CredentialsManager {
    11     private static CredentialsAgent instance;
     15public class CredentialsManager implements CredentialsAgent {
     16   
     17    private static CredentialsManager instance;
    1218
    1319    /**
     
    1622     * @return the single credential agent used in JOSM
    1723     */
    18     static public CredentialsAgent getInstance() {
     24    static public CredentialsManager getInstance() {
    1925        if (instance == null) {
    20             instance =  new JosmPreferencesCredentialAgent();
     26            CredentialsAgent delegate;
     27            if (agentFactory == null) {
     28                delegate = new JosmPreferencesCredentialAgent();
     29            } else {
     30                delegate = agentFactory.getCredentialsAgent();
     31            }
     32            instance = new CredentialsManager(delegate);
    2133        }
    2234        return instance;
    2335    }
     36   
     37    private static CredentialsAgentFactory agentFactory;
     38
     39    public interface CredentialsAgentFactory {
     40        CredentialsAgent getCredentialsAgent();
     41    }
     42   
     43    /**
     44     * Plugins can register a CredentialsAgentFactory, thereby overriding
     45     * JOSM's default credentials agent.
     46     */
     47    public static void registerCredentialsAgentFactory(CredentialsAgentFactory agentFactory) {
     48        CredentialsManager.agentFactory = agentFactory;
     49        CredentialsManager.instance = null;
     50    }
     51
     52    /*****
     53     * non-static fields and methods
     54     */
     55   
     56    private CredentialsAgent delegate;
     57
     58    public CredentialsManager(CredentialsAgent delegate) {
     59        this.delegate = delegate;
     60    }
     61
     62    @Override
     63    public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsAgentException {
     64        return delegate.lookup(requestorType);
     65    }
     66
     67    @Override
     68    public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsAgentException {
     69        delegate.store(requestorType, credentials);
     70    }
     71
     72    @Override
     73    public CredentialsAgentResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsAgentException {
     74        return delegate.getCredentials(requestorType, noSuccessWithLastResponse);
     75    }
     76
     77    @Override
     78    public OAuthToken lookupOAuthAccessToken() throws CredentialsAgentException {
     79        return delegate.lookupOAuthAccessToken();
     80    }
     81
     82    @Override
     83    public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsAgentException {
     84        delegate.storeOAuthAccessToken(accessToken);
     85    }
    2486}
  • trunk/src/org/openstreetmap/josm/io/auth/JosmPreferencesCredentialAgent.java

    r4245 r4246  
    44import java.net.PasswordAuthentication;
    55import java.net.Authenticator.RequestorType;
    6 import java.util.HashMap;
    7 import java.util.Map;
    86
    97import org.openstreetmap.josm.Main;
    108import org.openstreetmap.josm.data.oauth.OAuthToken;
    11 import org.openstreetmap.josm.gui.io.CredentialDialog;
    129import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
    1310
     
    1714 *
    1815 */
    19 public class JosmPreferencesCredentialAgent implements CredentialsAgent {
     16public class JosmPreferencesCredentialAgent extends AbstractCredentialsAgent {
    2017
    21     Map<RequestorType, PasswordAuthentication> memoryCredentialsCache = new HashMap<RequestorType, PasswordAuthentication>();
    2218    /**
    2319     * @see CredentialsAgent#lookup(RequestorType)
     
    7470
    7571    /**
    76      * @see CredentialsAgent#getCredentials(RequestorType, boolean)
    77      */
    78     @Override
    79     public CredentialsAgentResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsAgentException{
    80         if (requestorType == null)
    81             return null;
    82         PasswordAuthentication credentials =  lookup(requestorType);
    83         String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
    84         String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
    85 
    86         CredentialsAgentResponse response = new CredentialsAgentResponse();
    87 
    88         /*
    89          * Last request was successful and there was no credentials stored
    90          * in file (or only the username is stored).
    91          * -> Try to recall credentials that have been entered
    92          * manually in this session.
    93          */
    94         if (!noSuccessWithLastResponse && memoryCredentialsCache.containsKey(requestorType) &&
    95                 (credentials == null || credentials.getPassword() == null || credentials.getPassword().length == 0)) {
    96             PasswordAuthentication pa = memoryCredentialsCache.get(requestorType);
    97             response.setUsername(pa.getUserName());
    98             response.setPassword(pa.getPassword());
    99             response.setCanceled(false);
    100         /*
    101          * Prompt the user for credentials. This happens the first time each
    102          * josm start if the user does not save the credentials to preference
    103          * file (username=="") and each time after authentication failed
    104          * (noSuccessWithLastResponse == true).
    105          */
    106         } else if (noSuccessWithLastResponse || username.equals("") || password.equals("")) {
    107             CredentialDialog dialog = null;
    108             switch(requestorType) {
    109             case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password); break;
    110             case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password); break;
    111             }
    112             dialog.setVisible(true);
    113             response.setCanceled(dialog.isCanceled());
    114             if (dialog.isCanceled())
    115                 return response;
    116             response.setUsername(dialog.getUsername());
    117             response.setPassword(dialog.getPassword());
    118             if (dialog.isSaveCredentials()) {
    119                 store(requestorType, new PasswordAuthentication(
    120                         response.getUsername(),
    121                         response.getPassword()
    122                 ));
    123             /*
    124              * User decides not to save credentials to file. Keep it
    125              * in memory so we don't have to ask over and over again.
    126              */
    127             } else {
    128                 PasswordAuthentication pa = new PasswordAuthentication(dialog.getUsername(), dialog.getPassword());
    129                 memoryCredentialsCache.put(requestorType, pa);
    130             }
    131         /*
    132          * We got it from file.
    133          */
    134         } else {
    135             response.setUsername(username);
    136             response.setPassword(password.toCharArray());
    137             response.setCanceled(false);
    138         }
    139         return response;
    140     }
    141 
    142     /**
    14372     * Lookup the current OAuth Access Token to access the OSM server. Replies null, if no
    14473     * Access Token is currently managed by this CredentialManager.
Note: See TracChangeset for help on using the changeset viewer.