Skip to content

Commit

Permalink
Refactor flows.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Aug 15, 2023
1 parent 1b7f067 commit 3f1a39a
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 175 deletions.
5 changes: 2 additions & 3 deletions box/src/main/java/ch/cyberduck/core/box/BoxSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ public CloseableHttpClient connect(final Proxy proxy, final HostKeyCallback key,
authorizationService = new OAuth2RequestInterceptor(configuration.build(), host, prompt)
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl());
configuration.addInterceptorLast(authorizationService);
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt));
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService));
return configuration.build();
}

@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
authorizationService.authorize(host, prompt, cancel);
try {
final Credentials credentials = host.getCredentials();
final Credentials credentials = authorizationService.validate();
credentials.setUsername(new UsersApi(new BoxApiClient(client)).getUsersMe(Collections.emptyList()).getLogin());
}
catch(ApiException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* GNU General Public License for more details.
*/

import ch.cyberduck.core.DisabledCancelCallback;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.LoginCallback;
import ch.cyberduck.core.exception.BackgroundException;
Expand All @@ -30,24 +29,19 @@
import org.apache.logging.log4j.Logger;

public class PreconditionFailedResponseInterceptor extends OAuth2ErrorResponseInterceptor {

private static final Logger log = LogManager.getLogger(PreconditionFailedResponseInterceptor.class);

private static final int MAX_RETRIES = 1;

private final Host bookmark;
private final OAuth2RequestInterceptor service;
private final LoginCallback prompt;
private final ServiceUnavailableRetryStrategy next;

public PreconditionFailedResponseInterceptor(final Host bookmark,
final OAuth2RequestInterceptor service,
final LoginCallback prompt,
final ServiceUnavailableRetryStrategy next) {
super(bookmark, service, prompt);
this.bookmark = bookmark;
super(bookmark, service);
this.service = service;
this.prompt = prompt;
this.next = next;
}

Expand All @@ -58,7 +52,7 @@ public boolean retryRequest(final HttpResponse response, final int executionCoun
if(executionCount <= MAX_RETRIES) {
try {
log.warn(String.format("Invalidate OAuth tokens due to failed precondition %s", response));
service.save(service.authorize(bookmark, prompt, new DisabledCancelCallback()));
service.save(service.authorize());
// Try again
return true;
}
Expand Down
8 changes: 5 additions & 3 deletions dracoon/src/main/java/ch/cyberduck/core/sds/SDSSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void process(final HttpRequest request, final HttpContext context) throws
throw new DefaultIOExceptionMappingService().map(e);
}
configuration.setServiceUnavailableRetryStrategy(new PreconditionFailedResponseInterceptor(host, authorizationService, prompt,
new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt)));
new OAuth2ErrorResponseInterceptor(host, authorizationService)));
configuration.addInterceptorLast(authorizationService);
configuration.addInterceptorLast(new HttpRequestInterceptor() {
@Override
Expand Down Expand Up @@ -219,7 +219,7 @@ public void login(final Proxy proxy, final LoginCallback prompt, final CancelCal
LocaleFactory.localizedString("Your DRACOON environment is outdated and no longer works with this application. Please contact your administrator.", "SDS"));
}
}
final Credentials credentials = host.getCredentials();
final Credentials credentials;
// The provided token is valid for two hours, every usage resets this period to two full hours again. Logging off invalidates the token.
switch(SDSProtocol.Authorization.valueOf(host.getProtocol().getAuthorization())) {
case oauth:
Expand All @@ -234,8 +234,10 @@ public void login(final Proxy proxy, final LoginCallback prompt, final CancelCal
log.warn(String.format("Failure to parse software version %s", version));
}
}
authorizationService.authorize(host, prompt, cancel);
credentials = authorizationService.validate();
break;
default:
credentials = host.getCredentials();
}
final UserAccount account;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import ch.cyberduck.core.UseragentProvider;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.ConnectionCanceledException;
import ch.cyberduck.core.exception.LoginCanceledException;
import ch.cyberduck.core.features.*;
import ch.cyberduck.core.http.DefaultHttpRateLimiter;
import ch.cyberduck.core.http.HttpSession;
Expand Down Expand Up @@ -79,7 +78,7 @@ protected CustomDbxRawClientV2 connect(final Proxy proxy, final HostKeyCallback
authorizationService = new OAuth2RequestInterceptor(configuration.build(), host, prompt)
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl());
configuration.addInterceptorLast(authorizationService);
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt));
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService));
if(new HostPreferences(host).getBoolean("dropbox.limit.requests.enable")) {
configuration.addInterceptorLast(new RateLimitingHttpRequestInterceptor(new DefaultHttpRateLimiter(
new HostPreferences(host).getInteger("dropbox.limit.requests.second")
Expand All @@ -94,13 +93,12 @@ protected CustomDbxRawClientV2 connect(final Proxy proxy, final HostKeyCallback

@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
authorizationService.authorize(host, prompt, cancel);
try {
final Credentials credentials = authorizationService.validate();
final FullAccount account = new DbxUserUsersRequests(client).getCurrentAccount();
if(log.isDebugEnabled()) {
log.debug(String.format("Authenticated as user %s", account));
}
final Credentials credentials = host.getCredentials();
credentials.setUsername(account.getEmail());
switch(account.getAccountType()) {
// The features listed below are only available to customers on Dropbox Professional, Standard, Advanced, and Enterprise.
Expand Down
17 changes: 2 additions & 15 deletions eue/src/main/java/ch/cyberduck/core/eue/EueSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import ch.cyberduck.core.HostKeyCallback;
import ch.cyberduck.core.ListService;
import ch.cyberduck.core.LoginCallback;
import ch.cyberduck.core.OAuthTokens;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathNormalizer;
import ch.cyberduck.core.UrlProvider;
Expand All @@ -31,7 +30,6 @@
import ch.cyberduck.core.eue.io.swagger.client.api.UserInfoApi;
import ch.cyberduck.core.eue.io.swagger.client.model.UserSharesModel;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.InteroperabilityException;
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.features.*;
import ch.cyberduck.core.http.DefaultHttpRateLimiter;
Expand Down Expand Up @@ -110,7 +108,7 @@ public void process(final HttpRequest request, final HttpContext context) {
}).build(), host, prompt)
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl()
);
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt));
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService));
configuration.addInterceptorLast(authorizationService);
configuration.addInterceptorLast(new HttpRequestInterceptor() {
@Override
Expand Down Expand Up @@ -173,17 +171,7 @@ public void progress(final Integer seconds) {

@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
try {
authorizationService.refresh(authorizationService.authorize(host, prompt, cancel));
}
catch(InteroperabilityException e) {
// Perm.INVALID_GRANT
log.warn(String.format("Failure %s refreshing OAuth tokens", e));
// Reset OAuth Tokens
host.getCredentials().setOauth(OAuthTokens.EMPTY);
authorizationService.authorize(host, prompt, cancel
);
}
final Credentials credentials = authorizationService.validate();
try {
final StringBuilder url = new StringBuilder();
url.append(host.getProtocol().getScheme().toString()).append("://");
Expand Down Expand Up @@ -213,7 +201,6 @@ public void login(final Proxy proxy, final LoginCallback prompt, final CancelCal
throw new DefaultHttpResponseExceptionMappingService().map(new HttpResponseException(
response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
}
final Credentials credentials = host.getCredentials();
credentials.setUsername(new UserInfoApi(new EueApiClient(this))
.userinfoGet(null, null).getAccount().getOsServiceId());
if(StringUtils.isNotBlank(host.getProperty("pacs.url"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.ConnectionCanceledException;
import ch.cyberduck.core.exception.HostParserException;
import ch.cyberduck.core.exception.LoginCanceledException;
import ch.cyberduck.core.features.*;
import ch.cyberduck.core.http.DefaultHttpRateLimiter;
import ch.cyberduck.core.http.HttpSession;
Expand Down Expand Up @@ -75,7 +74,7 @@ protected Drive connect(final Proxy proxy, final HostKeyCallback callback, final
authorizationService = new OAuth2RequestInterceptor(builder.build(ProxyFactory.get().find(host.getProtocol().getOAuthAuthorizationUrl()), this, prompt).build(), host, prompt)
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl());
configuration.addInterceptorLast(authorizationService);
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt));
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService));
if(new HostPreferences(host).getBoolean("googledrive.limit.requests.enable")) {
configuration.addInterceptorLast(new RateLimitingHttpRequestInterceptor(new DefaultHttpRateLimiter(
new HostPreferences(host).getInteger("googledrive.limit.requests.second")
Expand Down Expand Up @@ -106,8 +105,7 @@ public void initialize(final HttpRequest request) {

@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
authorizationService.authorize(host, prompt, cancel);
final Credentials credentials = host.getCredentials();
final Credentials credentials = authorizationService.validate();
final About about;
try {
about = client.about().get().setFields("user").execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* GNU General Public License for more details.
*/

import ch.cyberduck.core.Credentials;
import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostKeyCallback;
Expand All @@ -26,7 +27,6 @@
import ch.cyberduck.core.cdn.DistributionConfiguration;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.ConnectionCanceledException;
import ch.cyberduck.core.exception.LoginCanceledException;
import ch.cyberduck.core.features.*;
import ch.cyberduck.core.http.HttpSession;
import ch.cyberduck.core.http.UserAgentHttpRequestInitializer;
Expand Down Expand Up @@ -70,18 +70,17 @@ protected Storage connect(final Proxy proxy, final HostKeyCallback key, final Lo
authorizationService = new OAuth2RequestInterceptor(builder.build(ProxyFactory.get().find(host.getProtocol().getOAuthAuthorizationUrl()), this, prompt).build(), host, prompt)
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl());
configuration.addInterceptorLast(authorizationService);
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt));
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService));
transport = new ApacheHttpTransport(configuration.build());
final UseragentProvider ua = new PreferencesUseragentProvider();
return new Storage.Builder(transport, new GsonFactory(), new UserAgentHttpRequestInitializer(ua))
.setApplicationName(ua.get())
.build();
.setApplicationName(ua.get())
.build();
}

@Override
public void login(final Proxy proxy, final LoginCallback prompt,
final CancelCallback cancel) throws BackgroundException {
authorizationService.authorize(host, prompt, cancel);
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
authorizationService.validate();
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions hubic/src/main/java/ch/cyberduck/core/hubic/HubicSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* GNU General Public License for more details.
*/

import ch.cyberduck.core.Credentials;
import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostKeyCallback;
Expand All @@ -23,7 +24,6 @@
import ch.cyberduck.core.cdn.DistributionConfiguration;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.ConnectionCanceledException;
import ch.cyberduck.core.exception.LoginCanceledException;
import ch.cyberduck.core.oauth.OAuth2ErrorResponseInterceptor;
import ch.cyberduck.core.oauth.OAuth2RequestInterceptor;
import ch.cyberduck.core.openstack.SwiftExceptionMappingService;
Expand Down Expand Up @@ -57,18 +57,18 @@ protected Client connect(final Proxy proxy, final HostKeyCallback key, final Log
authorizationService = new OAuth2RequestInterceptor(configuration.build(), host, prompt)
.withRedirectUri(host.getProtocol().getOAuthRedirectUrl());
configuration.addInterceptorLast(authorizationService);
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService, prompt));
configuration.setServiceUnavailableRetryStrategy(new OAuth2ErrorResponseInterceptor(host, authorizationService));
return new Client(configuration.build());
}

@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
final OAuthTokens tokens = authorizationService.authorize(host, prompt, cancel);
final Credentials credentials = authorizationService.validate();
try {
if(log.isInfoEnabled()) {
log.info(String.format("Attempt authentication with %s", tokens));
log.info(String.format("Attempt authentication with %s", credentials.getOauth()));
}
client.authenticate(new HubicAuthenticationRequest(tokens.getAccessToken()), new HubicAuthenticationResponseHandler());
client.authenticate(new HubicAuthenticationRequest(credentials.getOauth().getAccessToken()), new HubicAuthenticationResponseHandler());
}
catch(GenericException e) {
throw new SwiftExceptionMappingService().map(e);
Expand Down
Loading

0 comments on commit 3f1a39a

Please sign in to comment.