YubikeyController::provePossessionAction()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 42
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 27
nc 6
nop 1
dl 0
loc 42
rs 8.5546
c 2
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller\Registration;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifyYubikeyOtpCommand;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Controller\Controller;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\ProveYubikeyPossessionType;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Service\YubikeySecondFactorService;
26
use Symfony\Component\Form\FormError;
27
use Symfony\Component\HttpFoundation\Request;
28
29
class YubikeyController extends Controller
30
{
31
    /**
32
     * @Template
33
     */
34
    public function provePossessionAction(Request $request)
35
    {
36
        $this->assertSecondFactorEnabled('yubikey');
37
38
        $identity = $this->getIdentity();
39
40
        $command = new VerifyYubikeyOtpCommand();
41
        $command->identity = $identity->id;
42
        $command->institution = $identity->institution;
43
44
        $form = $this->createForm(ProveYubikeyPossessionType::class, $command)->handleRequest($request);
45
46
        if ($form->isSubmitted() && $form->isValid()) {
47
            /** @var YubikeySecondFactorService $service */
48
            $service = $this->get('surfnet_stepup_self_service_self_service.service.yubikey_second_factor');
49
            $result = $service->provePossession($command);
50
51
            if ($result->isSuccessful()) {
52
                if ($this->emailVerificationIsRequired()) {
53
                    return $this->redirectToRoute(
54
                        'ss_registration_email_verification_email_sent',
55
                        ['secondFactorId' => $result->getSecondFactorId()]
56
                    );
57
                } else {
58
                    return $this->redirectToRoute(
59
                        'ss_second_factor_vetting_types',
60
                        ['secondFactorId' => $result->getSecondFactorId()]
61
                    );
62
                }
63
            } elseif ($result->isOtpInvalid()) {
64
                $this->addFlash('error', 'ss.verify_yubikey_command.otp.otp_invalid');
65
            } elseif ($result->didOtpVerificationFail()) {
66
                $this->addFlash('error', 'ss.verify_yubikey_command.otp.verification_error');
67
            } else {
68
                $this->addFlash('error', 'ss.prove_yubikey_possession.proof_of_possession_failed');
69
            }
70
        }
71
72
        // OTP field is rendered empty in the template.
73
        return [
74
            'form' => $form->createView(),
75
            'verifyEmail' => $this->emailVerificationIsRequired(),
76
        ];
77
    }
78
}
79