|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\OpenID\Federation; |
| 6 | + |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | +use SimpleSAML\OpenID\Codebooks\ClaimsEnum; |
| 9 | +use SimpleSAML\OpenID\Codebooks\ContentTypesEnum; |
| 10 | +use SimpleSAML\OpenID\Decorators\DateIntervalDecorator; |
| 11 | +use SimpleSAML\OpenID\Exceptions\EntityStatementException; |
| 12 | +use SimpleSAML\OpenID\Exceptions\FetchException; |
| 13 | +use SimpleSAML\OpenID\Federation\Factories\TrustMarkFactory; |
| 14 | +use SimpleSAML\OpenID\Helpers; |
| 15 | +use SimpleSAML\OpenID\Jws\JwsFetcher; |
| 16 | +use SimpleSAML\OpenID\Utils\ArtifactFetcher; |
| 17 | + |
| 18 | +class TrustMarkFetcher extends JwsFetcher |
| 19 | +{ |
| 20 | + public function __construct( |
| 21 | + private readonly TrustMarkFactory $parsedJwsFactory, |
| 22 | + ArtifactFetcher $artifactFetcher, |
| 23 | + DateIntervalDecorator $maxCacheDuration, |
| 24 | + Helpers $helpers, |
| 25 | + ?LoggerInterface $logger = null, |
| 26 | + ) { |
| 27 | + parent::__construct($parsedJwsFactory, $artifactFetcher, $maxCacheDuration, $helpers, $logger); |
| 28 | + } |
| 29 | + |
| 30 | + protected function buildJwsInstance(string $token): TrustMark |
| 31 | + { |
| 32 | + return $this->parsedJwsFactory->fromToken($token); |
| 33 | + } |
| 34 | + |
| 35 | + public function getExpectedContentTypeHttpHeader(): string |
| 36 | + { |
| 37 | + return ContentTypesEnum::ApplicationTrustMarkJwt->value; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param \SimpleSAML\OpenID\Federation\EntityStatement $entityConfiguration Entity from which to use the |
| 42 | + * federation_trust_mark_endpoint (issuer). |
| 43 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 44 | + * @throws \SimpleSAML\OpenID\Exceptions\FetchException |
| 45 | + * @throws \SimpleSAML\OpenID\Exceptions\OpenIdException |
| 46 | + */ |
| 47 | + public function fromCacheOrFederationTrustMarkEndpoint( |
| 48 | + string $trustMarkId, |
| 49 | + string $subjectId, |
| 50 | + EntityStatement $entityConfiguration, |
| 51 | + ): TrustMark { |
| 52 | + $trustMarkEndpoint = $entityConfiguration->getFederationTrustMarkEndpoint() ?? |
| 53 | + throw new EntityStatementException('No federation trust mark endpoint found in entity configuration.'); |
| 54 | + |
| 55 | + $this->logger?->debug( |
| 56 | + 'Trust Mark fetch from cache or federation trust mark endpoint.', |
| 57 | + ['trustMarkId' => $trustMarkId, 'subjectId' => $subjectId, 'trustMarkEndpoint' => $trustMarkEndpoint], |
| 58 | + ); |
| 59 | + |
| 60 | + return $this->fromCacheOrNetwork( |
| 61 | + $this->helpers->url()->withParams( |
| 62 | + $trustMarkEndpoint, |
| 63 | + [ |
| 64 | + ClaimsEnum::TrustMarkId->value => $trustMarkId, |
| 65 | + ClaimsEnum::Sub->value => $subjectId, |
| 66 | + ], |
| 67 | + ), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 73 | + * @throws \SimpleSAML\OpenID\Exceptions\FetchException |
| 74 | + */ |
| 75 | + public function fromCacheOrNetwork(string $uri): TrustMark |
| 76 | + { |
| 77 | + return $this->fromCache($uri) ?? $this->fromNetwork($uri); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Fetch Trust Mark from cache, if available. URI is used as cache key. |
| 82 | + * |
| 83 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 84 | + * @throws \SimpleSAML\OpenID\Exceptions\FetchException |
| 85 | + */ |
| 86 | + public function fromCache(string $uri): ?TrustMark |
| 87 | + { |
| 88 | + $trustMark = parent::fromCache($uri); |
| 89 | + |
| 90 | + if (is_null($trustMark)) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + if ($trustMark instanceof TrustMark) { |
| 95 | + return $trustMark; |
| 96 | + } |
| 97 | + |
| 98 | + // @codeCoverageIgnoreStart |
| 99 | + $message = 'Unexpected Trust Mark instance encountered for cache fetch.'; |
| 100 | + $this->logger?->error( |
| 101 | + $message, |
| 102 | + ['uri' => $uri, 'trustMark' => $trustMark], |
| 103 | + ); |
| 104 | + |
| 105 | + throw new FetchException($message); |
| 106 | + // @codeCoverageIgnoreEnd |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Fetch Trust Mark from network. Each successful fetch will be cached, with URI being used as a cache key. |
| 111 | + * |
| 112 | + * @throws \SimpleSAML\OpenID\Exceptions\FetchException |
| 113 | + * @throws \SimpleSAML\OpenID\Exceptions\JwsException |
| 114 | + */ |
| 115 | + public function fromNetwork(string $uri): TrustMark |
| 116 | + { |
| 117 | + $trustMark = parent::fromNetwork($uri); |
| 118 | + |
| 119 | + if ($trustMark instanceof TrustMark) { |
| 120 | + return $trustMark; |
| 121 | + } |
| 122 | + |
| 123 | + // @codeCoverageIgnoreStart |
| 124 | + $message = 'Unexpected Trust Mark instance encountered for network fetch.'; |
| 125 | + $this->logger?->error( |
| 126 | + $message, |
| 127 | + ['uri' => $uri, 'trustMark' => $trustMark], |
| 128 | + ); |
| 129 | + |
| 130 | + throw new FetchException($message); |
| 131 | + // @codeCoverageIgnoreEnd |
| 132 | + } |
| 133 | +} |
0 commit comments