diff --git a/CHANGELOG.md b/CHANGELOG.md index 11da641..c483788 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ - Add support for PHP 8.5 - Keep the minimum required PHP version at ^8.2 +- New: preserve and expose the `mdui:UIInfo` SAML extension (service display name) from the AuthnRequest during + authentication and registration, via `StateHandlerInterface::getMdui()`/`hasMdui()` and + `AuthenticationService::getMdui()`/`RegistrationService::getMdui()`. +- New: `Surfnet\GsspBundle\Service\ServiceName\ServiceNameFormatter` and `ServiceNameResolver` sanitize and + locale-match a resolved display name. Contract: truncated to 40 characters total (39 characters + ellipsis when + truncated), whitespace trimmed/collapsed, control/format characters stripped, locale fallback order is exact match + -> `en` -> first available `DisplayName`. +- Not yet tagged/released. # 6.0.0 diff --git a/composer.json b/composer.json index dd84f16..f8c5946 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,13 @@ "license": "Apache-2.0", "description": "A Symfony 6 bundle to aid the creation of GSSP (Generic SAML Step-up Provider) device support.", "type": "symfony-bundle", + "prefer-stable": true, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/OpenConext/Stepup-saml-bundle" + } + ], "autoload": { "psr-4": { "Surfnet\\GsspBundle\\": "src" @@ -20,7 +27,7 @@ "php": "^8.2", "ext-openssl": "*", "beberlei/assert": "^3", - "surfnet/stepup-saml-bundle": "^7.0", + "surfnet/stepup-saml-bundle": "7.1.0-beta3", "symfony/dependency-injection": "^7.4", "symfony/framework-bundle": "^7.4", "symfony/http-foundation": "^7.4", @@ -74,6 +81,12 @@ "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true, "simplesamlphp/composer-xmlprovider-installer": false + }, + "audit": { + "ignore": { + "PKSA-1fc7-xrz7-vw78": "GHSA-5cjr-mxj5-wmrx: DoS via XPath Transform in ds:Signature processing (transitive via surfnet/stepup-saml-bundle -> simplesamlphp/saml2). No patched simplesamlphp/saml2 4.x release exists or is planned. Mitigated in surfnet/stepup-saml-bundle by Surfnet\\SamlBundle\\Signing\\SignatureTransformGuard, called from PostBinding::processResponse() before every signature verification. See Stepup-saml-bundle PR #137.", + "PKSA-yk3g-3g3t-ts6q": "GHSA-6929-8p9f-26jx: TLS validator confusion in HTTP-Artifact binding resolution (transitive via surfnet/stepup-saml-bundle -> simplesamlphp/saml2). Neither this bundle nor surfnet/stepup-saml-bundle implement HTTP-Artifact binding; the affected code path is unreachable." + } } } } diff --git a/src/Controller/SSOController.php b/src/Controller/SSOController.php index ee875da..ebc478e 100644 --- a/src/Controller/SSOController.php +++ b/src/Controller/SSOController.php @@ -147,9 +147,9 @@ private function registrationAction(Request $request, ReceivedAuthnRequest $orig */ private function getRelayStateFromRequest(Request $request): string { - if (!is_string($request->get(AuthnRequest::PARAMETER_RELAY_STATE))) { + if (!is_string($request->query->get(AuthnRequest::PARAMETER_RELAY_STATE))) { return ''; } - return $request->get(AuthnRequest::PARAMETER_RELAY_STATE); + return $request->query->get(AuthnRequest::PARAMETER_RELAY_STATE); } } diff --git a/src/Features/Context/GsspContext.php b/src/Features/Context/GsspContext.php index 08476d6..b5f6f38 100644 --- a/src/Features/Context/GsspContext.php +++ b/src/Features/Context/GsspContext.php @@ -47,6 +47,10 @@ use SAML2\XML\saml\NameID; use Surfnet\GsspBundle\Controller\SSOController; use Surfnet\GsspBundle\Controller\SSOReturnController; +use Surfnet\SamlBundle\SAML2\Extensions\Extensions; +use Surfnet\SamlBundle\SAML2\Extensions\GsspUserAttributesChunk; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; +use SAML2\XML\Chunk as SAML2Chunk; use Surfnet\GsspBundle\Logger\StepupRequestIdSariLogger; use Surfnet\GsspBundle\Saml\AssertionSigningService; use Surfnet\GsspBundle\Saml\ResponseContext; @@ -113,6 +117,8 @@ final class GsspContext implements Context private ?Exception $lastException = null; + private ?StateHandler $stateHandler = null; + /** * Every scenario we start with a clean slate. * @@ -129,7 +135,7 @@ public function bootstrapDependencies(): void $this->twigTemplate = null; // Create required dependencies. - $stateHandler = new StateHandler(new InMemoryValueStore()); + $this->stateHandler = $stateHandler = new StateHandler(new InMemoryValueStore()); $this->logger = new BufferingLogger(); $logger = new StepupRequestIdSariLogger( $this->logger, @@ -618,6 +624,30 @@ public function theIdentityProviderSetsAnError($message): void $this->registrationService->replyToServiceProvider(); } + #[Given('the AuthnRequest contains service name :serviceName')] + public function setServiceNameOnAuthnRequest(string $serviceName): void + { + $chunk = new GsspUserAttributesChunk(); + $chunk->addAttribute( + 'urn:mace:surf.nl:stepup:service-name', + 'urn:oasis:names:tc:SAML:2.0:attrname-format:uri', + $serviceName + ); + $this->authnRequest->setExtensions([new SAML2Chunk($chunk->getValue())]); + } + + #[Then('the service name :serviceName should be stored in state')] + public function assertServiceNameStoredInState(string $serviceName): void + { + $attributes = $this->stateHandler->getGsspUserAttributes(); + Assertion::notNull($attributes, 'Expected GsspUserAttributesChunk to be present in state'); + Assertion::eq( + $serviceName, + $attributes->getAttributeValue('urn:mace:surf.nl:stepup:service-name'), + sprintf('Expected service name "%s" in state', $serviceName) + ); + } + #[Given('set the subject nameId to :nameId')] public function setTheSubjectNameIdTo(string $nameId): void { @@ -638,4 +668,37 @@ public function theReturnEndpointShouldRaiseAnException($message): void { Assertion::eq($message, $this->lastException->getMessage()); } + + #[Given('the AuthnRequest contains mdui display name :firstName for language :firstLang and :secondName for language :secondLang')] + public function setMduiDisplayNamesOnAuthnRequest(string $firstName, string $firstLang, string $secondName, string $secondLang): void + { + $chunk = new MduiChunk(); + $doc = $chunk->getValue()->ownerDocument; + $ns = 'urn:oasis:names:tc:SAML:metadata:ui'; + + foreach ([[$firstLang, $firstName], [$secondLang, $secondName]] as [$lang, $name]) { + $el = $doc->createElementNS($ns, 'mdui:DisplayName'); + $el->setAttribute('xml:lang', $lang); + $el->textContent = $name; + $chunk->getValue()->appendChild($el); + } + + $this->authnRequest->setExtensions([new SAML2Chunk($chunk->getValue())]); + } + + #[Then('the mdui display name for language :lang should be :expected in state')] + public function assertMduiDisplayNameInState(string $lang, string $expected): void + { + $mdui = $this->stateHandler->getMdui(); + Assertion::notNull($mdui, 'Expected MduiChunk to be present in state'); + $names = $mdui->getDisplayNames(); + Assertion::keyExists($names, $lang, sprintf('Expected lang "%s" in mdui display names', $lang)); + Assertion::eq($expected, $names[$lang], sprintf('Expected display name "%s" for lang "%s"', $expected, $lang)); + } + + #[Then('no mdui data should be stored in state')] + public function assertNoMduiInState(): void + { + Assertion::false($this->stateHandler->hasMdui(), 'Expected no mdui data in state'); + } } diff --git a/src/Features/authentication.feature b/src/Features/authentication.feature index 7f74ee7..0ddf9b6 100644 --- a/src/Features/authentication.feature +++ b/src/Features/authentication.feature @@ -72,3 +72,36 @@ Feature: When an user needs to be authenticated When the user is redirected to the identity provider sso return endpoint Then the return endpoint should raise an exception with "There is no request state present" + + # Pre-existing GsspUserAttributes service-name path (urn:mace:surf.nl:stepup:service-name), + # not the mdui:UIInfo mechanism added in this PR. Kept here as regression coverage for the + # saveRequestExtensions() refactor, which now handles both extension types from one call site. + Scenario: Service name from SAML extension is preserved in state during authentication + Given a normal SAML 2.0 AuthnRequest + And the AuthnRequest contains service name 'My University Portal' + And AuthnRequest is signed with sha256 + And set the subject nameId to 'unique-identifier-token' + + When the service provider sends the AuthnRequest with HTTP-Redirect binding + + Then the service name 'My University Portal' should be stored in state + + Scenario: mdui:UIInfo display names are preserved in state during authentication + Given a normal SAML 2.0 AuthnRequest + And the AuthnRequest contains mdui display name "Online learning" for language "en" and "Leeromgeving" for language "nl" + And AuthnRequest is signed with sha256 + And set the subject nameId to 'unique-identifier-token' + + When the service provider sends the AuthnRequest with HTTP-Redirect binding + + Then the mdui display name for language "en" should be "Online learning" in state + And the mdui display name for language "nl" should be "Leeromgeving" in state + + Scenario: Authentication without mdui:UIInfo does not cause an error + Given a normal SAML 2.0 AuthnRequest + And AuthnRequest is signed with sha256 + And set the subject nameId to 'unique-identifier-token' + + When the service provider sends the AuthnRequest with HTTP-Redirect binding + + Then no mdui data should be stored in state diff --git a/src/Features/registration.feature b/src/Features/registration.feature index 5311797..4534108 100644 --- a/src/Features/registration.feature +++ b/src/Features/registration.feature @@ -40,6 +40,24 @@ Feature: When an user needs to enroll for a new token | notice | /Saml response created with id ".+", request ID: ".+"/ | present | | notice | Invalidate current state and redirect user to service provider assertion consumer url "https://service_provider/saml/acu" | present | + Scenario: mdui:UIInfo display names are preserved in state during registration + Given a normal SAML 2.0 AuthnRequest + And the AuthnRequest contains mdui display name "Online learning" for language "en" and "Leeromgeving" for language "nl" + And AuthnRequest is signed with sha256 + + When the service provider sends the AuthnRequest with HTTP-Redirect binding + + Then the mdui display name for language "en" should be "Online learning" in state + And the mdui display name for language "nl" should be "Leeromgeving" in state + + Scenario: Registration without mdui:UIInfo does not cause an error + Given a normal SAML 2.0 AuthnRequest + And AuthnRequest is signed with sha256 + + When the service provider sends the AuthnRequest with HTTP-Redirect binding + + Then no mdui data should be stored in state + Scenario: When an user request the sso return endpoint without being registered the user should be redirected to the application registration endpoint Given a normal SAML 2.0 AuthnRequest And AuthnRequest is signed with sha256 diff --git a/src/Service/AuthenticationService.php b/src/Service/AuthenticationService.php index f69f8f8..a9cdde1 100644 --- a/src/Service/AuthenticationService.php +++ b/src/Service/AuthenticationService.php @@ -22,6 +22,7 @@ use SAML2\Constants; use Surfnet\SamlBundle\SAML2\Extensions\GsspUserAttributesChunk; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; use Symfony\Component\HttpFoundation\Response; /** @@ -89,4 +90,8 @@ public function getIssuer(): string; public function getScopingRequesterIds(): array; public function getGsspUserAttributes(): ?GsspUserAttributesChunk; + + public function getMdui(): ?MduiChunk; + + public function hasMdui(): bool; } diff --git a/src/Service/RegistrationService.php b/src/Service/RegistrationService.php index 43e9d33..e9699ee 100644 --- a/src/Service/RegistrationService.php +++ b/src/Service/RegistrationService.php @@ -21,6 +21,7 @@ namespace Surfnet\GsspBundle\Service; use SAML2\Constants; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; use Symfony\Component\HttpFoundation\Response; /** @@ -62,6 +63,10 @@ public function register(string $subjectNameId): void; */ public function isRegistered(): bool; + public function getMdui(): ?MduiChunk; + + public function hasMdui(): bool; + public function reject(string $message, string $subCode = Constants::STATUS_AUTHN_FAILED): void; /* diff --git a/src/Service/ServiceName/ServiceNameResolver.php b/src/Service/ServiceName/ServiceNameResolver.php new file mode 100644 index 0000000..99f8770 --- /dev/null +++ b/src/Service/ServiceName/ServiceNameResolver.php @@ -0,0 +1,65 @@ +getDisplayNames() directly bypasses that sanitization — GSSPs must resolve display + * names through this class, never read MduiChunk's raw names for display. + */ +final class ServiceNameResolver +{ + private const FALLBACK_LANGUAGE = 'en'; + + public static function resolve(?MduiChunk $mdui, string $locale): ?string + { + if ($mdui === null) { + return null; + } + $names = $mdui->getDisplayNames(); + if ($names === []) { + return null; + } + $normalizedNames = self::normalizeLanguageKeys($names); + $values = array_values($normalizedNames); + $lang = strtolower(substr($locale, 0, 2)); + $raw = $normalizedNames[$lang] ?? $normalizedNames[self::FALLBACK_LANGUAGE] ?? $values[0]; + return ServiceNameFormatter::format($raw); + } + + /** + * @param array $names + * @return array + */ + private static function normalizeLanguageKeys(array $names): array + { + $normalized = []; + foreach ($names as $lang => $name) { + $normalized[strtolower(substr($lang, 0, 2))] ??= $name; + } + return $normalized; + } +} diff --git a/src/Service/StateBasedAuthenticationService.php b/src/Service/StateBasedAuthenticationService.php index bb78b5b..65b63ed 100644 --- a/src/Service/StateBasedAuthenticationService.php +++ b/src/Service/StateBasedAuthenticationService.php @@ -24,6 +24,7 @@ use SAML2\Constants; use Surfnet\GsspBundle\Exception\RuntimeException; use Surfnet\SamlBundle\SAML2\Extensions\GsspUserAttributesChunk; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\RouterInterface; @@ -111,4 +112,14 @@ public function getGsspUserAttributes(): ?GsspUserAttributesChunk { return $this->stateHandler->getGsspUserAttributes(); } + + public function getMdui(): ?MduiChunk + { + return $this->stateHandler->getMdui(); + } + + public function hasMdui(): bool + { + return $this->stateHandler->hasMdui(); + } } diff --git a/src/Service/StateBasedRegistrationService.php b/src/Service/StateBasedRegistrationService.php index ad8337d..bfff53c 100644 --- a/src/Service/StateBasedRegistrationService.php +++ b/src/Service/StateBasedRegistrationService.php @@ -23,6 +23,7 @@ use Psr\Log\LoggerInterface; use SAML2\Constants; use Surfnet\GsspBundle\Exception\RuntimeException; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\RouterInterface; @@ -59,6 +60,16 @@ public function isRegistered(): bool return $this->stateHandler->hasSubjectNameId(); } + public function getMdui(): ?MduiChunk + { + return $this->stateHandler->getMdui(); + } + + public function hasMdui(): bool + { + return $this->stateHandler->hasMdui(); + } + public function registrationRequired(): bool { return $this->stateHandler->isRequestTypeRegistration(); diff --git a/src/Service/StateHandler.php b/src/Service/StateHandler.php index e382d19..1268a1e 100644 --- a/src/Service/StateHandler.php +++ b/src/Service/StateHandler.php @@ -23,11 +23,14 @@ use SAML2\Constants; use Surfnet\GsspBundle\Exception\RuntimeException; use Surfnet\SamlBundle\SAML2\Extensions\GsspUserAttributesChunk; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; use Surfnet\SamlBundle\SAML2\ReceivedAuthnRequest; use TypeError; /** * Knows and preserves the integrity of the GSSP application state. + * + * @SuppressWarnings("PHPMD.ExcessiveClassComplexity") */ final readonly class StateHandler implements StateHandlerInterface { @@ -56,6 +59,8 @@ public const GSSP_USERATTRIBUTES = 'gssp_userattributes'; + public const GSSP_MDUI = 'gssp_mdui'; + public function __construct(private ValueStore $store) { } @@ -71,10 +76,7 @@ public function saveRegistrationRequest(ReceivedAuthnRequest $authnRequest, stri ->set(self::REQUEST_TYPE, self::REQUEST_TYPE_REGISTRATION) ; - $chunk = $authnRequest->getExtensions()->getGsspUserAttributesChunk(); - if ($chunk instanceof GsspUserAttributesChunk) { - $this->setGsspUserAttributes($chunk); - } + $this->saveRequestExtensions($authnRequest); } public function saveAuthenticationRequest(ReceivedAuthnRequest $authnRequest, string $relayState): void @@ -89,6 +91,8 @@ public function saveAuthenticationRequest(ReceivedAuthnRequest $authnRequest, st ->set(self::SCOPING_REQUESTER_IDS, $authnRequest->getScopingRequesterIds()) ->set(self::REQUEST_TYPE, self::REQUEST_TYPE_AUTHENTICATION) ; + + $this->saveRequestExtensions($authnRequest); } public function saveSubjectNameId(string $nameId): StateHandlerInterface @@ -156,13 +160,31 @@ public function getSubjectNameId(): string public function getGsspUserAttributes(): ?GsspUserAttributesChunk { - if (!is_string($this->store->get(self::GSSP_USERATTRIBUTES))) { + if (!$this->store->has(self::GSSP_USERATTRIBUTES)) { + return null; + } + $xml = $this->store->get(self::GSSP_USERATTRIBUTES); + if (!is_string($xml)) { throw new TypeError(sprintf('The "%s" must be of type string', self::GSSP_USERATTRIBUTES)); } - if ($this->store->has(self::GSSP_USERATTRIBUTES)) { - return GsspUserAttributesChunk::fromXML($this->store->get(self::GSSP_USERATTRIBUTES)); + return GsspUserAttributesChunk::fromXML($xml); + } + + public function getMdui(): ?MduiChunk + { + if (!$this->store->has(self::GSSP_MDUI)) { + return null; + } + $xml = $this->store->get(self::GSSP_MDUI); + if (!is_string($xml)) { + throw new TypeError(sprintf('The "%s" must be of type string', self::GSSP_MDUI)); } - return null; + return MduiChunk::fromXML($xml); + } + + public function hasMdui(): bool + { + return $this->store->has(self::GSSP_MDUI); } public function hasSubjectNameId(): bool @@ -237,9 +259,19 @@ private function setRequestId(string $originalRequestId): self return $this->set(self::REQUEST_ID, $originalRequestId); } - private function setGsspUserAttributes(GsspUserAttributesChunk $chunk): self + private function saveRequestExtensions(ReceivedAuthnRequest $authnRequest): void { - return $this->set(self::GSSP_USERATTRIBUTES, $chunk->toXML()); + $extensions = $authnRequest->getExtensions(); + + $chunk = $extensions->getGsspUserAttributesChunk(); + if ($chunk instanceof GsspUserAttributesChunk) { + $this->set(self::GSSP_USERATTRIBUTES, $chunk->toXML()); + } + + $mduiChunk = $extensions->getMduiChunk(); + if ($mduiChunk instanceof MduiChunk) { + $this->set(self::GSSP_MDUI, $mduiChunk->toXML()); + } } private function set(string $key, mixed $value): self @@ -250,9 +282,7 @@ private function set(string $key, mixed $value): self private function assertRequestTypeNotSet(): void { - // Request_type may not be set if ($this->store->has(self::REQUEST_TYPE)) { - // If it is set, it must be of type string. if (!is_string($this->store->get(self::REQUEST_TYPE))) { throw new TypeError(sprintf('The "%s" must be of type string', self::REQUEST_TYPE)); } diff --git a/src/Service/StateHandlerInterface.php b/src/Service/StateHandlerInterface.php index 9576631..0085792 100644 --- a/src/Service/StateHandlerInterface.php +++ b/src/Service/StateHandlerInterface.php @@ -21,6 +21,7 @@ namespace Surfnet\GsspBundle\Service; use Surfnet\SamlBundle\SAML2\Extensions\GsspUserAttributesChunk; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; use Surfnet\SamlBundle\SAML2\ReceivedAuthnRequest; /** @@ -56,6 +57,10 @@ public function getScopingRequesterIds(): array; public function getGsspUserAttributes(): ?GsspUserAttributesChunk; + public function getMdui(): ?MduiChunk; + + public function hasMdui(): bool; + /* * Is the current request type registration flow? */ diff --git a/tests/Service/ServiceName/ServiceNameResolverTest.php b/tests/Service/ServiceName/ServiceNameResolverTest.php new file mode 100644 index 0000000..f9ea811 --- /dev/null +++ b/tests/Service/ServiceName/ServiceNameResolverTest.php @@ -0,0 +1,112 @@ +assertNull(ServiceNameResolver::resolve(null, 'nl_NL')); + } + + public function testItReturnsNullWhenMduiChunkHasNoDisplayNames(): void + { + $mdui = MduiChunk::fromXML( + '' + ); + + $this->assertNull(ServiceNameResolver::resolve($mdui, 'nl_NL')); + } + + public function testItPrefersExactLocaleMatchOverEnglish(): void + { + $mdui = MduiChunk::fromXML( + '' + . 'English Service Name' + . 'Nederlandse Dienstnaam' + . '' + ); + + $this->assertSame('Nederlandse Dienstnaam', ServiceNameResolver::resolve($mdui, 'nl_NL')); + } + + public function testItFallsBackToEnglishWhenRequestedLocaleIsAbsent(): void + { + $mdui = MduiChunk::fromXML( + '' + . 'English Service Name' + . 'Deutscher Dienstname' + . '' + ); + + $this->assertSame('English Service Name', ServiceNameResolver::resolve($mdui, 'nl_NL')); + } + + public function testItFallsBackToFirstAvailableWhenNeitherRequestedLocaleNorEnglishIsPresent(): void + { + $mdui = MduiChunk::fromXML( + '' + . 'Deutscher Dienstname' + . 'Nom du service francais' + . '' + ); + + $this->assertSame('Deutscher Dienstname', ServiceNameResolver::resolve($mdui, 'nl_NL')); + } + + public function testItMatchesLocaleWithRegionSubtagAgainstLanguageOnlyDisplayName(): void + { + $mdui = MduiChunk::fromXML( + '' + . 'English Service Name' + . 'Nederlandse Dienstnaam' + . '' + ); + + $this->assertSame('English Service Name', ServiceNameResolver::resolve($mdui, 'en_GB')); + } + + public function testItMatchesUppercaseXmlLangAgainstLowercaseLocale(): void + { + $mdui = MduiChunk::fromXML( + '' + . 'English Service Name' + . '' + ); + + $this->assertSame('English Service Name', ServiceNameResolver::resolve($mdui, 'en_US')); + } + + public function testItFormatsTheResolvedRawNameThroughServiceNameFormatter(): void + { + $longName = str_repeat('a', 45); + $mdui = MduiChunk::fromXML( + '' + . '' . $longName . '' + . '' + ); + + $this->assertSame(str_repeat('a', 39) . "\u{2026}", ServiceNameResolver::resolve($mdui, 'en_US')); + } +} diff --git a/tests/Service/StateBasedAuthenticationServiceTest.php b/tests/Service/StateBasedAuthenticationServiceTest.php index 7f56574..0527455 100644 --- a/tests/Service/StateBasedAuthenticationServiceTest.php +++ b/tests/Service/StateBasedAuthenticationServiceTest.php @@ -24,6 +24,7 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Surfnet\GsspBundle\Exception\RuntimeException; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; use Symfony\Component\Routing\RouterInterface; class StateBasedAuthenticationServiceTest extends TestCase @@ -97,4 +98,29 @@ public function test_can_request_scoping_requester_ids_when_present() ); $this->assertEquals(['a', 'b', 'c'], $authenticationService->getScopingRequesterIds()); } + + public function test_getMdui_returns_mdui_chunk_when_present() + { + $mduiChunk = m::mock(MduiChunk::class); + $stateHandler = m::mock(StateHandlerInterface::class); + $stateHandler->shouldReceive('getMdui')->andReturn($mduiChunk); + $authenticationService = new StateBasedAuthenticationService( + $stateHandler, + m::mock(RouterInterface::class), + m::mock(LoggerInterface::class) + ); + $this->assertSame($mduiChunk, $authenticationService->getMdui()); + } + + public function test_getMdui_returns_null_when_not_present() + { + $stateHandler = m::mock(StateHandlerInterface::class); + $stateHandler->shouldReceive('getMdui')->andReturnNull(); + $authenticationService = new StateBasedAuthenticationService( + $stateHandler, + m::mock(RouterInterface::class), + m::mock(LoggerInterface::class) + ); + $this->assertNull($authenticationService->getMdui()); + } } diff --git a/tests/Service/StateBasedRegistrationServiceTest.php b/tests/Service/StateBasedRegistrationServiceTest.php index 924ffdd..48bb035 100644 --- a/tests/Service/StateBasedRegistrationServiceTest.php +++ b/tests/Service/StateBasedRegistrationServiceTest.php @@ -20,9 +20,11 @@ namespace Surfnet\GsspBundle\Service; +use Mockery as m; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Surfnet\GsspBundle\Exception\RuntimeException; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; use Symfony\Component\Routing\RouterInterface; class StateBasedRegistrationServiceTest extends TestCase @@ -73,4 +75,29 @@ public function testIsRegisteredShouldReturnFalseWhenRegistered() ); $this->assertTrue($registrationService->isRegistered()); } + + public function test_getMdui_returns_mdui_chunk_when_present() + { + $mduiChunk = m::mock(MduiChunk::class); + $stateHandler = m::mock(StateHandlerInterface::class); + $stateHandler->shouldReceive('getMdui')->andReturn($mduiChunk); + $registrationService = new StateBasedRegistrationService( + $stateHandler, + m::mock(RouterInterface::class), + m::mock(LoggerInterface::class) + ); + $this->assertSame($mduiChunk, $registrationService->getMdui()); + } + + public function test_getMdui_returns_null_when_not_present() + { + $stateHandler = m::mock(StateHandlerInterface::class); + $stateHandler->shouldReceive('getMdui')->andReturnNull(); + $registrationService = new StateBasedRegistrationService( + $stateHandler, + m::mock(RouterInterface::class), + m::mock(LoggerInterface::class) + ); + $this->assertNull($registrationService->getMdui()); + } } diff --git a/tests/Service/StateHandlerTest.php b/tests/Service/StateHandlerTest.php index 27f72b0..488b0f4 100644 --- a/tests/Service/StateHandlerTest.php +++ b/tests/Service/StateHandlerTest.php @@ -20,8 +20,10 @@ namespace Surfnet\GsspBundle\Service; +use DOMDocument; use Mockery as m; use PHPUnit\Framework\TestCase; +use Surfnet\SamlBundle\SAML2\Extensions\MduiChunk; class StateHandlerTest extends TestCase { @@ -67,4 +69,63 @@ public function test_can_get_scoping_requester_ids() ->andReturn(['a', 'b', 'c']); $this->assertEquals(['a', 'b', 'c'], $this->handler->getScopingRequesterIds()); } + + public function test_has_mdui_returns_false_when_absent(): void + { + $this->valueStore + ->shouldReceive('has') + ->with('gssp_mdui') + ->andReturnFalse(); + $this->assertFalse($this->handler->hasMdui()); + } + + public function test_has_mdui_returns_true_when_present(): void + { + $this->valueStore + ->shouldReceive('has') + ->with('gssp_mdui') + ->andReturnTrue(); + $this->assertTrue($this->handler->hasMdui()); + } + + public function test_get_mdui_returns_null_when_absent(): void + { + $this->valueStore + ->shouldReceive('has') + ->with('gssp_mdui') + ->andReturnFalse(); + $this->assertNull($this->handler->getMdui()); + } + + public function test_get_mdui_returns_chunk_with_display_names(): void + { + $chunk = $this->buildMduiChunk(['en' => 'My Service', 'nl' => 'Mijn Dienst']); + + $this->valueStore + ->shouldReceive('has') + ->with('gssp_mdui') + ->andReturnTrue(); + $this->valueStore + ->shouldReceive('get') + ->with('gssp_mdui') + ->andReturn($chunk->toXML()); + + $result = $this->handler->getMdui(); + $this->assertInstanceOf(MduiChunk::class, $result); + $this->assertEquals(['en' => 'My Service', 'nl' => 'Mijn Dienst'], $result->getDisplayNames()); + } + + private function buildMduiChunk(array $displayNames): MduiChunk + { + $chunk = new MduiChunk(); + $doc = $chunk->getValue()->ownerDocument; + $ns = 'urn:oasis:names:tc:SAML:metadata:ui'; + foreach ($displayNames as $lang => $name) { + $el = $doc->createElementNS($ns, 'mdui:DisplayName'); + $el->setAttribute('xml:lang', $lang); + $el->textContent = $name; + $chunk->getValue()->appendChild($el); + } + return $chunk; + } }