Changeset 18991 in josm for trunk/src/org/openstreetmap/josm/io/OsmConnection.java
- Timestamp:
- 2024-02-21T21:26:18+01:00 (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmConnection.java
r18665 r18991 6 6 import java.lang.reflect.InvocationTargetException; 7 7 import java.net.Authenticator.RequestorType; 8 import java.net.MalformedURLException;9 8 import java.net.URL; 10 9 import java.nio.charset.StandardCharsets; … … 36 35 import org.openstreetmap.josm.tools.Logging; 37 36 38 import oauth.signpost.OAuthConsumer;39 import oauth.signpost.exception.OAuthException;40 41 37 /** 42 38 * Base class that handles common things like authentication for the reader and writer … … 51 47 protected boolean cancel; 52 48 protected HttpClient activeConnection; 53 protected OAuthParameters oauthParameters;54 49 protected IOAuthParameters oAuth20Parameters; 55 50 … … 146 141 147 142 /** 148 * Signs the connection with an OAuth authentication header149 *150 * @param connection the connection151 *152 * @throws MissingOAuthAccessTokenException if there is currently no OAuth Access Token configured153 * @throws OsmTransferException if signing fails154 */155 protected void addOAuthAuthorizationHeader(HttpClient connection) throws OsmTransferException {156 if (oauthParameters == null) {157 oauthParameters = OAuthParameters.createFromApiUrl(OsmApi.getOsmApi().getServerUrl());158 }159 OAuthConsumer consumer = oauthParameters.buildConsumer();160 OAuthAccessTokenHolder holder = OAuthAccessTokenHolder.getInstance();161 if (!holder.containsAccessToken()) {162 obtainAccessToken(connection);163 }164 if (!holder.containsAccessToken()) { // check if wizard completed165 throw new MissingOAuthAccessTokenException();166 }167 consumer.setTokenWithSecret(holder.getAccessTokenKey(), holder.getAccessTokenSecret());168 try {169 consumer.sign(connection);170 } catch (OAuthException e) {171 throw new OsmTransferException(tr("Failed to sign a HTTP connection with an OAuth Authentication header"), e);172 }173 }174 175 /**176 * Obtains an OAuth access token for the connection.177 * Afterwards, the token is accessible via {@link OAuthAccessTokenHolder} / {@link CredentialsManager}.178 * @param connection connection for which the access token should be obtained179 * @throws MissingOAuthAccessTokenException if the process cannot be completed successfully180 */181 protected void obtainAccessToken(final HttpClient connection) throws MissingOAuthAccessTokenException {182 try {183 final URL apiUrl = new URL(OsmApi.getOsmApi().getServerUrl());184 if (!Objects.equals(apiUrl.getHost(), connection.getURL().getHost())) {185 throw new MissingOAuthAccessTokenException();186 }187 fetcher.obtainAccessToken(apiUrl);188 OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true);189 OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());190 } catch (MalformedURLException | InvocationTargetException e) {191 throw new MissingOAuthAccessTokenException(e);192 } catch (InterruptedException e) {193 Thread.currentThread().interrupt();194 throw new MissingOAuthAccessTokenException(e);195 }196 }197 198 /**199 143 * Obtains an OAuth access token for the connection. 200 144 * Afterwards, the token is accessible via {@link OAuthAccessTokenHolder} / {@link CredentialsManager}. … … 221 165 } 222 166 // Clean up old token/password 223 OAuthAccessTokenHolder.getInstance().setAccessToken(null);224 167 OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), authToken.orElse(null)); 225 168 OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance()); … … 281 224 addBasicAuthorizationHeader(connection); 282 225 return; 283 case "oauth":284 addOAuthAuthorizationHeader(connection);285 return;286 226 case "oauth20": 287 227 addOAuth20AuthorizationHeader(connection);
Note:
See TracChangeset
for help on using the changeset viewer.