Skip to content

Commit f02256b

Browse files
committed
Use SessionFactory for token exchange http client.
1 parent 1d01384 commit f02256b

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

hub/src/main/java/cloud/katta/protocols/hub/HubSession.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import ch.cyberduck.core.oauth.OAuth2AuthorizationService;
2828
import ch.cyberduck.core.oauth.OAuth2ErrorResponseInterceptor;
2929
import ch.cyberduck.core.oauth.OAuth2RequestInterceptor;
30+
import ch.cyberduck.core.preferences.HostPreferences;
3031
import ch.cyberduck.core.preferences.PreferencesFactory;
3132
import ch.cyberduck.core.proxy.ProxyFinder;
3233
import ch.cyberduck.core.ssl.X509KeyManager;
@@ -74,6 +75,8 @@ public class HubSession extends HttpSession<HubApiClient> {
7475

7576
private HubVaultListService vaults;
7677

78+
public static final String SKIP_LISTING_UPON_LOGIN = "skip.listing.upon.login";
79+
7780
/**
7881
* Interceptor for OpenID connect flow
7982
*/
@@ -163,7 +166,10 @@ public void login(final LoginCallback prompt, final CancelCallback cancel) throw
163166
final OAuthTokens tokens = new OAuthTokens(credentials.getOauth().getAccessToken(), credentials.getOauth().getRefreshToken(), credentials.getOauth().getExpiryInMilliseconds(),
164167
credentials.getOauth().getIdToken());
165168
vaults = new HubVaultListService(protocols, this, trust, key, registry, tokens);
166-
vaults.list(Home.root(), new DisabledListProgressListener());
169+
170+
if(!new HostPreferences(host).getBoolean(SKIP_LISTING_UPON_LOGIN)) {
171+
vaults.list(Home.root(), new DisabledListProgressListener());
172+
}
167173
}
168174
catch(SecurityFailure e) {
169175
throw new InteroperabilityException(LocaleFactory.localizedString("Login failed", "Credentials"), e);

hub/src/main/java/cloud/katta/protocols/s3/TokenExchangeRequestInterceptor.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,49 @@
55
package cloud.katta.protocols.s3;
66

77
import ch.cyberduck.core.Credentials;
8+
import ch.cyberduck.core.DisabledCancelCallback;
9+
import ch.cyberduck.core.DisabledHostKeyCallback;
10+
import ch.cyberduck.core.DisabledLoginCallback;
11+
import ch.cyberduck.core.DisabledPasswordCallback;
12+
import ch.cyberduck.core.DisabledProgressListener;
813
import ch.cyberduck.core.Host;
14+
import ch.cyberduck.core.HostParser;
915
import ch.cyberduck.core.LoginCallback;
16+
import ch.cyberduck.core.LoginConnectionService;
1017
import ch.cyberduck.core.OAuthTokens;
18+
import ch.cyberduck.core.PasswordStoreFactory;
19+
import ch.cyberduck.core.ProtocolFactory;
20+
import ch.cyberduck.core.SessionFactory;
1121
import ch.cyberduck.core.exception.BackgroundException;
1222
import ch.cyberduck.core.exception.LoginCanceledException;
1323
import ch.cyberduck.core.exception.LoginFailureException;
1424
import ch.cyberduck.core.oauth.OAuth2RequestInterceptor;
1525
import ch.cyberduck.core.preferences.HostPreferences;
1626
import ch.cyberduck.core.preferences.PreferencesReader;
27+
import ch.cyberduck.core.ssl.DefaultX509KeyManager;
28+
import ch.cyberduck.core.ssl.DefaultX509TrustManager;
29+
import ch.cyberduck.core.vault.VaultRegistryFactory;
1730

18-
import static cloud.katta.protocols.s3.S3AssumeRoleProtocol.OAUTH_TOKENEXCHANGE_BASEPATH;
31+
import static cloud.katta.protocols.hub.HubSession.SKIP_LISTING_UPON_LOGIN;
1932

20-
import org.apache.commons.lang3.StringUtils;
2133
import org.apache.http.client.HttpClient;
2234
import org.apache.logging.log4j.LogManager;
2335
import org.apache.logging.log4j.Logger;
2436

2537
import java.util.Arrays;
26-
import java.util.Collections;
2738
import java.util.List;
2839

2940
import cloud.katta.client.ApiClient;
3041
import cloud.katta.client.ApiException;
3142
import cloud.katta.client.api.StorageResourceApi;
32-
import cloud.katta.client.auth.HttpBearerAuth;
3343
import cloud.katta.client.model.AccessTokenResponse;
44+
import cloud.katta.protocols.hub.HubSession;
3445
import cloud.katta.protocols.hub.exceptions.HubExceptionMappingService;
3546
import com.auth0.jwt.JWT;
3647
import com.auth0.jwt.exceptions.JWTDecodeException;
3748
import com.auth0.jwt.interfaces.DecodedJWT;
3849

39-
import static cloud.katta.protocols.s3.S3AssumeRoleProtocol.OAUTH_TOKENEXCHANGE;
50+
import static cloud.katta.protocols.s3.S3AssumeRoleProtocol.OAUTH_TOKENEXCHANGE_BASEPATH;
4051

4152
/**
4253
* Exchange OIDC token to scoped token using OAuth 2.0 Token Exchange. Used for S3-STS in Katta.
@@ -78,9 +89,7 @@ public OAuthTokens refresh(final OAuthTokens previous) throws BackgroundExceptio
7889
public OAuthTokens exchange(final OAuthTokens previous) throws BackgroundException {
7990
log.info("Exchange tokens {} for {}", previous, bookmark);
8091
final PreferencesReader preferences = new HostPreferences(bookmark);
81-
final ApiClient apiClient = new ApiClient(Collections.singletonMap("bearer", new HttpBearerAuth("bearer")));
82-
apiClient.addDefaultHeader("Authorization",String.format("Bearer %s", previous.getAccessToken()));
83-
apiClient.setBasePath(preferences.getProperty(OAUTH_TOKENEXCHANGE_BASEPATH));
92+
final ApiClient apiClient = getHubApiClient(previous, preferences);
8493

8594
final StorageResourceApi api = new StorageResourceApi(apiClient);
8695
try {
@@ -97,6 +106,18 @@ public OAuthTokens exchange(final OAuthTokens previous) throws BackgroundExcepti
97106
}
98107
}
99108

109+
private static ApiClient getHubApiClient(final OAuthTokens previous, final PreferencesReader preferences) throws BackgroundException {
110+
final ProtocolFactory factory = ProtocolFactory.get();
111+
final Host hub = new HostParser(factory).get(preferences.getProperty(OAUTH_TOKENEXCHANGE_BASEPATH)).withCredentials(new Credentials().withOauth(new OAuthTokens(previous)));
112+
hub.setProperty(SKIP_LISTING_UPON_LOGIN, "true"); // prevent infinite recursion
113+
final HubSession session = (HubSession) SessionFactory.create(hub, new DefaultX509TrustManager(), new DefaultX509KeyManager())
114+
.withRegistry(VaultRegistryFactory.get(new DisabledPasswordCallback()));
115+
final LoginConnectionService login = new LoginConnectionService(new DisabledLoginCallback(), new DisabledHostKeyCallback(),
116+
PasswordStoreFactory.get(), new DisabledProgressListener());
117+
login.check(session, new DisabledCancelCallback());
118+
return session.getClient();
119+
}
120+
100121
@Override
101122
public Credentials validate() throws BackgroundException {
102123
final Credentials credentials = super.validate();

0 commit comments

Comments
 (0)