Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tokens in credentials to be saved in keychain after refresh. #15310

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import ch.cyberduck.core.Credentials;
import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostPasswordStore;
import ch.cyberduck.core.LocaleFactory;
import ch.cyberduck.core.LoginCallback;
import ch.cyberduck.core.LoginOptions;
import ch.cyberduck.core.OAuthTokens;
import ch.cyberduck.core.PasswordCallback;
import ch.cyberduck.core.PasswordStoreFactory;
import ch.cyberduck.core.PreferencesUseragentProvider;
import ch.cyberduck.core.Profile;
import ch.cyberduck.core.StringAppender;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.LocalAccessDeniedException;
import ch.cyberduck.core.exception.LoginCanceledException;
import ch.cyberduck.core.exception.LoginFailureException;
import ch.cyberduck.core.http.DefaultHttpResponseExceptionMappingService;
Expand Down Expand Up @@ -93,6 +96,8 @@ public class OAuth2AuthorizationService {

private final HttpTransport transport;

private final HostPasswordStore store = PasswordStoreFactory.get();

public OAuth2AuthorizationService(final HttpClient client, final Host host,
final String tokenServerUrl, final String authorizationServerUrl,
final String clientid, final String clientsecret, final List<String> scopes, final boolean pkce,
Expand Down Expand Up @@ -150,6 +155,21 @@ public Credentials validate() throws BackgroundException {
return credentials.withOauth(this.authorize());
}

/**
* Save updated tokens in keychain
*
* @return Same tokens saved
*/
public OAuthTokens save(final OAuthTokens tokens) throws LocalAccessDeniedException {
if(log.isDebugEnabled()) {
log.debug(String.format("Save new tokens %s for %s", tokens, host));
}
credentials.withOauth(tokens).withSaved(new LoginOptions().save);
store.save(host);
return tokens;
}


/**
* @return Tokens retrieved
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@

import ch.cyberduck.core.Credentials;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.HostPasswordStore;
import ch.cyberduck.core.HostUrlProvider;
import ch.cyberduck.core.LoginCallback;
import ch.cyberduck.core.OAuthTokens;
import ch.cyberduck.core.PasswordStoreFactory;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.LocalAccessDeniedException;
import ch.cyberduck.core.exception.LoginCanceledException;
import ch.cyberduck.core.exception.LoginFailureException;

Expand All @@ -50,10 +47,7 @@ public class OAuth2RequestInterceptor extends OAuth2AuthorizationService impleme
/**
* Currently valid tokens
*/
protected OAuthTokens tokens = OAuthTokens.EMPTY;

private final HostPasswordStore store = PasswordStoreFactory.get();
protected final Host host;
private OAuthTokens tokens = OAuthTokens.EMPTY;

public OAuth2RequestInterceptor(final HttpClient client, final Host host, final LoginCallback prompt) throws LoginCanceledException {
this(client, host,
Expand All @@ -70,7 +64,6 @@ public OAuth2RequestInterceptor(final HttpClient client, final Host host, final
public OAuth2RequestInterceptor(final HttpClient client, final Host host, final String tokenServerUrl, final String authorizationServerUrl,
final String clientid, final String clientsecret, final List<String> scopes, final boolean pkce, final LoginCallback prompt) throws LoginCanceledException {
super(client, host, tokenServerUrl, authorizationServerUrl, clientid, clientsecret, scopes, pkce, prompt);
this.host = host;
}

@Override
Expand Down Expand Up @@ -109,19 +102,6 @@ public OAuthTokens refresh(final OAuthTokens previous) throws BackgroundExceptio
}
}

/**
* Save updated tokens in keychain
*
* @return Same tokens saved
*/
public OAuthTokens save(final OAuthTokens tokens) throws LocalAccessDeniedException {
if(log.isDebugEnabled()) {
log.debug(String.format("Save new tokens %s for %s", tokens, host));
}
store.save(host);
return tokens;
}

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
if(tokens.isExpired()) {
Expand Down