From 7b7ad1a931c67519b1bb70d1e0abe8220ec98165 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Sat, 27 Jul 2024 00:09:11 +0200 Subject: [PATCH] WIP: Create ResponseValidator + necessary helper-classes --- .../ConstraintValidationFailedException.php | 12 +++++ .../ConstraintValidatorInterface.php | 19 +++++++ .../Response/DestinationMatches.php | 43 ++++++++++++++++ .../IdentityProviderAwareInterface.php | 15 ++++++ .../Process/IdentityProviderAwareTrait.php | 20 ++++++++ .../Process/ServiceProviderAwareInterface.php | 15 ++++++ .../Process/ServiceProviderAwareTrait.php | 20 ++++++++ .../Process/Validator/ResponseValidator.php | 42 ++++++++++++++++ .../Process/Validator/ValidatorInterface.php | 26 ++++++++++ .../Process/Validator/ValidatorTrait.php | 49 +++++++++++++++++++ 10 files changed, 261 insertions(+) create mode 100644 src/SAML2/Exception/ConstraintValidationFailedException.php create mode 100644 src/SAML2/Process/ConstraintValidation/ConstraintValidatorInterface.php create mode 100644 src/SAML2/Process/ConstraintValidation/Response/DestinationMatches.php create mode 100644 src/SAML2/Process/IdentityProviderAwareInterface.php create mode 100644 src/SAML2/Process/IdentityProviderAwareTrait.php create mode 100644 src/SAML2/Process/ServiceProviderAwareInterface.php create mode 100644 src/SAML2/Process/ServiceProviderAwareTrait.php create mode 100644 src/SAML2/Process/Validator/ResponseValidator.php create mode 100644 src/SAML2/Process/Validator/ValidatorInterface.php create mode 100644 src/SAML2/Process/Validator/ValidatorTrait.php diff --git a/src/SAML2/Exception/ConstraintValidationFailedException.php b/src/SAML2/Exception/ConstraintValidationFailedException.php new file mode 100644 index 000000000..3ad396f44 --- /dev/null +++ b/src/SAML2/Exception/ConstraintValidationFailedException.php @@ -0,0 +1,12 @@ +spMetadata->getAssertionConsumerService() as $assertionConsumerService) { + if ($assertionConsumerService->getLocation() === $response->getDestination()) { + if (Binding::getBinding($assertionConsumerService->getBinding()) instanceof $this->binding) { + return; + } + } + } + throw new ResourceNotRecognizedException(); + } +} diff --git a/src/SAML2/Process/IdentityProviderAwareInterface.php b/src/SAML2/Process/IdentityProviderAwareInterface.php new file mode 100644 index 000000000..d45021828 --- /dev/null +++ b/src/SAML2/Process/IdentityProviderAwareInterface.php @@ -0,0 +1,15 @@ +idpMetadata = $idpMetadata; + } +} diff --git a/src/SAML2/Process/ServiceProviderAwareInterface.php b/src/SAML2/Process/ServiceProviderAwareInterface.php new file mode 100644 index 000000000..7c04a265c --- /dev/null +++ b/src/SAML2/Process/ServiceProviderAwareInterface.php @@ -0,0 +1,15 @@ +spMetadata = $spMetadata; + } +} diff --git a/src/SAML2/Process/Validator/ResponseValidator.php b/src/SAML2/Process/Validator/ResponseValidator.php new file mode 100644 index 000000000..4e84af67f --- /dev/null +++ b/src/SAML2/Process/Validator/ResponseValidator.php @@ -0,0 +1,42 @@ +addConstraintValidator(new DestinationMatches($spMetadata, $binding)); +// $validator->addConstraintValidator(new IsSuccesful()); + + return $validator; + } +} diff --git a/src/SAML2/Process/Validator/ValidatorInterface.php b/src/SAML2/Process/Validator/ValidatorInterface.php new file mode 100644 index 000000000..6e9bccada --- /dev/null +++ b/src/SAML2/Process/Validator/ValidatorInterface.php @@ -0,0 +1,26 @@ + */ + protected array $validators; + + + /** + * Add a validation to the chain. + * + * @param \SimpleSAML\SAML2\Process\ConstraintValidation\ConstraintValidatorInterface $validation + */ + public function addConstraintValidator(ConstraintValidatorInterface $validator) + { + if ($validator instanceof IdentityProviderAwareInterface) { + $validator->setIdentityProvider($this->idpMetadata); + } + + if ($validator instanceof ServiceProviderAwareInterface) { + $validator->setServiceProvider($this->spMetadata); + } + + $this->validators[] = $validator; + } + + + /** + * Runs all the validations in the validation chain. + * + * If this function returns, all validations have been succesful. + * + * @throws \SimpleSAML\SAML2\Exception\ConstraintViolationFailedException when one of the conditions fail. + */ + public function validate(SerializableElementInterface $element): void + { + foreach ($this->validators as $validator) { + $validator->validate($element); + } + } +}