Passed
Pull Request — main (#308)
by Paul
14:17 queued 07:07
created

SelfVetController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 10
dl 0
loc 12
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2021 SURFnet B.V.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller\SelfVet;
22
23
use Psr\Log\LoggerInterface;
24
use Surfnet\SamlBundle\Http\RedirectBinding;
25
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger;
26
use Surfnet\StepupBundle\Service\LoaResolutionService;
27
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
28
use Surfnet\StepupBundle\Value\Loa;
29
use Surfnet\StepupBundle\Value\SecondFactorType;
30
use Surfnet\StepupBundle\Value\VettingType;
31
use Surfnet\StepupSelfService\SelfServiceBundle\Service\AuthorizationService;
32
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService;
33
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfVetMarshaller;
34
use Surfnet\StepupSelfService\SelfServiceBundle\Service\TestSecondFactor\TestAuthenticationRequestFactory;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupSelfServic...nticationRequestFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use Surfnet\StepupSelfService\SelfServiceBundle\Value\SelfVetRequestId;
36
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
37
use Symfony\Component\HttpFoundation\RedirectResponse;
38
use Symfony\Component\HttpFoundation\RequestStack;
39
use Symfony\Component\Routing\Attribute\Route;
40
use function sprintf;
41
42
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
44
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
45
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
46
class SelfVetController extends AbstractController
47
{
48
    final public const SELF_VET_SESSION_ID = 'second_factor_self_vet_request_id';
49
50
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
51
        private readonly LoggerInterface                  $logger,
52
        private readonly TestAuthenticationRequestFactory $authenticationRequestFactory,
53
        private readonly SecondFactorService              $secondFactorService,
54
        private readonly SecondFactorTypeService          $secondFactorTypeService,
55
        private readonly SelfVetMarshaller                $selfVetMarshaller,
56
        private readonly AuthorizationService             $authorizationService,
57
        private readonly RedirectBinding                  $redirectBinding,
58
        private readonly LoaResolutionService             $loaResolutionService,
59
        private readonly SamlAuthenticationLogger         $samlAuthenticationLogger,
60
        private readonly RequestStack                     $requestStack,
61
    ) {
62
    }
63
64
65
    #[Route(
66
        path: '/second-factor/{secondFactorId}/self-vet',
67
        name: 'ss_second_factor_self_vet',
68
        methods: ['GET'],
69
    )]
70
    public function selfVet(string $secondFactorId): RedirectResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function selfVet()
Loading history...
71
    {
72
        $this->logger->notice('Starting self vet proof of possession using higher or equal LoA token');
73
        $identity = $this->getUser()->getIdentity();
0 ignored issues
show
Bug introduced by
The method getIdentity() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as Surfnet\StepupSelfServic...n\AuthenticatedIdentity. ( Ignorable by Annotation )

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

73
        $identity = $this->getUser()->/** @scrutinizer ignore-call */ getIdentity();
Loading history...
74
75
        if (!$this->selfVetMarshaller->isAllowed($identity, $secondFactorId)) {
76
            throw $this->createNotFoundException();
77
        }
78
79
        // Start with some assumptions that are overwritten with the correct values in the code below
80
        $candidateSecondFactorLoa = $this->loaResolutionService->getLoaByLevel(Loa::LOA_SELF_VETTED);
0 ignored issues
show
Bug introduced by
Surfnet\StepupBundle\Value\Loa::LOA_SELF_VETTED of type double is incompatible with the type integer expected by parameter $loaLevel of Surfnet\StepupBundle\Ser...ervice::getLoaByLevel(). ( Ignorable by Annotation )

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

80
        $candidateSecondFactorLoa = $this->loaResolutionService->getLoaByLevel(/** @scrutinizer ignore-type */ Loa::LOA_SELF_VETTED);
Loading history...
81
        $isSelfVetOfSatToken = false;
82
83
        // Determine if we are dealing with a SelfVet action of a SAT token
84
        if ($this->authorizationService->maySelfVetSelfAssertedTokens($identity)) {
85
            $this->logger->notice('Determined we are self vetting a token using a self-asserted token');
86
            $isSelfVetOfSatToken = true;
87
        }
88
89
        // When a regular self-vet action is performed grab the candidate second factor loa from the SF projection
90
        if (!$isSelfVetOfSatToken) {
91
            $this->logger->notice('Determined we are self vetting a token using an identity vetted token');
92
            $candidateSecondFactor = $this->secondFactorService->findOneVerified($secondFactorId);
93
            $candidateSecondFactorLoa = $this->secondFactorTypeService->getLevel(
94
                new SecondFactorType($candidateSecondFactor->type),
95
                new VettingType(VettingType::TYPE_SELF_VET)
96
            );
97
            $candidateSecondFactorLoa = $this->loaResolutionService->getLoaByLevel($candidateSecondFactorLoa);
98
        }
99
        $this->logger->notice(
100
            sprintf(
101
                'Creating AuthNRequest requiring a LoA %s or higher token for self vetting.',
102
                $candidateSecondFactorLoa
103
            )
104
        );
105
        $authenticationRequest = $this->authenticationRequestFactory->createSecondFactorTestRequest(
106
            $identity->nameId,
107
            $candidateSecondFactorLoa
108
        );
109
110
        $this->requestStack->getSession()->set(
111
            self::SELF_VET_SESSION_ID,
112
            new SelfVetRequestId($authenticationRequest->getRequestId(), $secondFactorId)
113
        );
114
115
        $samlLogger = $this->samlAuthenticationLogger->forAuthentication($authenticationRequest->getRequestId());
116
        $samlLogger->notice('Sending authentication request to the second factor only IdP');
117
118
        return $this->redirectBinding->createResponseFor($authenticationRequest);
119
    }
120
}
121