Skip to content

Commit 5e1cca7

Browse files
committed
fmt
1 parent f485df5 commit 5e1cca7

File tree

3 files changed

+61
-30
lines changed

3 files changed

+61
-30
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public OAuthHeaderFactory configure(DatabricksConfig config) {
5959
String clientId = OAuthClientUtils.resolveClientId(config);
6060
String clientSecret = OAuthClientUtils.resolveClientSecret(config);
6161
OpenIDConnectEndpoints oidcEndpoints = null;
62-
try {
62+
try {
6363
oidcEndpoints = OAuthClientUtils.resolveOidcEndpoints(config);
6464
} catch (IOException e) {
6565
LOGGER.error("Failed to resolve OIDC endpoints: {}", e.getMessage());
@@ -132,7 +132,11 @@ protected List<String> getScopes(DatabricksConfig config, OpenIDConnectEndpoints
132132
}
133133

134134
CachedTokenSource performBrowserAuth(
135-
DatabricksConfig config, String clientId, String clientSecret, TokenCache tokenCache, OpenIDConnectEndpoints oidcEndpoints)
135+
DatabricksConfig config,
136+
String clientId,
137+
String clientSecret,
138+
TokenCache tokenCache,
139+
OpenIDConnectEndpoints oidcEndpoints)
136140
throws IOException {
137141
LOGGER.debug("Performing browser authentication");
138142

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,30 @@ public static String resolveClientSecret(DatabricksConfig config) {
4444
}
4545

4646
/**
47-
* Resolves the OAuth OIDC endpoints from the configuration. Prioritizes regular OIDC endpoints, then Azure OIDC endpoints.
48-
* If the client ID and client secret are provided, the OIDC endpoints are fetched from the discovery URL.
49-
* If the Azure client ID and client secret are provided, the OIDC endpoints are fetched from the Azure AD endpoint.
50-
* If no client ID and client secret are provided, the OIDC endpoints are fetched from the default OIDC endpoints.
47+
* Resolves the OAuth OIDC endpoints from the configuration. Prioritizes regular OIDC endpoints,
48+
* then Azure OIDC endpoints. If the client ID and client secret are provided, the OIDC endpoints
49+
* are fetched from the discovery URL. If the Azure client ID and client secret are provided, the
50+
* OIDC endpoints are fetched from the Azure AD endpoint. If no client ID and client secret are
51+
* provided, the OIDC endpoints are fetched from the default OIDC endpoints.
52+
*
5153
* @param config The Databricks configuration
5254
* @return The resolved OIDC endpoints
5355
* @throws IOException if the OIDC endpoints cannot be resolved
5456
*/
55-
public static OpenIDConnectEndpoints resolveOidcEndpoints(DatabricksConfig config) throws IOException {
57+
public static OpenIDConnectEndpoints resolveOidcEndpoints(DatabricksConfig config)
58+
throws IOException {
5659
if (config.getClientId() != null && config.getClientSecret() != null) {
5760
return config.getOidcEndpoints();
58-
} else if (config.getAzureClientId() != null && config.getAzureClientSecret() != null) {
59-
Request request = new Request("GET", config.getHost() + "/oidc/oauth2/v2.0/authorize");
60-
request.setRedirectionBehavior(false);
61-
Response resp = config.getHttpClient().execute(request);
62-
String realAuthUrl = resp.getFirstHeader("location");
63-
if (realAuthUrl == null) {
64-
return null;
65-
}
66-
return new OpenIDConnectEndpoints(
67-
realAuthUrl.replaceAll("/authorize", "/token"), realAuthUrl);
61+
} else if (config.getAzureClientId() != null && config.getAzureClientSecret() != null) {
62+
Request request = new Request("GET", config.getHost() + "/oidc/oauth2/v2.0/authorize");
63+
request.setRedirectionBehavior(false);
64+
Response resp = config.getHttpClient().execute(request);
65+
String realAuthUrl = resp.getFirstHeader("location");
66+
if (realAuthUrl == null) {
67+
return null;
68+
}
69+
return new OpenIDConnectEndpoints(
70+
realAuthUrl.replaceAll("/authorize", "/token"), realAuthUrl);
6871
}
6972
return config.getOidcEndpoints();
7073
}

databricks-sdk-java/src/test/java/com/databricks/sdk/core/oauth/ExternalBrowserCredentialsProviderTest.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,12 @@ void cacheWithValidNonRefreshableTokenTest() throws IOException {
365365

366366
// Verify performBrowserAuth was NOT called.
367367
Mockito.verify(provider, Mockito.never())
368-
.performBrowserAuth(any(DatabricksConfig.class), any(), any(), any(TokenCache.class), any(OpenIDConnectEndpoints.class));
368+
.performBrowserAuth(
369+
any(DatabricksConfig.class),
370+
any(),
371+
any(),
372+
any(TokenCache.class),
373+
any(OpenIDConnectEndpoints.class));
369374

370375
// Verify no token was saved (we're using the cached one as-is).
371376
Mockito.verify(mockTokenCache, Mockito.never()).save(any(Token.class));
@@ -433,8 +438,7 @@ void cacheWithInvalidAccessTokenValidRefreshTest() throws IOException {
433438
any(String.class),
434439
any(String.class),
435440
any(TokenCache.class),
436-
any(OpenIDConnectEndpoints.class)
437-
);
441+
any(OpenIDConnectEndpoints.class));
438442

439443
// Verify token was saved back to cache
440444
Mockito.verify(mockTokenCache, Mockito.times(1)).save(any(Token.class));
@@ -512,7 +516,12 @@ void cacheWithInvalidAccessTokenRefreshFailingTest() throws IOException {
512516
Mockito.spy(new ExternalBrowserCredentialsProvider(mockTokenCache));
513517
Mockito.doReturn(cachedTokenSource)
514518
.when(provider)
515-
.performBrowserAuth(any(DatabricksConfig.class), any(), any(), any(TokenCache.class), any(OpenIDConnectEndpoints.class));
519+
.performBrowserAuth(
520+
any(DatabricksConfig.class),
521+
any(),
522+
any(),
523+
any(TokenCache.class),
524+
any(OpenIDConnectEndpoints.class));
516525

517526
// Spy on the config to inject the endpoints
518527
DatabricksConfig spyConfig = Mockito.spy(config);
@@ -531,7 +540,12 @@ void cacheWithInvalidAccessTokenRefreshFailingTest() throws IOException {
531540

532541
// Verify performBrowserAuth was called since refresh failed
533542
Mockito.verify(provider, Mockito.times(1))
534-
.performBrowserAuth(any(DatabricksConfig.class), any(), any(), any(TokenCache.class), any(OpenIDConnectEndpoints.class));
543+
.performBrowserAuth(
544+
any(DatabricksConfig.class),
545+
any(),
546+
any(),
547+
any(TokenCache.class),
548+
any(OpenIDConnectEndpoints.class));
535549

536550
// Verify token was saved after browser auth (for the new token)
537551
Mockito.verify(mockTokenCache, Mockito.times(1)).save(any(Token.class));
@@ -584,7 +598,12 @@ void cacheWithInvalidTokensTest() throws IOException {
584598
Mockito.spy(new ExternalBrowserCredentialsProvider(mockTokenCache));
585599
Mockito.doReturn(cachedTokenSource)
586600
.when(provider)
587-
.performBrowserAuth(any(DatabricksConfig.class), any(), any(), any(TokenCache.class), any(OpenIDConnectEndpoints.class));
601+
.performBrowserAuth(
602+
any(DatabricksConfig.class),
603+
any(),
604+
any(),
605+
any(TokenCache.class),
606+
any(OpenIDConnectEndpoints.class));
588607

589608
// Spy on the config to inject the endpoints
590609
OpenIDConnectEndpoints endpoints =
@@ -606,7 +625,12 @@ void cacheWithInvalidTokensTest() throws IOException {
606625

607626
// Verify performBrowserAuth was called since we had an invalid token
608627
Mockito.verify(provider, Mockito.times(1))
609-
.performBrowserAuth(any(DatabricksConfig.class), any(), any(), any(TokenCache.class), any(OpenIDConnectEndpoints.class));
628+
.performBrowserAuth(
629+
any(DatabricksConfig.class),
630+
any(),
631+
any(),
632+
any(TokenCache.class),
633+
any(OpenIDConnectEndpoints.class));
610634

611635
// Verify token was saved after browser auth (for the new token)
612636
Mockito.verify(mockTokenCache, Mockito.times(1)).save(any(Token.class));
@@ -673,10 +697,7 @@ void externalBrowserAuthWithAzureClientIdTest() throws IOException {
673697
// Create valid token for browser auth
674698
Token browserAuthToken =
675699
new Token(
676-
"azure_access_token",
677-
"Bearer",
678-
"azure_refresh_token",
679-
Instant.now().plusSeconds(3600));
700+
"azure_access_token", "Bearer", "azure_refresh_token", Instant.now().plusSeconds(3600));
680701

681702
// Create token source
682703
SessionCredentialsTokenSource browserAuthTokenSource =
@@ -742,8 +763,11 @@ void externalBrowserAuthWithAzureClientIdTest() throws IOException {
742763
// Verify the captured endpoints match what we expect for Azure
743764
OpenIDConnectEndpoints capturedEndpoints = endpointsCaptor.getValue();
744765
assertNotNull(capturedEndpoints);
745-
assertEquals("https://test.azuredatabricks.net/oidc/v1/token", capturedEndpoints.getTokenEndpoint());
746-
assertEquals("https://test.azuredatabricks.net/oidc/v1/authorize", capturedEndpoints.getAuthorizationEndpoint());
766+
assertEquals(
767+
"https://test.azuredatabricks.net/oidc/v1/token", capturedEndpoints.getTokenEndpoint());
768+
assertEquals(
769+
"https://test.azuredatabricks.net/oidc/v1/authorize",
770+
capturedEndpoints.getAuthorizationEndpoint());
747771

748772
// Verify token was saved
749773
Mockito.verify(mockTokenCache, Mockito.times(1)).save(any(Token.class));

0 commit comments

Comments
 (0)