|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 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.saml2; |
| 18 | + |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import org.springframework.context.ApplicationContext; |
| 22 | +import org.springframework.security.config.annotation.web.HttpSecurityBuilder; |
| 23 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 24 | +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
| 25 | +import org.springframework.security.saml2.provider.service.metadata.OpenSamlMetadataResolver; |
| 26 | +import org.springframework.security.saml2.provider.service.metadata.RequestMatcherMetadataResponseResolver; |
| 27 | +import org.springframework.security.saml2.provider.service.metadata.Saml2MetadataResponseResolver; |
| 28 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 29 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository; |
| 30 | +import org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter; |
| 31 | +import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; |
| 32 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * An {@link AbstractHttpConfigurer} for SAML 2.0 Metadata. |
| 37 | + * |
| 38 | + * <p> |
| 39 | + * SAML 2.0 Metadata provides an application with the capability to publish configuration |
| 40 | + * information as a {@code <md:EntityDescriptor>} or {@code <md:EntitiesDescriptor>}. |
| 41 | + * |
| 42 | + * <p> |
| 43 | + * Defaults are provided for all configuration options with the only required |
| 44 | + * configuration being a {@link Saml2LoginConfigurer#relyingPartyRegistrationRepository}. |
| 45 | + * Alternatively, a {@link RelyingPartyRegistrationRepository} {@code @Bean} may be |
| 46 | + * registered instead. |
| 47 | + * |
| 48 | + * <h2>Security Filters</h2> |
| 49 | + * |
| 50 | + * The following {@code Filter} is populated: |
| 51 | + * |
| 52 | + * <ul> |
| 53 | + * <li>{@link Saml2MetadataFilter}</li> |
| 54 | + * </ul> |
| 55 | + * |
| 56 | + * <h2>Shared Objects Created</h2> |
| 57 | + * |
| 58 | + * none |
| 59 | + * |
| 60 | + * <h2>Shared Objects Used</h2> |
| 61 | + * |
| 62 | + * The following shared objects are used: |
| 63 | + * |
| 64 | + * <ul> |
| 65 | + * <li>{@link RelyingPartyRegistrationRepository} (required)</li> |
| 66 | + * </ul> |
| 67 | + * |
| 68 | + * @since 6.1 |
| 69 | + * @see HttpSecurity#saml2Metadata() |
| 70 | + * @see Saml2MetadataFilter |
| 71 | + * @see RelyingPartyRegistrationRepository |
| 72 | + */ |
| 73 | +public class Saml2MetadataConfigurer<H extends HttpSecurityBuilder<H>> |
| 74 | + extends AbstractHttpConfigurer<Saml2LogoutConfigurer<H>, H> { |
| 75 | + |
| 76 | + private final ApplicationContext context; |
| 77 | + |
| 78 | + private Function<RelyingPartyRegistrationRepository, Saml2MetadataResponseResolver> metadataResponseResolver; |
| 79 | + |
| 80 | + public Saml2MetadataConfigurer(ApplicationContext context) { |
| 81 | + this.context = context; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Use this endpoint to request relying party metadata. |
| 86 | + * |
| 87 | + * <p> |
| 88 | + * If you specify a {@code registrationId} placeholder in the URL, then the filter |
| 89 | + * will lookup a {@link RelyingPartyRegistration} using that. |
| 90 | + * |
| 91 | + * <p> |
| 92 | + * If there is no {@code registrationId} and your |
| 93 | + * {@link RelyingPartyRegistrationRepository} is {code Iterable}, the metadata |
| 94 | + * endpoint will try and show all relying parties' metadata in a single |
| 95 | + * {@code <md:EntitiesDecriptor} element. |
| 96 | + * |
| 97 | + * <p> |
| 98 | + * If you need a more sophisticated lookup strategy than these, use |
| 99 | + * {@link #metadataResponseResolver} instead. |
| 100 | + * @param metadataUrl the url to use |
| 101 | + * @return the {@link Saml2MetadataConfigurer} for more customizations |
| 102 | + */ |
| 103 | + public Saml2MetadataConfigurer<H> metadataUrl(String metadataUrl) { |
| 104 | + Assert.hasText(metadataUrl, "metadataUrl cannot be empty"); |
| 105 | + this.metadataResponseResolver = (registrations) -> { |
| 106 | + RequestMatcherMetadataResponseResolver metadata = new RequestMatcherMetadataResponseResolver(registrations, |
| 107 | + new OpenSamlMetadataResolver()); |
| 108 | + metadata.setRequestMatcher(new AntPathRequestMatcher(metadataUrl)); |
| 109 | + return metadata; |
| 110 | + }; |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Use this {@link Saml2MetadataResponseResolver} to parse the request and respond |
| 116 | + * with SAML 2.0 metadata. |
| 117 | + * @param metadataResponseResolver to use |
| 118 | + * @return the {@link Saml2MetadataConfigurer} for more customizations |
| 119 | + */ |
| 120 | + public Saml2MetadataConfigurer<H> metadataResponseResolver(Saml2MetadataResponseResolver metadataResponseResolver) { |
| 121 | + Assert.notNull(metadataResponseResolver, "metadataResponseResolver cannot be null"); |
| 122 | + this.metadataResponseResolver = (registrations) -> metadataResponseResolver; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public H and() { |
| 127 | + return getBuilder(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void configure(H http) throws Exception { |
| 132 | + Saml2MetadataResponseResolver metadataResponseResolver = createMetadataResponseResolver(http); |
| 133 | + http.addFilterBefore(new Saml2MetadataFilter(metadataResponseResolver), BasicAuthenticationFilter.class); |
| 134 | + } |
| 135 | + |
| 136 | + private Saml2MetadataResponseResolver createMetadataResponseResolver(H http) { |
| 137 | + if (this.metadataResponseResolver != null) { |
| 138 | + RelyingPartyRegistrationRepository registrations = getRelyingPartyRegistrationRepository(http); |
| 139 | + return this.metadataResponseResolver.apply(registrations); |
| 140 | + } |
| 141 | + Saml2MetadataResponseResolver metadataResponseResolver = getBeanOrNull(Saml2MetadataResponseResolver.class); |
| 142 | + if (metadataResponseResolver != null) { |
| 143 | + return metadataResponseResolver; |
| 144 | + } |
| 145 | + RelyingPartyRegistrationRepository registrations = getRelyingPartyRegistrationRepository(http); |
| 146 | + return new RequestMatcherMetadataResponseResolver(registrations, new OpenSamlMetadataResolver()); |
| 147 | + } |
| 148 | + |
| 149 | + private RelyingPartyRegistrationRepository getRelyingPartyRegistrationRepository(H http) { |
| 150 | + Saml2LoginConfigurer<H> login = http.getConfigurer(Saml2LoginConfigurer.class); |
| 151 | + if (login != null) { |
| 152 | + return login.relyingPartyRegistrationRepository(http); |
| 153 | + } |
| 154 | + else { |
| 155 | + return getBeanOrNull(RelyingPartyRegistrationRepository.class); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private <C> C getBeanOrNull(Class<C> clazz) { |
| 160 | + if (this.context == null) { |
| 161 | + return null; |
| 162 | + } |
| 163 | + if (this.context.getBeanNamesForType(clazz).length == 0) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + return this.context.getBean(clazz); |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments