|
16 | 16 | import java.security.spec.InvalidKeySpecException; |
17 | 17 | import java.time.temporal.ChronoUnit; |
18 | 18 | import java.util.Date; |
19 | | -import okhttp3.HttpUrl; |
20 | | -import okhttp3.OkHttpClient; |
| 19 | +import okhttp3.*; |
21 | 20 | import okhttp3.mockwebserver.MockResponse; |
22 | 21 | import okhttp3.mockwebserver.MockWebServer; |
23 | 22 | import org.junit.jupiter.api.AfterEach; |
@@ -62,6 +61,9 @@ class KeyFlowAuthenticatorTest { |
62 | 61 | + "h/9afEtu5aUE/m+1vGBoH8z1\n" |
63 | 62 | + "-----END PRIVATE KEY-----\n"; |
64 | 63 |
|
| 64 | + private static final Request mockRequest = |
| 65 | + new Request.Builder().url("https://stackit.com").get().build(); |
| 66 | + |
65 | 67 | private ServiceAccountKey createDummyServiceAccount() { |
66 | 68 | ServiceAccountCredentials credentials = |
67 | 69 | new ServiceAccountCredentials("aud", "iss", "kid", PRIVATE_KEY, "sub"); |
@@ -270,4 +272,96 @@ void createAccessTokenWithRefreshTokenResponse200WithEmptyBodyThrowsException() |
270 | 272 | assertThrows( |
271 | 273 | JsonSyntaxException.class, keyFlowAuthenticator::createAccessTokenWithRefreshToken); |
272 | 274 | } |
| 275 | + |
| 276 | + @Test |
| 277 | + @DisplayName("authenticator sets Authorization header") |
| 278 | + void authenticatorSetsAuthorizationHeaderIfNotAuthenticated() |
| 279 | + throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { |
| 280 | + // Setup mockServer |
| 281 | + final String authorizationHeader = "Authorization"; |
| 282 | + KeyFlowAuthenticator.KeyFlowTokenResponse mockResponse = mockResponseBody(false); |
| 283 | + MockResponse mockedResponse = |
| 284 | + new MockResponse() |
| 285 | + .setResponseCode(200) |
| 286 | + .setBody(new Gson().toJson(mockResponse)) |
| 287 | + .addHeader( |
| 288 | + "Content-type", |
| 289 | + "application/json"); // mock response for KeyFlow authentication |
| 290 | + // with mocked access token |
| 291 | + mockWebServer.enqueue(mockedResponse); |
| 292 | + HttpUrl url = mockWebServer.url(MOCK_WEBSERVER_PATH); |
| 293 | + |
| 294 | + // Set unauthorized request |
| 295 | + Response unauthorizedRequest = |
| 296 | + new Response.Builder() |
| 297 | + .request(mockRequest) |
| 298 | + .code(401) |
| 299 | + .message("Unauthorized") |
| 300 | + .protocol(Protocol.HTTP_2) |
| 301 | + .build(); |
| 302 | + |
| 303 | + // Config |
| 304 | + CoreConfiguration cfg = |
| 305 | + new CoreConfiguration().tokenCustomUrl(url.toString()); // Use mockWebServer |
| 306 | + |
| 307 | + // Check if "Authorization" header is unset |
| 308 | + assertNull(unauthorizedRequest.request().header(authorizationHeader)); |
| 309 | + |
| 310 | + // Prepare keyFlowAuthenticator |
| 311 | + KeyFlowAuthenticator keyFlowAuthenticator = |
| 312 | + new KeyFlowAuthenticator(httpClient, cfg, createDummyServiceAccount()); |
| 313 | + // authenticator creates new access token and sets it the Authorization header |
| 314 | + Request newRequest = keyFlowAuthenticator.authenticate(null, unauthorizedRequest); |
| 315 | + |
| 316 | + // Check if new request is not null |
| 317 | + assertNotNull(newRequest); |
| 318 | + // Check if the "Authorization" Header is set |
| 319 | + assertNotNull(newRequest.header(authorizationHeader)); |
| 320 | + } |
| 321 | + |
| 322 | + @Test |
| 323 | + @DisplayName("Authenticator returns null when already authenticated") |
| 324 | + void authenticatorReturnsNullWhenAlreadyAuthenticated() |
| 325 | + throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { |
| 326 | + // Setup mockServer |
| 327 | + final String authorizationHeader = "Authorization"; |
| 328 | + KeyFlowAuthenticator.KeyFlowTokenResponse mockResponse = mockResponseBody(false); |
| 329 | + MockResponse mockedResponse = |
| 330 | + new MockResponse() |
| 331 | + .setResponseCode(200) |
| 332 | + .setBody(new Gson().toJson(mockResponse)) |
| 333 | + .addHeader( |
| 334 | + "Content-type", |
| 335 | + "application/json"); // mock response for KeyFlow authentication |
| 336 | + // with mocked access token |
| 337 | + mockWebServer.enqueue(mockedResponse); |
| 338 | + HttpUrl url = mockWebServer.url(MOCK_WEBSERVER_PATH); |
| 339 | + |
| 340 | + // Set unauthorized request |
| 341 | + Response unauthorizedRequest = |
| 342 | + new Response.Builder() |
| 343 | + .request( |
| 344 | + mockRequest |
| 345 | + .newBuilder() |
| 346 | + .addHeader(authorizationHeader, "<my-access-token>") |
| 347 | + .build()) |
| 348 | + .code(401) |
| 349 | + .message("Unauthorized") |
| 350 | + .protocol(Protocol.HTTP_2) |
| 351 | + .build(); // Unauthorized request |
| 352 | + |
| 353 | + // Config |
| 354 | + CoreConfiguration cfg = |
| 355 | + new CoreConfiguration().tokenCustomUrl(url.toString()); // Use mockWebServer |
| 356 | + |
| 357 | + // Check if "Authorization" header is set |
| 358 | + assertNotNull(unauthorizedRequest.request().header(authorizationHeader)); |
| 359 | + |
| 360 | + // Prepare keyFlowAuthenticator |
| 361 | + KeyFlowAuthenticator keyFlowAuthenticator = |
| 362 | + new KeyFlowAuthenticator(httpClient, cfg, createDummyServiceAccount()); |
| 363 | + |
| 364 | + // Authenticator returns no new request, because "Authorization" header was already set |
| 365 | + assertNull(keyFlowAuthenticator.authenticate(null, unauthorizedRequest)); |
| 366 | + } |
273 | 367 | } |
0 commit comments