|
12 | 12 | import java.io.IOException; |
13 | 13 | import java.net.MalformedURLException; |
14 | 14 | import java.net.URL; |
| 15 | +import java.util.Arrays; |
15 | 16 | import java.util.HashMap; |
| 17 | +import java.util.List; |
16 | 18 | import java.util.Map; |
17 | 19 | import java.util.stream.Stream; |
| 20 | +import org.junit.jupiter.api.Test; |
18 | 21 | import org.junit.jupiter.params.ParameterizedTest; |
19 | 22 | import org.junit.jupiter.params.provider.MethodSource; |
20 | 23 | import org.mockito.Mockito; |
@@ -335,4 +338,114 @@ void testTokenSource(TestCase testCase) { |
335 | 338 | verify(testCase.idTokenSource, atLeastOnce()).getIDToken(testCase.expectedAudience); |
336 | 339 | } |
337 | 340 | } |
| 341 | + |
| 342 | + // Scope-specific tests for WIF/OIDC token exchange |
| 343 | + |
| 344 | + @Test |
| 345 | + void testDefaultScopesInTokenExchange() throws IOException { |
| 346 | + // Verify default "all-apis" scope is used when no scopes specified |
| 347 | + OpenIDConnectEndpoints testEndpoints = |
| 348 | + new OpenIDConnectEndpoints(TEST_TOKEN_ENDPOINT, TEST_AUTHORIZATION_ENDPOINT); |
| 349 | + IDTokenSource testIdTokenSource = Mockito.mock(IDTokenSource.class); |
| 350 | + when(testIdTokenSource.getIDToken(any())).thenReturn(new IDToken(TEST_ID_TOKEN)); |
| 351 | + |
| 352 | + Map<String, Object> successResponse = new HashMap<>(); |
| 353 | + successResponse.put("access_token", TOKEN); |
| 354 | + successResponse.put("token_type", TOKEN_TYPE); |
| 355 | + successResponse.put("expires_in", EXPIRES_IN); |
| 356 | + String successJson = new ObjectMapper().writeValueAsString(successResponse); |
| 357 | + |
| 358 | + // Expected request with default scope |
| 359 | + Map<String, String> formParams = new HashMap<>(); |
| 360 | + formParams.put("client_id", TEST_CLIENT_ID); |
| 361 | + formParams.put("subject_token", TEST_ID_TOKEN); |
| 362 | + formParams.put("subject_token_type", "urn:ietf:params:oauth:token-type:jwt"); |
| 363 | + formParams.put("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"); |
| 364 | + formParams.put("scope", "all-apis"); |
| 365 | + FormRequest expectedRequest = new FormRequest(TEST_TOKEN_ENDPOINT, formParams); |
| 366 | + |
| 367 | + HttpClient mockHttpClient = createMockHttpClient(expectedRequest, 200, successJson); |
| 368 | + |
| 369 | + DatabricksOAuthTokenSource tokenSource = |
| 370 | + new DatabricksOAuthTokenSource.Builder( |
| 371 | + TEST_CLIENT_ID, TEST_HOST, testEndpoints, testIdTokenSource, mockHttpClient) |
| 372 | + .scopes(Arrays.asList("all-apis")) |
| 373 | + .build(); |
| 374 | + |
| 375 | + Token token = tokenSource.getToken(); |
| 376 | + assertEquals(TOKEN, token.getAccessToken()); |
| 377 | + } |
| 378 | + |
| 379 | + @Test |
| 380 | + void testCustomScopesInTokenExchange() throws IOException { |
| 381 | + // Verify custom scopes are correctly passed to token exchange |
| 382 | + OpenIDConnectEndpoints testEndpoints = |
| 383 | + new OpenIDConnectEndpoints(TEST_TOKEN_ENDPOINT, TEST_AUTHORIZATION_ENDPOINT); |
| 384 | + IDTokenSource testIdTokenSource = Mockito.mock(IDTokenSource.class); |
| 385 | + when(testIdTokenSource.getIDToken(any())).thenReturn(new IDToken(TEST_ID_TOKEN)); |
| 386 | + |
| 387 | + Map<String, Object> successResponse = new HashMap<>(); |
| 388 | + successResponse.put("access_token", TOKEN); |
| 389 | + successResponse.put("token_type", TOKEN_TYPE); |
| 390 | + successResponse.put("expires_in", EXPIRES_IN); |
| 391 | + String successJson = new ObjectMapper().writeValueAsString(successResponse); |
| 392 | + |
| 393 | + // Expected request with custom scope |
| 394 | + Map<String, String> formParams = new HashMap<>(); |
| 395 | + formParams.put("client_id", TEST_CLIENT_ID); |
| 396 | + formParams.put("subject_token", TEST_ID_TOKEN); |
| 397 | + formParams.put("subject_token_type", "urn:ietf:params:oauth:token-type:jwt"); |
| 398 | + formParams.put("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"); |
| 399 | + formParams.put("scope", "unity-catalog:read"); |
| 400 | + FormRequest expectedRequest = new FormRequest(TEST_TOKEN_ENDPOINT, formParams); |
| 401 | + |
| 402 | + HttpClient mockHttpClient = createMockHttpClient(expectedRequest, 200, successJson); |
| 403 | + |
| 404 | + DatabricksOAuthTokenSource tokenSource = |
| 405 | + new DatabricksOAuthTokenSource.Builder( |
| 406 | + TEST_CLIENT_ID, TEST_HOST, testEndpoints, testIdTokenSource, mockHttpClient) |
| 407 | + .scopes(Arrays.asList("unity-catalog:read")) |
| 408 | + .build(); |
| 409 | + |
| 410 | + Token token = tokenSource.getToken(); |
| 411 | + assertEquals(TOKEN, token.getAccessToken()); |
| 412 | + } |
| 413 | + |
| 414 | + @Test |
| 415 | + void testMultipleScopesJoinedWithSpaces() throws IOException { |
| 416 | + // Verify multiple scopes are joined with spaces in token exchange request |
| 417 | + OpenIDConnectEndpoints testEndpoints = |
| 418 | + new OpenIDConnectEndpoints(TEST_TOKEN_ENDPOINT, TEST_AUTHORIZATION_ENDPOINT); |
| 419 | + IDTokenSource testIdTokenSource = Mockito.mock(IDTokenSource.class); |
| 420 | + when(testIdTokenSource.getIDToken(any())).thenReturn(new IDToken(TEST_ID_TOKEN)); |
| 421 | + |
| 422 | + Map<String, Object> successResponse = new HashMap<>(); |
| 423 | + successResponse.put("access_token", TOKEN); |
| 424 | + successResponse.put("token_type", TOKEN_TYPE); |
| 425 | + successResponse.put("expires_in", EXPIRES_IN); |
| 426 | + String successJson = new ObjectMapper().writeValueAsString(successResponse); |
| 427 | + |
| 428 | + // Scopes should be sorted before being passed (sorted in DatabricksConfig.resolve()) |
| 429 | + List<String> scopes = Arrays.asList("clusters:read", "jobs:read", "mlflow:read"); |
| 430 | + |
| 431 | + // Expected request with multiple scopes joined by spaces |
| 432 | + Map<String, String> formParams = new HashMap<>(); |
| 433 | + formParams.put("client_id", TEST_CLIENT_ID); |
| 434 | + formParams.put("subject_token", TEST_ID_TOKEN); |
| 435 | + formParams.put("subject_token_type", "urn:ietf:params:oauth:token-type:jwt"); |
| 436 | + formParams.put("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange"); |
| 437 | + formParams.put("scope", "clusters:read jobs:read mlflow:read"); |
| 438 | + FormRequest expectedRequest = new FormRequest(TEST_TOKEN_ENDPOINT, formParams); |
| 439 | + |
| 440 | + HttpClient mockHttpClient = createMockHttpClient(expectedRequest, 200, successJson); |
| 441 | + |
| 442 | + DatabricksOAuthTokenSource tokenSource = |
| 443 | + new DatabricksOAuthTokenSource.Builder( |
| 444 | + TEST_CLIENT_ID, TEST_HOST, testEndpoints, testIdTokenSource, mockHttpClient) |
| 445 | + .scopes(scopes) |
| 446 | + .build(); |
| 447 | + |
| 448 | + Token token = tokenSource.getToken(); |
| 449 | + assertEquals(TOKEN, token.getAccessToken()); |
| 450 | + } |
338 | 451 | } |
0 commit comments