|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.security.oauth2.jwt; |
| 17 | + |
| 18 | +import okhttp3.mockwebserver.MockResponse; |
| 19 | +import okhttp3.mockwebserver.MockWebServer; |
| 20 | +import org.junit.After; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.springframework.http.HttpHeaders; |
| 25 | +import org.springframework.http.MediaType; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link JwtDecoders} |
| 31 | + * |
| 32 | + * @author Josh Cummings |
| 33 | + */ |
| 34 | +public class JwtDecodersTests { |
| 35 | + /** |
| 36 | + * Contains those parameters required to construct a JwtDecoder as well as any required parameters |
| 37 | + */ |
| 38 | + private static final String DEFAULT_RESPONSE_TEMPLATE = |
| 39 | + "{\n" |
| 40 | + + " \"authorization_endpoint\": \"https://example.com/o/oauth2/v2/auth\", \n" |
| 41 | + + " \"id_token_signing_alg_values_supported\": [\n" |
| 42 | + + " \"RS256\"\n" |
| 43 | + + " ], \n" |
| 44 | + + " \"issuer\": \"%s\", \n" |
| 45 | + + " \"jwks_uri\": \"%s/.well-known/jwks.json\", \n" |
| 46 | + + " \"response_types_supported\": [\n" |
| 47 | + + " \"code\", \n" |
| 48 | + + " \"token\", \n" |
| 49 | + + " \"id_token\", \n" |
| 50 | + + " \"code token\", \n" |
| 51 | + + " \"code id_token\", \n" |
| 52 | + + " \"token id_token\", \n" |
| 53 | + + " \"code token id_token\", \n" |
| 54 | + + " \"none\"\n" |
| 55 | + + " ], \n" |
| 56 | + + " \"subject_types_supported\": [\n" |
| 57 | + + " \"public\"\n" |
| 58 | + + " ], \n" |
| 59 | + + " \"token_endpoint\": \"https://example.com/oauth2/v4/token\"\n" |
| 60 | + + "}"; |
| 61 | + |
| 62 | + private static final String JWK_SET = "{\"keys\":[{\"p\":\"49neceJFs8R6n7WamRGy45F5Tv0YM-R2ODK3eSBUSLOSH2tAqjEVKOkLE5fiNA3ygqq15NcKRadB2pTVf-Yb5ZIBuKzko8bzYIkIqYhSh_FAdEEr0vHF5fq_yWSvc6swsOJGqvBEtuqtJY027u-G2gAQasCQdhyejer68zsTn8M\",\"kty\":\"RSA\",\"q\":\"tWR-ysspjZ73B6p2vVRVyHwP3KQWL5KEQcdgcmMOE_P_cPs98vZJfLhxobXVmvzuEWBpRSiqiuyKlQnpstKt94Cy77iO8m8ISfF3C9VyLWXi9HUGAJb99irWABFl3sNDff5K2ODQ8CmuXLYM25OwN3ikbrhEJozlXg_NJFSGD4E\",\"d\":\"FkZHYZlw5KSoqQ1i2RA2kCUygSUOf1OqMt3uomtXuUmqKBm_bY7PCOhmwbvbn4xZYEeHuTR8Xix-0KpHe3NKyWrtRjkq1T_un49_1LLVUhJ0dL-9_x0xRquVjhl_XrsRXaGMEHs8G9pLTvXQ1uST585gxIfmCe0sxPZLvwoic-bXf64UZ9BGRV3lFexWJQqCZp2S21HfoU7wiz6kfLRNi-K4xiVNB1gswm_8o5lRuY7zB9bRARQ3TS2G4eW7p5sxT3CgsGiQD3_wPugU8iDplqAjgJ5ofNJXZezoj0t6JMB_qOpbrmAM1EnomIPebSLW7Ky9SugEd6KMdL5lW6AuAQ\",\"e\":\"AQAB\",\"use\":\"sig\",\"kid\":\"one\",\"qi\":\"wdkFu_tV2V1l_PWUUimG516Z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JtXJDCzwg0ZDVUE6rS0QadSxsMMGbl7c4y0lG_7TfLX2iWeNJukJj_oSW9KzW4FsBp1BoocWjrreesqQU3fZHbikH-c_Fs2TsAIpHnxflyEzfOFWpJ8D4DtzHXqfvieMwpy42xsPZK3LR84zlasf0Ne1tC_hLHvyHRdAXwn0CMoKxc7-8j0r9Mq8kAzUsPn9If7bMLqGkxUcTPdk5x7opAUajDZx95SXHLmtztNtBa2S6EfPJXuPKG6tM5Wq5Ug"; |
| 64 | + |
| 65 | + private MockWebServer server; |
| 66 | + private String issuer; |
| 67 | + private String jwkSetUri; |
| 68 | + |
| 69 | + @Before |
| 70 | + public void setup() throws Exception { |
| 71 | + this.server = new MockWebServer(); |
| 72 | + this.server.start(); |
| 73 | + this.issuer = createIssuerFromServer(); |
| 74 | + this.jwkSetUri = this.issuer + "/.well-known/jwks.json"; |
| 75 | + } |
| 76 | + |
| 77 | + @After |
| 78 | + public void cleanup() throws Exception { |
| 79 | + this.server.shutdown(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void issuerWhenResponseIsTypicalThenReturnedDecoderValidatesIssuer() { |
| 84 | + prepareOpenIdConfigurationResponse(); |
| 85 | + this.server.enqueue(new MockResponse().setBody(JWK_SET)); |
| 86 | + |
| 87 | + JwtDecoder decoder = JwtDecoders.fromOidcIssuerLocation(this.issuer); |
| 88 | + |
| 89 | + assertThatCode(() -> decoder.decode(ISSUER_MISMATCH)) |
| 90 | + .isInstanceOf(JwtValidationException.class) |
| 91 | + .hasMessageContaining("This iss claim is not equal to the configured issuer"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void issuerWhenResponseIsNonCompliantThenThrowsRuntimeException() { |
| 96 | + prepareOpenIdConfigurationResponse("{ \"missing_required_keys\" : \"and_values\" }"); |
| 97 | + |
| 98 | + assertThatCode(() -> JwtDecoders.fromOidcIssuerLocation(this.issuer)) |
| 99 | + .isInstanceOf(RuntimeException.class); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void issuerWhenResponseIsMalformedThenThrowsRuntimeException() { |
| 104 | + prepareOpenIdConfigurationResponse("malformed"); |
| 105 | + |
| 106 | + assertThatCode(() -> JwtDecoders.fromOidcIssuerLocation(this.issuer)) |
| 107 | + .isInstanceOf(RuntimeException.class); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void issuerWhenRespondingIssuerMismatchesRequestedIssuerThenThrowsIllegalStateException() { |
| 112 | + prepareOpenIdConfigurationResponse(); |
| 113 | + |
| 114 | + assertThatCode(() -> JwtDecoders.fromOidcIssuerLocation(this.issuer + "/wrong")) |
| 115 | + .isInstanceOf(IllegalStateException.class); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void issuerWhenRequestedIssuerIsUnresponsiveThenThrowsIllegalArgumentException() |
| 120 | + throws Exception { |
| 121 | + |
| 122 | + this.server.shutdown(); |
| 123 | + |
| 124 | + assertThatCode(() -> JwtDecoders.fromOidcIssuerLocation("https://issuer")) |
| 125 | + .isInstanceOf(IllegalArgumentException.class); |
| 126 | + } |
| 127 | + |
| 128 | + private void prepareOpenIdConfigurationResponse() { |
| 129 | + String body = String.format(DEFAULT_RESPONSE_TEMPLATE, this.issuer, this.issuer); |
| 130 | + prepareOpenIdConfigurationResponse(body); |
| 131 | + } |
| 132 | + |
| 133 | + private void prepareOpenIdConfigurationResponse(String body) { |
| 134 | + MockResponse mockResponse = new MockResponse() |
| 135 | + .setBody(body) |
| 136 | + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); |
| 137 | + this.server.enqueue(mockResponse); |
| 138 | + } |
| 139 | + |
| 140 | + private String createIssuerFromServer() { |
| 141 | + return this.server.url("").toString(); |
| 142 | + } |
| 143 | +} |
0 commit comments