YubikeyController::provePossession()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 31
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 47
rs 8.4906
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2014 SURFnet bv
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\Registration;
22
23
use Surfnet\StepupSelfService\SelfServiceBundle\Service\ControllerCheckerService;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifyYubikeyOtpCommand;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\ProveYubikeyPossessionType;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Service\YubikeySecondFactorServiceInterface;
27
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\Routing\Attribute\Route;
31
32
class YubikeyController extends AbstractController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class YubikeyController
Loading history...
33
{
34
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
35
        private readonly ControllerCheckerService   $checkerService,
36
        private readonly YubikeySecondFactorServiceInterface $yubikeySecondFactorService,
37
    ) {
38
    }
39
40
    #[Route(
41
        path: '/registration/yubikey/prove-possession',
42
        name: 'ss_registration_yubikey_prove_possession',
43
        methods: ['GET','POST'],
44
    )]
45
    public function provePossession(Request $request): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function provePossession()
Loading history...
46
    {
47
        $this->checkerService->assertSecondFactorEnabled('yubikey');
48
49
        $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

49
        $identity = $this->getUser()->/** @scrutinizer ignore-call */ getIdentity();
Loading history...
50
51
        $command = new VerifyYubikeyOtpCommand();
52
        $command->identity = $identity->id;
53
        $command->institution = $identity->institution;
54
55
        $form = $this->createForm(ProveYubikeyPossessionType::class, $command)->handleRequest($request);
56
57
        if ($form->isSubmitted() && $form->isValid()) {
58
            $result = $this->yubikeySecondFactorService->provePossession($command);
59
60
            if ($result->isSuccessful()) {
61
                if ($this->checkerService->emailVerificationIsRequired()) {
62
                    return $this->redirectToRoute(
63
                        'ss_registration_email_verification_email_sent',
64
                        ['secondFactorId' => $result->getSecondFactorId()]
65
                    );
66
                } else {
67
                    return $this->redirectToRoute(
68
                        'ss_second_factor_vetting_types',
69
                        ['secondFactorId' => $result->getSecondFactorId()]
70
                    );
71
                }
72
            } elseif ($result->isOtpInvalid()) {
73
                $this->addFlash('error', 'ss.verify_yubikey_command.otp.otp_invalid');
74
            } elseif ($result->didOtpVerificationFail()) {
75
                $this->addFlash('error', 'ss.verify_yubikey_command.otp.verification_error');
76
            } else {
77
                $this->addFlash('error', 'ss.prove_yubikey_possession.proof_of_possession_failed');
78
            }
79
        }
80
81
        // OTP field is rendered empty in the template.
82
        return $this->render(
83
            'registration/yubikey/prove_possession.html.twig',
84
            [
85
                'form' => $form->createView(),
86
                'verifyEmail' => $this->checkerService->emailVerificationIsRequired(),
87
            ]
88
        );
89
    }
90
}
91