55package cloud .katta .protocols .s3 ;
66
77import 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 ;
813import ch .cyberduck .core .Host ;
14+ import ch .cyberduck .core .HostParser ;
915import ch .cyberduck .core .LoginCallback ;
16+ import ch .cyberduck .core .LoginConnectionService ;
1017import ch .cyberduck .core .OAuthTokens ;
18+ import ch .cyberduck .core .PasswordStoreFactory ;
19+ import ch .cyberduck .core .ProtocolFactory ;
20+ import ch .cyberduck .core .SessionFactory ;
1121import ch .cyberduck .core .exception .BackgroundException ;
1222import ch .cyberduck .core .exception .LoginCanceledException ;
1323import ch .cyberduck .core .exception .LoginFailureException ;
1424import ch .cyberduck .core .oauth .OAuth2RequestInterceptor ;
1525import ch .cyberduck .core .preferences .HostPreferences ;
1626import 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 ;
2133import org .apache .http .client .HttpClient ;
2234import org .apache .logging .log4j .LogManager ;
2335import org .apache .logging .log4j .Logger ;
2436
2537import java .util .Arrays ;
26- import java .util .Collections ;
2738import java .util .List ;
2839
2940import cloud .katta .client .ApiClient ;
3041import cloud .katta .client .ApiException ;
3142import cloud .katta .client .api .StorageResourceApi ;
32- import cloud .katta .client .auth .HttpBearerAuth ;
3343import cloud .katta .client .model .AccessTokenResponse ;
44+ import cloud .katta .protocols .hub .HubSession ;
3445import cloud .katta .protocols .hub .exceptions .HubExceptionMappingService ;
3546import com .auth0 .jwt .JWT ;
3647import com .auth0 .jwt .exceptions .JWTDecodeException ;
3748import 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