Passed
Pull Request — main (#308)
by Paul
18:42 queued 09:10
created

SelfVetController::selfVet()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 33
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 54
rs 9.392

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
41
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
42
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
43
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
44
 */
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...
45
class SelfVetController extends AbstractController
46
{
47
    final public const SELF_VET_SESSION_ID = 'second_factor_self_vet_request_id';
48
49
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
50
        private readonly LoggerInterface                  $logger,
51
        private readonly TestAuthenticationRequestFactory $authenticationRequestFactory,
52
        private readonly SecondFactorService              $secondFactorService,
53
        private readonly SecondFactorTypeService          $secondFactorTypeService,
54
        private readonly SelfVetMarshaller                $selfVetMarshaller,
55
        private readonly AuthorizationService             $authorizationService,
56
        private readonly RedirectBinding                  $redirectBinding,
57
        private readonly LoaResolutionService             $loaResolutionService,
58
        private readonly SamlAuthenticationLogger         $samlAuthenticationLogger,
59
        private readonly RequestStack                     $requestStack,
60
    ) {
61
    }
62
63
64
    #[Route(
65
        path: '/second-factor/{secondFactorId}/self-vet',
66
        name: 'ss_second_factor_self_vet',
67
        methods: ['GET'],
68
    )]
69
    public function selfVet(string $secondFactorId): RedirectResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function selfVet()
Loading history...
70
    {
71
        $this->logger->notice('Starting self vet proof of possession using higher or equal LoA token');
72
        $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

72
        $identity = $this->getUser()->/** @scrutinizer ignore-call */ getIdentity();
Loading history...
73
74
        if (!$this->selfVetMarshaller->isAllowed($identity, $secondFactorId)) {
75
            throw $this->createNotFoundException();
76
        }
77
78
        // Start with some assumptions that are overwritten with the correct values in the code below
79
        $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

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