1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of Biurad opensource projects. |
||
7 | * |
||
8 | * PHP version 7.4 and above required |
||
9 | * |
||
10 | * @author Divine Niiquaye Ibok <[email protected]> |
||
11 | * @copyright 2019 Biurad Group (https://biurad.com/) |
||
12 | * @license https://opensource.org/licenses/BSD-3-Clause License |
||
13 | * |
||
14 | * For the full copyright and license information, please view the LICENSE |
||
15 | * file that was distributed with this source code. |
||
16 | * |
||
17 | */ |
||
18 | |||
19 | namespace Biurad\Security\Handler; |
||
20 | |||
21 | use Biurad\Security\Interfaces\AccessMapInterface; |
||
22 | use Psr\Http\Message\ServerRequestInterface; |
||
23 | use Symfony\Component\Security\Core\Authentication\Token\NullToken; |
||
24 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
25 | use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; |
||
26 | use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; |
||
27 | use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
||
28 | |||
29 | /** |
||
30 | * Enforces access control rules. |
||
31 | * |
||
32 | * @author Divine Niiquaye Ibok <[email protected]> |
||
33 | */ |
||
34 | class FirewallAccessHandler |
||
35 | { |
||
36 | private AccessMapInterface $accessMap; |
||
37 | private TokenStorageInterface $tokenStorage; |
||
38 | private AccessDecisionManagerInterface $accessDecisionManager; |
||
39 | |||
40 | public function __construct(AccessMapInterface $accessMap, TokenStorageInterface $tokenStorage, AccessDecisionManagerInterface $accessDecisionManager) |
||
41 | { |
||
42 | $this->accessMap = $accessMap; |
||
43 | $this->tokenStorage = $tokenStorage; |
||
44 | $this->accessDecisionManager = $accessDecisionManager; |
||
45 | } |
||
46 | |||
47 | public function authenticate(ServerRequestInterface $request): bool |
||
48 | { |
||
49 | [$attributes, $channel] = $this->accessMap->getPatterns($request); |
||
50 | |||
51 | if ($channel && $channel !== $request->getUri()->getScheme()) { |
||
52 | return false; |
||
53 | } |
||
54 | |||
55 | if (!$attributes || [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes) { |
||
56 | return true; |
||
57 | } |
||
58 | |||
59 | if (null === $token = $this->tokenStorage->getToken()) { |
||
60 | $token = new NullToken(); |
||
61 | } |
||
62 | |||
63 | if (!$this->accessDecisionManager->decide($token, $attributes, $request, true)) { |
||
0 ignored issues
–
show
|
|||
64 | $exception = new AccessDeniedException(); |
||
65 | $exception->setAttributes($attributes); |
||
66 | $exception->setSubject($request); |
||
67 | |||
68 | throw $exception; |
||
69 | } |
||
70 | |||
71 | return true; |
||
72 | } |
||
73 | } |
||
74 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.