|
| 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 | + |
| 17 | +package org.springframework.security.config.oauth2.client; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import okhttp3.mockwebserver.MockResponse; |
| 22 | +import okhttp3.mockwebserver.MockWebServer; |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.springframework.http.HttpHeaders; |
| 27 | +import org.springframework.http.MediaType; |
| 28 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 29 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 30 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.*; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Rob Winch |
| 39 | + * @since 5.1 |
| 40 | + */ |
| 41 | +public class OidcConfigurationProviderTests { |
| 42 | + |
| 43 | + /** |
| 44 | + * Contains all optional parameters that are found in ClientRegistration |
| 45 | + */ |
| 46 | + private static final String DEFAULT_RESPONSE = |
| 47 | + "{\n" |
| 48 | + + " \"authorization_endpoint\": \"https://example.com/o/oauth2/v2/auth\", \n" |
| 49 | + + " \"claims_supported\": [\n" |
| 50 | + + " \"aud\", \n" |
| 51 | + + " \"email\", \n" |
| 52 | + + " \"email_verified\", \n" |
| 53 | + + " \"exp\", \n" |
| 54 | + + " \"family_name\", \n" |
| 55 | + + " \"given_name\", \n" |
| 56 | + + " \"iat\", \n" |
| 57 | + + " \"iss\", \n" |
| 58 | + + " \"locale\", \n" |
| 59 | + + " \"name\", \n" |
| 60 | + + " \"picture\", \n" |
| 61 | + + " \"sub\"\n" |
| 62 | + + " ], \n" |
| 63 | + + " \"code_challenge_methods_supported\": [\n" |
| 64 | + + " \"plain\", \n" |
| 65 | + + " \"S256\"\n" |
| 66 | + + " ], \n" |
| 67 | + + " \"id_token_signing_alg_values_supported\": [\n" |
| 68 | + + " \"RS256\"\n" |
| 69 | + + " ], \n" |
| 70 | + + " \"issuer\": \"https://example.com\", \n" |
| 71 | + + " \"jwks_uri\": \"https://example.com/oauth2/v3/certs\", \n" |
| 72 | + + " \"response_types_supported\": [\n" |
| 73 | + + " \"code\", \n" |
| 74 | + + " \"token\", \n" |
| 75 | + + " \"id_token\", \n" |
| 76 | + + " \"code token\", \n" |
| 77 | + + " \"code id_token\", \n" |
| 78 | + + " \"token id_token\", \n" |
| 79 | + + " \"code token id_token\", \n" |
| 80 | + + " \"none\"\n" |
| 81 | + + " ], \n" |
| 82 | + + " \"revocation_endpoint\": \"https://example.com/o/oauth2/revoke\", \n" |
| 83 | + + " \"scopes_supported\": [\n" |
| 84 | + + " \"openid\", \n" |
| 85 | + + " \"email\", \n" |
| 86 | + + " \"profile\"\n" |
| 87 | + + " ], \n" |
| 88 | + + " \"subject_types_supported\": [\n" |
| 89 | + + " \"public\"\n" |
| 90 | + + " ], \n" |
| 91 | + + " \"grant_types_supported\" : [\"authorization_code\"], \n" |
| 92 | + + " \"token_endpoint\": \"https://example.com/oauth2/v4/token\", \n" |
| 93 | + + " \"token_endpoint_auth_methods_supported\": [\n" |
| 94 | + + " \"client_secret_post\", \n" |
| 95 | + + " \"client_secret_basic\"\n" |
| 96 | + + " ], \n" |
| 97 | + + " \"userinfo_endpoint\": \"https://example.com/oauth2/v3/userinfo\"\n" |
| 98 | + + "}"; |
| 99 | + |
| 100 | + private MockWebServer server; |
| 101 | + |
| 102 | + private ObjectMapper mapper = new ObjectMapper(); |
| 103 | + |
| 104 | + private Map<String, Object> response; |
| 105 | + |
| 106 | + private String issuer; |
| 107 | + |
| 108 | + @Before |
| 109 | + public void setup() throws Exception { |
| 110 | + this.server = new MockWebServer(); |
| 111 | + this.server.start(); |
| 112 | + this.response = this.mapper.readValue(DEFAULT_RESPONSE, new TypeReference<Map<String, Object>>(){}); |
| 113 | + } |
| 114 | + |
| 115 | + @After |
| 116 | + public void cleanup() throws Exception { |
| 117 | + this.server.shutdown(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void issuerWhenAllInformationThenSuccess() throws Exception { |
| 122 | + ClientRegistration registration = registration(""); |
| 123 | + ClientRegistration.ProviderDetails provider = registration.getProviderDetails(); |
| 124 | + |
| 125 | + assertThat(registration.getClientAuthenticationMethod()).isEqualTo(ClientAuthenticationMethod.BASIC); |
| 126 | + assertThat(registration.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE); |
| 127 | + assertThat(registration.getRegistrationId()).isEqualTo(this.server.getHostName()); |
| 128 | + assertThat(registration.getClientName()).isEqualTo(this.issuer); |
| 129 | + assertThat(registration.getScopes()).containsOnly("openid", "email", "profile"); |
| 130 | + assertThat(provider.getAuthorizationUri()).isEqualTo("https://example.com/o/oauth2/v2/auth"); |
| 131 | + assertThat(provider.getTokenUri()).isEqualTo("https://example.com/oauth2/v4/token"); |
| 132 | + assertThat(provider.getJwkSetUri()).isEqualTo("https://example.com/oauth2/v3/certs"); |
| 133 | + assertThat(provider.getUserInfoEndpoint().getUri()).isEqualTo("https://example.com/oauth2/v3/userinfo"); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata |
| 138 | + * |
| 139 | + * RECOMMENDED. JSON array containing a list of the OAuth 2.0 [RFC6749] scope values that this server supports. The |
| 140 | + * server MUST support the openid scope value. |
| 141 | + * @throws Exception |
| 142 | + */ |
| 143 | + @Test |
| 144 | + public void issuerWhenScopesNullThenScopesDefaulted() throws Exception { |
| 145 | + this.response.remove("scopes_supported"); |
| 146 | + |
| 147 | + ClientRegistration registration = registration(""); |
| 148 | + |
| 149 | + assertThat(registration.getScopes()).containsOnly("openid"); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void issuerWhenGrantTypesSupportedNullThenDefaulted() throws Exception { |
| 154 | + this.response.remove("grant_types_supported"); |
| 155 | + |
| 156 | + ClientRegistration registration = registration(""); |
| 157 | + |
| 158 | + assertThat(registration.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * We currently only support authorization_code, so verify we have a meaningful error until we add support. |
| 163 | + * @throws Exception |
| 164 | + */ |
| 165 | + @Test |
| 166 | + public void issuerWhenGrantTypesSupportedInvalidThenException() throws Exception { |
| 167 | + this.response.put("grant_types_supported", Arrays.asList("implicit")); |
| 168 | + |
| 169 | + assertThatThrownBy(() -> registration("")) |
| 170 | + .isInstanceOf(IllegalArgumentException.class) |
| 171 | + .hasMessageContaining("Only AuthorizationGrantType.AUTHORIZATION_CODE is supported. The issuer \"" + this.issuer + "\" returned a configuration of [implicit]"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void issuerWhenTokenEndpointAuthMethodsNullThenDefaulted() throws Exception { |
| 176 | + this.response.remove("token_endpoint_auth_methods_supported"); |
| 177 | + |
| 178 | + ClientRegistration registration = registration(""); |
| 179 | + |
| 180 | + assertThat(registration.getClientAuthenticationMethod()).isEqualTo(ClientAuthenticationMethod.BASIC); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * We currently only support client_secret_basic, so verify we have a meaningful error until we add support. |
| 185 | + * @throws Exception |
| 186 | + */ |
| 187 | + @Test |
| 188 | + public void issuerWhenTokenEndpointAuthMethodsInvalidThenException() throws Exception { |
| 189 | + this.response.put("token_endpoint_auth_methods_supported", Arrays.asList("client_secret_post")); |
| 190 | + |
| 191 | + assertThatThrownBy(() -> registration("")) |
| 192 | + .isInstanceOf(IllegalArgumentException.class) |
| 193 | + .hasMessageContaining("Only ClientAuthenticationMethod.BASIC is supported. The issuer \"" + this.issuer + "\" returned a configuration of [client_secret_post]"); |
| 194 | + } |
| 195 | + |
| 196 | + private ClientRegistration registration(String path) throws Exception { |
| 197 | + String body = this.mapper.writeValueAsString(this.response); |
| 198 | + MockResponse mockResponse = new MockResponse() |
| 199 | + .setBody(body) |
| 200 | + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); |
| 201 | + this.server.enqueue(mockResponse); |
| 202 | + this.issuer = this.server.url(path).toString(); |
| 203 | + |
| 204 | + return OidcConfigurationProvider.issuer(this.issuer) |
| 205 | + .clientId("client-id") |
| 206 | + .clientSecret("client-secret") |
| 207 | + .build(); |
| 208 | + } |
| 209 | +} |
0 commit comments