Completed
Push — develop ( 9bc470...44ec0d )
by Michiel
06:03
created

Controller/Registration/U2fController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Controller\Controller;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Service\U2fSecondFactorService;
24
use Surfnet\StepupU2fBundle\Dto\RegisterRequest;
25
use Surfnet\StepupU2fBundle\Dto\RegisterResponse;
26
use Symfony\Component\HttpFoundation\Request;
27
28
class U2fController extends Controller
29
{
30
    /**
31
     * @Template
32
     */
33
    public function registrationAction()
34
    {
35
        $this->assertSecondFactorEnabled('u2f');
36
37
        $service = $this->get('surfnet_stepup_self_service_self_service.service.u2f_second_factor');
38
        $session = $this->get('self_service.session.u2f_registration');
39
40
        $registerRequestCreationResult = $service->createRegisterRequest($this->getIdentity());
41
42
        if (!$registerRequestCreationResult->wasSuccessful()) {
43
            $this->addFlash('error', 'ss.registration.u2f.alert.error');
44
45
            return ['registrationFailed' => true];
46
        }
47
48
        $registerRequest = $registerRequestCreationResult->getRegisterRequest();
49
        $registerResponse = new RegisterResponse();
50
51
        $form = $this
52
            ->createForm(
53
                'surfnet_stepup_u2f_register_device',
54
                $registerResponse,
55
                [
56
                    'register_request' => $registerRequest,
57
                    'action'           => $this->generateUrl('ss_registration_u2f_prove_possession'),
58
                ]
59
            );
60
61
        $session->set('request', $registerRequest);
62
63
        return [
64
            'form' => $form->createView(),
65
            'verifyEmail' => $this->emailVerificationIsRequired(),
66
        ];
67
    }
68
69
    /**
70
     * @Template
71
     */
72
    public function provePossessionAction(Request $request)
73
    {
74
        $this->assertSecondFactorEnabled('u2f');
75
76
        $session = $this->get('self_service.session.u2f_registration');
77
78
        /** @var RegisterRequest $registerRequest */
79
        $registerRequest = $session->get('request');
80
        $registerResponse = new RegisterResponse();
81
82
        $form = $this
83
            ->createForm(
84
                'surfnet_stepup_u2f_register_device',
85
                $registerResponse,
86
                [
87
                    'register_request' => $registerRequest,
88
                    'action'           => $this->generateUrl('ss_registration_u2f_prove_possession'),
89
                ]
90
            )
91
            ->handleRequest($request);
92
93
        if (!$form->isValid()) {
94
            return $this->render('SurfnetStepupSelfServiceSelfServiceBundle:Registration/U2f:registration.html.twig', [
95
                'registrationFailed' => true
96
            ]);
97
        }
98
99
        /** @var U2fSecondFactorService $service */
100
        $service = $this->get('surfnet_stepup_self_service_self_service.service.u2f_second_factor');
101
102
        $result = $service->provePossession($this->getIdentity(), $registerRequest, $registerResponse);
103
104
        if ($result->wasSuccessful()) {
105 View Code Duplication
            if ($this->emailVerificationIsRequired()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
                return $this->redirectToRoute(
107
                    'ss_registration_email_verification_email_sent',
108
                    ['secondFactorId' => $result->getSecondFactorId()]
109
                );
110
            } else {
111
                return $this->redirectToRoute(
112
                    'ss_registration_registration_email_sent',
113
                    ['secondFactorId' => $result->getSecondFactorId()]
114
                );
115
            }
116
        } elseif ($result->didDeviceReportAnyError()) {
117
            $this->addFlash('error', 'ss.registration.u2f.alert.device_reported_an_error');
118
        } else {
119
            $this->addFlash('error', 'ss.registration.u2f.alert.error');
120
        }
121
122
        return $this->render(
123
            'SurfnetStepupSelfServiceSelfServiceBundle:Registration/U2f:registration.html.twig',
124
            ['registrationFailed' => true]
125
        );
126
    }
127
}
128