|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\oidc\Controllers\Admin; |
| 6 | + |
| 7 | +use SimpleSAML\Module\oidc\Admin\Authorization; |
| 8 | +use SimpleSAML\Module\oidc\Codebooks\RoutesEnum; |
| 9 | +use SimpleSAML\Module\oidc\Exceptions\OidcException; |
| 10 | +use SimpleSAML\Module\oidc\Factories\TemplateFactory; |
| 11 | +use SimpleSAML\Module\oidc\Helpers; |
| 12 | +use SimpleSAML\Module\oidc\ModuleConfig; |
| 13 | +use SimpleSAML\Module\oidc\Utils\Debug\ArrayLogger; |
| 14 | +use SimpleSAML\OpenID\Codebooks\EntityTypesEnum; |
| 15 | +use SimpleSAML\OpenID\Exceptions\TrustChainException; |
| 16 | +use SimpleSAML\OpenID\Federation; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | + |
| 20 | +class TestController |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + protected readonly ModuleConfig $moduleConfig, |
| 24 | + protected readonly TemplateFactory $templateFactory, |
| 25 | + protected readonly Authorization $authorization, |
| 26 | + protected readonly Federation $federation, |
| 27 | + protected readonly Helpers $helpers, |
| 28 | + protected readonly ArrayLogger $arrayLogger, |
| 29 | + ) { |
| 30 | + $this->authorization->requireAdmin(true); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @throws \SimpleSAML\Error\ConfigurationError |
| 35 | + * @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException |
| 36 | + * @throws \SimpleSAML\Module\oidc\Exceptions\OidcException |
| 37 | + */ |
| 38 | + public function trustChainResolution(Request $request): Response |
| 39 | + { |
| 40 | + $this->arrayLogger->setWeight(ArrayLogger::WEIGHT_WARNING); |
| 41 | + // Let's create new Federation instance so we can inject our debug logger and go without cache. |
| 42 | + $federation = new Federation( |
| 43 | + supportedAlgorithms: $this->federation->supportedAlgorithms(), |
| 44 | + cache: null, |
| 45 | + logger: $this->arrayLogger, |
| 46 | + ); |
| 47 | + |
| 48 | + $leafEntityId = $this->moduleConfig->getIssuer(); |
| 49 | + $trustChainBag = null; |
| 50 | + $resolvedMetadata = []; |
| 51 | + $isFormSubmitted = false; |
| 52 | + |
| 53 | + try { |
| 54 | + $trustAnchorIds = $this->moduleConfig->getFederationTrustAnchorIds(); |
| 55 | + } catch (\Throwable $exception) { |
| 56 | + $this->arrayLogger->error('Module config error: ' . $exception->getMessage()); |
| 57 | + $trustAnchorIds = []; |
| 58 | + } |
| 59 | + |
| 60 | + if ($request->isMethod(Request::METHOD_POST)) { |
| 61 | + $isFormSubmitted = true; |
| 62 | + |
| 63 | + !empty($leafEntityId = $request->request->getString('leafEntityId')) || |
| 64 | + throw new OidcException('Empty leaf entity ID.'); |
| 65 | + !empty($rawTrustAnchorIds = $request->request->getString('trustAnchorIds')) || |
| 66 | + throw new OidcException('Empty Trust Anchor IDs.'); |
| 67 | + |
| 68 | + /** @var non-empty-array<non-empty-string> $trustAnchorIds */ |
| 69 | + $trustAnchorIds = $this->helpers->str()->convertTextToArray($rawTrustAnchorIds); |
| 70 | + |
| 71 | + try { |
| 72 | + $trustChainBag = $federation->trustChainResolver()->for($leafEntityId, $trustAnchorIds); |
| 73 | + |
| 74 | + foreach ($trustChainBag->getAll() as $index => $trustChain) { |
| 75 | + $metadataEntries = []; |
| 76 | + foreach (EntityTypesEnum::cases() as $entityTypeEnum) { |
| 77 | + try { |
| 78 | + $metadataEntries[$entityTypeEnum->value] = |
| 79 | + $trustChain->getResolvedMetadata($entityTypeEnum); |
| 80 | + } catch (\Throwable $exception) { |
| 81 | + $this->arrayLogger->error( |
| 82 | + 'Metadata resolving error: ' . $exception->getMessage(), |
| 83 | + compact('index', 'entityTypeEnum'), |
| 84 | + ); |
| 85 | + continue; |
| 86 | + } |
| 87 | + } |
| 88 | + $resolvedMetadata[$index] = array_filter($metadataEntries); |
| 89 | + } |
| 90 | + } catch (TrustChainException $exception) { |
| 91 | + $this->arrayLogger->error('Trust chain error: ' . $exception->getMessage()); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + $trustAnchorIds = implode("\n", $trustAnchorIds); |
| 96 | + $logMessages = $this->arrayLogger->getEntries(); |
| 97 | +//dd($this->arrayLogger->getEntries()); |
| 98 | + return $this->templateFactory->build( |
| 99 | + 'oidc:tests/trust-chain-resolution.twig', |
| 100 | + compact( |
| 101 | + 'leafEntityId', |
| 102 | + 'trustAnchorIds', |
| 103 | + 'trustChainBag', |
| 104 | + 'resolvedMetadata', |
| 105 | + 'logMessages', |
| 106 | + 'isFormSubmitted', |
| 107 | + ), |
| 108 | + RoutesEnum::AdminTestTrustChainResolution->value, |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments