Changeset 4729 in josm
- Timestamp:
- 2011-12-28T00:05:30+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java
r4310 r4729 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.BufferedReader; 6 7 import java.io.DataOutputStream; 8 import java.io.InputStreamReader; 7 9 import java.io.IOException; 8 10 import java.io.UnsupportedEncodingException; … … 17 19 import java.util.Map; 18 20 import java.util.Map.Entry; 21 import java.util.regex.Matcher; 22 import java.util.regex.Pattern; 19 23 20 24 import oauth.signpost.OAuth; … … 41 45 private boolean canceled; 42 46 private HttpURLConnection connection; 47 48 private class SessionId { 49 String id; 50 String token; 51 String userName; 52 } 43 53 44 54 /** … … 192 202 } 193 203 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) { 195 222 List<String> setCookies = connection.getHeaderFields().get("Set-Cookie"); 196 223 if (setCookies == null) … … 209 236 continue; 210 237 } 211 if (kv[0].equals("_osm_session")) 238 if (kv[0].equals("_osm_session")) { 212 239 // 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 } 214 248 } 215 249 } … … 274 308 * a cookie. 275 309 * 276 * @return the session ID 310 * @return the session ID structure 277 311 * @throws OsmOAuthAuthorizationException thrown if something went wrong 278 312 */ 279 protected S tringfetchOsmWebsiteSessionId() throws OsmOAuthAuthorizationException {313 protected SessionId fetchOsmWebsiteSessionId() throws OsmOAuthAuthorizationException { 280 314 try { 281 315 StringBuilder sb = new StringBuilder(); … … 290 324 setHttpRequestParameters(connection); 291 325 connection.connect(); 292 S tringsessionId = extractOsmSession(connection);326 SessionId sessionId = extractOsmSession(connection); 293 327 if (sessionId == null) 294 328 throw new OsmOAuthAuthorizationException(tr("OSM website did not return a session cookie in response to ''{0}'',", url.toString())); … … 303 337 } 304 338 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 { 306 370 DataOutputStream dout = null; 307 371 try { … … 320 384 parameters.put("referer", "/"); 321 385 parameters.put("commit", "Login"); 386 parameters.put("authenticity_token", sessionId.token); 322 387 323 388 String request = buildPostRequest(parameters); … … 325 390 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 326 391 connection.setRequestProperty("Content-Length", Integer.toString(request.length())); 327 connection.setRequestProperty("Cookie", "_osm_session=" + sessionId );392 connection.setRequestProperty("Cookie", "_osm_session=" + sessionId.id); 328 393 // make sure we can catch 302 Moved Temporarily below 329 394 connection.setInstanceFollowRedirects(false); … … 360 425 } 361 426 362 protected void logoutOsmSession(S tringsessionId) throws OsmOAuthAuthorizationException {427 protected void logoutOsmSession(SessionId sessionId) throws OsmOAuthAuthorizationException { 363 428 try { 364 429 URL url = new URL(buildOsmLogoutUrl()); … … 382 447 } 383 448 384 protected void sendAuthorisationRequest(S tringsessionId, OAuthToken requestToken, OsmPrivileges privileges) throws OsmOAuthAuthorizationException {449 protected void sendAuthorisationRequest(SessionId sessionId, OAuthToken requestToken, OsmPrivileges privileges) throws OsmOAuthAuthorizationException { 385 450 Map<String, String> parameters = new HashMap<String, String>(); 451 fetchOAuthToken(sessionId, requestToken); 386 452 parameters.put("oauth_token", requestToken.getKey()); 387 453 parameters.put("oauth_callback", ""); 454 parameters.put("authenticity_token", sessionId.token); 388 455 if (privileges.isAllowWriteApi()) { 389 456 parameters.put("allow_write_api", "yes"); … … 417 484 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 418 485 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); 420 487 connection.setInstanceFollowRedirects(false); 421 488 setHttpRequestParameters(connection); … … 480 547 monitor.setTicksCount(4); 481 548 monitor.indeterminateSubTask(tr("Initializing a session at the OSM website...")); 482 String sessionId = fetchOsmWebsiteSessionId(); 549 SessionId sessionId = fetchOsmWebsiteSessionId(); 550 sessionId.userName = osmUserName; 483 551 if (canceled) 484 552 throw new OsmTransferCanceledException();
Note:
See TracChangeset
for help on using the changeset viewer.