Skip to content

Commit

Permalink
Save OAuth tokens with port number from token URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Nov 22, 2023
1 parent 085698a commit 36ae079
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions core/src/main/java/ch/cyberduck/core/DefaultHostPasswordStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ public OAuthTokens findOAuthTokens(final Host bookmark) {
try {
final String expiry = this.getPassword(this.getOAuthHostname(bookmark), String.format("%s OAuth2 Token Expiry", prefix));
return (new OAuthTokens(
this.getPassword(bookmark.getProtocol().getScheme(), bookmark.getPort(), hostname,
this.getPassword(bookmark.getProtocol().getScheme(), this.getOAuthPort(bookmark), hostname,
String.format("%s OAuth2 Access Token", prefix)),
this.getPassword(bookmark.getProtocol().getScheme(), bookmark.getPort(), hostname,
this.getPassword(bookmark.getProtocol().getScheme(), this.getOAuthPort(bookmark), hostname,
String.format("%s OAuth2 Refresh Token", prefix)),
expiry != null ? Long.parseLong(expiry) : -1L,
this.getPassword(bookmark.getProtocol().getScheme(), bookmark.getPort(), hostname,
this.getPassword(bookmark.getProtocol().getScheme(), this.getOAuthPort(bookmark), hostname,
String.format("%s OIDC Id Token", prefix))));
}
catch(LocalAccessDeniedException e) {
Expand All @@ -174,6 +174,13 @@ protected String getOAuthHostname(final Host bookmark) {
return bookmark.getHostname();
}

protected int getOAuthPort(final Host bookmark) {
if(-1 != URI.create(bookmark.getProtocol().getOAuthTokenUrl()).getPort()) {
return URI.create(bookmark.getProtocol().getOAuthTokenUrl()).getPort();
}
return Scheme.valueOf(URI.create(bookmark.getProtocol().getOAuthTokenUrl()).getScheme()).getPort();
}

private String getOAuthPrefix(final Host bookmark) {
if(StringUtils.isNotBlank(bookmark.getCredentials().getUsername())) {
return String.format("%s (%s)", bookmark.getProtocol().getDescription(), bookmark.getCredentials().getUsername());
Expand Down Expand Up @@ -217,12 +224,12 @@ public void save(final Host bookmark) throws LocalAccessDeniedException {
final String prefix = this.getOAuthPrefix(bookmark);
if(StringUtils.isNotBlank(credentials.getOauth().getAccessToken())) {
this.addPassword(bookmark.getProtocol().getScheme(),
bookmark.getPort(), this.getOAuthHostname(bookmark),
this.getOAuthPort(bookmark), this.getOAuthHostname(bookmark),
String.format("%s OAuth2 Access Token", prefix), credentials.getOauth().getAccessToken());
}
if(StringUtils.isNotBlank(credentials.getOauth().getRefreshToken())) {
this.addPassword(bookmark.getProtocol().getScheme(),
bookmark.getPort(), this.getOAuthHostname(bookmark),
this.getOAuthPort(bookmark), this.getOAuthHostname(bookmark),
String.format("%s OAuth2 Refresh Token", prefix), credentials.getOauth().getRefreshToken());
}
// Save expiry
Expand All @@ -232,7 +239,7 @@ public void save(final Host bookmark) throws LocalAccessDeniedException {
}
if(StringUtils.isNotBlank(credentials.getOauth().getIdToken())) {
this.addPassword(bookmark.getProtocol().getScheme(),
bookmark.getPort(), this.getOAuthHostname(bookmark),
this.getOAuthPort(bookmark), this.getOAuthHostname(bookmark),
String.format("%s OIDC Id Token", prefix), credentials.getOauth().getIdToken());
}
}
Expand Down Expand Up @@ -264,19 +271,19 @@ public void delete(final Host bookmark) throws LocalAccessDeniedException {
if(protocol.isOAuthConfigurable()) {
final String prefix = this.getOAuthPrefix(bookmark);
if(StringUtils.isNotBlank(credentials.getOauth().getAccessToken())) {
this.deletePassword(protocol.getScheme(), bookmark.getPort(), this.getOAuthHostname(bookmark),
this.deletePassword(protocol.getScheme(), this.getOAuthPort(bookmark), this.getOAuthHostname(bookmark),
String.format("%s OAuth2 Access Token", prefix));
}
if(StringUtils.isNotBlank(credentials.getOauth().getRefreshToken())) {
this.deletePassword(protocol.getScheme(), bookmark.getPort(), this.getOAuthHostname(bookmark),
this.deletePassword(protocol.getScheme(), this.getOAuthPort(bookmark), this.getOAuthHostname(bookmark),
String.format("%s OAuth2 Refresh Token", prefix));
}
// Save expiry
if(credentials.getOauth().getExpiryInMilliseconds() != null) {
this.deletePassword(this.getOAuthHostname(bookmark), String.format("%s OAuth2 Token Expiry", prefix));
}
if(StringUtils.isNotBlank(credentials.getOauth().getIdToken())) {
this.deletePassword(protocol.getScheme(), bookmark.getPort(), this.getOAuthHostname(bookmark),
this.deletePassword(protocol.getScheme(), this.getOAuthPort(bookmark), this.getOAuthHostname(bookmark),
String.format("%s OIDC Id Token", prefix));
}
}
Expand Down

0 comments on commit 36ae079

Please sign in to comment.