-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioApiBundle\Security\Voter; | ||
|
||
use Pimcore\Bundle\StudioApiBundle\Service\SecurityServiceInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
|
||
final class TokenVoter extends Voter | ||
{ | ||
private const BEARER_PREFIX = 'Bearer '; | ||
|
||
private const AUTHORIZATION_HEADER = 'Authorization'; | ||
|
||
private const SUPPORTED_ATTRIBUTE = 'API_PLATFORM'; | ||
|
||
public function __construct( | ||
private readonly RequestStack $requestStack, | ||
private readonly SecurityServiceInterface $securityService | ||
|
||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function supports(string $attribute, mixed $subject): bool | ||
{ | ||
return $attribute === self::SUPPORTED_ATTRIBUTE; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool | ||
{ | ||
if($attribute !== self::SUPPORTED_ATTRIBUTE){ | ||
return false; | ||
} | ||
|
||
$authToken = $this->requestStack->getCurrentRequest()->headers->get(self::AUTHORIZATION_HEADER); | ||
if($authToken === null){ | ||
return false; | ||
} | ||
|
||
return $this->securityService->isAllowed($this->removeBearerPrefix($authToken)); | ||
} | ||
|
||
private function removeBearerPrefix(string $token): string | ||
{ | ||
return str_replace( self::BEARER_PREFIX, '', $token); | ||
} | ||
} |