Changeset 9355 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2016-01-09T18:07:26+01:00 (8 years ago)
Author:
simon04
Message:

fix #7943 - Make OAuth work for non-standard OSM APIs

You will need to obtain an application via
http://<server>/user/<user>/oauth_clients/new, and set the OAuth
consumer key+secret in the advanced OAuth parameters.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java

    r9220 r9355  
    44import java.net.MalformedURLException;
    55import java.net.URL;
     6import java.util.Objects;
    67
    78import oauth.signpost.OAuthConsumer;
    89import oauth.signpost.OAuthProvider;
    9 
    1010import org.openstreetmap.josm.Main;
    1111import org.openstreetmap.josm.data.Preferences;
     
    2727     */
    2828    public static final String DEFAULT_JOSM_CONSUMER_SECRET = "rIkjpPcBNkMQxrqzcOvOC4RRuYupYr7k8mfP13H5";
    29     /**
    30      * The default OSM OAuth request token URL.
    31      */
    32     public static final String DEFAULT_REQUEST_TOKEN_URL = Main.getOSMWebsite() + "/oauth/request_token";
    33     /**
    34      * The default OSM OAuth access token URL.
    35      */
    36     public static final String DEFAULT_ACCESS_TOKEN_URL = Main.getOSMWebsite() + "/oauth/access_token";
    37     /**
    38      * The default OSM OAuth authorize URL.
    39      */
    40     public static final String DEFAULT_AUTHORISE_URL = Main.getOSMWebsite() + "/oauth/authorize";
    4129
    4230    /**
     
    6048     */
    6149    public static OAuthParameters createDefault(String apiUrl) {
    62         String host = "";
    63         if (!OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
     50        final String consumerKey;
     51        final String consumerSecret;
     52        final String serverUrl;
     53
     54        if (apiUrl != null) {
     55            // validate URL syntax
    6456            try {
    65                 host = new URL(apiUrl).getHost();
     57                new URL(apiUrl);
    6658            } catch (MalformedURLException e) {
    67                 // Ignored
    68                 if (Main.isTraceEnabled()) {
    69                     Main.trace(e.getMessage());
    70                 }
     59                apiUrl = null;
    7160            }
    7261        }
    73         boolean osmDevServer = host.endsWith("dev.openstreetmap.org");
     62
     63        if (apiUrl != null && !OsmApi.DEFAULT_API_URL.equals(apiUrl)) {
     64            consumerKey = ""; // a custom consumer key is required
     65            consumerSecret = ""; // a custom consumer secret is requireds
     66            serverUrl = apiUrl.replaceAll("/api$", "");
     67        } else {
     68            consumerKey = DEFAULT_JOSM_CONSUMER_KEY;
     69            consumerSecret = DEFAULT_JOSM_CONSUMER_SECRET;
     70            serverUrl = Main.getOSMWebsite();
     71        }
     72
    7473        return new OAuthParameters(
    75             DEFAULT_JOSM_CONSUMER_KEY,
    76             DEFAULT_JOSM_CONSUMER_SECRET,
    77             osmDevServer ? DEFAULT_REQUEST_TOKEN_URL.replace("www.openstreetmap.org", host) : DEFAULT_REQUEST_TOKEN_URL,
    78             osmDevServer ? DEFAULT_ACCESS_TOKEN_URL.replace("www.openstreetmap.org", host) : DEFAULT_ACCESS_TOKEN_URL,
    79             osmDevServer ? DEFAULT_AUTHORISE_URL.replace("www.openstreetmap.org", host) : DEFAULT_AUTHORISE_URL);
     74                consumerKey,
     75                consumerSecret,
     76                serverUrl + "/oauth/request_token",
     77                serverUrl + "/oauth/access_token",
     78                serverUrl + "/oauth/authorize",
     79                serverUrl + "/login",
     80                serverUrl + "/logout");
    8081    }
    8182
     
    9394                pref.get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()),
    9495                pref.get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()),
    95                 pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl())
    96                 );
     96                pref.get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()),
     97                pref.get("oauth.settings.osm-login-url", parameters.getOsmLoginUrl()),
     98                pref.get("oauth.settings.osm-logout-url", parameters.getOsmLogoutUrl()));
     99    }
     100
     101    /**
     102     * Remembers the current values in the preferences <code>pref</code>.
     103     *
     104     * @param pref the preferences. Must not be null.
     105     * @throws IllegalArgumentException if pref is null.
     106     */
     107    public void rememberPreferences(Preferences pref) {
     108        CheckParameterUtil.ensureParameterNotNull(pref, "pref");
     109        pref.put("oauth.settings.consumer-key", getConsumerKey());
     110        pref.put("oauth.settings.consumer-secret", getConsumerSecret());
     111        pref.put("oauth.settings.request-token-url", getRequestTokenUrl());
     112        pref.put("oauth.settings.access-token-url", getAccessTokenUrl());
     113        pref.put("oauth.settings.authorise-url", getAuthoriseUrl());
     114        pref.put("oauth.settings.osm-login-url", getOsmLoginUrl());
     115        pref.put("oauth.settings.osm-logout-url", getOsmLogoutUrl());
    97116    }
    98117
     
    102121    private final String accessTokenUrl;
    103122    private final String authoriseUrl;
     123    private final String osmLoginUrl;
     124    private final String osmLogoutUrl;
    104125
    105126    /**
     
    110131     * @param accessTokenUrl access token URL
    111132     * @param authoriseUrl authorise URL
    112      *
     133     * @param osmLoginUrl the OSM login URL (for automatic mode)
     134     * @param osmLogoutUrl the OSM logout URL (for automatic mode)
    113135     * @see #createDefault
    114136     * @see #createFromPreferences
     
    116138     */
    117139    public OAuthParameters(String consumerKey, String consumerSecret,
    118             String requestTokenUrl, String accessTokenUrl, String authoriseUrl) {
     140                           String requestTokenUrl, String accessTokenUrl, String authoriseUrl, String osmLoginUrl, String osmLogoutUrl) {
    119141        this.consumerKey = consumerKey;
    120142        this.consumerSecret = consumerSecret;
     
    122144        this.accessTokenUrl = accessTokenUrl;
    123145        this.authoriseUrl = authoriseUrl;
     146        this.osmLoginUrl = osmLoginUrl;
     147        this.osmLogoutUrl = osmLogoutUrl;
    124148    }
    125149
     
    137161        this.requestTokenUrl = other.requestTokenUrl;
    138162        this.authoriseUrl = other.authoriseUrl;
     163        this.osmLoginUrl = other.osmLoginUrl;
     164        this.osmLogoutUrl = other.osmLogoutUrl;
    139165    }
    140166
     
    177203    public String getAuthoriseUrl() {
    178204        return authoriseUrl;
     205    }
     206
     207    /**
     208     * Gets the URL used to login users on the website (for automatic mode).
     209     * @return The URL used to login users
     210     */
     211    public String getOsmLoginUrl() {
     212        return osmLoginUrl;
     213    }
     214
     215    /**
     216     * Gets the URL used to logout users on the website (for automatic mode).
     217     * @return The URL used to logout users
     218     */
     219    public String getOsmLogoutUrl() {
     220        return osmLogoutUrl;
    179221    }
    180222
     
    205247
    206248    @Override
     249    public boolean equals(Object o) {
     250        if (this == o) return true;
     251        if (o == null || getClass() != o.getClass()) return false;
     252        OAuthParameters that = (OAuthParameters) o;
     253        return Objects.equals(consumerKey, that.consumerKey) &&
     254                Objects.equals(consumerSecret, that.consumerSecret) &&
     255                Objects.equals(requestTokenUrl, that.requestTokenUrl) &&
     256                Objects.equals(accessTokenUrl, that.accessTokenUrl) &&
     257                Objects.equals(authoriseUrl, that.authoriseUrl) &&
     258                Objects.equals(osmLoginUrl, that.osmLoginUrl) &&
     259                Objects.equals(osmLogoutUrl, that.osmLogoutUrl);
     260    }
     261
     262    @Override
    207263    public int hashCode() {
    208         final int prime = 31;
    209         int result = 1;
    210         result = prime * result + ((accessTokenUrl == null) ? 0 : accessTokenUrl.hashCode());
    211         result = prime * result + ((authoriseUrl == null) ? 0 : authoriseUrl.hashCode());
    212         result = prime * result + ((consumerKey == null) ? 0 : consumerKey.hashCode());
    213         result = prime * result + ((consumerSecret == null) ? 0 : consumerSecret.hashCode());
    214         result = prime * result + ((requestTokenUrl == null) ? 0 : requestTokenUrl.hashCode());
    215         return result;
    216     }
    217 
    218     @Override
    219     public boolean equals(Object obj) {
    220         if (this == obj)
    221             return true;
    222         if (obj == null)
    223             return false;
    224         if (getClass() != obj.getClass())
    225             return false;
    226         OAuthParameters other = (OAuthParameters) obj;
    227         if (accessTokenUrl == null) {
    228             if (other.accessTokenUrl != null)
    229                 return false;
    230         } else if (!accessTokenUrl.equals(other.accessTokenUrl))
    231             return false;
    232         if (authoriseUrl == null) {
    233             if (other.authoriseUrl != null)
    234                 return false;
    235         } else if (!authoriseUrl.equals(other.authoriseUrl))
    236             return false;
    237         if (consumerKey == null) {
    238             if (other.consumerKey != null)
    239                 return false;
    240         } else if (!consumerKey.equals(other.consumerKey))
    241             return false;
    242         if (consumerSecret == null) {
    243             if (other.consumerSecret != null)
    244                 return false;
    245         } else if (!consumerSecret.equals(other.consumerSecret))
    246             return false;
    247         if (requestTokenUrl == null) {
    248             if (other.requestTokenUrl != null)
    249                 return false;
    250         } else if (!requestTokenUrl.equals(other.requestTokenUrl))
    251             return false;
    252         return true;
     264        return Objects.hash(consumerKey, consumerSecret, requestTokenUrl, accessTokenUrl, authoriseUrl, osmLoginUrl, osmLogoutUrl);
    253265    }
    254266}
  • trunk/src/org/openstreetmap/josm/gui/oauth/AdvancedOAuthPropertiesPanel.java

    r9220 r9355  
    3535 * <li>Access token URL</li>
    3636 * <li>Authorize URL</li>
     37 * <li>OSM login URL</li>
     38 * <li>OSM logout URL</li>
    3739 * </ul>
    3840 *
     
    4850    private JosmTextField tfAccessTokenURL;
    4951    private JosmTextField tfAuthoriseURL;
     52    private JosmTextField tfOsmLoginURL;
     53    private JosmTextField tfOsmLogoutURL;
    5054    private transient UseDefaultItemListener ilUseDefault;
    5155    private String apiUrl;
     
    120124        SelectAllOnFocusGainedDecorator.decorate(tfAuthoriseURL);
    121125
     126
     127        // -- OSM login URL
     128        gc.gridy = 6;
     129        gc.gridx = 0;
     130        gc.weightx = 0.0;
     131        add(new JLabel(tr("OSM login URL:")), gc);
     132
     133        gc.gridx = 1;
     134        gc.weightx = 1.0;
     135        add(tfOsmLoginURL = new JosmTextField(), gc);
     136        SelectAllOnFocusGainedDecorator.decorate(tfOsmLoginURL);
     137
     138
     139        // -- OSM logout URL
     140        gc.gridy = 7;
     141        gc.gridx = 0;
     142        gc.weightx = 0.0;
     143        add(new JLabel(tr("OSM logout URL:")), gc);
     144
     145        gc.gridx = 1;
     146        gc.weightx = 1.0;
     147        add(tfOsmLogoutURL = new JosmTextField(), gc);
     148        SelectAllOnFocusGainedDecorator.decorate(tfOsmLogoutURL);
     149
    122150        cbUseDefaults.addItemListener(ilUseDefault = new UseDefaultItemListener());
    123151    }
     
    125153    protected boolean hasCustomSettings() {
    126154        OAuthParameters params = OAuthParameters.createDefault(apiUrl);
    127         return
    128            !tfConsumerKey.getText().equals(params.getConsumerKey())
    129         || !tfConsumerSecret.getText().equals(params.getConsumerSecret())
    130         || !tfRequestTokenURL.getText().equals(params.getRequestTokenUrl())
    131         || !tfAccessTokenURL.getText().equals(params.getAccessTokenUrl())
    132         || !tfAuthoriseURL.getText().equals(params.getAuthoriseUrl());
     155        return !params.equals(getAdvancedParameters());
    133156    }
    134157
     
    173196        tfAccessTokenURL.setText(params.getAccessTokenUrl());
    174197        tfAuthoriseURL.setText(params.getAuthoriseUrl());
     198        tfOsmLoginURL.setText(params.getOsmLoginUrl());
     199        tfOsmLogoutURL.setText(params.getOsmLogoutUrl());
    175200
    176201        setChildComponentsEnabled(false);
     
    198223            tfRequestTokenURL.getText(),
    199224            tfAccessTokenURL.getText(),
    200             tfAuthoriseURL.getText());
     225            tfAuthoriseURL.getText(),
     226            tfOsmLoginURL.getText(),
     227            tfOsmLogoutURL.getText());
    201228    }
    202229
     
    220247            tfAccessTokenURL.setText(parameters.getAccessTokenUrl() == null ? "" : parameters.getAccessTokenUrl());
    221248            tfAuthoriseURL.setText(parameters.getAuthoriseUrl() == null ? "" : parameters.getAuthoriseUrl());
     249            tfOsmLoginURL.setText(parameters.getOsmLoginUrl() == null ? "" : parameters.getOsmLoginUrl());
     250            tfOsmLogoutURL.setText(parameters.getOsmLogoutUrl() == null ? "" : parameters.getOsmLogoutUrl());
    222251        }
    223252    }
     
    244273            resetToDefaultSettings();
    245274        } else {
    246             cbUseDefaults.setSelected(false);
    247             tfConsumerKey.setText(pref.get("oauth.settings.consumer-key", OAuthParameters.DEFAULT_JOSM_CONSUMER_KEY));
    248             tfConsumerSecret.setText(pref.get("oauth.settings.consumer-secret", OAuthParameters.DEFAULT_JOSM_CONSUMER_SECRET));
    249             tfRequestTokenURL.setText(pref.get("oauth.settings.request-token-url", OAuthParameters.DEFAULT_REQUEST_TOKEN_URL));
    250             tfAccessTokenURL.setText(pref.get("oauth.settings.access-token-url", OAuthParameters.DEFAULT_ACCESS_TOKEN_URL));
    251             tfAuthoriseURL.setText(pref.get("oauth.settings.authorise-url", OAuthParameters.DEFAULT_AUTHORISE_URL));
    252             setChildComponentsEnabled(true);
     275            setAdvancedParameters(OAuthParameters.createFromPreferences(pref));
    253276        }
    254277        ilUseDefault.setEnabled(true);
     
    265288        pref.put("oauth.settings.use-default", cbUseDefaults.isSelected());
    266289        if (cbUseDefaults.isSelected()) {
    267             pref.put("oauth.settings.consumer-key", null);
    268             pref.put("oauth.settings.consumer-secret", null);
    269             pref.put("oauth.settings.request-token-url", null);
    270             pref.put("oauth.settings.access-token-url", null);
    271             pref.put("oauth.settings.authorise-url", null);
     290            new OAuthParameters(null, null, null, null, null, null, null).rememberPreferences(pref);
    272291        } else {
    273             pref.put("oauth.settings.consumer-key", tfConsumerKey.getText().trim());
    274             pref.put("oauth.settings.consumer-secret", tfConsumerSecret.getText().trim());
    275             pref.put("oauth.settings.request-token-url", tfRequestTokenURL.getText().trim());
    276             pref.put("oauth.settings.access-token-url", tfAccessTokenURL.getText().trim());
    277             pref.put("oauth.settings.authorise-url", tfAuthoriseURL.getText().trim());
     292            getAdvancedParameters().rememberPreferences(pref);
    278293        }
    279294    }
  • trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java

    r9352 r9355  
    479479
    480480        protected void alertLoginFailed(OsmLoginFailedException e) {
    481             String loginUrl = null;
    482             try {
    483                 loginUrl = authClient.buildOsmLoginUrl();
    484             } catch (OsmOAuthAuthorizationException e1) {
    485                 alertInvalidLoginUrl();
    486                 return;
    487             }
     481            final String loginUrl = getAdvancedPropertiesPanel().getAdvancedParameters().getOsmLoginUrl();
    488482            HelpAwareOptionPane.showOptionDialog(
    489483                    FullyAutomaticAuthorizationUI.this,
  • trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java

    r9309 r9355  
    88import java.lang.reflect.Field;
    99import java.net.HttpURLConnection;
    10 import java.net.MalformedURLException;
    1110import java.net.URL;
    1211import java.nio.charset.StandardCharsets;
     
    247246
    248247    /**
    249      * Derives the OSM login URL from the OAuth Authorization Website URL
    250      *
    251      * @return the OSM login URL
    252      * @throws OsmOAuthAuthorizationException if something went wrong, in particular if the
    253      * URLs are malformed
    254      */
    255     public String buildOsmLoginUrl() throws OsmOAuthAuthorizationException {
    256         try {
    257             URL autUrl = new URL(oauthProviderParameters.getAuthoriseUrl());
    258             URL url = new URL(Main.pref.get("oauth.protocol", "https"), autUrl.getHost(), autUrl.getPort(), "/login");
    259             return url.toString();
    260         } catch (MalformedURLException e) {
    261             throw new OsmOAuthAuthorizationException(e);
    262         }
    263     }
    264 
    265     /**
    266      * Derives the OSM logout URL from the OAuth Authorization Website URL
    267      *
    268      * @return the OSM logout URL
    269      * @throws OsmOAuthAuthorizationException if something went wrong, in particular if the
    270      * URLs are malformed
    271      */
    272     protected String buildOsmLogoutUrl() throws OsmOAuthAuthorizationException {
    273         try {
    274             URL autUrl = new URL(oauthProviderParameters.getAuthoriseUrl());
    275             URL url = new URL(Main.pref.get("oauth.protocol", "https"), autUrl.getHost(), autUrl.getPort(), "/logout");
    276             return url.toString();
    277         } catch (MalformedURLException e) {
    278             throw new OsmOAuthAuthorizationException(e);
    279         }
    280     }
    281 
    282     /**
    283248     * Submits a request to the OSM website for a login form. The OSM website replies a session ID in
    284249     * a cookie.
     
    289254    protected SessionId fetchOsmWebsiteSessionId() throws OsmOAuthAuthorizationException {
    290255        try {
    291             StringBuilder sb = new StringBuilder();
    292             sb.append(buildOsmLoginUrl()).append("?cookie_test=true");
    293             URL url = new URL(sb.toString());
     256            final URL url = new URL(oauthProviderParameters.getOsmLoginUrl() + "?cookie_test=true");
    294257            synchronized (this) {
    295258                connection = HttpClient.create(url);
     
    341304    protected void authenticateOsmSession(SessionId sessionId, String userName, String password) throws OsmLoginFailedException {
    342305        try {
    343             URL url = new URL(buildOsmLoginUrl());
     306            final URL url = new URL(oauthProviderParameters.getOsmLoginUrl());
    344307            final HttpClient client = HttpClient.create(url, "POST").useCache(false);
    345308
     
    383346    protected void logoutOsmSession(SessionId sessionId) throws OsmOAuthAuthorizationException {
    384347        try {
    385             URL url = new URL(buildOsmLogoutUrl());
     348            URL url = new URL(oauthProviderParameters.getOsmLogoutUrl());
    386349            synchronized (this) {
    387350                connection = HttpClient.create(url).setMaxRedirects(-1);
Note: See TracChangeset for help on using the changeset viewer.