|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.core.http.policy; |
| 5 | + |
| 6 | +import com.azure.core.credential.AccessToken; |
| 7 | +import com.azure.core.credential.TokenCredential; |
| 8 | +import com.azure.core.http.HttpClient; |
| 9 | +import com.azure.core.http.HttpHeaderName; |
| 10 | +import com.azure.core.http.HttpHeaders; |
| 11 | +import com.azure.core.http.HttpMethod; |
| 12 | +import com.azure.core.http.HttpPipeline; |
| 13 | +import com.azure.core.http.HttpPipelineBuilder; |
| 14 | +import com.azure.core.http.HttpRequest; |
| 15 | +import com.azure.core.http.HttpResponse; |
| 16 | +import com.azure.core.http.MockHttpResponse; |
| 17 | +import com.azure.core.implementation.http.policy.AuthorizationChallengeParser; |
| 18 | +import com.azure.core.util.Context; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.Arguments; |
| 21 | +import org.junit.jupiter.params.provider.MethodSource; |
| 22 | +import reactor.core.publisher.Mono; |
| 23 | +import reactor.test.StepVerifier; |
| 24 | + |
| 25 | +import java.time.OffsetDateTime; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | +import java.util.stream.Stream; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | + |
| 33 | +public class BearerTokenAuthenticationPolicyTests { |
| 34 | + |
| 35 | + @ParameterizedTest |
| 36 | + @MethodSource("caeTestArguments") |
| 37 | + public void testDefaultCae(String challenge, int expectedStatusCode, String expectedClaims, String encodedClaims) { |
| 38 | + AtomicReference<String> claims = new AtomicReference<>(); |
| 39 | + AtomicInteger callCount = new AtomicInteger(); |
| 40 | + TokenCredential credential = getCaeTokenCredential(claims, callCount); |
| 41 | + BearerTokenAuthenticationPolicy policy = new BearerTokenAuthenticationPolicy(credential, "scope"); |
| 42 | + HttpClient client = getCaeHttpClient(challenge, callCount); |
| 43 | + |
| 44 | + HttpPipeline pipeline = new HttpPipelineBuilder().policies(policy).httpClient(client).build(); |
| 45 | + StepVerifier.create(pipeline.send(new HttpRequest(HttpMethod.GET, "https://localhost"))) |
| 46 | + .assertNext(response -> assertEquals(expectedStatusCode, response.getStatusCode())) |
| 47 | + .verifyComplete(); |
| 48 | + assertEquals(expectedClaims, claims.get()); |
| 49 | + |
| 50 | + if (expectedClaims != null) { |
| 51 | + String actualEncodedClaims = AuthorizationChallengeParser.getChallengeParameterFromResponse( |
| 52 | + new MockHttpResponse(null, 401, new HttpHeaders().add(HttpHeaderName.WWW_AUTHENTICATE, challenge)), |
| 53 | + "Bearer", "claims"); |
| 54 | + assertEquals(encodedClaims, actualEncodedClaims); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @ParameterizedTest |
| 59 | + @MethodSource("caeTestArguments") |
| 60 | + public void testDefaultCaeSync(String challenge, int expectedStatusCode, String expectedClaims, |
| 61 | + String encodedClaims) { |
| 62 | + AtomicReference<String> claims = new AtomicReference<>(); |
| 63 | + AtomicInteger callCount = new AtomicInteger(); |
| 64 | + |
| 65 | + TokenCredential credential = getCaeTokenCredential(claims, callCount); |
| 66 | + BearerTokenAuthenticationPolicy policy = new BearerTokenAuthenticationPolicy(credential, "scope"); |
| 67 | + HttpClient client = getCaeHttpClient(challenge, callCount); |
| 68 | + HttpPipeline pipeline = new HttpPipelineBuilder().policies(policy).httpClient(client).build(); |
| 69 | + |
| 70 | + try (HttpResponse response |
| 71 | + = pipeline.sendSync(new HttpRequest(HttpMethod.GET, "https://localhost"), Context.NONE)) { |
| 72 | + assertEquals(expectedStatusCode, response.getStatusCode()); |
| 73 | + } |
| 74 | + assertEquals(expectedClaims, claims.get()); |
| 75 | + |
| 76 | + if (expectedClaims != null) { |
| 77 | + String actualEncodedClaims = AuthorizationChallengeParser.getChallengeParameterFromResponse( |
| 78 | + new MockHttpResponse(null, 401, new HttpHeaders().add(HttpHeaderName.WWW_AUTHENTICATE, challenge)), |
| 79 | + "Bearer", "claims"); |
| 80 | + assertEquals(encodedClaims, actualEncodedClaims); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // A fake token credential that lets us keep track of what got parsed out of a CAE claim for assertion. |
| 85 | + private static TokenCredential getCaeTokenCredential(AtomicReference<String> claims, AtomicInteger callCount) { |
| 86 | + return request -> { |
| 87 | + claims.set(request.getClaims()); |
| 88 | + assertTrue(request.isCaeEnabled()); |
| 89 | + callCount.incrementAndGet(); |
| 90 | + return Mono.just(new AccessToken("token", OffsetDateTime.now().plusHours(2))); |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + // This http client is effectively a state sentinel for how we progressed through the challenge. |
| 95 | + // If we had a challenge, and it is invalid, the policy stops and returns 401 all the way out. |
| 96 | + // If the CAE challenge parses properly we will end complete the policy normally and get 200. |
| 97 | + private static HttpClient getCaeHttpClient(String challenge, AtomicInteger callCount) { |
| 98 | + return request -> { |
| 99 | + if (callCount.get() <= 1) { |
| 100 | + if (challenge == null) { |
| 101 | + return Mono.just(new MockHttpResponse(request, 200)); |
| 102 | + } |
| 103 | + return Mono.just(new MockHttpResponse(request, 401, |
| 104 | + new HttpHeaders().add(HttpHeaderName.WWW_AUTHENTICATE, challenge))); |
| 105 | + } |
| 106 | + return Mono.just(new MockHttpResponse(request, 200)); |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + private static Stream<Arguments> caeTestArguments() { |
| 111 | + return Stream.of(Arguments.of(null, 200, null, null), // no challenge |
| 112 | + Arguments.of( |
| 113 | + "Bearer authorization_uri=\"https://login.windows.net/\", error=\"invalid_token\", claims=\"ey==\"", |
| 114 | + 401, null, "ey=="), // unexpected error value |
| 115 | + Arguments.of("Bearer claims=\"not base64\", error=\"insufficient_claims\"", 401, null, "not base64"), // parsing error |
| 116 | + Arguments.of( |
| 117 | + "Bearer realm=\"\", authorization_uri=\"http://localhost\", client_id=\"00000003-0000-0000-c000-000000000000\", error=\"insufficient_claims\", claims=\"ey==\"", |
| 118 | + 200, "{", "ey=="), // more parameters in a different order |
| 119 | + Arguments.of( |
| 120 | + "Bearer realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", error=\"insufficient_claims\", claims=\"eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwidmFsdWUiOiIxNzI2MDc3NTk1In0sInhtc19jYWVlcnJvciI6eyJ2YWx1ZSI6IjEwMDEyIn19fQ==\"", |
| 121 | + 200, |
| 122 | + "{\"access_token\":{\"nbf\":{\"essential\":true,\"value\":\"1726077595\"},\"xms_caeerror\":{\"value\":\"10012\"}}}", |
| 123 | + "eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwidmFsdWUiOiIxNzI2MDc3NTk1In0sInhtc19jYWVlcnJvciI6eyJ2YWx1ZSI6IjEwMDEyIn19fQ=="), // standard request |
| 124 | + Arguments.of( |
| 125 | + "PoP realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", client_id=\"00000003-0000-0000-c000-000000000000\", nonce=\"ey==\", Bearer realm=\"\", authorization_uri=\"https://login.microsoftonline.com/common/oauth2/authorize\", client_id=\"00000003-0000-0000-c000-000000000000\", error_description=\"Continuous access evaluation resulted in challenge with result: InteractionRequired and code: TokenIssuedBeforeRevocationTimestamp\", error=\"insufficient_claims\", claims=\"eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTcyNjI1ODEyMiJ9fX0=\"", |
| 126 | + 200, "{\"access_token\":{\"nbf\":{\"essential\":true, \"value\":\"1726258122\"}}}", |
| 127 | + "eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTcyNjI1ODEyMiJ9fX0="), // multiple challenges |
| 128 | + Arguments.of("Bearer claims=\"\" error=\"insufficient_claims\"", 401, null, ""), // empty claims |
| 129 | + Arguments.of("Bearer error=\"insufficient_claims\"", 401, null, "") // missing claims |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
0 commit comments