Completed
Push — develop ( b9b24c...1466e6 )
by Michiel
02:02
created

U2fController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 6.56 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 8
loc 122
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A startAuthenticationAction() 4 16 2
A authenticationAction() 4 39 3
A getVettingService() 0 4 1
B provePossessionAction() 0 39 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\StepupRa\RaBundle\Controller\Vetting;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use Surfnet\StepupRa\RaBundle\Command\VerifyU2fPublicIdCommand;
23
use Surfnet\StepupRa\RaBundle\Service\VettingService;
24
use Surfnet\StepupU2fBundle\Dto\SignResponse;
25
use Surfnet\StepupU2fBundle\Form\Type\VerifyDeviceAuthenticationType;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
30
class U2fController extends SecondFactorController
31
{
32
    /**
33
     * @Template
34
     * @param Request $request
35
     * @param string  $procedureId
36
     * @return array|Response
37
     */
38
    public function startAuthenticationAction(Request $request, $procedureId)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        $this->assertSecondFactorEnabled('u2f');
41
42
        $this->denyAccessUnlessGranted(['ROLE_RA']);
43
44
        $logger = $this->get('ra.procedure_logger')->forProcedure($procedureId);
45
        $logger->notice('Suggesting RA start U2F authentication');
46
47 View Code Duplication
        if (!$this->getVettingService()->hasProcedure($procedureId)) {
0 ignored issues
show
Duplication introduced by
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...
48
            $logger->notice(sprintf('Vetting procedure "%s" not found', $procedureId));
49
            throw $this->createNotFoundException(sprintf('Vetting procedure "%s" not found', $procedureId));
50
        }
51
52
        return ['procedureId' => $procedureId];
53
    }
54
55
    /**
56
     * @Template
57
     * @param Request $request
58
     * @param string  $procedureId
59
     * @return array|Response
60
     */
61
    public function authenticationAction(Request $request, $procedureId)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        $this->assertSecondFactorEnabled('u2f');
64
65
        $this->denyAccessUnlessGranted(['ROLE_RA']);
66
67
        $logger = $this->get('ra.procedure_logger')->forProcedure($procedureId);
68
        $logger->notice('Requested U2F Verfication');
69
70 View Code Duplication
        if (!$this->getVettingService()->hasProcedure($procedureId)) {
0 ignored issues
show
Duplication introduced by
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...
71
            $logger->notice(sprintf('Vetting procedure "%s" not found', $procedureId));
72
            throw new NotFoundHttpException(sprintf('Vetting procedure "%s" not found', $procedureId));
73
        }
74
75
        $service = $this->getVettingService();
76
        $session = $this->get('ra.session.u2f');
77
78
        $result = $service->createU2fSignRequest($procedureId);
79
80
        if (!$result->wasSuccessful()) {
81
            $this->addFlash('error', 'ra.vetting.u2f.alert.error');
82
83
            return ['authenticationFailed' => true, 'procedureId' => $procedureId];
84
        }
85
86
        $signRequest = $result->getSignRequest();
87
        $signResponse = new SignResponse();
88
89
        $formAction = $this->generateUrl('ra_vetting_u2f_prove_possession', ['procedureId' => $procedureId]);
90
        $form = $this->createForm(
91
            VerifyDeviceAuthenticationType::class,
92
            $signResponse,
93
            ['sign_request' => $signRequest, 'action' => $formAction,]
94
        );
95
96
        $session->set('request', $signRequest);
97
98
        return ['form' => $form->createView()];
99
    }
100
101
    /**
102
     * @Template
103
     */
104
    public function provePossessionAction(Request $request, $procedureId)
105
    {
106
        $this->assertSecondFactorEnabled('u2f');
107
108
        $session = $this->get('ra.session.u2f');
109
110
        /** @var RegisterRequest $signRequest */
111
        $signRequest = $session->get('request');
112
        $signResponse = new SignResponse();
113
114
        $formAction = $this->generateUrl('ra_vetting_u2f_prove_possession', ['procedureId' => $procedureId]);
115
        $form = $this
116
            ->createForm(
117
                VerifyDeviceAuthenticationType::class,
118
                $signResponse,
119
                ['sign_request' => $signRequest, 'action' => $formAction]
120
            )
121
            ->handleRequest($request);
122
123
        if (!$form->isSubmitted() || !$form->isValid()) {
124
            return $this->render('SurfnetStepupRaRaBundle:Vetting/U2f:authentication.html.twig', [
125
                'authenticationFailed' => true,
126
                'procedureId' => $procedureId,
127
            ]);
128
        }
129
130
        $service = $this->getVettingService();
131
        $result = $service->verifyU2fAuthentication($procedureId, $signRequest, $signResponse);
132
133
        if ($result->wasSuccessful()) {
134
            return $this->redirectToRoute('ra_vetting_verify_identity', ['procedureId' => $procedureId]);
135
        } elseif ($result->didDeviceReportAnyError()) {
136
            $this->addFlash('error', 'ra.vetting.u2f.alert.device_reported_an_error');
137
            return ['authenticationFailed' => true, 'procedureId' => $procedureId];
138
        } else {
139
            $this->addFlash('error', 'ra.vetting.u2f.alert.error');
140
            return ['authenticationFailed' => true, 'procedureId' => $procedureId];
141
        }
142
    }
143
144
    /**
145
     * @return VettingService
146
     */
147
    private function getVettingService()
148
    {
149
        return $this->get('ra.service.vetting');
150
    }
151
}
152