Skip to content

Commit b57f6e2

Browse files
fmt
1 parent e8080b3 commit b57f6e2

File tree

7 files changed

+315
-272
lines changed

7 files changed

+315
-272
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/DatabricksConfig.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.databricks.sdk.core.http.Request;
66
import com.databricks.sdk.core.http.Response;
77
import com.databricks.sdk.core.oauth.OpenIDConnectEndpoints;
8-
import com.databricks.sdk.core.oauth.Token;
98
import com.databricks.sdk.core.oauth.TokenCache;
109
import com.databricks.sdk.core.utils.Cloud;
1110
import com.databricks.sdk.core.utils.Environment;
@@ -41,15 +40,18 @@ public class DatabricksConfig {
4140
private String redirectUrl;
4241

4342
/**
44-
* The passphrase used to encrypt the OAuth token cache.
45-
* This is optional and only used with token caching.
43+
* The passphrase used to encrypt the OAuth token cache. This is optional and only used with token
44+
* caching.
4645
*/
47-
@ConfigAttribute(env = "DATABRICKS_OAUTH_TOKEN_CACHE_PASSPHRASE", auth = "oauth", sensitive = true)
46+
@ConfigAttribute(
47+
env = "DATABRICKS_OAUTH_TOKEN_CACHE_PASSPHRASE",
48+
auth = "oauth",
49+
sensitive = true)
4850
private String oAuthTokenCachePassphrase;
4951

5052
/**
51-
* Controls whether OAuth token caching is enabled.
52-
* When set to false, tokens will not be cached or loaded from cache.
53+
* Controls whether OAuth token caching is enabled. When set to false, tokens will not be cached
54+
* or loaded from cache.
5355
*/
5456
@ConfigAttribute(env = "DATABRICKS_OAUTH_TOKEN_CACHE_ENABLED", auth = "oauth")
5557
private Boolean isTokenCacheEnabled;
@@ -699,9 +701,8 @@ public DatabricksConfig setOAuthTokenCachePassphrase(String oAuthPassphrase) {
699701
}
700702

701703
/**
702-
* Gets whether OAuth token caching is enabled.
703-
* Default is true.
704-
*
704+
* Gets whether OAuth token caching is enabled. Default is true.
705+
*
705706
* @return true if token caching is enabled, false otherwise
706707
*/
707708
public boolean isTokenCacheEnabled() {
@@ -710,7 +711,7 @@ public boolean isTokenCacheEnabled() {
710711

711712
/**
712713
* Sets whether OAuth token caching is enabled.
713-
*
714+
*
714715
* @param enabled true to enable token caching, false to disable
715716
* @return this config instance
716717
*/
@@ -720,25 +721,30 @@ public DatabricksConfig setTokenCacheEnabled(boolean enabled) {
720721
}
721722

722723
/**
723-
* Gets the default OAuth redirect URL.
724-
* If one is not provided explicitly, uses http://localhost:8080/callback
725-
*
724+
* Gets the default OAuth redirect URL. If one is not provided explicitly, uses
725+
* http://localhost:8080/callback
726+
*
726727
* @return The OAuth redirect URL to use
727728
*/
728729
public String getEffectiveOAuthRedirectUrl() {
729730
return redirectUrl != null ? redirectUrl : "http://localhost:8080/callback";
730731
}
731-
732+
732733
/**
733-
* Gets the TokenCache instance for the current configuration.
734-
* Creates it if it doesn't exist yet.
734+
* Gets the TokenCache instance for the current configuration. Creates it if it doesn't exist yet.
735735
* When token caching is disabled, the TokenCache will be created but operations will be no-ops.
736-
*
736+
*
737737
* @return A TokenCache instance for the current host, client ID, and scopes
738738
*/
739739
public synchronized TokenCache getTokenCache() {
740740
if (tokenCache == null) {
741-
tokenCache = new TokenCache(getHost(), getClientId(), getScopes(), getOAuthTokenCachePassphrase(), isTokenCacheEnabled());
741+
tokenCache =
742+
new TokenCache(
743+
getHost(),
744+
getClientId(),
745+
getScopes(),
746+
getOAuthTokenCachePassphrase(),
747+
isTokenCacheEnabled());
742748
}
743749
return tokenCache;
744750
}

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProvider.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
/**
1212
* A {@code CredentialsProvider} which implements the Authorization Code + PKCE flow by opening a
13-
* browser for the user to authorize the application. When cache support is enabled with
14-
* {@link DatabricksConfig#setOAuthTokenCachePassphrase} and {@link DatabricksConfig#setTokenCacheEnabled(boolean)},
15-
* tokens will be cached to avoid repeated authentication.
13+
* browser for the user to authorize the application. When cache support is enabled with {@link
14+
* DatabricksConfig#setOAuthTokenCachePassphrase} and {@link
15+
* DatabricksConfig#setTokenCacheEnabled(boolean)}, tokens will be cached to avoid repeated
16+
* authentication.
1617
*/
1718
public class ExternalBrowserCredentialsProvider implements CredentialsProvider {
18-
private static final Logger LOGGER = LoggerFactory.getLogger(ExternalBrowserCredentialsProvider.class);
19+
private static final Logger LOGGER =
20+
LoggerFactory.getLogger(ExternalBrowserCredentialsProvider.class);
1921

2022
@Override
2123
public String authType() {
@@ -24,10 +26,12 @@ public String authType() {
2426

2527
@Override
2628
public HeaderFactory configure(DatabricksConfig config) {
27-
if (config.getHost() == null || config.getClientId() == null || !config.getAuthType().equals("external-browser")) {
29+
if (config.getHost() == null
30+
|| config.getClientId() == null
31+
|| !config.getAuthType().equals("external-browser")) {
2832
return null;
2933
}
30-
34+
3135
try {
3236
// Get the token cache from config
3337
TokenCache tokenCache = config.getTokenCache();
@@ -36,18 +40,19 @@ public HeaderFactory configure(DatabricksConfig config) {
3640
Token cachedToken = tokenCache.load();
3741
if (cachedToken != null && cachedToken.getRefreshToken() != null) {
3842
LOGGER.debug("Found cached token for {}:{}", config.getHost(), config.getClientId());
39-
43+
4044
try {
4145
// Create SessionCredentials with the cached token and try to refresh if needed
42-
SessionCredentials cachedCreds = new SessionCredentials.Builder()
43-
.withToken(cachedToken)
44-
.withHttpClient(config.getHttpClient())
45-
.withClientId(config.getClientId())
46-
.withClientSecret(config.getClientSecret())
47-
.withTokenUrl(config.getOidcEndpoints().getTokenEndpoint())
48-
.withRedirectUrl(config.getEffectiveOAuthRedirectUrl())
49-
.build();
50-
46+
SessionCredentials cachedCreds =
47+
new SessionCredentials.Builder()
48+
.withToken(cachedToken)
49+
.withHttpClient(config.getHttpClient())
50+
.withClientId(config.getClientId())
51+
.withClientSecret(config.getClientSecret())
52+
.withTokenUrl(config.getOidcEndpoints().getTokenEndpoint())
53+
.withRedirectUrl(config.getEffectiveOAuthRedirectUrl())
54+
.build();
55+
5156
LOGGER.debug("Using cached token, will immediately refresh");
5257
cachedCreds.token = cachedCreds.refresh();
5358
tokenCache.save(cachedToken);
@@ -67,7 +72,7 @@ public HeaderFactory configure(DatabricksConfig config) {
6772
return null;
6873
}
6974
}
70-
75+
7176
SessionCredentials performBrowserAuth(DatabricksConfig config) throws IOException {
7277
LOGGER.debug("Performing browser authentication");
7378
OAuthClient client = new OAuthClient(config);

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/SessionCredentials.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ protected Token refresh() {
119119
// cross-origin requests
120120
headers.put("Origin", redirectUrl);
121121
}
122-
Token newToken = retrieveToken(
123-
hc, clientId, clientSecret, tokenUrl, params, headers, AuthParameterPosition.BODY);
122+
Token newToken =
123+
retrieveToken(
124+
hc, clientId, clientSecret, tokenUrl, params, headers, AuthParameterPosition.BODY);
124125

125126
// Save the refreshed token directly to cache
126127
if (tokenCache != null) {
@@ -131,7 +132,7 @@ protected Token refresh() {
131132
LOGGER.warn("Failed to save token to cache: {}", e.getMessage());
132133
}
133134
}
134-
135+
135136
return newToken;
136137
}
137138
}

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/Token.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public Token(
4040
/** Constructor for refreshable tokens. */
4141
@JsonCreator
4242
public Token(
43-
@JsonProperty("accessToken") String accessToken,
44-
@JsonProperty("tokenType") String tokenType,
45-
@JsonProperty("refreshToken") String refreshToken,
43+
@JsonProperty("accessToken") String accessToken,
44+
@JsonProperty("tokenType") String tokenType,
45+
@JsonProperty("refreshToken") String refreshToken,
4646
@JsonProperty("expiry") LocalDateTime expiry) {
4747
this(accessToken, tokenType, refreshToken, expiry, new SystemClockSupplier());
4848
}

0 commit comments

Comments
 (0)