Ignore:
Timestamp:
2011-12-28T00:05:30+01:00 (12 years ago)
Author:
stoecker
Message:

fix #7183 - fully automatic OAuth not working

File:
1 edited

Legend:

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

    r4310 r4729  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.BufferedReader;
    67import java.io.DataOutputStream;
     8import java.io.InputStreamReader;
    79import java.io.IOException;
    810import java.io.UnsupportedEncodingException;
     
    1719import java.util.Map;
    1820import java.util.Map.Entry;
     21import java.util.regex.Matcher;
     22import java.util.regex.Pattern;
    1923
    2024import oauth.signpost.OAuth;
     
    4145    private boolean canceled;
    4246    private HttpURLConnection connection;
     47
     48    private class SessionId {
     49        String id;
     50        String token;
     51        String userName;
     52    }
    4353
    4454    /**
     
    192202    }
    193203
    194     protected String extractOsmSession(HttpURLConnection connection) {
     204    protected String extractToken(HttpURLConnection connection) {
     205        try {
     206            BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
     207            String c;
     208            Pattern p = Pattern.compile(".*authenticity_token.*value=\"([^\"]+)\".*");
     209            while((c = r.readLine()) != null) {
     210                Matcher m = p.matcher(c);
     211                if(m.find()) {
     212                    return m.group(1);
     213                }
     214            }
     215        } catch (IOException e) {
     216            return null;
     217        }
     218        return null;
     219    }
     220
     221    protected SessionId extractOsmSession(HttpURLConnection connection) {
    195222        List<String> setCookies = connection.getHeaderFields().get("Set-Cookie");
    196223        if (setCookies == null)
     
    209236                    continue;
    210237                }
    211                 if (kv[0].equals("_osm_session"))
     238                if (kv[0].equals("_osm_session")) {
    212239                    // osm session cookie found
    213                     return kv[1];
     240                    String token = extractToken(connection);
     241                    if(token == null)
     242                        return null;
     243                    SessionId si = new SessionId();
     244                    si.id = kv[1];
     245                    si.token = token;
     246                    return si;
     247                }
    214248            }
    215249        }
     
    274308     * a cookie.
    275309     *
    276      * @return the session ID
     310     * @return the session ID structure
    277311     * @throws OsmOAuthAuthorizationException thrown if something went wrong
    278312     */
    279     protected String fetchOsmWebsiteSessionId() throws OsmOAuthAuthorizationException {
     313    protected SessionId fetchOsmWebsiteSessionId() throws OsmOAuthAuthorizationException {
    280314        try {
    281315            StringBuilder sb = new StringBuilder();
     
    290324            setHttpRequestParameters(connection);
    291325            connection.connect();
    292             String sessionId = extractOsmSession(connection);
     326            SessionId sessionId = extractOsmSession(connection);
    293327            if (sessionId == null)
    294328                throw new OsmOAuthAuthorizationException(tr("OSM website did not return a session cookie in response to ''{0}'',", url.toString()));
     
    303337    }
    304338
    305     protected void authenticateOsmSession(String sessionId, String userName, String password) throws OsmLoginFailedException {
     339    /**
     340     * Submits a request to the OSM website for a OAuth form. The OSM website replies a session token in
     341     * a hidden parameter.
     342     *
     343     * @throws OsmOAuthAuthorizationException thrown if something went wrong
     344     */
     345    protected void fetchOAuthToken(SessionId sessionId, OAuthToken requestToken) throws OsmOAuthAuthorizationException {
     346        try {
     347            URL url = new URL(getAuthoriseUrl(requestToken));
     348            synchronized(this) {
     349                connection = (HttpURLConnection)url.openConnection();
     350            }
     351            connection.setRequestMethod("GET");
     352            connection.setDoInput(true);
     353            connection.setDoOutput(false);
     354            connection.setRequestProperty("Cookie", "_osm_session=" + sessionId.id + "; _osm_username=" + sessionId.userName);
     355            setHttpRequestParameters(connection);
     356            connection.connect();
     357            sessionId.token = extractToken(connection);
     358            if (sessionId.token == null)
     359                throw new OsmOAuthAuthorizationException(tr("OSM website did not return a session cookie in response to ''{0}'',", url.toString()));
     360        } catch(IOException e) {
     361            throw new OsmOAuthAuthorizationException(e);
     362        } finally {
     363            synchronized(this) {
     364                connection = null;
     365            }
     366        }
     367    }
     368
     369    protected void authenticateOsmSession(SessionId sessionId, String userName, String password) throws OsmLoginFailedException {
    306370        DataOutputStream dout = null;
    307371        try {
     
    320384            parameters.put("referer", "/");
    321385            parameters.put("commit", "Login");
     386            parameters.put("authenticity_token", sessionId.token);
    322387
    323388            String request = buildPostRequest(parameters);
     
    325390            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    326391            connection.setRequestProperty("Content-Length", Integer.toString(request.length()));
    327             connection.setRequestProperty("Cookie", "_osm_session=" + sessionId);
     392            connection.setRequestProperty("Cookie", "_osm_session=" + sessionId.id);
    328393            // make sure we can catch 302 Moved Temporarily below
    329394            connection.setInstanceFollowRedirects(false);
     
    360425    }
    361426
    362     protected void logoutOsmSession(String sessionId) throws OsmOAuthAuthorizationException {
     427    protected void logoutOsmSession(SessionId sessionId) throws OsmOAuthAuthorizationException {
    363428        try {
    364429            URL url = new URL(buildOsmLogoutUrl());
     
    382447    }
    383448
    384     protected void sendAuthorisationRequest(String sessionId, OAuthToken requestToken, OsmPrivileges privileges) throws OsmOAuthAuthorizationException {
     449    protected void sendAuthorisationRequest(SessionId sessionId, OAuthToken requestToken, OsmPrivileges privileges) throws OsmOAuthAuthorizationException {
    385450        Map<String, String> parameters = new HashMap<String, String>();
     451        fetchOAuthToken(sessionId, requestToken);
    386452        parameters.put("oauth_token", requestToken.getKey());
    387453        parameters.put("oauth_callback", "");
     454        parameters.put("authenticity_token", sessionId.token);
    388455        if (privileges.isAllowWriteApi()) {
    389456            parameters.put("allow_write_api", "yes");
     
    417484            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    418485            connection.setRequestProperty("Content-Length", Integer.toString(request.length()));
    419             connection.setRequestProperty("Cookie", "_osm_session=" + sessionId);
     486            connection.setRequestProperty("Cookie", "_osm_session=" + sessionId.id + "; _osm_username=" + sessionId.userName);
    420487            connection.setInstanceFollowRedirects(false);
    421488            setHttpRequestParameters(connection);
     
    480547            monitor.setTicksCount(4);
    481548            monitor.indeterminateSubTask(tr("Initializing a session at the OSM website..."));
    482             String sessionId = fetchOsmWebsiteSessionId();
     549            SessionId sessionId = fetchOsmWebsiteSessionId();
     550            sessionId.userName = osmUserName;
    483551            if (canceled)
    484552                throw new OsmTransferCanceledException();
Note: See TracChangeset for help on using the changeset viewer.