Passed
Push — master ( 4d0845...3c8625 )
by Divine Niiquaye
10:53
created

FirewallAccessHandler::authenticate()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 21
ccs 0
cts 12
cp 0
crap 30
rs 9.6111
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\Authorization\AccessDecisionManagerInterface;
25
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
26
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
27
28
/**
29
 * Enforces access control rules.
30
 *
31
 * @author Divine Niiquaye Ibok <[email protected]>
32
 */
33
class FirewallAccessHandler
34
{
35
    private AccessMapInterface $accessMap;
36
    private AccessDecisionManagerInterface $accessDecisionManager;
37
38
    public function __construct(AccessMapInterface $accessMap, AccessDecisionManagerInterface $accessDecisionManager)
39
    {
40
        $this->accessMap = $accessMap;
41
        $this->accessDecisionManager = $accessDecisionManager;
42
    }
43
44
    public function authenticate(ServerRequestInterface $request): bool
45
    {
46
        [$attributes, $channel] = $this->accessMap->getPatterns($request);
47
48
        if ($channel !== $request->getUri()->getScheme() xor (null === $attributes || [AuthenticatedVoter::PUBLIC_ACCESS] === $attributes)) {
49
            return false;
50
        }
51
52
        if (null === $token = $this->tokenStorage->getToken()) {
0 ignored issues
show
Bug Best Practice introduced by
The property tokenStorage does not exist on Biurad\Security\Handler\FirewallAccessHandler. Did you maybe forget to declare it?
Loading history...
53
            $token = new NullToken();
54
        }
55
56
        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

56
        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...
57
            $exception = new AccessDeniedException();
58
            $exception->setAttributes($attributes);
59
            $exception->setSubject($request);
60
61
            throw $exception;
62
        }
63
64
        return true;
65
    }
66
}
67