|
| 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.authentication.logout; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import org.opensaml.saml.saml2.core.LogoutRequest; |
| 23 | +import org.opensaml.saml.saml2.core.NameID; |
| 24 | + |
| 25 | +import org.springframework.security.core.Authentication; |
| 26 | +import org.springframework.security.saml2.core.OpenSamlInitializationService; |
| 27 | +import org.springframework.security.saml2.core.Saml2Error; |
| 28 | +import org.springframework.security.saml2.core.Saml2ErrorCodes; |
| 29 | +import org.springframework.security.saml2.core.Saml2X509Credential; |
| 30 | +import org.springframework.security.saml2.provider.service.authentication.logout.OpenSamlOperations.VerificationConfigurer; |
| 31 | +import org.springframework.security.saml2.provider.service.authentication.logout.OpenSamlOperations.VerificationConfigurer.RedirectParameters; |
| 32 | +import org.springframework.security.saml2.provider.service.registration.AssertingPartyMetadata; |
| 33 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 34 | +import org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding; |
| 35 | + |
| 36 | +class BaseOpenSamlLogoutRequestValidator implements Saml2LogoutRequestValidator { |
| 37 | + |
| 38 | + static { |
| 39 | + OpenSamlInitializationService.initialize(); |
| 40 | + } |
| 41 | + |
| 42 | + private final OpenSamlOperations saml; |
| 43 | + |
| 44 | + BaseOpenSamlLogoutRequestValidator(OpenSamlOperations saml) { |
| 45 | + this.saml = saml; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritDoc} |
| 50 | + */ |
| 51 | + @Override |
| 52 | + public Saml2LogoutValidatorResult validate(Saml2LogoutRequestValidatorParameters parameters) { |
| 53 | + Saml2LogoutRequest request = parameters.getLogoutRequest(); |
| 54 | + RelyingPartyRegistration registration = parameters.getRelyingPartyRegistration(); |
| 55 | + Authentication authentication = parameters.getAuthentication(); |
| 56 | + LogoutRequest logoutRequest = this.saml.deserialize(Saml2Utils.withEncoded(request.getSamlRequest()) |
| 57 | + .inflate(request.getBinding() == Saml2MessageBinding.REDIRECT) |
| 58 | + .decode()); |
| 59 | + return Saml2LogoutValidatorResult.withErrors() |
| 60 | + .errors(verifySignature(request, logoutRequest, registration)) |
| 61 | + .errors(validateRequest(logoutRequest, registration, authentication)) |
| 62 | + .build(); |
| 63 | + } |
| 64 | + |
| 65 | + private Consumer<Collection<Saml2Error>> verifySignature(Saml2LogoutRequest request, LogoutRequest logoutRequest, |
| 66 | + RelyingPartyRegistration registration) { |
| 67 | + AssertingPartyMetadata details = registration.getAssertingPartyMetadata(); |
| 68 | + Collection<Saml2X509Credential> credentials = details.getVerificationX509Credentials(); |
| 69 | + VerificationConfigurer verify = this.saml.withVerificationKeys(credentials).entityId(details.getEntityId()); |
| 70 | + return (errors) -> { |
| 71 | + if (logoutRequest.isSigned()) { |
| 72 | + errors.addAll(verify.verify(logoutRequest)); |
| 73 | + } |
| 74 | + else { |
| 75 | + RedirectParameters params = new RedirectParameters(request.getParameters(), |
| 76 | + request.getParametersQuery(), logoutRequest); |
| 77 | + errors.addAll(verify.verify(params)); |
| 78 | + } |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + private Consumer<Collection<Saml2Error>> validateRequest(LogoutRequest request, |
| 83 | + RelyingPartyRegistration registration, Authentication authentication) { |
| 84 | + return (errors) -> { |
| 85 | + validateIssuer(request, registration).accept(errors); |
| 86 | + validateDestination(request, registration).accept(errors); |
| 87 | + validateSubject(request, registration, authentication).accept(errors); |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + private Consumer<Collection<Saml2Error>> validateIssuer(LogoutRequest request, |
| 92 | + RelyingPartyRegistration registration) { |
| 93 | + return (errors) -> { |
| 94 | + if (request.getIssuer() == null) { |
| 95 | + errors.add(new Saml2Error(Saml2ErrorCodes.INVALID_ISSUER, "Failed to find issuer in LogoutRequest")); |
| 96 | + return; |
| 97 | + } |
| 98 | + String issuer = request.getIssuer().getValue(); |
| 99 | + if (!issuer.equals(registration.getAssertingPartyMetadata().getEntityId())) { |
| 100 | + errors |
| 101 | + .add(new Saml2Error(Saml2ErrorCodes.INVALID_ISSUER, "Failed to match issuer to configured issuer")); |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + private Consumer<Collection<Saml2Error>> validateDestination(LogoutRequest request, |
| 107 | + RelyingPartyRegistration registration) { |
| 108 | + return (errors) -> { |
| 109 | + if (request.getDestination() == null) { |
| 110 | + errors.add(new Saml2Error(Saml2ErrorCodes.INVALID_DESTINATION, |
| 111 | + "Failed to find destination in LogoutRequest")); |
| 112 | + return; |
| 113 | + } |
| 114 | + String destination = request.getDestination(); |
| 115 | + if (!destination.equals(registration.getSingleLogoutServiceLocation())) { |
| 116 | + errors.add(new Saml2Error(Saml2ErrorCodes.INVALID_DESTINATION, |
| 117 | + "Failed to match destination to configured destination")); |
| 118 | + } |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + private Consumer<Collection<Saml2Error>> validateSubject(LogoutRequest request, |
| 123 | + RelyingPartyRegistration registration, Authentication authentication) { |
| 124 | + return (errors) -> { |
| 125 | + if (authentication == null) { |
| 126 | + return; |
| 127 | + } |
| 128 | + NameID nameId = getNameId(request, registration); |
| 129 | + if (nameId == null) { |
| 130 | + errors |
| 131 | + .add(new Saml2Error(Saml2ErrorCodes.SUBJECT_NOT_FOUND, "Failed to find subject in LogoutRequest")); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + validateNameId(nameId, authentication, errors); |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + private NameID getNameId(LogoutRequest request, RelyingPartyRegistration registration) { |
| 140 | + this.saml.withDecryptionKeys(registration.getDecryptionX509Credentials()).decrypt(request); |
| 141 | + return request.getNameID(); |
| 142 | + } |
| 143 | + |
| 144 | + private void validateNameId(NameID nameId, Authentication authentication, Collection<Saml2Error> errors) { |
| 145 | + String name = nameId.getValue(); |
| 146 | + if (!name.equals(authentication.getName())) { |
| 147 | + errors.add(new Saml2Error(Saml2ErrorCodes.INVALID_REQUEST, |
| 148 | + "Failed to match subject in LogoutRequest with currently logged in user")); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments