FirewallAccessHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
Unused Code introduced by
The call to Symfony\Component\Securi...agerInterface::decide() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        if (!$this->accessDecisionManager->/** @scrutinizer ignore-call */ decide($token, $attributes, $request, true)) {

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.

Loading history...
64
            $exception = new AccessDeniedException();
65
            $exception->setAttributes($attributes);
66
            $exception->setSubject($request);
67
68
            throw $exception;
69
        }
70
71
        return true;
72
    }
73
}
74