|
6 | 6 | import com.fasterxml.jackson.annotation.JsonProperty; |
7 | 7 | import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; |
8 | 8 | import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
| 9 | +import dev.openfga.sdk.api.configuration.ApiToken; |
9 | 10 | import dev.openfga.sdk.api.configuration.ClientConfiguration; |
| 11 | +import dev.openfga.sdk.api.configuration.ClientCredentials; |
| 12 | +import dev.openfga.sdk.api.configuration.Credentials; |
10 | 13 | import dev.openfga.sdk.errors.FgaError; |
11 | 14 | import dev.openfga.sdk.errors.FgaInvalidParameterException; |
12 | 15 | import java.util.HashMap; |
@@ -382,6 +385,78 @@ public void rawApi_throwsExceptionForNullResponseType() throws Exception { |
382 | 385 | assertThrows(IllegalArgumentException.class, () -> client.apiExecutor().send(request, null)); |
383 | 386 | } |
384 | 387 |
|
| 388 | + @Test |
| 389 | + public void rawApi_appliesApiTokenAuthHeader() throws Exception { |
| 390 | + String apiToken = "static-api-token"; |
| 391 | + stubFor(get(urlEqualTo("/stores/" + DEFAULT_STORE_ID + "/experimental-feature")) |
| 392 | + .withHeader("Authorization", equalTo("Bearer " + apiToken)) |
| 393 | + .willReturn(aResponse() |
| 394 | + .withStatus(200) |
| 395 | + .withHeader("Content-Type", "application/json") |
| 396 | + .withBody("{\"success\":true,\"count\":0,\"message\":\"OK\"}"))); |
| 397 | + |
| 398 | + ClientConfiguration config = new ClientConfiguration() |
| 399 | + .apiUrl(fgaApiUrl) |
| 400 | + .storeId(DEFAULT_STORE_ID) |
| 401 | + .credentials(new Credentials(new ApiToken(apiToken))); |
| 402 | + OpenFgaClient client = new OpenFgaClient(config); |
| 403 | + |
| 404 | + ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder(HttpMethod.GET, EXPERIMENTAL_ENDPOINT) |
| 405 | + .pathParam("store_id", DEFAULT_STORE_ID) |
| 406 | + .build(); |
| 407 | + |
| 408 | + ApiResponse<ExperimentalResponse> response = |
| 409 | + client.apiExecutor().send(request, ExperimentalResponse.class).get(); |
| 410 | + |
| 411 | + assertEquals(200, response.getStatusCode()); |
| 412 | + verify(getRequestedFor(urlEqualTo("/stores/" + DEFAULT_STORE_ID + "/experimental-feature")) |
| 413 | + .withHeader("Authorization", equalTo("Bearer " + apiToken))); |
| 414 | + } |
| 415 | + |
| 416 | + @Test |
| 417 | + public void rawApi_appliesClientCredentialsAuthHeader() throws Exception { |
| 418 | + String clientId = "some-client-id"; |
| 419 | + String clientSecret = "some-client-secret"; |
| 420 | + String apiAudience = "some-audience"; |
| 421 | + String exchangedToken = "exchanged-access-token"; |
| 422 | + |
| 423 | + stubFor(post(urlEqualTo("/oauth/token")) |
| 424 | + .willReturn(aResponse() |
| 425 | + .withStatus(200) |
| 426 | + .withHeader("Content-Type", "application/json") |
| 427 | + .withBody(String.format("{\"access_token\":\"%s\",\"expires_in\":3600}", exchangedToken)))); |
| 428 | + stubFor(get(urlEqualTo("/stores/" + DEFAULT_STORE_ID + "/experimental-feature")) |
| 429 | + .withHeader("Authorization", equalTo("Bearer " + exchangedToken)) |
| 430 | + .willReturn(aResponse() |
| 431 | + .withStatus(200) |
| 432 | + .withHeader("Content-Type", "application/json") |
| 433 | + .withBody("{\"success\":true,\"count\":0,\"message\":\"OK\"}"))); |
| 434 | + |
| 435 | + ClientConfiguration config = new ClientConfiguration() |
| 436 | + .apiUrl(fgaApiUrl) |
| 437 | + .storeId(DEFAULT_STORE_ID) |
| 438 | + .credentials(new Credentials(new ClientCredentials() |
| 439 | + .clientId(clientId) |
| 440 | + .clientSecret(clientSecret) |
| 441 | + .apiAudience(apiAudience) |
| 442 | + .apiTokenIssuer(fgaApiUrl))); |
| 443 | + OpenFgaClient client = new OpenFgaClient(config); |
| 444 | + |
| 445 | + ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder(HttpMethod.GET, EXPERIMENTAL_ENDPOINT) |
| 446 | + .pathParam("store_id", DEFAULT_STORE_ID) |
| 447 | + .build(); |
| 448 | + |
| 449 | + ApiResponse<ExperimentalResponse> response = |
| 450 | + client.apiExecutor().send(request, ExperimentalResponse.class).get(); |
| 451 | + |
| 452 | + assertEquals(200, response.getStatusCode()); |
| 453 | + verify(postRequestedFor(urlEqualTo("/oauth/token")) |
| 454 | + .withRequestBody(containing("client_id=" + clientId)) |
| 455 | + .withRequestBody(containing("grant_type=client_credentials"))); |
| 456 | + verify(getRequestedFor(urlEqualTo("/stores/" + DEFAULT_STORE_ID + "/experimental-feature")) |
| 457 | + .withHeader("Authorization", equalTo("Bearer " + exchangedToken))); |
| 458 | + } |
| 459 | + |
385 | 460 | @Test |
386 | 461 | public void twoParamConstructor_shouldCreateWithOwnTelemetry() throws Exception { |
387 | 462 | // Verifies the backward-compatible 2-param constructor works |
|
0 commit comments