|
| 1 | +/* |
| 2 | + * Copyright 2004-present 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 | + * https://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.annotation.web.configurers.oauth2.server.resource; |
| 18 | + |
| 19 | +import java.util.function.Consumer; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.context.annotation.Bean; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.security.config.Customizer; |
| 28 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 29 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 30 | +import org.springframework.security.config.test.SpringTestContext; |
| 31 | +import org.springframework.security.config.test.SpringTestContextExtension; |
| 32 | +import org.springframework.security.oauth2.jose.TestKeys; |
| 33 | +import org.springframework.security.oauth2.jwt.JwtDecoder; |
| 34 | +import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; |
| 35 | +import org.springframework.security.oauth2.server.resource.OAuth2ProtectedResourceMetadata; |
| 36 | +import org.springframework.security.oauth2.server.resource.OAuth2ProtectedResourceMetadataClaimNames; |
| 37 | +import org.springframework.security.web.SecurityFilterChain; |
| 38 | +import org.springframework.test.web.servlet.MockMvc; |
| 39 | + |
| 40 | +import static org.hamcrest.Matchers.hasItem; |
| 41 | +import static org.hamcrest.Matchers.hasSize; |
| 42 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 43 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 44 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 45 | + |
| 46 | +/** |
| 47 | + * Integration tests for OAuth 2.0 Protected Resource Metadata Requests. |
| 48 | + * |
| 49 | + * @author Joe Grandja |
| 50 | + */ |
| 51 | +@ExtendWith(SpringTestContextExtension.class) |
| 52 | +public class OAuth2ProtectedResourceMetadataTests { |
| 53 | + |
| 54 | + private static final String DEFAULT_OAUTH2_PROTECTED_RESOURCE_METADATA_ENDPOINT_URI = "/.well-known/oauth-protected-resource"; |
| 55 | + |
| 56 | + private static final String RESOURCE = "https://resource.com:8443"; |
| 57 | + |
| 58 | + private static final String ISSUER_1 = "https://provider1.com"; |
| 59 | + |
| 60 | + private static final String ISSUER_2 = "https://provider2.com"; |
| 61 | + |
| 62 | + public final SpringTestContext spring = new SpringTestContext(this); |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private MockMvc mvc; |
| 66 | + |
| 67 | + @Test |
| 68 | + public void requestWhenProtectedResourceMetadataRequestThenReturnMetadataResponse() throws Exception { |
| 69 | + this.spring.register(ResourceServerConfiguration.class).autowire(); |
| 70 | + |
| 71 | + this.mvc.perform(get(RESOURCE.concat(DEFAULT_OAUTH2_PROTECTED_RESOURCE_METADATA_ENDPOINT_URI))) |
| 72 | + .andExpect(status().is2xxSuccessful()) |
| 73 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.RESOURCE).value(RESOURCE)) |
| 74 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.BEARER_METHODS_SUPPORTED).isArray()) |
| 75 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.BEARER_METHODS_SUPPORTED).value(hasSize(1))) |
| 76 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.BEARER_METHODS_SUPPORTED) |
| 77 | + .value(hasItem("header"))) |
| 78 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.TLS_CLIENT_CERTIFICATE_BOUND_ACCESS_TOKENS) |
| 79 | + .value(true)) |
| 80 | + .andReturn(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void requestWhenProtectedResourceMetadataRequestIncludesResourcePathThenMetadataResponseHasResourcePath() |
| 85 | + throws Exception { |
| 86 | + this.spring.register(ResourceServerConfiguration.class).autowire(); |
| 87 | + |
| 88 | + String host = RESOURCE; |
| 89 | + |
| 90 | + String resourcePath = "/resource1"; |
| 91 | + String resource = host.concat(resourcePath); |
| 92 | + this.mvc.perform(get(host.concat(DEFAULT_OAUTH2_PROTECTED_RESOURCE_METADATA_ENDPOINT_URI).concat(resourcePath))) |
| 93 | + .andExpect(status().is2xxSuccessful()) |
| 94 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.RESOURCE).value(resource)) |
| 95 | + .andReturn(); |
| 96 | + |
| 97 | + resourcePath = "/path1/resource2"; |
| 98 | + resource = host.concat(resourcePath); |
| 99 | + this.mvc.perform(get(host.concat(DEFAULT_OAUTH2_PROTECTED_RESOURCE_METADATA_ENDPOINT_URI).concat(resourcePath))) |
| 100 | + .andExpect(status().is2xxSuccessful()) |
| 101 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.RESOURCE).value(resource)) |
| 102 | + .andReturn(); |
| 103 | + |
| 104 | + resourcePath = "/path1/path2/resource3"; |
| 105 | + resource = host.concat(resourcePath); |
| 106 | + this.mvc.perform(get(host.concat(DEFAULT_OAUTH2_PROTECTED_RESOURCE_METADATA_ENDPOINT_URI).concat(resourcePath))) |
| 107 | + .andExpect(status().is2xxSuccessful()) |
| 108 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.RESOURCE).value(resource)) |
| 109 | + .andReturn(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void requestWhenProtectedResourceMetadataRequestAndMetadataCustomizerSetThenReturnCustomMetadataResponse() |
| 114 | + throws Exception { |
| 115 | + this.spring.register(ResourceServerConfigurationWithMetadataCustomizer.class).autowire(); |
| 116 | + |
| 117 | + this.mvc.perform(get(RESOURCE.concat(DEFAULT_OAUTH2_PROTECTED_RESOURCE_METADATA_ENDPOINT_URI))) |
| 118 | + .andExpect(status().is2xxSuccessful()) |
| 119 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.RESOURCE).value(RESOURCE)) |
| 120 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.AUTHORIZATION_SERVERS).isArray()) |
| 121 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.AUTHORIZATION_SERVERS).value(hasSize(2))) |
| 122 | + .andExpect( |
| 123 | + jsonPath(OAuth2ProtectedResourceMetadataClaimNames.AUTHORIZATION_SERVERS).value(hasItem(ISSUER_1))) |
| 124 | + .andExpect( |
| 125 | + jsonPath(OAuth2ProtectedResourceMetadataClaimNames.AUTHORIZATION_SERVERS).value(hasItem(ISSUER_2))) |
| 126 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.SCOPES_SUPPORTED).isArray()) |
| 127 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.SCOPES_SUPPORTED).value(hasSize(2))) |
| 128 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.SCOPES_SUPPORTED).value(hasItem("scope1"))) |
| 129 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.SCOPES_SUPPORTED).value(hasItem("scope2"))) |
| 130 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.BEARER_METHODS_SUPPORTED).isArray()) |
| 131 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.BEARER_METHODS_SUPPORTED).value(hasSize(1))) |
| 132 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.BEARER_METHODS_SUPPORTED) |
| 133 | + .value(hasItem("header"))) |
| 134 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.RESOURCE_NAME).value("resourceName")) |
| 135 | + .andExpect(jsonPath(OAuth2ProtectedResourceMetadataClaimNames.TLS_CLIENT_CERTIFICATE_BOUND_ACCESS_TOKENS) |
| 136 | + .value(true)) |
| 137 | + .andReturn(); |
| 138 | + } |
| 139 | + |
| 140 | + @EnableWebSecurity |
| 141 | + @Configuration(proxyBeanMethods = false) |
| 142 | + static class ResourceServerConfiguration { |
| 143 | + |
| 144 | + @Bean |
| 145 | + SecurityFilterChain securityFilterChain(HttpSecurity http) { |
| 146 | + // @formatter:off |
| 147 | + http |
| 148 | + .authorizeHttpRequests((authorize) -> |
| 149 | + authorize |
| 150 | + .anyRequest().authenticated() |
| 151 | + ) |
| 152 | + .oauth2ResourceServer((oauth2) -> |
| 153 | + oauth2 |
| 154 | + .jwt(Customizer.withDefaults()) |
| 155 | + ); |
| 156 | + // @formatter:on |
| 157 | + return http.build(); |
| 158 | + } |
| 159 | + |
| 160 | + @Bean |
| 161 | + JwtDecoder jwtDecoder() { |
| 162 | + return NimbusJwtDecoder.withPublicKey(TestKeys.DEFAULT_PUBLIC_KEY).build(); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + @EnableWebSecurity |
| 168 | + @Configuration(proxyBeanMethods = false) |
| 169 | + static class ResourceServerConfigurationWithMetadataCustomizer extends ResourceServerConfiguration { |
| 170 | + |
| 171 | + @Bean |
| 172 | + SecurityFilterChain securityFilterChain(HttpSecurity http) { |
| 173 | + // @formatter:off |
| 174 | + http |
| 175 | + .authorizeHttpRequests((authorize) -> |
| 176 | + authorize |
| 177 | + .anyRequest().authenticated() |
| 178 | + ) |
| 179 | + .oauth2ResourceServer((oauth2) -> |
| 180 | + oauth2 |
| 181 | + .jwt(Customizer.withDefaults()) |
| 182 | + .protectedResourceMetadata((metadata) -> |
| 183 | + metadata.protectedResourceMetadataCustomizer(protectedResourceMetadataCustomizer()) |
| 184 | + ) |
| 185 | + ); |
| 186 | + // @formatter:on |
| 187 | + return http.build(); |
| 188 | + } |
| 189 | + |
| 190 | + private Consumer<OAuth2ProtectedResourceMetadata.Builder> protectedResourceMetadataCustomizer() { |
| 191 | + return (protectedResourceMetadata) -> protectedResourceMetadata.authorizationServer(ISSUER_1) |
| 192 | + .authorizationServer(ISSUER_2) |
| 193 | + .scope("scope1") |
| 194 | + .scope("scope2") |
| 195 | + .resourceName("resourceName"); |
| 196 | + } |
| 197 | + |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments