|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Rest\Server\Controller\Session; |
| 10 | + |
| 11 | +use ApiPlatform\Metadata\Delete; |
| 12 | +use ApiPlatform\Metadata\Get; |
| 13 | +use ApiPlatform\Metadata\Post; |
| 14 | +use ApiPlatform\OpenApi\Factory\OpenApiFactory; |
| 15 | +use ApiPlatform\OpenApi\Model; |
| 16 | +use Ibexa\Contracts\Core\Repository\PermissionResolver; |
| 17 | +use Ibexa\Contracts\Core\Repository\UserService; |
| 18 | +use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; |
| 19 | +use Ibexa\Contracts\Rest\Exceptions\UnauthorizedException; |
| 20 | +use Ibexa\Rest\Server\Controller; |
| 21 | +use Ibexa\Rest\Server\Exceptions; |
| 22 | +use Ibexa\Rest\Server\Security\CsrfTokenManager; |
| 23 | +use Ibexa\Rest\Server\Values; |
| 24 | +use Ibexa\Rest\Value as RestValue; |
| 25 | +use Symfony\Component\HttpFoundation\Request; |
| 26 | +use Symfony\Component\HttpFoundation\Response; |
| 27 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface as SecurityTokenStorageInterface; |
| 28 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 29 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 30 | +use Symfony\Component\Security\Csrf\CsrfToken; |
| 31 | + |
| 32 | +class SessionBaseController extends Controller |
| 33 | +{ |
| 34 | + public function __construct( |
| 35 | + protected readonly PermissionResolver $permissionResolver, |
| 36 | + protected readonly UserService $userService, |
| 37 | + protected readonly CsrfTokenManager $csrfTokenManager, |
| 38 | + protected readonly SecurityTokenStorageInterface $securityTokenStorage, |
| 39 | + protected readonly string $csrfTokenIntention, |
| 40 | + protected readonly ConfigResolverInterface $configResolver, |
| 41 | + ) { |
| 42 | + } |
| 43 | + |
| 44 | + protected function hasStoredCsrfToken(): bool |
| 45 | + { |
| 46 | + return $this->csrfTokenManager->hasToken($this->csrfTokenIntention); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Checks the presence / validity of the CSRF token. |
| 51 | + * |
| 52 | + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException if the token is missing or invalid |
| 53 | + */ |
| 54 | + protected function checkCsrfToken(Request $request): void |
| 55 | + { |
| 56 | + if (!$request->headers->has('X-CSRF-Token')) { |
| 57 | + throw $this->createInvalidCsrfTokenException($request); |
| 58 | + } |
| 59 | + |
| 60 | + $csrfToken = new CsrfToken( |
| 61 | + $this->csrfTokenIntention, |
| 62 | + $request->headers->get('X-CSRF-Token') |
| 63 | + ); |
| 64 | + |
| 65 | + if (!$this->csrfTokenManager->isTokenValid($csrfToken)) { |
| 66 | + throw $this->createInvalidCsrfTokenException($request); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected function getCsrfToken(): string |
| 71 | + { |
| 72 | + return $this->csrfTokenManager->getToken($this->csrfTokenIntention)->getValue(); |
| 73 | + } |
| 74 | + |
| 75 | + protected function createInvalidCsrfTokenException(Request $request): UnauthorizedException |
| 76 | + { |
| 77 | + return new UnauthorizedException('Missing or invalid CSRF token'); |
| 78 | + } |
| 79 | + |
| 80 | + protected function logout(Request $request): Response |
| 81 | + { |
| 82 | + $path = '/'; |
| 83 | + $domain = null; |
| 84 | + |
| 85 | + $session = $this->configResolver->getParameter('session'); |
| 86 | + if (array_key_exists('cookie_domain', $session)) { |
| 87 | + $domain = $session['cookie_domain']; |
| 88 | + } |
| 89 | + |
| 90 | + if (array_key_exists('cookie_path', $session)) { |
| 91 | + $path = $session['cookie_path']; |
| 92 | + } |
| 93 | + |
| 94 | + $response = new Response(); |
| 95 | + $requestSession = $request->getSession(); |
| 96 | + |
| 97 | + $response->headers->clearCookie( |
| 98 | + $requestSession->getName(), |
| 99 | + $path, |
| 100 | + $domain |
| 101 | + ); |
| 102 | + |
| 103 | + $response->setStatusCode(404); |
| 104 | + $requestSession->clear(); |
| 105 | + |
| 106 | + return $response; |
| 107 | + } |
| 108 | +} |
0 commit comments