Changeset 18764 in josm for trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java
- Timestamp:
- 2023-06-20T19:40:11+02:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/oauth/TestAccessTokenTask.java
r13901 r18764 12 12 import javax.xml.parsers.ParserConfigurationException; 13 13 14 import org.openstreetmap.josm.data.oauth.IOAuthParameters; 15 import org.openstreetmap.josm.data.oauth.IOAuthToken; 16 import org.openstreetmap.josm.data.oauth.OAuth20Token; 14 17 import org.openstreetmap.josm.data.oauth.OAuthParameters; 15 18 import org.openstreetmap.josm.data.oauth.OAuthToken; … … 42 45 */ 43 46 public class TestAccessTokenTask extends PleaseWaitRunnable { 44 private final OAuthToken token; 45 private final OAuthParameters oauthParameters; 47 private final OAuthToken tokenOAuth1; 48 private final IOAuthToken tokenOAuth2; 49 private final IOAuthParameters oauthParameters; 46 50 private boolean canceled; 47 51 private final Component parent; … … 62 66 CheckParameterUtil.ensureParameterNotNull(parameters, "parameters"); 63 67 CheckParameterUtil.ensureParameterNotNull(accessToken, "accessToken"); 64 this.token = accessToken; 68 this.tokenOAuth1 = accessToken; 69 this.tokenOAuth2 = null; 70 this.oauthParameters = parameters; 71 this.parent = parent; 72 this.apiUrl = apiUrl; 73 } 74 75 /** 76 * Create the task 77 * 78 * @param parent the parent component relative to which the {@link PleaseWaitRunnable}-Dialog is displayed 79 * @param apiUrl the API URL. Must not be null. 80 * @param parameters the OAuth parameters. Must not be null. 81 * @param accessToken the Access Token. Must not be null. 82 * @since xxx 83 */ 84 public TestAccessTokenTask(Component parent, String apiUrl, IOAuthParameters parameters, IOAuthToken accessToken) { 85 super(parent, tr("Testing OAuth Access Token"), false /* don't ignore exceptions */); 86 CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl"); 87 CheckParameterUtil.ensureParameterNotNull(parameters, "parameters"); 88 CheckParameterUtil.ensureParameterNotNull(accessToken, "accessToken"); 89 this.tokenOAuth1 = null; 90 this.tokenOAuth2 = accessToken; 65 91 this.oauthParameters = parameters; 66 92 this.parent = parent; … … 84 110 85 111 protected void sign(HttpClient con) throws OAuthException { 86 OAuthConsumer consumer = oauthParameters.buildConsumer(); 87 consumer.setTokenWithSecret(token.getKey(), token.getSecret()); 88 consumer.sign(con); 112 if (oauthParameters instanceof OAuthParameters) { 113 OAuthConsumer consumer = ((OAuthParameters) oauthParameters).buildConsumer(); 114 consumer.setTokenWithSecret(tokenOAuth1.getKey(), tokenOAuth1.getSecret()); 115 consumer.sign(con); 116 } else { 117 try { 118 this.tokenOAuth2.sign(con); 119 } catch (org.openstreetmap.josm.data.oauth.OAuthException e) { 120 // Adapt our OAuthException to the SignPost OAuth exception 121 throw new OAuthException(e) {}; 122 } 123 } 89 124 } 90 125 … … 114 149 } 115 150 151 final String oauthKey = getAuthKey(); 116 152 if (connection.getResponse().getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) 117 153 throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, 118 tr("Retrieving user details with Access Token Key ''{0}'' was rejected.", token.getKey()), null); 154 tr("Retrieving user details with Access Token Key ''{0}'' was rejected.", 155 oauthKey), null); 119 156 120 157 if (connection.getResponse().getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) 121 158 throw new OsmApiException(HttpURLConnection.HTTP_FORBIDDEN, 122 tr("Retrieving user details with Access Token Key ''{0}'' was forbidden.", token.getKey()), null);159 tr("Retrieving user details with Access Token Key ''{0}'' was forbidden.", oauthKey), null); 123 160 124 161 if (connection.getResponse().getResponseCode() != HttpURLConnection.HTTP_OK) … … 146 183 + "You are accessing the OSM server as user ''{2}'' with id ''{3}''." 147 184 + "</html>", 148 token.getKey(),185 getAuthKey(), 149 186 apiUrl, 150 187 Utils.escapeReservedCharactersHTML(userInfo.getDisplayName()), … … 167 204 +"</html>", 168 205 apiUrl, 169 token.getKey()206 getAuthKey() 170 207 ), 171 208 tr("Test failed"), … … 185 222 +"</html>", 186 223 apiUrl, 187 token.getKey()224 getAuthKey() 188 225 ), 189 226 tr("Token allows restricted access"), … … 204 241 +"</html>", 205 242 apiUrl, 206 token.getKey()243 getAuthKey() 207 244 ), 208 245 tr("Test failed"), … … 221 258 +"</html>", 222 259 apiUrl, 223 token.getKey()260 getAuthKey() 224 261 ), 225 262 tr("Test failed"), … … 237 274 + "</html>", 238 275 apiUrl, 239 token.getKey()276 getAuthKey() 240 277 ), 241 278 tr("Test failed"), … … 276 313 } 277 314 } 315 316 private String getAuthKey() { 317 if (this.tokenOAuth1 != null) { 318 return this.tokenOAuth1.getKey(); 319 } 320 if (this.tokenOAuth2 instanceof OAuth20Token) { 321 return ((OAuth20Token) this.tokenOAuth2).getBearerToken(); 322 } 323 throw new IllegalArgumentException("Only OAuth1 and OAuth2 tokens are understood: " + this.tokenOAuth2); 324 } 278 325 }
Note:
See TracChangeset
for help on using the changeset viewer.
