|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Silverback API Components Bundle Project |
| 5 | + * |
| 6 | + * (c) Daniel West <daniel@silverback.is> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Silverback\ApiComponentsBundle\EventListener\Api; |
| 13 | + |
| 14 | +use Silverback\ApiComponentsBundle\AttributeReader\PublishableAttributeReader; |
| 15 | +use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker; |
| 16 | +use Symfony\Component\HttpKernel\Event\ResponseEvent; |
| 17 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 18 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Several resource responses are served from an identical URL but vary by the authenticated |
| 22 | + * session: a draft is returned to a permitted user and the published version to everyone else |
| 23 | + * (Route, ResourceManifest), and ComponentPosition rewrites its component IRI / exposes admin-only |
| 24 | + * groups by role. There is no distinguishing URL and no query marker, so a shared cache cannot tell |
| 25 | + * a public response from a personalised one. |
| 26 | + * |
| 27 | + * This listener makes that decision legible in the response itself: when an affected resource is |
| 28 | + * requested by an authenticated user, its response is marked `private, no-store` so no shared cache |
| 29 | + * (CDN, reverse proxy, or service worker) ever stores it. Anonymous requests are left on API |
| 30 | + * Platform's public cache headers, so the only variant a shared cache retains is the published one — |
| 31 | + * the same rule the edge cache already enforces by excluding cookie-bearing requests. |
| 32 | + * |
| 33 | + * @author Daniel West <daniel@silverback.is> |
| 34 | + */ |
| 35 | +final class CacheHeadersEventListener |
| 36 | +{ |
| 37 | + private readonly PublishableAttributeReader $publishableAttributeReader; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param array<class-string> $personalisedResourceClasses |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + private readonly TokenStorageInterface $tokenStorage, |
| 44 | + PublishableStatusChecker $publishableStatusChecker, |
| 45 | + private readonly array $personalisedResourceClasses = [], |
| 46 | + ) { |
| 47 | + $this->publishableAttributeReader = $publishableStatusChecker->getAttributeReader(); |
| 48 | + } |
| 49 | + |
| 50 | + public function onPostRespond(ResponseEvent $event): void |
| 51 | + { |
| 52 | + $request = $event->getRequest(); |
| 53 | + if (!$request->isMethodCacheable()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $resourceClass = $request->attributes->get('_api_resource_class'); |
| 58 | + if (!\is_string($resourceClass) || !$this->isPersonalisableResource($resourceClass)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + if (!$this->isAuthenticated()) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + $response = $event->getResponse(); |
| 67 | + // The body may carry draft or role-specific data tied to this authenticated session, so it |
| 68 | + // must never be stored by a shared cache. `private` overrides API Platform's default |
| 69 | + // `public`; `no-store` is the authoritative marker a service worker's cacheWillUpdate drops. |
| 70 | + $response->setPrivate(); |
| 71 | + $response->headers->removeCacheControlDirective('s-maxage'); |
| 72 | + $response->headers->addCacheControlDirective('no-store'); |
| 73 | + } |
| 74 | + |
| 75 | + private function isPersonalisableResource(string $resourceClass): bool |
| 76 | + { |
| 77 | + foreach ($this->personalisedResourceClasses as $affectedClass) { |
| 78 | + if (is_a($resourceClass, $affectedClass, true)) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Any resource configured as Publishable varies by auth (draft vs published) even when it is |
| 84 | + // an app-defined component that cannot be enumerated in the configured list above. |
| 85 | + return $this->publishableAttributeReader->isConfigured($resourceClass); |
| 86 | + } |
| 87 | + |
| 88 | + private function isAuthenticated(): bool |
| 89 | + { |
| 90 | + $token = $this->tokenStorage->getToken(); |
| 91 | + |
| 92 | + return null !== $token && $token->getUser() instanceof UserInterface; |
| 93 | + } |
| 94 | +} |
0 commit comments