|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.saml2.provider.service.web; |
| 18 | + |
| 19 | +import jakarta.servlet.http.HttpServletRequest; |
| 20 | +import org.opensaml.saml.saml2.core.Response; |
| 21 | + |
| 22 | +import org.springframework.http.HttpMethod; |
| 23 | +import org.springframework.security.saml2.core.OpenSamlInitializationService; |
| 24 | +import org.springframework.security.saml2.core.Saml2Error; |
| 25 | +import org.springframework.security.saml2.core.Saml2ErrorCodes; |
| 26 | +import org.springframework.security.saml2.core.Saml2ParameterNames; |
| 27 | +import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest; |
| 28 | +import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException; |
| 29 | +import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationToken; |
| 30 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 31 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository; |
| 32 | +import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationPlaceholderResolvers.UriResolver; |
| 33 | +import org.springframework.security.web.authentication.AuthenticationConverter; |
| 34 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 35 | +import org.springframework.security.web.util.matcher.OrRequestMatcher; |
| 36 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 37 | +import org.springframework.util.Assert; |
| 38 | + |
| 39 | +final class BaseOpenSamlAuthenticationTokenConverter implements AuthenticationConverter { |
| 40 | + |
| 41 | + static { |
| 42 | + OpenSamlInitializationService.initialize(); |
| 43 | + } |
| 44 | + |
| 45 | + private final OpenSamlOperations saml; |
| 46 | + |
| 47 | + private final RelyingPartyRegistrationRepository registrations; |
| 48 | + |
| 49 | + private RequestMatcher requestMatcher = new OrRequestMatcher( |
| 50 | + new AntPathRequestMatcher("/login/saml2/sso/{registrationId}"), |
| 51 | + new AntPathRequestMatcher("/login/saml2/sso")); |
| 52 | + |
| 53 | + private Saml2AuthenticationRequestRepository<?> authenticationRequests = new HttpSessionSaml2AuthenticationRequestRepository(); |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructs a {@link BaseOpenSamlAuthenticationTokenConverter} given a repository |
| 57 | + * for {@link RelyingPartyRegistration}s |
| 58 | + * @param registrations the repository for {@link RelyingPartyRegistration}s |
| 59 | + * {@link RelyingPartyRegistration}s |
| 60 | + */ |
| 61 | + BaseOpenSamlAuthenticationTokenConverter(RelyingPartyRegistrationRepository registrations, |
| 62 | + OpenSamlOperations saml) { |
| 63 | + Assert.notNull(registrations, "relyingPartyRegistrationRepository cannot be null"); |
| 64 | + this.registrations = registrations; |
| 65 | + this.saml = saml; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Resolve an authentication request from the given {@link HttpServletRequest}. |
| 70 | + * |
| 71 | + * <p> |
| 72 | + * First uses the configured {@link RequestMatcher} to deduce whether an |
| 73 | + * authentication request is being made and optionally for which |
| 74 | + * {@code registrationId}. |
| 75 | + * |
| 76 | + * <p> |
| 77 | + * If there is an associated {@code <saml2:AuthnRequest>}, then the |
| 78 | + * {@code registrationId} is looked up and used. |
| 79 | + * |
| 80 | + * <p> |
| 81 | + * If a {@code registrationId} is found in the request, then it is looked up and used. |
| 82 | + * In that case, if none is found a {@link Saml2AuthenticationException} is thrown. |
| 83 | + * |
| 84 | + * <p> |
| 85 | + * Finally, if no {@code registrationId} is found in the request, then the code |
| 86 | + * attempts to resolve the {@link RelyingPartyRegistration} from the SAML Response's |
| 87 | + * Issuer. |
| 88 | + * @param request the HTTP request |
| 89 | + * @return the {@link Saml2AuthenticationToken} authentication request |
| 90 | + * @throws Saml2AuthenticationException if the {@link RequestMatcher} specifies a |
| 91 | + * non-existent {@code registrationId} |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public Saml2AuthenticationToken convert(HttpServletRequest request) { |
| 95 | + String serialized = request.getParameter(Saml2ParameterNames.SAML_RESPONSE); |
| 96 | + if (serialized == null) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + RequestMatcher.MatchResult result = this.requestMatcher.matcher(request); |
| 100 | + if (!result.isMatch()) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + Saml2AuthenticationToken token = tokenByAuthenticationRequest(request); |
| 104 | + if (token == null) { |
| 105 | + token = tokenByRegistrationId(request, result); |
| 106 | + } |
| 107 | + if (token == null) { |
| 108 | + token = tokenByEntityId(request); |
| 109 | + } |
| 110 | + return token; |
| 111 | + } |
| 112 | + |
| 113 | + private Saml2AuthenticationToken tokenByAuthenticationRequest(HttpServletRequest request) { |
| 114 | + AbstractSaml2AuthenticationRequest authenticationRequest = this.authenticationRequests |
| 115 | + .loadAuthenticationRequest(request); |
| 116 | + if (authenticationRequest == null) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + String registrationId = authenticationRequest.getRelyingPartyRegistrationId(); |
| 120 | + RelyingPartyRegistration registration = this.registrations.findByRegistrationId(registrationId); |
| 121 | + return tokenByRegistration(request, registration, authenticationRequest); |
| 122 | + } |
| 123 | + |
| 124 | + private Saml2AuthenticationToken tokenByRegistrationId(HttpServletRequest request, |
| 125 | + RequestMatcher.MatchResult result) { |
| 126 | + String registrationId = result.getVariables().get("registrationId"); |
| 127 | + if (registrationId == null) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + RelyingPartyRegistration registration = this.registrations.findByRegistrationId(registrationId); |
| 131 | + return tokenByRegistration(request, registration, null); |
| 132 | + } |
| 133 | + |
| 134 | + private Saml2AuthenticationToken tokenByEntityId(HttpServletRequest request) { |
| 135 | + Response response = this.saml.deserialize(decode(request)); |
| 136 | + String issuer = response.getIssuer().getValue(); |
| 137 | + RelyingPartyRegistration registration = this.registrations.findUniqueByAssertingPartyEntityId(issuer); |
| 138 | + return tokenByRegistration(request, registration, null); |
| 139 | + } |
| 140 | + |
| 141 | + private Saml2AuthenticationToken tokenByRegistration(HttpServletRequest request, |
| 142 | + RelyingPartyRegistration registration, AbstractSaml2AuthenticationRequest authenticationRequest) { |
| 143 | + if (registration == null) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + String decoded = decode(request); |
| 147 | + UriResolver resolver = RelyingPartyRegistrationPlaceholderResolvers.uriResolver(request, registration); |
| 148 | + registration = registration.mutate() |
| 149 | + .entityId(resolver.resolve(registration.getEntityId())) |
| 150 | + .assertionConsumerServiceLocation(resolver.resolve(registration.getAssertionConsumerServiceLocation())) |
| 151 | + .build(); |
| 152 | + return new Saml2AuthenticationToken(registration, decoded, authenticationRequest); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Use the given {@link Saml2AuthenticationRequestRepository} to load authentication |
| 157 | + * request. |
| 158 | + * @param authenticationRequestRepository the |
| 159 | + * {@link Saml2AuthenticationRequestRepository} to use |
| 160 | + */ |
| 161 | + void setAuthenticationRequestRepository( |
| 162 | + Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> authenticationRequestRepository) { |
| 163 | + Assert.notNull(authenticationRequestRepository, "authenticationRequestRepository cannot be null"); |
| 164 | + this.authenticationRequests = authenticationRequestRepository; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Use the given {@link RequestMatcher} to match the request. |
| 169 | + * @param requestMatcher the {@link RequestMatcher} to use |
| 170 | + */ |
| 171 | + void setRequestMatcher(RequestMatcher requestMatcher) { |
| 172 | + Assert.notNull(requestMatcher, "requestMatcher cannot be null"); |
| 173 | + this.requestMatcher = requestMatcher; |
| 174 | + } |
| 175 | + |
| 176 | + private String decode(HttpServletRequest request) { |
| 177 | + String encoded = request.getParameter(Saml2ParameterNames.SAML_RESPONSE); |
| 178 | + try { |
| 179 | + return Saml2Utils.withEncoded(encoded) |
| 180 | + .requireBase64(true) |
| 181 | + .inflate(HttpMethod.GET.matches(request.getMethod())) |
| 182 | + .decode(); |
| 183 | + } |
| 184 | + catch (Exception ex) { |
| 185 | + throw new Saml2AuthenticationException(new Saml2Error(Saml2ErrorCodes.INVALID_RESPONSE, ex.getMessage()), |
| 186 | + ex); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments