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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.