Completed
Pull Request — develop (#120)
by Nic
02:41
created

SamlController::consumeAssertionAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 3
eloc 18
nc 4
nop 1
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;
20
21
use Exception;
22
use Surfnet\SamlBundle\Http\XMLResponse;
23
use Surfnet\StepupBundle\Value\SecondFactorType;
24
use Symfony\Component\HttpFoundation\Request;
25
26
class SamlController extends Controller
27
{
28
    /**
29
     * @param string $secondFactorId
30
     *
31
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
32
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
33
     * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
34
     */
35
    public function testSecondFactorAction($secondFactorId)
36
    {
37
        $this->get('logger')->notice(
38
            'Starting second factor test'
39
        );
40
41
        $secondFactorService = $this->get('surfnet_stepup_self_service_self_service.service.second_factor');
42
        $identity            = $this->getIdentity();
43
44 View Code Duplication
        if (!$secondFactorService->identityHasSecondFactorOfStateWithId($identity->id, 'vetted', $secondFactorId)) {
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...
45
            $this->get('logger')->error(
46
                sprintf(
47
                    'Identity "%s" tried to test second factor "%s", but does not own that second factor or it is not vetted',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
48
                    $identity->id,
49
                    $secondFactorId
50
                )
51
            );
52
53
            throw $this->createNotFoundException();
54
        }
55
56
        $loaResolutionService         = $this->get('surfnet_stepup.service.loa_resolution');
57
        $authenticationRequestFactory = $this->get('self_service.test_second_factor_authentication_request_factory');
58
59
        $secondFactor     = $secondFactorService->findOneVetted($secondFactorId);
60
        $secondFactorType = new SecondFactorType($secondFactor->type);
61
62
        $authenticationRequest = $authenticationRequestFactory->createSecondFactorTestRequest(
63
            $identity->nameId,
64
            $loaResolutionService->getLoaByLevel($secondFactorType->getLevel())
65
        );
66
67
        $this->get('session')->set('second_factor_test_request_id', $authenticationRequest->getRequestId());
68
69
        $logger = $this->get('surfnet_saml.logger')->forAuthentication($authenticationRequest->getRequestId());
70
71
        $logger->notice(
72
            sprintf(
73
                'Sending authentication request with ID "%s" to the second factor test IDP',
74
                $authenticationRequest->getRequestId()
75
            )
76
        );
77
78
        return $this->get('surfnet_saml.http.redirect_binding')->createRedirectResponseFor($authenticationRequest);
79
    }
80
81
    public function consumeAssertionAction(Request $httpRequest)
82
    {
83
        $this->get('logger')->notice('Received an authentication response for testing a second factor');
84
85
        $session = $this->get('session');
86
87
        if (!$session->has('second_factor_test_request_id')) {
88
            $this->get('logger')->error(
89
                'Received an authentication response for testing a second factor, but no second factor test response was expected'
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
90
            );
91
92
            throw $this->createAccessDeniedException('Did not expect an authentication response');
93
        }
94
95
        $session->remove('second_factor_test_request_id');
96
97
        $postBinding = $this->get('surfnet_saml.http.post_binding');
98
99
        try {
100
            $postBinding->processResponse(
101
                $httpRequest,
102
                $this->get('self_service.second_factor_test_idp'),
103
                $this->get('surfnet_saml.hosted.service_provider')
104
            );
105
106
            $session->getFlashBag()->add('success', 'ss.test_second_factor.verification_successful');
107
        } catch (Exception $exception) {
108
            $session->getFlashBag()->add('error', 'ss.test_second_factor.verification_failed');
109
        }
110
111
        return $this->redirectToRoute('ss_second_factor_list');
112
    }
113
114
    public function metadataAction()
115
    {
116
        /** @var \Surfnet\SamlBundle\Metadata\MetadataFactory $metadataFactory */
117
        $metadataFactory = $this->get('surfnet_saml.metadata_factory');
118
119
        return new XMLResponse($metadataFactory->generate());
120
    }
121
}
122